— 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.

@vitus-labs/rocketstylev2.9.1 · MIT
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.

01

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.

02

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.

03

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.

04

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.

beforeHand-rolled variants
Button.tsx
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.

after@vitus-labs/rocketstyle
Button.tsx
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.

v2 shipped
state
size
modifier
mode
Badge.tsx
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 />
$rocketstyle → CSSlive
reading the stylesheet…

— Features

What a dimension gets you.

  • Five built-in dimensions

    states, sizes, variants, the multi-select multiple and the multi-select transform modifiers

  • Custom dimensions

    rocketstyle({ dimensions: { intent: 'intent' } }) adds an .intent() method and an intent prop

  • 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 through consumer

  • Immutable chain

    every call returns a new component, so Button.states({...}) forks a variant without touching Button

  • 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 styled component 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's makeItResponsive, 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 css prop, 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 earlier attrs, priorityAttrs and compose chains, 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 set useBooleans: false.

  • Reserved names are checked in development only

    Dimensions can't be called theme, styles, attrs, compose, light, dark or a config key. The factory throws in development; production builds skip the check.

— Recipes

Copy, paste, ship.

01

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.

Button.tsx
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>
02

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.

Tag.tsx
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)
03

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.

ButtonIcon.tsx
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>
04

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.

App.tsx
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/core
Sources · 31 claims, each traced to v2.9.1 code or docs
  1. Five built-in dimensions: states, sizes, variants, multiple (multi), modifiers (multi + transform)ui-system:packages/rocketstyle/src/constants/defaultDimensions.ts
  2. Each dimension creates a chain method and a corresponding propnpm:@vitus-labs/rocketstyle/README.md
  3. Boolean shorthand on by default (useBooleans defaults to true)ui-system:packages/rocketstyle/src/init.ts
  4. Single-select: last matching truthy prop wins; multi-select collects matchesui-system:packages/rocketstyle/src/utils/attrs.ts
  5. Truthy prop with an option's name activates it (DOM prop collision)ui-system:packages/rocketstyle/src/utils/attrs.ts
  6. TypeScript infers dimension props (ExtractDimensionProps, type tests)ui-system:packages/rocketstyle/src/__tests__/ExtractProps.test-d.ts
  7. mode(light, dark) callback type; theme callbacks receive (theme, mode, css)npm:@vitus-labs/rocketstyle/lib/index.d.ts
  8. mode() values resolved per active mode (including those returned by transform callbacks)ui-system:packages/rocketstyle/src/utils/theme.ts
  9. Provider mode prop, inversed flips mode for a subtreeui-system:packages/rocketstyle/src/context/context.tsx
  10. Computed themes cached in WeakMaps, per modeui-system:packages/rocketstyle/src/cache/LocalThemeManager.ts
  11. Styles built with core's config.css / config.styled → engine via connectorui-system:packages/rocketstyle/src/utils/styles.ts
  12. Connectors exist for styler, styled-components and Emotionui-system:packages/connector-emotion/package.json
  13. Connector for styled-componentsui-system:packages/connector-styled-components/package.json
  14. Rules use a doubled class selector (boost) → specificity (0,2,0)ui-system:packages/rocketstyle/src/rocketstyle.tsx
  15. Boost semantics: doubled selector raises specificity from (0,1,0) to (0,2,0)npm:@vitus-labs/styler/lib/index.d.ts
  16. The doubled selector is intentional; a single-class css prop on the same element cannot override a theme valuenpm:@vitus-labs/rocketstyle/README.md
  17. Override via .theme(), a dimension value or .styles(); one-off via &&& {} or !importantnpm:@vitus-labs/rocketstyle/README.md
  18. Transform dimensions evaluated last with the accumulated theme; callback (t, appTheme, mode, css)ui-system:packages/rocketstyle/src/utils/theme.ts
  19. $rocketstyle (merged theme) and $rocketstate passed to .styles()npm:@vitus-labs/rocketstyle/README.md
  20. Provider components track hover / focus / pressed via eventsui-system:packages/rocketstyle/src/context/createLocalProvider.tsx
  21. Consumer recipe (ButtonGroup / ButtonIcon)npm:@vitus-labs/rocketstyle/README.md
  22. attrs / compose / statics follow @vitus-labs/attrs APInpm:@vitus-labs/rocketstyle/README.md
  23. Swapping base component drops attrs, priorityAttrs and composeui-system:packages/rocketstyle/src/rocketstyle.tsx
  24. Reserved dimension names (theme modes, config keys, theme/styles/compose, attrs)ui-system:packages/rocketstyle/src/constants/index.ts
  25. Reserved-name validation only outside productionui-system:packages/rocketstyle/src/init.ts
  26. getStaticDimensions / getDefaultAttrs staticsui-system:packages/rocketstyle/src/rocketstyle.tsx
  27. rocketstories generates stories from getStaticDimensionsui-system:packages/rocketstories/src/internal/RocketStoryHoc.tsx
  28. Runtime exports: rocketstyle (default + named), Provider, context, isRocketComponentnpm:@vitus-labs/rocketstyle/lib/index.d.ts
  29. Theme → CSS conversion is the .styles() callback's job (typically unistyle makeItResponsive)npm:@vitus-labs/rocketstyle/README.md
  30. This site's buttons, cards, headings are rocketstyle components on Elementsite:src/ui/Button.tsx
  31. Peer dependencies: react >= 19 and @vitus-labs/corenpm:@vitus-labs/rocketstyle/package.json