leptos-css
[!IMPORTANT]
leptos-csshas been renamed totyped-css. This is the finalleptos-cssrelease and receives no further updates.typed-cssis the same library under a framework-neutral name, with its Leptos integration behind theleptosfeature:[] = { = "0.2", = ["leptos"] }To migrate, change your imports from
leptos_css::...totyped_css::....typed-css0.2 also redesigns the API and adds many properties. Its changelog lists every change sinceleptos-css0.1.The documentation below describes
leptos-css0.1.
Typed CSS primitives and property-specific declaration grammars for Leptos. The checked API is deliberately fail-closed: unsupported CSS cannot be smuggled in as an arbitrary value string, and a value can become a declaration only through a selector for a property whose grammar it satisfies.
leptos-css is the typed foundation of the leptos-styles family. Its
checked boundary is the owned CheckedDeclaration returned by a property selector. Integrations can store and pass
that concrete type without separating its checked property/value pairing.
Applications can use handwritten CSS or Leptos string styles for declarations outside the checked API.
Features
- Closed value types for lengths, percentages, colors, angles, times, global keywords, and selected property grammars
- Constructor helpers (
px,pct,em,rem,vh,vw,deg,rgb,hsl, ...) with finite-input validation, plus fallibletry_*counterparts for runtime-derived values. Helpers return the narrowest modeled grammar, sorgb()andhsl()feed every selector acceptingCssColordirectly. - Typed arithmetic and typed
css_min(),css_max(),css_clamp(), andcss_env()constructors; function bodies are expression trees rather than strings - Validated, grammar-typed custom properties and
var()references with mandatory typed fallbacks - Property selectors for checked Leptos declarations, including padding, margin, inset, sizing and radius properties, colors, opacity, color adjustment, view transitions, font weight, touch action, and z-index
- A curated, non-exhaustive
PropertyNamecatalog with kebab-caseas_str()output; it is neither a declaration API nor a complete CSS property index
Installation
leptos-css is compatible with Leptos 0.8 and requires Rust 1.89 or newer.
Enable the nightly feature when the application also enables leptos/nightly:
Example
use ;
use ;
css_custom_property!;
let mut buf = Stringnew;
px.write_to;
assert_eq!;
let measured_width = try_px.expect;
assert_eq!;
let padding = all;
assert_eq!;
let padding_declaration = PaddingProperty.declare;
let _view = view! ;
let _rgb_declaration = ColorProperty.declare;
let width = Calculation;
assert_eq!;
let _width_declaration = WidthProperty.declare;
let touch_gestures = horizontal
.with_vertical
.with_pinch_zoom;
let _touch_declaration = TouchActionProperty.declare;
let _accent_declaration = ACCENT_COLOR.declare;
let _color_declaration = ColorProperty.declare;
Usage notes
- Convenience constructors (
px,pct,deg,s,hsl, ...) panic on invalid input. Use their fallibletry_*counterparts for runtime-derived values. - Property selectors enforce their CSS grammars. Margin and inset accept
LengthPercentageAuto, while padding and gap accept non-negative length-percentage values, soautoand directly negative values work only where CSS permits. - Ordinary property selectors accept CSS-wide keywords through
declare_global().AllPropertyaccepts only a CSS-wide keyword. - Public enums are
#[non_exhaustive]. Downstream matches must include a wildcard arm.
License
Dual licensed under either of:
- MIT License (LICENSE-MIT)
- Apache License, Version 2.0 (LICENSE-APACHE)
at your option.