useElement
A composable that gives a component the native element that it renders.
Examples
Render another element
Use useElement to build a component in the form of a Vase UI part. The component renders its own element, or the element that a consumer renders in its element slot. Here a consumer renders a strong element in the element slot of a Text component:
Text component rendered as a paragraph tag
Text component rendered as a strong tag<script setup lang="ts">
import Text from './Text.vue';
</script>
<template>
<div>
<Text>Text component rendered as a paragraph tag</Text>
<Text>
<template #element="{ props, elementRef }">
<strong v-bind="props" :ref="elementRef">Text component rendered as a strong tag</strong>
</template>
</Text>
</div>
</template><script setup lang="ts">
import { mergeProps, useAttrs } from 'vue';
import type { ElementSlotProps } from '@vase-ui/vue/types';
import { useElement } from '@vase-ui/vue/use-element';
import styles from './index.module.css';
defineOptions({ inheritAttrs: false });
defineSlots<{
default?(): unknown;
element?(scope: ElementSlotProps<Record<string, never>>): unknown;
}>();
const attrs = useAttrs();
const { element, setElement } = useElement();
const elementProps = () => mergeProps({ class: styles.Text }, attrs);
defineExpose({ element });
</script>
<template>
<slot
v-if="$slots.element"
name="element"
:props="elementProps()"
:elementRef="setElement"
:state="{}"
/>
<p v-else v-bind="elementProps()" :ref="setElement"><slot /></p>
</template>.Text {
font-size: 0.875rem;
line-height: 1rem;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
strong& {
font-weight: 700;
}
}- Bind
setElementon the element with:ref, because Vapor ignores arefinv-bind. GivesetElementto theelementslot aselementRef. elementis the element thatsetElementgets. For a component in the slot, it is theelementthat the component exposes, else its root element. A Vapor component has no root element to give.elementisnullon a server, before the mount and after the unmount.- Expose
element. A template ref of the consumer then reads the native element. The component can also be the element of a Vase UI part, and a part can be the element of the component. - With
inheritAttrs: false, merge the attributes of the consumer with the attributes of the component inmergePropsof Vue. The value of the consumer wins.classandstylejoin, and each listener stays. ElementSlotPropsof@vase-ui/vue/typestypes the scope of theelementslot.
Read the state
The element slot also gives the state of the component:
<script setup lang="ts">
import Counter from './Counter.vue';
import styles from './index.module.css';
</script>
<template>
<Counter>
<template #element="{ props, elementRef, state }">
<button v-bind="props" :ref="elementRef">
Counter: <span :class="styles.count">{{ state.count }}</span>
<span :class="styles.suffix">{{ state.odd ? '👎' : '👍' }}</span>
</button>
</template>
</Counter>
</template><script setup lang="ts">
import { computed, mergeProps, ref, useAttrs } from 'vue';
import type { ElementSlotProps } from '@vase-ui/vue/types';
import { useElement } from '@vase-ui/vue/use-element';
import styles from './index.module.css';
interface CounterState {
odd: boolean;
count: number;
}
defineOptions({ inheritAttrs: false });
defineSlots<{
default?(scope: { state: CounterState }): unknown;
element?(scope: ElementSlotProps<CounterState>): unknown;
}>();
const attrs = useAttrs();
const count = ref(0);
const state = computed<CounterState>(() => ({ odd: count.value % 2 === 1, count: count.value }));
const { element, setElement } = useElement();
const elementProps = () =>
mergeProps(
{
class: styles.Button,
type: 'button',
'aria-label': `Counter: ${count.value}, click to increase.`,
onClick() {
count.value += 1;
},
},
attrs,
);
defineExpose({ element });
</script>
<template>
<slot
v-if="$slots.element"
name="element"
:props="elementProps()"
:elementRef="setElement"
:state="state"
/>
<button v-else v-bind="elementProps()" :ref="setElement">
<slot :state="state">
Counter: <span :class="styles.count">{{ count }}</span>
</slot>
</button>
</template>.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.25rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1.25rem;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.count {
display: inline-block;
min-width: 2ch;
text-align: end;
font-variant-numeric: tabular-nums;
}
.suffix {
margin-left: 0.5rem;
padding-left: 0.5rem;
border-left: 1px solid currentColor;
font-size: 0.75rem;
font-weight: 700;
line-height: 1rem;
text-transform: uppercase;
}API reference
useElement
Gives the native element of a component, and the function ref that sets it.
Return value
{ element: ComputedRef<T | null>; setElement: (value: unknown) => void; }
Differences from Base UI
Base UI has no useElement. useElement is the Vue form of useRender of Base UI. A component of the developer gets its native element from a function ref, and a component in its element slot gives the element that it exposes. The component and a part can then be the element of each other.
- https://vuejs.org/guide/essentials/template-refs.html#function-refs
- https://vuejs.org/guide/components/attrs.html
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.