Skip to content

Composables & utilities

Besides components, @nyxwire/vue exports four composables, two class helpers and a plugin. Everything on this page is a named export of the package root.

import {
NyxwirePlugin,
cn,
tv,
useFlavor,
useNyxwireTheme,
useNwFieldControl,
useToast,
} from '@nyxwire/vue'

Reads and sets the flavor. Flavors explains the flavors themselves; this section is the reference.

flavor: auto · resolvedFlavor: nocturne · isDark: true
UseFlavorDemo.vue
<script setup lang="ts">
import { NwButton, NwProvider, useFlavor } from '@nyxwire/vue'
const { flavor, resolvedFlavor, isDark, flavors, setFlavor } = useFlavor()
</script>
<template>
<NwProvider>
<div class="grid gap-3">
<div class="flex flex-wrap gap-2" role="group" aria-label="Flavor">
<NwButton
size="sm"
intent="primary"
:variant="flavor === 'auto' ? 'solid' : 'outline'"
:aria-pressed="flavor === 'auto'"
@click="setFlavor('auto')"
>
Auto
</NwButton>
<NwButton
v-for="item in flavors"
:key="item.id"
size="sm"
intent="primary"
:variant="flavor === item.id ? 'solid' : 'outline'"
:aria-pressed="flavor === item.id"
@click="setFlavor(item.id)"
>
{{ item.label }}
</NwButton>
</div>
<output class="font-mono text-xs text-fg-muted">
flavor: {{ flavor }} · resolvedFlavor: {{ resolvedFlavor }} · isDark: {{ isDark }}
</output>
</div>
</NwProvider>
</template>

On this site the buttons drive the page runtime, so the header’s flavor select and every other island follow.

Name Type Description
flavor Readonly<Ref<FlavorPreference>> The preference: 'auto', 'abyss', 'nocturne', 'twilight' or 'lumen'
resolvedFlavor ComputedRef<FlavorName> The flavor actually applied; 'auto' resolved through the system preference
isDark ComputedRef<boolean> Whether resolvedFlavor is a dark flavor (every flavor except Lumen)
flavors readonly FlavorMeta[] { id, label, dark } for the four flavors
setFlavor (preference: FlavorPreference) => void Changes the preference

Options are read by the first useFlavor call in a Vue app, which creates the app’s shared state. Later calls in the same app return that state and ignore their options.

Option Type Default Effect
defaults { dark?: DarkFlavorName; light?: LightFlavorName } { dark: 'nocturne', light: 'lumen' } Flavors that 'auto' resolves to
defaultFlavor FlavorPreference 'auto' Preference used when nothing is stored
storageKey string 'nyxwire-flavor' localStorage key of the preference
storage { getItem; setItem } | null window.localStorage Where the preference persists; null disables persistence
target HTMLElement | (() => HTMLElement | null) | null document.documentElement Element that receives the attributes; null applies nothing
matchMedia (query: string) => MediaQueryLike | null window.matchMedia Source of prefers-color-scheme, replaceable in tests
runtime FlavorRuntimeLike | null window.nyxwire Page runtime to delegate to; null forces standalone mode

Storage errors such as a blocked localStorage are ignored: the flavor still applies, it just does not persist.

With a page runtime. When window.nyxwire exists (installed by the Astro integration or the Starlight plugin), the runtime owns <html>, storage and the media query. useFlavor reads its current preference on mount, setFlavor calls nyxwire.setFlavor(), and the change comes back through the runtime’s event. Storage, target and media options are not used in this mode. A runtime without resolve() only accepts concrete flavors; then useFlavor follows the system preference itself while the preference is 'auto' and hands the runtime the resolved flavor.

Standalone. A Vite app has no runtime. Then useFlavor loads the stored preference, follows prefers-color-scheme while the preference is 'auto', writes each change to storage, and sets data-flavor, data-theme and color-scheme on the target.

Every instance listens on document for the nyxwire:flavor event, a CustomEvent whose detail holds { flavor, resolved, theme, accent }. The runtime dispatches it whenever the flavor changes, so all islands on a page, the header select and NwFlavorSelect stay in agreement. Code outside Vue can listen to it too:

import { FLAVOR_CHANGE_EVENT } from '@nyxwire/vue'
document.addEventListener(FLAVOR_CHANGE_EVENT, (event) => {
if (event instanceof CustomEvent) console.log(event.detail.flavor, event.detail.resolved)
})

In standalone mode useFlavor does not dispatch the event itself; it only mirrors changes that another source announces.

  • useFlavor touches neither window nor document until the calling component mounts, so it renders on the server.
  • Every call inside one Vue app shares a single state, removed when the app unmounts. Each Astro island is its own app, with its own state, kept in step by the runtime event.
  • Called outside a component, it uses one detached state for the whole page and starts it at once.
Export Value or signature
FLAVOR_STORAGE_KEY 'nyxwire-flavor'
FLAVOR_CHANGE_EVENT 'nyxwire:flavor'
flavors Flavor metadata, { id, label, dark }[]
flavorNames The four flavor names, in palette order
flavorPreferences 'auto' followed by the flavor names
defaultFlavor 'nocturne'
accentNames The 12 accent names, indigo to steel
isFlavorName, isFlavorPreference, isAccentName Type guards for untrusted strings
isDarkFlavor(flavor) true for Abyss, Nocturne and Twilight
themeOf(flavor) 'dark' or 'light', the value of data-theme

The types FlavorName, FlavorPreference, DarkFlavorName, LightFlavorName, ThemeName, AccentName, FlavorMeta, UseFlavorOptions and UseFlavorReturn are exported too.

Queues toasts for the NwToaster of the nearest NwProvider. Toast components and the toaster’s props are documented in Overlays & menus.

  • The queue is empty.
UseToastDemo.vue
<script setup lang="ts">
import { NwProvider, NwToaster } from '@nyxwire/vue'
import ToastQueue from './ToastQueue.vue'
</script>
<template>
<NwProvider>
<NwToaster position="top-right" />
<ToastQueue />
</NwProvider>
</template>
ToastQueue.vue
<script setup lang="ts">
import { NwButton, useToast } from '@nyxwire/vue'
// useToast reads the queue of the nearest NwProvider, so it runs in a child of the provider.
const { toast, dismiss, toasts } = useToast()
let count = 0
function notify(): void {
count += 1
toast({
title: `Relay ${count} online`,
description: 'Traffic is routing through the new node.',
intent: 'success',
duration: 0,
})
}
</script>
<template>
<div class="grid gap-3">
<div class="flex flex-wrap gap-2">
<NwButton intent="primary" variant="solid" @click="notify">Queue a toast</NwButton>
<NwButton variant="outline" :disabled="toasts.length === 0" @click="dismiss(toasts[0]?.id)">
Dismiss oldest
</NwButton>
<NwButton variant="ghost" :disabled="toasts.length === 0" @click="dismiss()">
Dismiss all
</NwButton>
</div>
<ul class="m-0 grid list-none gap-1 p-0 font-mono text-xs text-fg-muted">
<li v-for="item in toasts" :key="item.id">
{{ item.id }}: {{ item.title }} ({{ item.open ? 'open' : 'closing' }})
</li>
<li v-if="toasts.length === 0">The queue is empty.</li>
</ul>
</div>
</template>

Each NwProvider owns its queue, and useToast() injects it, so call it in a component rendered inside the provider, never in the component that renders the provider: there it throws useToast requires an ancestor NwProvider. That is why the demo splits into two files. useToast() takes no arguments and returns:

Name Type Description
toast (options: NwToastOptions) => string Queues a toast and returns its id
dismiss (id?: string) => void Closes the toast with that id, or every open toast without one
toasts ComputedRef<readonly NwToastItem[]> The live queue, including toasts playing their exit animation (open: false)
Option Type Default Effect
title string required First line of the toast
description string none Second line
intent 'primary' | 'secondary' | 'tertiary' | 'neutral' | 'info' | 'success' | 'warning' | 'danger' 'primary' Glowing edge bar and icon color; warning and danger are announced assertively
duration number NwProvider’s toast-duration (5000) Visible time in ms; 0 keeps the toast until it is dismissed
action { label: string; onClick: () => void } none A button; the toast closes after onClick runs
icon Component intent icon Leading icon; info, success, warning and danger get one by default

An NwToastItem is the options plus a readonly id and open. The types are exported as NwToastOptions, NwToastItem, NwToastIntent, NwToastActionOptions and UseToastReturn.

  • Any component under the provider can call toast(); the toasts appear in that provider’s NwToaster. Mount exactly one per provider.
  • NwToaster’s position picks the corner or edge: 'top-right', 'top-left', 'top-center', 'bottom-right' (default), 'bottom-left' or 'bottom-center'. The demo uses 'top-right'. Its variant is 'solid' (default) or 'glass'.
  • A closed toast leaves the queue 600 ms after it closes, once its exit animation is over.
  • When the last NwToaster of a provider unmounts, the queue is cleared, so toasts never outlive their viewport.

Returns the theme scope of the nearest NwProvider, as a ComputedRef<ThemeScope> of { flavor?, accent?, provided? }. flavor and accent are set when a provider (or one of its ancestors) scopes them with v-model:flavor or v-model:accent, and provided is true inside any provider. Outside a provider it returns an empty scope.

Portalled overlays use it to carry the scope onto content that leaves the provider’s subtree, so a dialog opened from a Lumen panel stays Lumen. Use it the same way when you teleport your own content:

<script setup lang="ts">
import { useNyxwireTheme } from '@nyxwire/vue'
const theme = useNyxwireTheme()
</script>
<template>
<Teleport to="body">
<div :data-flavor="theme.flavor" :data-accent="theme.accent">Floating HUD</div>
</Teleport>
</template>

Returns the attributes that join a control to the surrounding NwField, as a ComputedRef<NwFieldControlAttrs>: id, name, disabled, required, aria-labelledby, aria-describedby and aria-invalid. Outside a field it is empty. NwField’s default slot exposes the same value as control. Forms shows it wiring a select trigger; bind it on the focusable element of any custom control.

cn(...classes) joins class values (strings, arrays, objects) and resolves Tailwind conflicts with the Nyxwire tailwind-merge configuration, so a later utility replaces an earlier one, theme utilities such as glow-sm, cut or text-fg-muted included. It is the same function as @nyxwire/tailwind/merge’s cn, documented in Class merging. Every component merges its class prop with it.

cn('rounded-md glow-sm px-3 text-fg-muted', isActive && 'text-primary glow-md', 'rounded-full')
// → 'px-3 text-primary glow-md rounded-full' when isActive is true

tv is tailwind-variants’ tv, created with createTV({ twMergeConfig }) from @nyxwire/tailwind/merge. Recipes you build with it merge classes exactly like the components’ own recipes, so variant classes and the class you pass at call time override the base without duplicates. Recipes with tailwind-variants covers the API.

node hel-01: online
inline-flex items-center gap-2 rounded-md border px-3 py-1 font-mono text-xs [--nw-fill:var(--nw-panel)] border-success text-success [--nw-edge:var(--nw-success)]
TvDemo.vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { NwButton, NwProvider, NwSwitch, cn, tv } from '@nyxwire/vue'
// A recipe for a project-level component, built with the package's configured `tv`.
const nodeStatus = tv({
base: 'inline-flex items-center gap-2 rounded-md border px-3 py-1 font-mono text-xs [--nw-fill:var(--nw-panel)]',
variants: {
status: {
idle: 'border-line text-fg-muted [--nw-edge:var(--nw-line)]',
online: 'border-success text-success [--nw-edge:var(--nw-success)]',
alarm: 'border-danger text-danger [--nw-edge:var(--nw-danger)]',
},
},
defaultVariants: { status: 'idle' },
})
const statuses = ['idle', 'online', 'alarm'] as const
const status = ref<(typeof statuses)[number]>('online')
const cut = ref(false)
// `cut` replaces the recipe's radius and border width; `cn` drops the classes it overrides.
const classes = computed(() => cn(nodeStatus({ status: status.value }), cut.value && 'cut'))
</script>
<template>
<NwProvider>
<div class="grid gap-4">
<div class="flex flex-wrap items-center gap-3">
<span :class="classes">node hel-01: {{ status }}</span>
<NwButton
v-for="item in statuses"
:key="item"
size="sm"
intent="primary"
:variant="status === item ? 'solid' : 'outline'"
:aria-pressed="status === item"
@click="status = item"
>
{{ item }}
</NwButton>
<NwSwitch v-model="cut" label="Cut corners" size="sm" />
</div>
<code class="block font-mono text-xs break-all text-fg-muted">{{ classes }}</code>
</div>
</NwProvider>
</template>

The merged class list is printed under the chip. Turn on Cut corners and rounded-md and border leave it: cut draws its own chamfered edge from --nw-edge and --nw-fill, which the recipe sets per status. Use tv to style your own components in the same idiom as the library. The package also re-exports the VariantProps and ClassValue types.

A Vue plugin that registers every Nw* component globally, from every family: foundations, actions, forms, selection, date and time, color, overlays and navigation. Composables and helpers are not registered; import them.

src/main.ts
import { createApp } from 'vue'
import { NyxwirePlugin } from '@nyxwire/vue'
import App from './App.vue'
createApp(App).use(NyxwirePlugin).mount('#app')

It takes no options. Registering everything pulls every component into the bundle, so prefer named imports outside prototypes. You still need NwProvider at the root of the app.