Getting started
vael-ui is a Vue 3 component library with a full Vue Vapor build. Every component animates through one motion system, and you can animate any part with your own library instead.
Install
vael-ui is on npm. Install it with your package manager:
npm install vael-uiUsage
Import components from 'vael-ui' for the VDOM build. For the Vapor build (Vue 3.6’s mode without a virtual DOM), import from 'vael-ui/vapor'. The props are the same in both:
<script setup lang="ts">
import { Button, ConfigProvider } from 'vael-ui'
</script>
<template>
<ConfigProvider :theme="{ primary: '#ea580c', radius: '12px' }">
<Button @click="save">Save</Button>
</ConfigProvider>
</template>Components use the font of the page around them and ship none of their own. Without one, the browser falls back to its default serif font, so set a font on body:
body {
font-family: system-ui, sans-serif;
}Vue Vapor support
Both builds ship in the same package, from the same source, with the same props. Pick the one your app runs. Every component page has a toggle to compare them.
The Vapor build needs Vue 3.6, which is still a prerelease. Install the exact version vael-ui is built against, 3.6.0-rc.16. A range such as ^3.5.0 can install Vue 3.5, which has no Vapor mode, and another prerelease can break the compiled components:
npm install [email protected] --save-exactA Vapor app starts with createVaporApp and imports components, composables and directives from 'vael-ui/vapor'. Your own components opt in with <script setup vapor>:
// main.ts
import { createVaporApp } from 'vue'
import App from './App.vue'
createVaporApp(App).mount('#app')<!-- App.vue -->
<script setup vapor lang="ts">
import { Button, Toaster, toast } from 'vael-ui/vapor'
</script>
<template>
<Toaster />
<Button @click="toast('Saved')">Save</Button>
</template>Components from a library that isn't built for Vapor, such as an icon set, render nothing in a Vapor app on their own. Install Vue’s vaporInteropPlugin as well: createVaporApp(App).use(vaporInteropPlugin).
To skip the vapor attribute in every file, turn on features.vapor in @vitejs/plugin-vue 6.0.8 or later. Every <script setup> component in your app then compiles as Vapor:
// vite.config.ts
import vue from '@vitejs/plugin-vue'
export default {
plugins: [
vue({
features: {
vapor: true,
// vael-ui doesn't use the Options API. If your own components don't either,
// this drops its code from your bundle.
optionsAPI: false,
},
}),
],
}Mixing both builds
A VDOM app can also host Vapor components, so you can move an existing app to Vapor a few components at a time. Keep createApp, install Vue’s vaporInteropPlugin, and import from 'vael-ui/vapor' in the components you mark vapor. For a new app, pick one build instead. A component marked vapor imports directives such as vTooltip from 'vael-ui/vapor' too, because app.directive only registers them for VDOM components.
// main.ts
import { createApp, vaporInteropPlugin } from 'vue'
import App from './App.vue'
createApp(App).use(vaporInteropPlugin).mount('#app')<!-- Chart.vue, one component moved to Vapor -->
<script setup vapor lang="ts">
import { Button } from 'vael-ui/vapor'
</script>
<template>
<Button>Export</Button>
</template>Dark mode
Dark mode is CSS only. It follows <html data-theme="dark"> and falls back to prefers-color-scheme. vael-ui ships no toggle, but useColorScheme() tracks changes to the OS setting and saves the choice through the persist option you pass:
import { useColorScheme } from 'vael-ui'
const { mode, setMode } = useColorScheme({
persist: {
get: () => localStorage.getItem('theme'),
set: (m) => localStorage.setItem('theme', m ?? ''),
},
})To animate the switch, pass where it came from. The new theme spreads in a circle from the click, an element or a point, and browsers without View Transitions, or with reduced motion, switch at once. The useColorScheme page has a live demo.
// <button @click="toggleTheme">Toggle theme</button>
const toggleTheme = (event: MouseEvent) =>
setMode(mode.value === 'dark' ? 'light' : 'dark', { from: event })If you save the color mode or an app-wide font size in localStorage, put this script in your HTML <head>, before the app bundle. It restores both before the first paint. Save a CSS font size such as 13px or 1rem under app-font-size. Components inherit it unless you set a scoped theme.fontSize.
<!-- index.html: place in <head> before the app bundle -->
<script>
(() => {
try {
const root = document.documentElement
const theme = localStorage.getItem('theme')
if (theme === 'light' || theme === 'dark') root.dataset.theme = theme
const fontSize = localStorage.getItem('app-font-size')
if (fontSize && CSS.supports('font-size', fontSize)) root.style.fontSize = fontSize
} catch {}
})()
</script>Directives
v-tooltip, v-scroll-mask and v-hover-indicator aren’t registered globally by default. If you use any of them in more than one component, register them once:
// main.ts
import { createApp } from 'vue'
import { vTooltip, vScrollMask, vHoverIndicator } from 'vael-ui'
import App from './App.vue'
const app = createApp(App)
app.directive('tooltip', vTooltip)
app.directive('scroll-mask', vScrollMask)
app.directive('hover-indicator', vHoverIndicator)
app.mount('#app')v-tooltip also needs one <TooltipHost /> mounted in your app. In a Vapor app, import both directives from vael-ui/vapor instead.
Server rendering
Both builds render on the server and hydrate in the browser. The Nuxt module sets up everything below for you.
With plain Vite server rendering, let Vite bundle vael-ui for the server. Each component imports its own CSS, and Node can’t load CSS files on its own:
// vite.config.ts
export default {
ssr: { noExternal: ['vael-ui'] },
}Dialogs, popovers, toasts and tooltips render into <body> through <Teleport>. Vue returns that HTML separately, in ctx.teleports. Add it at the start of <body>, before your app’s element, or the page won’t hydrate cleanly:
// server.js
const ctx = {}
const appHtml = await renderToString(app, ctx)
const html = template
.replace('<!--app-html-->', appHtml)
.replace('<!--teleports-->', ctx.teleports?.body ?? '')<!-- index.html -->
<body>
<!--teleports-->
<div id="app"><!--app-html--></div>
</body>Set locale on ConfigProvider when you render on a server. Without it, dates, numbers and the first day of the week follow the server’s language, then change once the page runs in the browser.