— Styling · @vitus-labs/rocketstyle
Variants as data. Props for free.
Describe a component's looks as dimensions: states, sizes, variants or your own. Each dimension becomes a chain method and a typed prop, so <Button danger lg outline> just works. Values switch with light/dark through mode(), and the CSS goes through whichever engine you plugged into @vitus-labs/core.
npm i @vitus-labs/rocketstyle @vitus-labs/core- built-in dimensions
- 5
- per value, via mode()
- light · dark
- selector specificity of its rules
- (0,2,0)
— Why rocketstyle
Stop writing ternaries in CSS.
Variants are data
A dimension is a map from option names to theme objects. Rocketstyle merges the base theme with every active option and hands the result to your styles as $rocketstyle, with no conditionals in the template.
Props that write themselves
Each dimension adds a chain method (.states(), .sizes()) and a prop (state, size). Boolean shorthand is on by default, so <Button primary lg> works, and TypeScript infers the allowed values from your keys.
Light and dark, per value
Write mode('#fff', '#111') anywhere in a theme. It resolves against the active mode from Provider, and inversed flips it for a subtree. Computed themes are cached in WeakMaps, per mode.
Any engine underneath
Styles are built with @vitus-labs/core's css and styled, so the same component renders on styler, styled-components or Emotion, whichever connector you pass to init().
— Before / after
Same button, zero ternaries.
One button: two states plus a default, three sizes, an outline modifier and light/dark. On the left the variant logic lives in the template; on the right it's data.
import { styled } from '@vitus-labs/styler'
const bg = { primary: '#2f4dff', danger: '#d93a14' }
const pad = { sm: '6px 12px', md: '8px 16px', lg: '12px 24px' }
const Button = styled.button`
padding: ${({ $size = 'md' }) => pad[$size]};
background: ${({ $state, $dark }) =>
$state ? bg[$state] : $dark ? '#1e2025' : '#fff'};
color: ${({ $state, $dark }) =>
$state || $dark ? '#fff' : '#0a0b0d'};
${({ $outline, $state }) =>
$outline &&
`background: transparent; color: ${bg[$state ?? 'primary']};`}
`
<Button $state="danger" $size="lg" $outline $dark={isDark} />Every new option touches every interpolation, and the dark-mode flag travels as a prop.
import { rocketstyle } from '@vitus-labs/rocketstyle'
import { Element } from '@vitus-labs/elements'
const Button = rocketstyle()({ name: 'Button', component: Element })
.attrs({ tag: 'button' })
.theme((t, mode) => ({
backgroundColor: mode('#fff', '#1e2025'),
color: mode('#0a0b0d', '#fff'),
}))
.states({
primary: { backgroundColor: '#2f4dff', color: '#fff' },
danger: { backgroundColor: '#d93a14', color: '#fff' },
})
.sizes({
sm: { paddingX: 12, paddingY: 6 },
md: { paddingX: 16, paddingY: 8 },
lg: { paddingX: 24, paddingY: 12 },
})
.modifiers({
outline: (t) => ({ backgroundColor: 'transparent', color: t.backgroundColor }),
})
.styles((css) => css`
${({ $rocketstyle }) => makeItResponsive({ theme: $rocketstyle, styles, css })};
`)
<Button danger lg outline />Options are theme objects. The outline modifier derives from whatever state is active, and the mode comes from context.
— Live, not a screenshot
Flip a dimension, read the CSS.
A real rocketstyle Badge with states, sizes, a transform modifiers dimension and mode() colours. The rule on the right is read live from the stylesheet. Note the doubled class selector.
const Badge = rocketstyle()({ name: 'Badge', component: Element })
.attrs({ tag: 'span' })
.theme({ fontFamily: mono, borderRadius: 999, borderWidth: 1 })
.states((t, mode) => ({
info: { backgroundColor: mode('#2f4dff', '#7aa2ff') },
success: { backgroundColor: mode('#5e7f0c', '#c8ff3a') },
danger: { backgroundColor: mode('#d93a14', '#ff7a59') },
}))
.sizes({
sm: { fontSize: 11, paddingX: 10, paddingY: 6 },
md: { fontSize: 13, paddingX: 14, paddingY: 8 },
lg: { fontSize: 16, paddingX: 20, paddingY: 11 },
})
// transform dimension: derives from the theme so far
.modifiers(() => ({
soft: (t) => ({ color: t.backgroundColor, backgroundColor: tint(t) }),
outline: (t) => ({ color: t.backgroundColor, borderColor: t.backgroundColor,
backgroundColor: 'transparent' }),
}))
.styles(themeToCss)<Provider mode="dark"><Badge success md />reading the stylesheet…
— Features
What a dimension gets you.
- Five built-in dimensions
states,sizes,variants, the multi-selectmultipleand the multi-select transformmodifiers - Custom dimensions
rocketstyle({ dimensions: { intent: 'intent' } })adds an.intent()method and anintentprop - Boolean shorthand
<Button primary lg>; for a single-select dimension the last matching prop wins, multi-select collects them all - Multi-select
{ propName, multi: true }lets several options apply at once, as an array or as booleans - Transform dimensions
evaluated last, with functions that receive the theme accumulated so far, for derived looks like outline or soft
- mode(light, dark)
mode-aware values anywhere in a theme, resolved per render against
Provider's mode - $rocketstyle · $rocketstate
the merged theme and the active dimension and pseudo state, as interpolation props in
.styles() - Provider / consumer
.config({ provider: true })tracks hover, focus and pressed; children react throughconsumer - Immutable chain
every call returns a new component, so
Button.states({...})forks a variant without touchingButton - attrs, compose, statics
the same default-props, HOC and metadata chain as
@vitus-labs/attrs - Boosted rules
its CSS uses a doubled class selector, so it wins over the styles of the component it wraps
- Introspection
getStaticDimensions(theme)lists a component's dimensions; rocketstories generates stories from it
— API
One factory, one chain.
Every name links to its reference in the docs.
— Honest limits
When a dimension is overkill.
Where it isn't the right fit, and what to reach for instead.
Components with one look
A dimension system is structure you pay for in setup. For a one-off box, a plain
styledcomponent is shorter and just as fast.Theme values aren't CSS yet
Rocketstyle computes
$rocketstyle; your.styles()callback decides how it becomes CSS. Most setups pass it to unistyle'smakeItResponsive, but you write that callback (once, and reuse it).A css prop can't beat the theme
Rocketstyle's rules use a doubled class selector on purpose, so the theme wins over the styles of the component it wraps. A single-class style on the same element, such as an Element's
cssprop, can't override a value the theme sets. Go through the chain with.theme(), a dimension value or.styles(); for a one-off, raise specificity with&&& { … }or use!important.Swapping the base resets the chain
.config({ component })with a different component drops the earlierattrs,priorityAttrsandcomposechains, because they were written for the old component's props. Re-chain them explicitly.Shorthand can collide with DOM props
With boolean shorthand, a truthy prop whose name matches an option activates it. Don't name options after props you also pass (like
disabled), or setuseBooleans: false.Reserved names are checked in development only
Dimensions can't be called
theme,styles,attrs,compose,light,darkor a config key. The factory throws in development; production builds skip the check.
— Recipes
Copy, paste, ship.
Your own dimensions
Name the axes your design system actually has. Each one becomes a chain method and a prop; multi: true lets options stack.
import { rocketstyle } from '@vitus-labs/rocketstyle'
import { Element } from '@vitus-labs/elements'
const ds = rocketstyle({
dimensions: {
intents: 'intent',
sizes: 'size',
looks: { propName: 'look', multi: true },
},
})
export const Button = ds({ name: 'Button', component: Element })
.attrs({ tag: 'button' })
.intents({ neutral: { color: '#0a0b0d' }, brand: { color: '#2f4dff' } })
.sizes({ sm: { fontSize: 13 }, lg: { fontSize: 17 } })
.looks({ rounded: { borderRadius: 999 }, raised: { boxShadow: '0 2px 8px #0002' } })
// <Button brand lg rounded raised>Save</Button>Derive a look from the active state
Transform dimensions run last and receive the theme so far, so one outline definition works for every state, including ones added later.
const Tag = rocketstyle()({ name: 'Tag', component: Element })
.states({
info: { backgroundColor: '#2f4dff', color: '#fff' },
danger: { backgroundColor: '#d93a14', color: '#fff' },
})
.modifiers({
outline: (t, appTheme, mode) => ({
backgroundColor: 'transparent',
color: t.backgroundColor,
borderColor: mode(t.backgroundColor, '#fff'),
}),
})
// <Tag danger outline /> → transparent, red text, red border (light mode)A child that reacts to its parent's hover
A provider component tracks its own hover, focus and pressed state; components inside read it through consumer and map it to their own dimensions.
const ButtonGroup = Button.config({ provider: true })
const ButtonIcon = rocketstyle()({ name: 'ButtonIcon', component: Element })
.config({
consumer: (ctx) =>
ctx<typeof ButtonGroup>(({ pseudo }) => ({
state: pseudo.hover ? 'active' : 'default',
})),
})
.states({
default: { color: '#666' },
active: { color: '#fff' },
})
// <ButtonGroup primary><ButtonIcon /> Save</ButtonGroup>Dark mode for a whole subtree
mode() values resolve against the nearest Provider. inversed flips the mode for a section, such as a dark footer on a light page.
import { Provider } from '@vitus-labs/rocketstyle'
export const App = () => (
<Provider theme={theme} mode={prefersDark ? 'dark' : 'light'}>
<Page />
<Provider inversed>
<Footer />
</Provider>
</Provider>
)— FAQ
Questions, answered.
Do I have to use styler?
No. Rocketstyle builds its styles with @vitus-labs/core's css and styled, which delegate to the connector you pass to init(): styler, styled-components or Emotion.
Where do hover and focus styles go?
Usually in the theme as nested objects (hover: { … }), turned into &:hover by your .styles() callback. For JS-tracked state shared with children, make the parent a provider with .config({ provider: true }).
What's the difference between variants and modifiers?
variants is a regular single-select dimension. modifiers is multi-select and a transform: its options are functions of the theme accumulated so far, applied last.
Can tools read a component's options?
Yes. Every rocketstyle component has getStaticDimensions(theme) and getDefaultAttrs(). @vitus-labs/rocketstories uses them to generate a Storybook story for each dimension.
Is this site built with it?
Yes. The buttons, cards, headings and badges on this page are rocketstyle components on Element, styled by styler.
— @vitus-labs/rocketstyle
Name the dimensions. Skip the ternaries.
npm i @vitus-labs/rocketstyle @vitus-labs/coreSources · 31 claims, each traced to v2.9.1 code or docs
- Five built-in dimensions: states, sizes, variants, multiple (multi), modifiers (multi + transform)ui-system:packages/rocketstyle/src/constants/defaultDimensions.ts
- Each dimension creates a chain method and a corresponding propnpm:@vitus-labs/rocketstyle/README.md
- Boolean shorthand on by default (useBooleans defaults to true)ui-system:packages/rocketstyle/src/init.ts
- Single-select: last matching truthy prop wins; multi-select collects matchesui-system:packages/rocketstyle/src/utils/attrs.ts
- Truthy prop with an option's name activates it (DOM prop collision)ui-system:packages/rocketstyle/src/utils/attrs.ts
- TypeScript infers dimension props (ExtractDimensionProps, type tests)ui-system:packages/rocketstyle/src/__tests__/ExtractProps.test-d.ts
- mode(light, dark) callback type; theme callbacks receive (theme, mode, css)npm:@vitus-labs/rocketstyle/lib/index.d.ts
- mode() values resolved per active mode (including those returned by transform callbacks)ui-system:packages/rocketstyle/src/utils/theme.ts
- Provider mode prop, inversed flips mode for a subtreeui-system:packages/rocketstyle/src/context/context.tsx
- Computed themes cached in WeakMaps, per modeui-system:packages/rocketstyle/src/cache/LocalThemeManager.ts
- Styles built with core's config.css / config.styled → engine via connectorui-system:packages/rocketstyle/src/utils/styles.ts
- Connectors exist for styler, styled-components and Emotionui-system:packages/connector-emotion/package.json
- Connector for styled-componentsui-system:packages/connector-styled-components/package.json
- Rules use a doubled class selector (boost) → specificity (0,2,0)ui-system:packages/rocketstyle/src/rocketstyle.tsx
- Boost semantics: doubled selector raises specificity from (0,1,0) to (0,2,0)npm:@vitus-labs/styler/lib/index.d.ts
- The doubled selector is intentional; a single-class css prop on the same element cannot override a theme valuenpm:@vitus-labs/rocketstyle/README.md
- Override via .theme(), a dimension value or .styles(); one-off via &&& {} or !importantnpm:@vitus-labs/rocketstyle/README.md
- Transform dimensions evaluated last with the accumulated theme; callback (t, appTheme, mode, css)ui-system:packages/rocketstyle/src/utils/theme.ts
- $rocketstyle (merged theme) and $rocketstate passed to .styles()npm:@vitus-labs/rocketstyle/README.md
- Provider components track hover / focus / pressed via eventsui-system:packages/rocketstyle/src/context/createLocalProvider.tsx
- Consumer recipe (ButtonGroup / ButtonIcon)npm:@vitus-labs/rocketstyle/README.md
- attrs / compose / statics follow @vitus-labs/attrs APInpm:@vitus-labs/rocketstyle/README.md
- Swapping base component drops attrs, priorityAttrs and composeui-system:packages/rocketstyle/src/rocketstyle.tsx
- Reserved dimension names (theme modes, config keys, theme/styles/compose, attrs)ui-system:packages/rocketstyle/src/constants/index.ts
- Reserved-name validation only outside productionui-system:packages/rocketstyle/src/init.ts
- getStaticDimensions / getDefaultAttrs staticsui-system:packages/rocketstyle/src/rocketstyle.tsx
- rocketstories generates stories from getStaticDimensionsui-system:packages/rocketstories/src/internal/RocketStoryHoc.tsx
- Runtime exports: rocketstyle (default + named), Provider, context, isRocketComponentnpm:@vitus-labs/rocketstyle/lib/index.d.ts
- Theme → CSS conversion is the .styles() callback's job (typically unistyle makeItResponsive)npm:@vitus-labs/rocketstyle/README.md
- This site's buttons, cards, headings are rocketstyle components on Elementsite:src/ui/Button.tsx
- Peer dependencies: react >= 19 and @vitus-labs/corenpm:@vitus-labs/rocketstyle/package.json