Composition
A guide to composing Vase UI components with your own Vue components.
Composing custom components
A part renders one element. Two ways replace it:
- The
asprop takes a tag or a component. The part gives it the attributes, the listeners and the content of its own element. - The
elementslot gives full control: render your own element or component in it.
When a part has as and the element slot, the slot wins.
For example, a tab renders a <button>. The first tab of the sample gives a component to as. The second tab sets the size prop of the component, so it uses the slot.
<script setup lang="ts">
import { Tabs } from '@vase-ui/vue/tabs';
import MyButton from './MyButton.vue';
</script>
<template>
<Tabs.Root default-value="overview">
<Tabs.List>
<Tabs.Tab value="overview" :as="MyButton">Overview</Tabs.Tab>
<Tabs.Tab value="activity">
<template #element="{ props, elementRef }">
<MyButton v-bind="props" :ref="elementRef" size="lg">Activity</MyButton>
</template>
</Tabs.Tab>
</Tabs.List>
<Tabs.Panel value="overview">Workspace stats.</Tabs.Panel>
<Tabs.Panel value="activity">Recent activity.</Tabs.Panel>
</Tabs.Root>
</template>A part that is a native form control, another form element or an image has no as. Examples are Input, NumberField.Input, Checkbox, Fieldset.Root, Field.Label and Avatar.Image. Use the element slot.
The component needs one root element, and Vue gives the attributes to it. The part reads the element from the element that the component exposes, else from its root element. A component in Vapor mode has no root element for the part, so it must expose element. Expose element as a ref, so that the part sees each change of it.
With as, the part keeps its content and its own nodes, such as the hidden text of Progress.Root.
Composing multiple components
To compose more than one part, nest the element slots. In this sample, Tooltip.Trigger renders Dialog.Trigger, which renders Menu.Trigger, which renders MyButton:
<script setup lang="ts">
import { Dialog } from '@vase-ui/vue/dialog';
import { Menu } from '@vase-ui/vue/menu';
import { Tooltip } from '@vase-ui/vue/tooltip';
import MyButton from './MyButton.vue';
</script>
<template>
<Dialog.Root>
<Tooltip.Root>
<Menu.Root>
<Tooltip.Trigger>
<template #element="{ props: tooltipTrigger, elementRef: tooltipRef }">
<Dialog.Trigger v-bind="tooltipTrigger" :ref="tooltipRef">
<template #element="{ props: dialogTrigger, elementRef: dialogRef }">
<Menu.Trigger v-bind="dialogTrigger" :ref="dialogRef">
<template #element="{ props: menuTrigger, elementRef: menuRef }">
<MyButton v-bind="menuTrigger" :ref="menuRef" size="md">Open menu</MyButton>
</template>
</Menu.Trigger>
</template>
</Dialog.Trigger>
</template>
</Tooltip.Trigger>
<Tooltip.Portal>
<Tooltip.Positioner>
<Tooltip.Popup>Opens a menu and a dialog</Tooltip.Popup>
</Tooltip.Positioner>
</Tooltip.Portal>
<Menu.Portal>
<Menu.Positioner>
<Menu.Popup>
<Menu.Item>Add to Library</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
</Tooltip.Root>
<Dialog.Portal>
<Dialog.Popup>
<Dialog.Title>Dialog</Dialog.Title>
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>
</template>How attributes are merged
A part takes the props that it declares. It gives each other attribute and listener to its element, merged with its own by the rules of Vue:
- Vue joins the
classvalues and merges thestylevalues. - Vue calls each listener.
- For each other attribute, the value that you give wins. A value of
undefinedornullalso wins, and Vue then writes no attribute.
A part that writes its own id takes id as a prop. An id that is empty or holds only white space counts as no id, and the part keeps its own id.
In a Field, each control adds the ids of the descriptions and the errors of the field after the ids of your aria-describedby. With null, the control writes no aria-describedby.
Set a prop on the part, and not on the component in as or in the slot. For example, set disabled on Toolbar.Button: the toolbar then keeps the button in its keyboard navigation.
In the element slot, the attributes that you write beside v-bind="props" follow the same rules, in the order of the template: the later value wins. To merge in a script or a render function, use mergeProps of Vue. It merges in the order of its arguments.
<script setup lang="ts">
import { mergeProps } from 'vue';
import { Menu } from '@vase-ui/vue/menu';
import { Toolbar } from '@vase-ui/vue/toolbar';
function handleToolbarClick() {}
function handleMenuClick() {}
</script>
<template>
<Toolbar.Root>
<Menu.Root>
<Toolbar.Button
class="toolbar-button"
:style="{ color: 'red', padding: '4px' }"
@click="handleToolbarClick"
>
<template #element="{ props, elementRef }">
<Menu.Trigger
v-bind="
mergeProps(
{ class: 'menu-trigger', style: { color: 'blue' }, onClick: handleMenuClick },
props,
)
"
:ref="elementRef"
>
Open menu
</Menu.Trigger>
</template>
</Toolbar.Button>
</Menu.Root>
</Toolbar.Root>
</template>The <button> gets:
class="menu-trigger toolbar-button".style="color: red; padding: 4px;": the last argument wins.- The two click listeners.
handleMenuClickruns first, because it is in the first argument.
To stop an event before the part gets it, see Stop a native event before a part gets it.
Changing the default rendered element
The as prop also changes the tag of a part. For example, Accordion.Header renders an <h3>. The sample renders an <h2>, to fit the heading levels of the page:
<script setup lang="ts">
import { Accordion } from '@vase-ui/vue/accordion';
</script>
<template>
<Accordion.Root>
<Accordion.Item>
<Accordion.Header as="h2">
<Accordion.Trigger>What is Vase UI?</Accordion.Trigger>
</Accordion.Header>
<Accordion.Panel>Vase UI is a library of unstyled Vue components.</Accordion.Panel>
</Accordion.Item>
</Accordion.Root>
</template>Each part renders the most applicable element. Change it only when you have a reason.
The element slot
The element slot gets three props:
props: the attributes, the listeners, the class and the style of the part, merged with yours. Bind them withv-bind="props".elementRef: a function ref. Bind it with:ref="elementRef", so that the part gets your element.state: the state of the part. Use it for a class, a style or the content.
The part does not render its default slot in the element slot. Put the content in your element.
<script setup lang="ts">
import { Menu } from '@vase-ui/vue/menu';
import MyButton from './MyButton.vue';
</script>
<template>
<Menu.Root>
<Menu.Trigger>
<template #element="{ props, elementRef }">
<MyButton v-bind="props" :ref="elementRef" size="md">Open menu</MyButton>
</template>
</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner>
<Menu.Popup>
<Menu.Item>Add to Library</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
</template>Your element can be a component: it exposes element, or its root element is the element of the part. For a router link, use the custom form of RouterLink or NuxtLink, and render a native <a> in the element slot. The Navigation Menu page shows this.
An element takes one ref. To keep the ref of the part and add one of your own, call elementRef in a function ref: :ref="(element) => { elementRef(element); own = element }".
Slots and template refs
Some parts give more slot props. TimePicker.Item gives text, its time in the locale of the picker:
<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">
<template v-if="state.selected">Selected: </template>{{ text }}
</button>
</template>
</TimePicker.Item>
</template>
</TimePicker.List>
</TimePicker.Root>
</template>The element slot of Slider.Thumb gives its range input as inputProps and inputRef. Render the input in your element, or in another place:
<script setup lang="ts">
import { Slider } from '@vase-ui/vue/slider';
</script>
<template>
<Slider.Root :default-value="50">
<Slider.Control>
<Slider.Track>
<Slider.Thumb aria-label="Volume">
<template #element="{ props, elementRef, inputProps, inputRef }">
<span v-bind="props" :ref="elementRef">
<input v-bind="inputProps" :ref="inputRef" />
</span>
</template>
</Slider.Thumb>
</Slider.Track>
</Slider.Control>
</Slider.Root>
</template>A template ref on a part gives the members that the part exposes. element is its DOM element, or null while the part renders no element. Some parts expose more, such as close() of Menu.Root.
Wrap a part
Declare only your own props and events in a wrapper. Vue gives each attribute and listener that the wrapper does not declare to its root, the part. The part then keeps its default, or the value of its parent.
To type the props and the events of the part, extend its Props and Emits types with /* @vue-ignore */. The SFC compiler skips these types, so the wrapper declares no prop and no event at run time. The type check of the caller knows each prop, each v-model and each event of the part:
<script setup lang="ts">
import { Dialog } from '@vase-ui/vue/dialog';
import type { DialogRootEmits, DialogRootProps } from '@vase-ui/vue/dialog';
interface Props extends /* @vue-ignore */ DialogRootProps {}
interface Emits extends /* @vue-ignore */ DialogRootEmits {}
defineProps<Props>();
defineEmits<Emits>();
</script>
<template>
<Dialog.Root><slot /></Dialog.Root>
</template>A wrapper of a generic part, such as Select.Root, has the type parameters of the part. The value then keeps the type of the v-model of the caller:
<script setup lang="ts" generic="Value, Multiple extends boolean | undefined = false">
import { Select } from '@vase-ui/vue/select';
import type { SelectRootEmits, SelectRootProps } from '@vase-ui/vue/select';
interface Props extends /* @vue-ignore */ SelectRootProps<Value, Multiple> {}
interface Emits extends /* @vue-ignore */ SelectRootEmits<Value, Multiple> {}
defineProps<Props>();
defineEmits<Emits>();
</script>
<template>
<Select.Root><slot /></Select.Root>
</template>Vue sets an absent Boolean prop to false, unless you give it a default. If you declare a Boolean prop of the part, give it the default undefined. Else the wrapper changes the part: modal of Dialog.Root becomes false, and Accordion.Panel ignores the keepMounted of its root. An explicit false still reaches the part.
<script setup lang="ts">
import { Accordion } from '@vase-ui/vue/accordion';
import type { AccordionPanelProps } from '@vase-ui/vue/accordion';
// Vue cannot compile the native attribute types, so the other props pass through $attrs.
interface Props extends /* @vue-ignore */ AccordionPanelProps {
keepMounted?: boolean | undefined;
hiddenUntilFound?: boolean | undefined;
}
const props = withDefaults(defineProps<Props>(), {
keepMounted: undefined,
hiddenUntilFound: undefined,
});
</script>
<template>
<Accordion.Panel v-bind="props"><slot /></Accordion.Panel>
</template>Do not bind an attribute that you declared and that can be undefined, such as :aria-labelledby="labelledby". A value of undefined removes the value of the part, such as the title that names a dialog. Leave the attribute undeclared, and Vue passes it on only when the caller gives it.
Reka UI and shadcn-vue
A wrapper of a Reka UI part, such as a shadcn-vue component, declares the props and the emits of the part. It gives them to the part with useForwardPropsEmits(props, emits). In Vase UI, Vue gives the part what the wrapper does not declare:
defineProps<DialogRootProps>()becomesdefineProps<Props>(), withinterface Props extends /* @vue-ignore */ DialogRootProps {}.- Vue does the Boolean casting only for a declared prop, so the undeclared props need no
useForwardProps. Give a declared Boolean prop the defaultundefined. defineEmits<DialogRootEmits>()becomesdefineEmits<Emits>(), withinterface Emits extends /* @vue-ignore */ DialogRootEmits {}. Vue gives each listener to the part, so the wrapper needs nouseEmitAsProps.v-bind="forwarded"is not necessary when the part is the root of the wrapper. Else, setdefineOptions({ inheritAttrs: false })and writev-bind="$attrs"on the part.
Portals, Transition and KeepAlive
A Portal part renders its content in a Vue <Teleport>, in another place of the document. While its popup is open, the portal of a popover, a menu, a select or a combobox can also render a visually hidden element in place. That element names the popup in aria-owns, so a screen reader reads the popup after its trigger. A Portal part has no root element, so an attribute on it reaches no element. Its props:
to: the target, a CSS selector, an element or a shadow root. With noto, the target of the nearest portal above, else<body>. Whiletoisnull, such as a template ref before its mount, the portal renders nothing. A change oftomoves the content.disabled: renders the content in its place, also on the server.defer: moves the content after the other content of the same update mounts.keepMounted: the closed popup stays in the DOM, withhiddenandinert.
<Dialog.Portal to="#overlays">
<Dialog.Popup>Account settings</Dialog.Popup>
</Dialog.Portal>Put a <Transition> in the portal, around the part that it animates. See Animation.
KeepAlive takes the elements of a held part out of the document. A part has no code for a hold:
- A held part gets no event.
- A held control has no form. It is not in the form data, it does not stop a submit, and a reset does not change it. When it returns, it has the same value and belongs to its form again.
- A toolbar moves its tab stop to an item that is in the document, so that Tab still reaches the toolbar.
Vapor mode
Vase UI parts are Vue components. An application in Vapor mode renders them with the vaporInteropPlugin of Vue.
- In the
elementslot, bindelementRefwith:ref. Vapor ignores arefkey in the object ofv-bind. - A Vapor component that you give to
as, or render in theelementslot, must expose its element:defineExpose({ element }), withelementa template ref of its root element. A Vapor component has no root element that the part can read. useElement gives such a component itselement. - A label function of Select or Combobox that returns vnodes needs the virtual DOM runtime of Vue, which the
vaporInteropPlugingives. With no plugin, a label is text, and the slot ofSelect.Value, ofCombobox.Valueor of the item gives other content.
Differences from Base UI
| Base UI | Vase UI |
|---|---|
render with an element, such as render={<h2 />} | The as prop, with a tag or a component. |
render with a function of the props and the state | The element slot, with props, elementRef and state. |
A component in render takes a ref and spreads its props on its element | A component in as or in the element slot gets the attributes as fallthrough attributes. It exposes element, else its root element is the element. |
ref on a part gives its element | A template ref on a part gives its exposed members. Read element. |
render keeps the ref of each part that it merges | An element takes one ref. To keep the ref of the part, call elementRef in your function ref. |
An empty id stays on the element in most parts | An id that is empty or holds only white space counts as no id. The part keeps its own id. |
mergeProps and mergePropsN | mergeProps of Vue: import { mergeProps } from 'vue'. |
useRender | useElement and the element slot in your template. |
A Portal part renders a <div> | A Portal part renders a <Teleport> and no element. |
container of a Portal part: an element, a shadow root or a ref | to: a CSS selector, an element or a shadow root. With no to, the target of the nearest portal above, else <body>. |