StyleX Styling
Overview
This project uses StyleX for styling with design tokens, responsive breakpoints, and theme-aware colors.
Key Patterns
Design Tokens
- Import tokens from:
@/tokens.stylex.ts
- Available token categories:
color - Theme-aware colors (textMain, backgroundRaised, controlActive, etc.)
controlSize - Spacing and sizing values (_1 through _9)
font - Typography values (weight_5, etc.)
Breakpoints
- Import from:
@/breakpoints
- Defined via: Babel plugin in
.babelrc.js with typing in src/babel.d.ts
- Usage:
{ default: value, [breakpoints.md]: largeScreenValue }
Custom CSS Prop
- Use
css={styles.someStyle} prop instead of {...stylex.props(styles.someStyle)}
- Transpiled by custom Babel plugin
- Supports arrays:
css={[styles.base, isActive && styles.active]}
Complete Example
import * as stylex from "@stylexjs/stylex";
import { breakpoints } from "@/breakpoints";
import { color, controlSize, font } from "@/tokens.stylex";
function Button({ children, isActive, hideLabelOnMobile, ...props }) {
return (
<button
{...props}
css={[
styles.button,
isActive && styles.active,
hideLabelOnMobile && styles.hideLabelOnMobile,
]}
>
{children}
</button>
);
}
const styles = stylex.create({
button: {
// Use design tokens
fontSize: controlSize._4,
fontWeight: font.weight_5,
minHeight: controlSize._9,
paddingBlock: controlSize._1,
paddingInline: controlSize._3,
// Responsive design with breakpoints
display: { default: "none", [breakpoints.md]: "inline-flex" },
// Theme-aware colors
color: color.textMain,
backgroundColor: {
default: color.backgroundRaised,
":hover": color.backgroundHover,
},
},
active: {
backgroundColor: color.controlActive,
color: color.textOnActive,
},
hideLabelOnMobile: {
paddingLeft: {
default: controlSize._3,
[breakpoints.md]: controlSize._2,
},
},
});
Best Practices
- Always use design tokens - Never hardcode colors, spacing, or font values
- Use the css prop - Don't use
{...stylex.props()} directly
- Conditional styles with arrays -
css={[base, condition && conditional]}
- Responsive by default - Consider mobile-first with breakpoint overrides
- Theme-aware colors - Use color tokens that adapt to light/dark themes
- Pseudo-selectors in objects -
{ default: value, ":hover": hoverValue }
Common Patterns
Responsive Display
display: { default: "none", [breakpoints.md]: "flex" }
Conditional Styles
css={[styles.base, isActive && styles.active, hasError && styles.error]}
Hover States
backgroundColor: {
default: color.backgroundRaised,
":hover": color.backgroundHover,
}
Mobile-First Padding
padding: {
default: controlSize._2,
[breakpoints.md]: controlSize._4,
[breakpoints.lg]: controlSize._6,
}
1---2name: styling-with-stylex3description: StyleX styling patterns using design tokens, breakpoints, and custom css prop. Use when working with styles, CSS, design tokens, breakpoints, responsive design, themes, styling components, css prop, stylex.create, or when the user mentions StyleX, tokens.stylex, controlSize, color tokens, or breakpoints.4---5
6# StyleX Styling
7
8## Overview
9
10This project uses StyleX for styling with design tokens, responsive breakpoints, and theme-aware colors.
11
12## Key Patterns
13
14### Design Tokens
15
16- **Import tokens from**: `@/tokens.stylex.ts`
17- **Available token categories**:
18 - `color` - Theme-aware colors (textMain, backgroundRaised, controlActive, etc.)
19 - `controlSize` - Spacing and sizing values (\_1 through \_9)
20 - `font` - Typography values (weight_5, etc.)
21
22### Breakpoints
23
24- **Import from**: `@/breakpoints`
25- **Defined via**: Babel plugin in `.babelrc.js` with typing in `src/babel.d.ts`
26- **Usage**: `{ default: value, [breakpoints.md]: largeScreenValue }`
27
28### Custom CSS Prop
29
30- Use `css={styles.someStyle}` prop instead of `{...stylex.props(styles.someStyle)}`
31- Transpiled by custom Babel plugin
32- Supports arrays: `css={[styles.base, isActive && styles.active]}`
33
34## Complete Example
35
36```tsx
37import * as stylex from "@stylexjs/stylex";
38import { breakpoints } from "@/breakpoints";
39import { color, controlSize, font } from "@/tokens.stylex";
40
41function Button({ children, isActive, hideLabelOnMobile, ...props }) {
42 return (
43 <button
44 {...props}
45 css={[
46 styles.button,
47 isActive && styles.active,
48 hideLabelOnMobile && styles.hideLabelOnMobile,
49 ]}
50 >
51 {children}
52 </button>
53 );
54}
55
56const styles = stylex.create({
57 button: {
58 // Use design tokens
59 fontSize: controlSize._4,
60 fontWeight: font.weight_5,
61 minHeight: controlSize._9,
62 paddingBlock: controlSize._1,
63 paddingInline: controlSize._3,
64
65 // Responsive design with breakpoints
66 display: { default: "none", [breakpoints.md]: "inline-flex" },
67
68 // Theme-aware colors
69 color: color.textMain,
70 backgroundColor: {
71 default: color.backgroundRaised,
72 ":hover": color.backgroundHover,
73 },
74 },
75 active: {
76 backgroundColor: color.controlActive,
77 color: color.textOnActive,
78 },
79 hideLabelOnMobile: {
80 paddingLeft: {
81 default: controlSize._3,
82 [breakpoints.md]: controlSize._2,
83 },
84 },
85});
86```
87
88## Best Practices
89
901. **Always use design tokens** - Never hardcode colors, spacing, or font values
912. **Use the css prop** - Don't use `{...stylex.props()}` directly
923. **Conditional styles with arrays** - `css={[base, condition && conditional]}`
934. **Responsive by default** - Consider mobile-first with breakpoint overrides
945. **Theme-aware colors** - Use color tokens that adapt to light/dark themes
956. **Pseudo-selectors in objects** - `{ default: value, ":hover": hoverValue }`
96
97## Common Patterns
98
99### Responsive Display
100
101```tsx
102display: { default: "none", [breakpoints.md]: "flex" }
103```
104
105### Conditional Styles
106
107```tsx
108css={[styles.base, isActive && styles.active, hasError && styles.error]}
109```
110
111### Hover States
112
113```tsx
114backgroundColor: {
115 default: color.backgroundRaised,
116 ":hover": color.backgroundHover,
117}
118```
119
120### Mobile-First Padding
121
122```tsx
123padding: {
124 default: controlSize._2,
125 [breakpoints.md]: controlSize._4,
126 [breakpoints.lg]: controlSize._6,
127}
128```