Skill v1.0.1
currentAutomated scan100/100+8 new
version: "1.0.1" name: jaspr-styling description: Write type-safe CSS-in-Dart to style Jaspr components. Use this skill when styling components, implementing themes, or using CSS properties. metadata: jaspr_version: 0.23.3
Rules for Styling
Jaspr provides a native, type-safe CSS-in-Dart styling API via the Styles class.
1. Component-Level Styles (@css annotation)
The recommended approach for defining styles in Jaspr is inside your components to ensure better code locality.
- Rule 1: You MUST use the
@cssannotation on a static getter returningList<StyleRule>to scope styles to a component. - Rule 2: You MUST use specific CSS selectors (like
.mainclasses or#idids) withincss()to prevent your styles from bleeding into other components, as@cssstyles are globalized during rendering.
class App extends StatelessComponent {const App({super.key});@overrideComponent build(BuildContext context) {return div(classes: 'main', [p([.text('Hello World')]),]);}@cssstatic List<StyleRule> get styles => [css('.main', [// Use css('&') to refer to the parent selector (.main)css('&').styles(width: 100.px, // Note: Use `.px`, `.rem`, `.percent` extensions on numbers.padding: .all(10.rem),display: .flex,),css('&:hover').styles(backgroundColor: Colors.blue,),css('p').styles(color: Colors.blue,),]),// Responsive queriescss.media(MediaQuery.screen(maxWidth: 600.px), [css('.main').styles(flexDirection: .column),])];}
2. Inline Styles
You can pass Styles instances directly to native HTML components. This renders as the style="..." HTML attribute.
- Rule 1: You MUST ONLY use inline styles for dynamic styles (e.g., styles that change based on state or parameters).
- Rule 2: For static styles, you MUST use the component-level
@cssannotation. - Rule 3: You MUST NOT use inline styles for complex rules like media queries, hover states, or animations.
// Example of dynamic inline styling driven by stateclass ColorBox extends StatelessComponent {final Color boxColor;const ColorBox({required this.boxColor, super.key});@overrideComponent build(BuildContext context) {return div(styles: Styles(backgroundColor: boxColor), []);}}
3. Global @css Styles
You can define a global set of styles directly from Dart using the @css annotation on a global variable or getter.
- Rule 1: Just like component-level
@css, global@cssis ONLY supported in server and static modes. - Rule 2: You MUST place global
@cssannotations on top-level getters or static getters returningList<StyleRule>.
@cssList<StyleRule> get globalStyles => [css('body').styles(margin: .zero,fontFamily: .list([FontFamily('Roboto'), FontFamilies.sansSerif]),backgroundColor: Colors.white,),css('a').styles(textDecoration: TextDecoration(line: .none),color: Colors.blue,),];
4. Global External Stylesheets
- Rule: If you are using raw
.cssfiles (or other css frameworks like sass/scss, tailwind, etc.), you MUST include them using a<link rel="stylesheet">element inside theDocument(head: [...])component (for server/static mode) or inweb/index.html(for client mode).
Jaspr Styles Properties
To avoid guessing CSS properties and overwhelming context, Jaspr maps CSS properties to strongly-typed Dart classes.
Here is the full list of properties both the Styles() class and .styles() method support:
Styles({ // or .styles({All? all,// Box StylesString? content,Display? display,Position? position,ZIndex? zIndex,Unit? width,Unit? height,Unit? minWidth,Unit? minHeight,Unit? maxWidth,Unit? maxHeight,AspectRatio? aspectRatio,Padding? padding,Margin? margin,BoxSizing? boxSizing,Border? border,BorderRadius? radius,Outline? outline,double? opacity,Visibility? visibility,Overflow? overflow,Appearance? appearance,BoxShadow? shadow,Filter? filter,Filter? backdropFilter,Cursor? cursor,UserSelect? userSelect,PointerEvents? pointerEvents,Animation? animation,Transition? transition,Transform? transform,// Flexbox StylesFlexDirection? flexDirection,FlexWrap? flexWrap,JustifyContent? justifyContent,AlignItems? alignItems,AlignContent? alignContent,// Grid StylesGridTemplate? gridTemplate,List<TrackSize>? autoRows,List<TrackSize>? autoColumns,JustifyItems? justifyItems,Gap? gap,// Item StylesFlex? flex,int? order,AlignSelf? alignSelf,JustifySelf? justifySelf,GridPlacement? gridPlacement,// List StylesListStyle? listStyle,ImageStyle? listImage,ListStylePosition? listPosition,// Text StylesColor? color,TextAlign? textAlign,FontFamily? fontFamily,Unit? fontSize,FontWeight? fontWeight,FontStyle? fontStyle,TextDecoration? textDecoration,TextTransform? textTransform,Unit? textIndent,Unit? letterSpacing,Unit? wordSpacing,Unit? lineHeight,TextShadow? textShadow,TextOverflow? textOverflow,WhiteSpace? whiteSpace,Quotes? quotes,// Background StylesColor? backgroundColor,ImageStyle? backgroundImage,BackgroundOrigin? backgroundOrigin,BackgroundPosition? backgroundPosition,BackgroundAttachment? backgroundAttachment,BackgroundRepeat? backgroundRepeat,BackgroundSize? backgroundSize,BackgroundClip? backgroundClip,// Raw StylesMap<String, String>? raw,})
- Rule 1: You MUST define all properties in the order they are defined in the
Stylesclass. - Rule 2: You MUST use dot-shorthands for all style properties and values where applicable (e.g.,
padding: .all(10.px)instead ofpadding: Padding.all(Unit.pixels(10)), orjustifyContent: .centerinstead ofjustifyContent: JustifyContent.center). - Rule 3: You MUST use
rawfor any properties that are not supported by theStylesclass.
IMPORTANT: Before writing styles in one of the below areas, you MUST read the respective reference file provided alongside this skill:
references/sizing.md(Units like.px,.percent, dimensions, margin, padding, borders, border-radius)references/color.md(Colors, HEX, RGB, HSL values)references/box.md(Position, overflow, transform, background, cursor, filters, z-index, visibility)references/typography.md(Text alignment, fonts, text decoration, letter spacing)references/flexbox.md(Flexbox container/item, flex layout, alignments)references/grid.md(Grid layout, track sizes, gaps, templates)references/animation.md(Transitions, Animations, keyframes, curves, durations)