Skip to content

Design systems ​

The components carry the behaviour (input, focus, states, layout). The look is data: a theme. The scene below is the same set of components, untouched, under five themes. Pick one at the top.

Neon, Glass, Brutal and Clay ship with the library as themePresets. They are not special: each is a plain ThemeTokens object, written with defineTheme, that you can copy and change. The playground has the same switcher for every component it shows: open it.

What a theme can change ​

LayerChangesExample
colorsEvery surface, text, accent and state colourInk on paper, neon on black
radiusCorner radii, from square to pill{ sm: 0, md: 0, lg: 0, full: 0 }
typographySizes, line heights and weights per text styleBold labels
interactionTarget size, how far a press sinks, hover scalepressDepth: 12 for a deep press
componentsRaw uikit properties per component and per partThick borders, a diamond slider thumb, raised buttons

components is what turns a palette into a design system. For each component it takes ui (its outer element) and parts (its named inner parts, the same names as the parts prop), merged under what an instance passes:

ts
import { darkTheme, defineTheme } from 'vue-xr-ui'

export const neon = defineTheme(darkTheme, {
  name: 'neon',
  colors: { surface: '#0a0518', border: '#ff2bd6', primary: '#22e8ff', onPrimary: '#06030f' },
  radius: { sm: 2, md: 3, lg: 4, full: 4 },
  components: {
    XrPanel: { ui: { borderWidth: 2 } },
    XrSlider: {
      parts: {
        track: { height: 4, borderWidth: 0 },
        thumb: { width: 18, height: 18, backgroundColor: '#ff2bd6', transformRotateZ: 45 },
      },
    },
  },
})

When a style depends on the instance, give a function of its props:

ts
components: {
  // A hard offset shadow, drawn as thicker right and bottom borders.
  XrButton: (props) => ({
    ui: props.variant === 'ghost' ? {} : { borderWidth: 3, borderRightWidth: 6, borderBottomWidth: 6, borderColor: '#111' },
  }),
  XrText: (props) => (props.variant === 'title' ? { ui: { letterSpacing: 3 } } : {}),
}

Order of merging, last wins: the component's own defaults, then the theme's components entry, then the instance's ui and parts. One level deep, as with parts: a hover or active you give replaces the component's for that element, so keep transformTranslateZ in an active you write (it is the only feedback a fingertip gets).

Two things to keep in mind when writing one:

  • A colour you set wins over the colour the component picks for its state: a fixed borderColor on a text field or a select trigger hides their focused and open outline. Set widths there and let colors give the colour, and check props.disabled in a function style.
  • Components are built from other components: a checkbox's box is an <XrBox>, every label an <XrText>, a number input's steppers are <XrButton>s, a select's list and a dialog's body are <XrPanel>s. A theme entry for XrBox or XrText reaches those too, so keep layout (padding, sizes) out of those two and style the parts instead.

Using one ​

For the whole app:

ts
import { createXrUi, themePresets } from 'vue-xr-ui'
app.use(createXrUi({ theme: themePresets.brutal }))

Switch at runtime, as the demo above does:

ts
const theme = useXrTheme()
theme.value = themePresets.glass

Or for one part of the scene, with <XrThemeProvider :theme="themePresets.clay">.

The four presets ​

PresetLookBuilt with
neonThemeNear-black, cyan and magenta outlines, sharp corners, spaced headingsColours, small radii, borders on panels and controls, a diamond slider thumb, letterSpacing on titles
glassThemeTranslucent smoked panels, bright bent edges, pills, white selectionTranslucent colours, large radii, borderBend on edges
brutalThemeCream paper, black ink, square corners, hard offset shadowsSquare radii, bold type, thick right and bottom borders as shadows
clayThemeSoft pastels, very round, controls raised off the panel that sink deeptransformTranslateZ on buttons and tabs, pressDepth: 12, a soft bottom edge

Further than a theme ​

A theme restyles every instance. For one instance, use its ui and parts props; for a control whose shape is different altogether, build it with <XrBox> and the headless composables. All three: Build your own look.

MIT License