Skip to content

Guides

Extending and overriding

One verb changes a stylesheet without editing it: sheet.extend(rules, variants?). It returns a new sheet and leaves the original as it was. Use the new sheet in a component of your own, or give it to StyleOverrides to restyle components you do not own inside one subtree.

The examples change the button from Getting Started: its buttonStyles sheet has an accent background and a size variant that sets the padding.

The extension wins

The rules of an extension sit in a layer above the sheet's variants. A value the extension sets holds whatever variant is selected; a value it does not mention is left to the original sheet. The new sheet keeps the same parts, variant axes and defaults.

link-button.styles.ts
import { buttonStyles } from './styles.ts'

export const linkButtonStyles = buttonStyles.extend({
  // The background changes. The padding is still set by the size variants.
  Root: { background: 'neutral' },
})

extend restyles the parts a sheet has. Naming a part it does not have is a type error and a runtime error, and a part's $kind cannot change. A component with more parts needs a stylesheet of its own.

Restating variants and removing values

Because the extension wins, a value it sets is the same for every variant. To vary it again, pass a second argument: variant rules over the axes the sheet already has. Those win over everything. null removes the inherited value at that exact place.

compact-button.styles.ts
import { buttonStyles } from './styles.ts'

export const compactButtonStyles = buttonStyles.extend(
  {
    // Wins over the padding that both size variants set.
    Root: { padding: 1 },
    // Removes the base value. A variant that sets text still applies.
    Label: { text: null },
  },
  $ => ({
    // Restated for one size, above the rules of the extension.
    [$.size('m')]: { Root: { padding: 2 }, Label: { text: 'caption' } },
  }),
)

The rules can also be a callback, q => ({ … }), over the sheet's query builder. A derived sheet is an ordinary sheet. Bind it like the original:

CompactButton.tsx
import { createElements } from '@toned/react'
import { compactButtonStyles } from './compact-button.styles.ts'

const S = createElements(compactButtonStyles)

export function CompactButton({ label, size = 'm' }: {
  label: string
  size?: 's' | 'm'
}) {
  return (
    <S size={size}>
      <S.Root as="button" type="button">
        <S.Label as="span">{label}</S.Label>
      </S.Root>
    </S>
  )
}

An existing component, inside one subtree

StyleOverrides takes the same kind of derived sheet. A component inside it that renders buttonStyles resolves the derived sheet instead, without being changed itself. An entry written as { sheet, scope } applies only where the host's ambient scope matches. Declare the list at module scope so it is the same array on every render.

Toolbar.tsx
import { StyleOverrides } from '@toned/react'
import { Button } from './Button.tsx'
import { buttonStyles } from './styles.ts'

const toolbarOverrides = [
  buttonStyles.extend({ Root: { background: 'neutral' } }, $ => ({
    [$.size('s')]: { Root: { padding: 2 } },
  })),
  // Applies only where the ambient scope matches 'editor/toolbar'.
  {
    sheet: buttonStyles.extend({ Root: { padding: 3 } }),
    scope: 'editor/toolbar',
  },
]

export function Toolbar() {
  return (
    <StyleOverrides value={toolbarOverrides}>
      <Button label="Undo" size="s" />
      <Button label="Redo" size="s" />
    </StyleOverrides>
  )
}

An entry applies to the sheet it was derived from. One derived from buttonStyles does not reach a component that renders compactButtonStyles; derive from that sheet for its own entry. Nested StyleOverrides add to the outer ones, and the later entry wins where both set a value. Passing a sheet that was not derived throws.

Include derived sheets in the build

A sheet from extend that adds a condition, a state or web rules needs CSS of its own. List it in the build's sheets with the others. The same holds for a sheet given to StyleOverrides, which is why these are module-scope values rather than something built during render.

vite.config.ts
import toned from '@toned/core/vite'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
import { compactButtonStyles } from './compact-button.styles.ts'
import { linkButtonStyles } from './link-button.styles.ts'
import { buttonStyles } from './styles.ts'
import { ui } from './system.ts'

export default defineConfig({
  plugins: [
    toned({
      system: ui,
      sheets: [buttonStyles, linkButtonStyles, compactButtonStyles],
      inputs: ['system.ts', 'styles.ts', 'link-button.styles.ts', 'compact-button.styles.ts'],
    }),
    react(),
  ],
})

Choosing one

If the difference is a fixed set of choices, add a variant to the sheet instead: it is typed at the call site and needs none of the above. Use extend for a related component of your own, and give the derived sheet to StyleOverrides when the component is not yours to change or the change belongs to one region of the application. The React reference specifies the precedence in full.