TypeScript
A guide to using TypeScript with Vase UI.
Namespaces
The namespace of a part holds its types. The main types are:
Props, such asTooltip.Root.PropsEmits, such asTooltip.Root.Emits: the events of the part, in the form ofdefineEmitsState, such asTooltip.Root.State
Each type also has a flat name, such as TooltipRootProps. The entry point of the component exports both names. A component with one part has its types on the component, such as Checkbox.Props and CheckboxProps.
Props and Emits
A component that wraps a part takes the types of the props and the events of the part from its Props and Emits types. Extend the flat names with /* @vue-ignore */, so that Vue gives the props and the listeners to the part. See Wrap a part.
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';
import type { TooltipRootEmits, TooltipRootProps } from '@vase-ui/vue/tooltip';
interface Props extends /* @vue-ignore */ TooltipRootProps {}
interface Emits extends /* @vue-ignore */ TooltipRootEmits {}
defineProps<Props>();
defineEmits<Emits>();
</script>
<template>
<Tooltip.Root><slot /></Tooltip.Root>
</template>State
The State type is the state of the part. For example, the state of Popover.Positioner gives the side and the alignment of the popup. The element slot gives the state to a typed function:
<script setup lang="ts">
import { Popover } from '@vase-ui/vue/popover';
function popupClass(state: Popover.Positioner.State) {
return state.open ? 'open' : 'closed';
}
</script>
<template>
<Popover.Root>
<Popover.Trigger>Trigger</Popover.Trigger>
<Popover.Portal>
<Popover.Positioner>
<template #element="{ props, elementRef, state }">
<div v-bind="props" :ref="elementRef" :class="popupClass(state)">
<ul>
<li>The popover is {{ state.open ? 'open' : 'closed' }}</li>
<li>I am on the {{ state.side }} side of the anchor</li>
<li>I am aligned at the {{ state.align }} of the side</li>
<li>The anchor is {{ state.anchorHidden ? 'hidden' : 'visible' }}</li>
</ul>
<Popover.Popup>Popover</Popover.Popup>
</div>
</template>
</Popover.Positioner>
</Popover.Portal>
</Popover.Root>
</template>Events
The namespace of a part also has the types of its change events:
ChangeEventDetails, such asCombobox.Root.ChangeEventDetails: the details that a change listener, such asvalueChange, gets.OpenChangeEventDetails, such asCombobox.Root.OpenChangeEventDetails: the details thatopenChangegets, on a part whoseChangeEventDetailsserves other listeners.ChangeEventReason, such asCombobox.Root.ChangeEventReason: the union of the reasons of a change.
<script setup lang="ts">
import { Combobox } from '@vase-ui/vue/combobox';
const fruits = ['Apple', 'Banana', 'Orange'];
function onValueChange(value: string | null, eventDetails: Combobox.Root.ChangeEventDetails) {
const reason: Combobox.Root.ChangeEventReason = eventDetails.reason;
console.log(value, reason);
}
function onOpenChange(open: boolean, eventDetails: Combobox.Root.OpenChangeEventDetails) {
console.log(open, eventDetails);
}
</script>
<template>
<Combobox.Root
:items="fruits"
default-value="Apple"
@value-change="onValueChange"
@open-change="onOpenChange"
>
<Combobox.Input />
</Combobox.Root>
</template>Other accessible types
A part or a utility can export other types. Its page documents them. Some examples:
A template ref on a part has the types of the members that the part exposes.
useTemplateRefgives the ref ofMenu.Rootthe type of itsclosemethod:vue<script setup lang="ts"> import { useTemplateRef } from 'vue'; import { Menu } from '@vase-ui/vue/menu'; const menu = useTemplateRef('menu');function close() { menu.value?.close(); } </script> <template> <button type="button" @click="close">Close the menu</button> <Menu.Root ref="menu"> <Menu.Trigger>Trigger</Menu.Trigger> </Menu.Root> </template> ToastObjectis the type of a toast ofToast.Root.ElementSlotProps<State>of@vase-ui/vue/typesis the type of the props of theelementslot. A component of yours can have the same slot. See useElement.
Generic components
A part whose types follow a value or a payload is a generic component. Its type parameter comes from its props:
- A part with a value, such as
RadioGroup,ToggleGroup,Slider.Root,Select.RootorAccordion.Root, takes it fromv-modelordefault-value. - A popup root and its trigger, such as
Tooltip.RootandTooltip.Trigger, take the type of their payload fromhandle.
In this sample, the type of the listener follows default-value: a number for one thumb, and an array of numbers for a range. The type check refuses a listener with another type.
<script setup lang="ts">
import { Slider } from '@vase-ui/vue/slider';
function onVolumeChange(value: number) {
console.log(value);
}
function onRangeChange(value: number[]) {
console.log(value[0], value[1]);
}
</script>
<template>
<Slider.Root :default-value="50" @value-change="onVolumeChange">
<Slider.Control>
<Slider.Track>
<Slider.Thumb aria-label="Volume" />
</Slider.Track>
</Slider.Control>
</Slider.Root>
<Slider.Root :default-value="[25, 75]" @value-change="onRangeChange">
<Slider.Control>
<Slider.Track>
<Slider.Thumb aria-label="Minimum" />
<Slider.Thumb aria-label="Maximum" />
</Slider.Track>
</Slider.Control>
</Slider.Root>
</template>Differences from Base UI
| Base UI | Vase UI |
|---|---|
| A wrapper spreads the props of the part on it | A wrapper types its props and events with Props and Emits of the part and /* @vue-ignore */. Vue gives the attributes and the listeners to the part. |
OpenChangeEventDetails with preventUnmountOnClose() | OpenChangeEventDetails with trigger. A <Transition> keeps a closing popup in the DOM. |
The types of actionsRef, such as Menu.Root.Actions | The types of the exposed members of the part, which a template ref gets. |
useRender.ComponentProps for a component with a render prop | ElementSlotProps<State> of @vase-ui/vue/types, for a component with an element slot. |