Styling
Vase UI components ship no styles. Style them with Tailwind CSS, CSS Modules, scoped styles or any other tool.
Style hooks
CSS classes
A part that renders an element takes the class attribute.
<script setup lang="ts">
import { Toggle } from '@vase-ui/vue/toggle';
</script>
<template>
<Toggle class="Toggle" aria-label="Bold" />
</template>For a class that depends on the state, render your own element in the element slot, and bind :class on it.
<script setup lang="ts">
import { TimePicker } from '@vase-ui/vue/time-picker';
</script>
<template>
<TimePicker.Root>
<TimePicker.List aria-label="Times">
<template #item="{ time }">
<TimePicker.Item :time="time">
<template #element="{ props, elementRef, state, text }">
<button v-bind="props" :ref="elementRef" :class="state.selected ? 'selected' : 'idle'">
{{ text }}
</button>
</template>
</TimePicker.Item>
</template>
</TimePicker.List>
</TimePicker.Root>
</template>State selectors
Select a state of a part with the selector that the browser gives for it:
- A CSS pseudo-class, where the element has one:
:checkedon a checkbox,:disabledon a native button. - An ARIA attribute that the part writes for accessibility:
[aria-expanded="true"]on a trigger,[aria-pressed="true"]on a toggle. - A
data-*attribute, for each other state:[data-orientation="vertical"]on a toggle group,[data-open]on a collapsible.
.Trigger[aria-expanded='true'] {
font-weight: 600;
}
.Trigger:disabled {
opacity: 0.5;
}The API reference of each component lists the selectors of each part.
CSS variables
Some parts write CSS variables with values that the part measures. For example, Popover.Positioner writes --available-height and --anchor-width:
.Popup {
max-height: var(--available-height);
}The API reference of each component lists the CSS variables of each part.
Inline styles
A part that renders an element takes the style attribute.
<script setup lang="ts">
import { Toggle } from '@vase-ui/vue/toggle';
</script>
<template>
<Toggle :style="{ height: '100px' }" aria-label="Bold" />
</template>For a style that depends on the state, bind :style on your element in the element slot.
<script setup lang="ts">
import { TimePicker } from '@vase-ui/vue/time-picker';
</script>
<template>
<TimePicker.Root>
<TimePicker.List aria-label="Times">
<template #item="{ time }">
<TimePicker.Item :time="time">
<template #element="{ props, elementRef, state, text }">
<button
v-bind="props"
:ref="elementRef"
:style="{ color: state.selected ? 'red' : 'blue' }"
>
{{ text }}
</button>
</template>
</TimePicker.Item>
</template>
</TimePicker.List>
</TimePicker.Root>
</template>Scoped styles
A part that renders its own element has one root node: that element. A rule of <style scoped>, v-show and a custom directive on the part apply to it. An element that you render in the element slot is in your template, so it has your scope.
<script setup lang="ts">
import { DateField } from '@vase-ui/vue/date-field';
</script>
<template>
<DateField.Root class="root" aria-label="Date">
<DateField.Input class="input">
<template #segment="{ segment }">
<DateField.Segment v-if="segment.editable" :segment="segment" />
<DateField.Literal v-else :segment="segment" />
</template>
</DateField.Input>
</DateField.Root>
</template>
<style scoped>
.input {
background-color: green;
}
.root {
border: 1px solid green;
}
</style>A Portal part has no root element. Write your own element in it, and give the rule to that element:
<script setup lang="ts">
import { Popover } from '@vase-ui/vue/popover';
</script>
<template>
<Popover.Root>
<Popover.Trigger>Open</Popover.Trigger>
<Popover.Portal>
<div class="portal">
<Popover.Positioner>
<Popover.Popup>Content</Popover.Popup>
</Popover.Positioner>
</div>
</Popover.Portal>
</Popover.Root>
</template>
<style scoped>
.portal {
isolation: isolate;
}
</style>NavigationMenu.Content renders its element in a <Teleport>, so a scoped rule does not reach it. Render your own element in its element slot.
Text direction
Set the dir attribute on <html>, or on an element around the parts. Each part reads the text direction of its own element, as a native control does. Its arrow keys and its layout follow that direction.
- In CSS,
:dir(rtl)selects an element in right-to-left text. - A popup in a portal is in
<body>, so it has the direction of<html>. For another direction, givedirto its positioner. - A server cannot read the direction of an element around a part.
Slider.Rootplaces its thumbs in the server HTML, so give it its owndir.
<script setup lang="ts">
import { Slider } from '@vase-ui/vue/slider';
</script>
<template>
<div dir="rtl">
<Slider.Root dir="rtl" :default-value="25">
<Slider.Control class="Control">
<Slider.Track class="Track">
<Slider.Indicator class="Indicator" />
<Slider.Thumb aria-label="Volume" class="Thumb" />
</Slider.Track>
</Slider.Control>
</Slider.Root>
</div>
</template>Zoom
A CSS zoom on a popup, or on an element in it, scales the popup and its arrow. The arrow points at the anchor, and --transform-origin is in the pixels of the popup.
Give zoom to the popup, not to its positioner. Floating UI places the positioner in the pixels of its offset parent, so a zoom of the positioner moves it away from its anchor.
Floating UI 1.8.0 reads a zoom of an ancestor only on the offset parent of the positioner, and only when that element has a height. Another zoom, such as one on <html> or <body>, moves the popup away from its anchor: up to 255 px at zoom: 1.5. With positionMethod: 'fixed', each zoom of an ancestor does this.
floating-ui#3492 corrects a zoom on <html> or <body>, and a zoom of an ancestor with positionMethod: 'fixed'. It reads the zoom from Element.currentCSSZoom, so it has no effect in a browser without that property. No release has it yet. It does not correct a zoom on an offset parent with no height, or on an element between the positioner and its offset parent: these still move the popup away from its anchor. For these cases, and until a release has the correction, render the portal with to into an element with the zoom, a position and a height.
In WebKit, when an ancestor and the popup both have a zoom, a margin of the popup or a padding of the positioner moves the arrow and the transform origin. A 16 px margin moves them 24 px under zoom: 1.5 on the ancestor.
Tailwind CSS
Give the Tailwind CSS classes to the class attribute of each part. Use the variants of the state selectors: aria-expanded:, disabled:, data-highlighted:.
<script setup lang="ts">
import { Menu } from '@vase-ui/vue/menu';
</script>
<template>
<Menu.Root>
<Menu.Trigger
class="flex h-8 items-center justify-center rounded-md border border-neutral-950 bg-white px-3 text-sm text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 data-pressed:bg-neutral-100 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700 dark:data-pressed:bg-neutral-800 dark:focus-visible:outline-white"
>
Song
</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner class="outline-hidden" :side-offset="8">
<Transition enter-from-class="scale-95 opacity-0" leave-to-class="scale-95 opacity-0">
<Menu.Popup
class="origin-(--transform-origin) border border-neutral-950 bg-white py-1 text-neutral-950 outline-hidden transition dark:border-white dark:bg-neutral-950 dark:text-white"
>
<Menu.Item
class="flex cursor-default py-2 pr-8 pl-4 text-sm leading-4 outline-hidden select-none data-highlighted:bg-neutral-950 data-highlighted:text-white dark:data-highlighted:bg-white dark:data-highlighted:text-neutral-950"
>
Add to Library
</Menu.Item>
<Menu.Item
class="flex cursor-default py-2 pr-8 pl-4 text-sm leading-4 outline-hidden select-none data-highlighted:bg-neutral-950 data-highlighted:text-white dark:data-highlighted:bg-white dark:data-highlighted:text-neutral-950"
>
Add to Playlist
</Menu.Item>
</Menu.Popup>
</Transition>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
</template>CSS Modules
Give the classes of a CSS module to the class attribute of each part. In a Vue file, write a <style module> block and use the $style object.
<script setup lang="ts">
import { Menu } from '@vase-ui/vue/menu';
</script>
<template>
<Menu.Root>
<Menu.Trigger :class="$style.Button">Song</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner :class="$style.Positioner" :side-offset="8">
<Transition :enter-from-class="$style.PopupHidden" :leave-to-class="$style.PopupHidden">
<Menu.Popup :class="$style.Popup">
<Menu.Item :class="$style.Item">Add to Library</Menu.Item>
<Menu.Item :class="$style.Item">Add to Playlist</Menu.Item>
</Menu.Popup>
</Transition>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
</template>
<style module>
.Button {
height: 2rem;
padding: 0 0.75rem;
}
.Positioner {
outline: 0;
}
.Popup {
padding: 0.25rem 0;
transition: opacity 150ms;
}
.PopupHidden {
opacity: 0;
}
.Item {
padding: 0.5rem 1rem;
}
.Item[data-highlighted] {
outline: 1px solid currentColor;
}
</style>An imported *.module.css file works in the same way.
CSS-in-JS
Give the class name that the library makes to the class attribute of the part.
Differences from Base UI
| Base UI | Vase UI |
|---|---|
className, a string or a function of the state | The class attribute. For a class from the state, bind :class on your element in the element slot. |
style, an object or a function of the state | The style attribute. For a style from the state, bind :style on your element in the element slot. |
A data attribute for each state, such as data-checked or data-disabled | A CSS pseudo-class or an ARIA attribute of the same element, where one gives the state. A data-* attribute for each other state. |
DirectionProvider and useDirection | The dir attribute on an element around the parts. Each part reads the direction of its own element. |
Constants of the names, such as MenuItemDataAttributes and MenuPositionerCssVariables | No constants. Write the selector or the variable name that the API reference of the part lists. |