Select Menu
A Discord-inspired string select: combobox trigger, keyboard-driven listbox and a portalled panel.
A select for one value from a list: a trigger, a keyboard-driven listbox and a panel that flips when it runs out of room. This is the option picker from the client, not a styled <select>. Each option can carry an icon or an emoji, a description and a note.
The trigger is the same box as Input: 40px tall, an 8px radius, a stronger edge on hover, an inset ring on focus and while the list is open, a danger edge when invalid. The panel fades in from 98% scale on the floating surface, and the highlighted row takes the hover background.
It is built on Radix Select in React and Reka UI Select in Vue. The primitive owns the open state, the keyboard, typeahead and the placement. The component adds the look, the option model and the 25-option cap of a message select.
newt-select-menu"use client"
import * as React from "react"
import { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
/*
* Drawn here rather than pulled from an icon package: the registry ships no
* runtime dependency, and a device picker needs exactly two glyphs.
*/
const Monitor = () => (
<svg viewBox="0 0 24 24" className="h-full w-full" aria-hidden="true">
<path
fill="currentColor"
d="M4 5h16a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1h-6v2h3a1 1 0 1 1 0 2H7a1 1 0 1 1 0-2h3v-2H4a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1Zm1 2v7h14V7H5Z"
/>
</svg>
)
const Headset = () => (
<svg viewBox="0 0 24 24" className="h-full w-full" aria-hidden="true">
<path
fill="currentColor"
d="M12 3a8 8 0 0 0-8 8v6a3 3 0 0 0 3 3h1a1 1 0 0 0 1-1v-6a1 1 0 0 0-1-1H6v-1a6 6 0 1 1 12 0v1h-2a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h1a3 3 0 0 0 3-3v-6a8 8 0 0 0-8-8Z"
/>
</svg>
)
const OPTIONS = [
{
value: "default",
label: "System Default",
description: "Speakers (Built-in Output)",
note: "(Speakers)",
icon: <Monitor />,
},
{
value: "speakers",
label: "Speakers",
description: "Built-in Output",
icon: <Monitor />,
},
{
value: "headset",
label: "Headset",
description: "USB Audio Device",
icon: <Headset />,
},
{
value: "bluetooth",
label: "Bluetooth Headset",
description: "Not connected",
icon: <Headset />,
disabled: true,
},
]
export default function SelectMenuDemo() {
const [value, setValue] = React.useState<string>("default")
return (
<SelectMenu
options={OPTIONS}
value={value}
onValueChange={setValue}
label="Output device"
/>
)
}<script setup lang="ts">
import { h, markRaw, ref } from "vue"
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
/*
* Drawn here rather than pulled from an icon package: the registry ships no
* runtime dependency, and a device picker needs exactly two glyphs.
*/
const Monitor = markRaw({
render: () =>
h(
"svg",
{ viewBox: "0 0 24 24", class: "h-full w-full", "aria-hidden": "true" },
[
h("path", {
fill: "currentColor",
d: "M4 5h16a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1h-6v2h3a1 1 0 1 1 0 2H7a1 1 0 1 1 0-2h3v-2H4a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1Zm1 2v7h14V7H5Z",
}),
]
),
})
const Headset = markRaw({
render: () =>
h(
"svg",
{ viewBox: "0 0 24 24", class: "h-full w-full", "aria-hidden": "true" },
[
h("path", {
fill: "currentColor",
d: "M12 3a8 8 0 0 0-8 8v6a3 3 0 0 0 3 3h1a1 1 0 0 0 1-1v-6a1 1 0 0 0-1-1H6v-1a6 6 0 1 1 12 0v1h-2a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h1a3 3 0 0 0 3-3v-6a8 8 0 0 0-8-8Z",
}),
]
),
})
const OPTIONS: SelectMenuOption[] = [
{
value: "default",
label: "System Default",
description: "Speakers (Built-in Output)",
note: "(Speakers)",
icon: Monitor,
},
{
value: "speakers",
label: "Speakers",
description: "Built-in Output",
icon: Monitor,
},
{
value: "headset",
label: "Headset",
description: "USB Audio Device",
icon: Headset,
},
{
value: "bluetooth",
label: "Bluetooth Headset",
description: "Not connected",
icon: Headset,
disabled: true,
},
]
const value = ref("default")
</script>
<template>
<SelectMenu v-model="value" :options="OPTIONS" label="Output device" />
</template>Installation
npx newtui@latest add select-menunpx shadcn@latest add @newtui/select-menunpx shadcn-vue@latest add @newtui/select-menupnpm dlx newtui@latest add select-menupnpm dlx shadcn@latest add @newtui/select-menupnpm dlx shadcn-vue@latest add @newtui/select-menuyarn dlx newtui@latest add select-menuyarn dlx shadcn@latest add @newtui/select-menuyarn dlx shadcn-vue@latest add @newtui/select-menubunx newtui@latest add select-menubunx shadcn@latest add @newtui/select-menubunx shadcn-vue@latest add @newtui/select-menudeno run -A npm:newtui@latest add select-menudeno run -A npm:shadcn@latest add @newtui/select-menudeno run -A npm:shadcn-vue@latest add @newtui/select-menuvlx newtui@latest add select-menuvlx shadcn@latest add @newtui/select-menuvlx shadcn-vue@latest add @newtui/select-menuvp dlx newtui@latest add select-menuvp dlx shadcn@latest add @newtui/select-menuvp dlx shadcn-vue@latest add @newtui/select-menunubx newtui@latest add select-menunubx shadcn@latest add @newtui/select-menunubx shadcn-vue@latest add @newtui/select-menunlx newtui@latest add select-menunlx shadcn@latest add @newtui/select-menunlx shadcn-vue@latest add @newtui/select-menuupx newtui@latest add select-menuupx shadcn@latest add @newtui/select-menuupx shadcn-vue@latest add @newtui/select-menu- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install radix-uipnpm add radix-uiyarn add radix-uibun add radix-uideno add npm:radix-uivlt install radix-uivp add radix-uinub add radix-uini radix-uiupm add radix-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
npm install reka-uipnpm add reka-uiyarn add reka-uibun add reka-uideno add npm:reka-uivlt install reka-uivp add reka-uinub add reka-uini reka-uiupm add reka-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Add the components it builds on
Copy the source into your project
"use client"
import { Select as SelectPrimitive } from "radix-ui"
import * as React from "react"
import { cn } from "@/lib/utils"
import {
controlVariants,
useControlWiring,
} from "@/registry/bases/newt/ui/form-fields"
export interface SelectMenuOption {
value: string
label: string
/** Secondary line under the label. */
description?: string
/**
* Rendered before the label, in the row and on the trigger once chosen —
* what the client's device pickers use to tell a microphone from a monitor
* at a glance. Sized by the menu, so pass the glyph and nothing else.
*/
icon?: React.ReactNode
/**
* A unicode emoji drawn in the place of `icon`, for an option that has no
* node to pass. `icon` wins when an option carries both.
*/
emoji?: string
/**
* A muted aside after the label on the trigger, for what the option resolves
* to. It is the first thing allowed to truncate.
*/
note?: string
disabled?: boolean
}
/**
* What a select attached to a message may carry. The platform rejects a
* payload with more, so a menu that rendered them would be showing something
* that cannot exist.
*/
export const SELECT_MENU_MAX_OPTIONS = 25
/* The name a menu gets when nothing else names it. */
const DEFAULT_LABEL = "Select an option"
/**
* Where the menu is: in a message, or in a settings panel. The two are not the
* same control — a message select caps its list, shows the scrollbar it needs
* to say the list continues, and marks nothing as chosen, because the choice
* is submitted rather than kept. A settings select has no cap and keeps a tick
* against the row that is in force.
*/
export type SelectMenuVariant = "settings" | "message"
/**
* The side of the trigger the panel opens on. The panel flips to the opposite
* side when the preferred one has no room.
*/
export type SelectMenuSide = "top" | "right" | "bottom" | "left"
/** How the panel lines up with the trigger along its side. */
export type SelectMenuAlign = "start" | "center" | "end"
/** The trigger height: `md` is 40px, `sm` 32px, as on `Input`. */
export type SelectMenuSize = "sm" | "md"
/** What a row render prop is told about the option it draws. */
export interface SelectMenuItemState {
index: number
selected: boolean
}
export interface SelectMenuProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onSelect" | "defaultValue"
> {
options: readonly SelectMenuOption[]
variant?: SelectMenuVariant
/** Controlled selection. Leave undefined to let the menu own it. */
value?: string
defaultValue?: string
onValueChange?: (value: string) => void
placeholder?: string
disabled?: boolean
/** The trigger height: `md` is 40px, `sm` 32px. */
size?: SelectMenuSize
/**
* Marks the menu as failing validation: sets `aria-invalid` and the
* danger edge on the trigger. Inside a `Field`, the field's `invalid`
* applies when this is left undefined, and the trigger is described by the
* field's help and error lines.
*/
invalid?: boolean
/**
* Accessible name of the trigger and the listbox. Outside a `Field` it
* defaults to "Select an option". Inside a `Field` the trigger takes its
* name from the field's `Label` unless you pass one.
*/
label?: string
/**
* The element the panel is portalled into. Defaults to `document.body`;
* pass the enclosing native `<dialog>` or a themed wrapper to keep it there.
*/
container?: React.ComponentProps<typeof SelectPrimitive.Portal>["container"]
/**
* The side of the trigger the panel opens on. It flips to the opposite side
* when the preferred one has no room.
*/
side?: SelectMenuSide
/** How the panel lines up with the trigger along that side. */
align?: SelectMenuAlign
/** Classes merged onto the trigger button, to size or restyle it. */
triggerClassName?: string
/** Classes merged onto the panel. */
contentClassName?: string
/**
* Replaces what a row draws: the icon or emoji, the label and the
* description. The row itself and its tick stay the menu's. The Vue port
* takes the same content through its `#item` slot.
*/
renderItem?: (
option: SelectMenuOption,
state: SelectMenuItemState
) => React.ReactNode
/**
* Replaces the text of the description of an option that has one. The Vue
* port takes the same content through its `#item-description` slot.
*/
renderItemDescription?: (
option: SelectMenuOption,
state: SelectMenuItemState
) => React.ReactNode
}
/*
* The trigger is the box of `Input`: the same height, edge, hover, inset
* focus ring, invalid edge and disabled dimming. An open list keeps the ring,
* because focus moves into the list while it is open. The chevron sits 12px
* from the outer edge, as on `Select`.
*/
const triggerClassName =
"group cursor-pointer justify-between gap-[var(--newt-space-2)] py-0 ps-[var(--newt-space-3)] pe-[calc(var(--newt-space-3)-1px)] text-start data-[state=open]:border-newt-focus-ring data-[state=open]:outline data-[state=open]:outline-2 data-[state=open]:outline-offset-[-2px] data-[state=open]:outline-newt-focus-ring data-[invalid]:data-[state=open]:border-newt-text-danger data-[invalid]:data-[state=open]:outline-newt-text-danger"
/*
* The panel: 4px off the trigger, 4px of padding around the rows, an 8px
* radius and a subtle edge on the floating surface. It fades in from 98%
* scale out of the side that touches the trigger. Radix Select unmounts the
* panel at once on close, so unlike Vue and HTML there is no exit animation.
* `scale` leaves `transform` free for the placement.
*/
const contentClassName =
"z-[110] box-border w-[var(--radix-select-trigger-width)] origin-[var(--radix-select-content-transform-origin)] rounded-[var(--newt-radius-md)] border border-solid border-newt-border-subtle bg-newt-bg-floating p-[var(--newt-space-1)] shadow-elevation-high data-[state=open]:animate-[newt-select-menu-in_var(--newt-duration-fast)_var(--newt-ease-overlay)] motion-reduce:data-[state=open]:animate-none"
const keyframes = `
@keyframes newt-select-menu-in { from { opacity: 0; scale: 0.98; } }`
/*
* A row is at least 40px tall, with 8px before the text and 4px after the
* tick, so the label lines up with the text of the trigger. The highlight
* arrives a little slower than it leaves, like a hover.
*/
const itemClassName =
"group/item box-border flex min-h-newt-control-md cursor-pointer select-none items-center gap-[var(--newt-space-2)] rounded-[var(--newt-radius-sm)] py-[calc(var(--newt-space-1)-1px)] pe-[var(--newt-space-1)] ps-[var(--newt-space-2)] text-[length:var(--newt-font-size-md)] leading-[var(--newt-line-height-tight)] text-newt-text-secondary outline-none transition-colors duration-instant ease-newt-in motion-reduce:transition-none data-[state=checked]:text-newt-text-primary data-[highlighted]:bg-newt-bg-hover data-[highlighted]:text-newt-text-primary data-[highlighted]:duration-base data-[highlighted]:ease-newt-out data-[disabled]:cursor-not-allowed data-[disabled]:text-newt-text-muted data-[disabled]:opacity-50"
/*
* Radix Select owns the behaviour: open state, Escape and outside press, focus
* moving through the rows and back to the trigger, typeahead, and
* collision-aware placement. Only the look, the option model and the cap are
* newt/ui's.
*/
const SelectMenu = React.forwardRef<HTMLDivElement, SelectMenuProps>(
(
{
className,
options: given,
variant = "settings",
value,
defaultValue,
onValueChange,
placeholder = "Make a selection",
disabled = false,
size,
invalid,
label,
container,
side = "bottom",
align = "start",
triggerClassName: triggerClassNameProp,
contentClassName: contentClassNameProp,
renderItem,
renderItemDescription,
...props
},
ref
) => {
/*
* The cap is applied here rather than left to the caller: a row the
* platform would reject should not be one the keyboard can reach.
*/
const options = React.useMemo(
() =>
variant === "message" ? given.slice(0, SELECT_MENU_MAX_OPTIONS) : given,
[given, variant]
)
const id = React.useId()
/*
* The selection is mirrored here because the trigger draws more than the
* label of the chosen row: its icon and its note come from the option.
*/
const [uncontrolled, setUncontrolled] = React.useState(defaultValue)
const selected = value ?? uncontrolled
const selectedOption = options.find((option) => option.value === selected)
/*
* While the list is open, Radix hides everything outside it from assistive
* technology, the trigger included. A hidden element that still takes Tab
* is what an accessibility audit reports, so the trigger leaves the tab
* order until the list closes. Focus is held in the list meanwhile, and it
* returns to the trigger by script, which a negative tabindex allows.
*/
const [open, setOpen] = React.useState(false)
/* Inside a `Field`, the trigger is the field's control. */
const wiring = useControlWiring({ invalid })
/*
* An `aria-label` wins over the field's visible `Label`, so inside a field
* the default name stays off the trigger.
*/
const triggerLabel = label ?? (wiring.inField ? undefined : DEFAULT_LABEL)
return (
<SelectPrimitive.Root
/* An empty string is how Radix spells "nothing chosen". */
value={selected ?? ""}
onValueChange={(next) => {
if (value === undefined) setUncontrolled(next)
onValueChange?.(next)
}}
onOpenChange={setOpen}
disabled={disabled}
>
<div
ref={ref}
className={cn("relative w-full max-w-[400px]", className)}
{...props}
>
<style>{keyframes}</style>
<SelectPrimitive.Trigger
data-slot="select-menu-trigger"
id={wiring.id}
aria-label={triggerLabel}
aria-invalid={wiring.ariaInvalid}
aria-describedby={wiring.describedBy}
data-invalid={wiring.invalid || undefined}
tabIndex={open ? -1 : undefined}
className={cn(
controlVariants({ size }),
triggerClassName,
triggerClassNameProp
)}
>
<span className="flex min-w-0 flex-1 items-center gap-[var(--newt-space-2)]">
{/* An icon wins over an emoji when an option carries both. */}
{selectedOption?.icon || selectedOption?.emoji ? (
<span
aria-hidden="true"
className="flex h-[18px] w-[18px] shrink-0 items-center justify-center text-[length:var(--newt-font-size-md)] leading-none text-newt-text-secondary"
>
{selectedOption.icon || selectedOption.emoji}
</span>
) : null}
<span
className={cn(
"truncate",
!selectedOption && "text-newt-text-muted"
)}
>
{selectedOption?.label ?? placeholder}
</span>
{selectedOption?.note ? (
<span className="truncate text-newt-text-muted">
{selectedOption.note}
</span>
) : null}
</span>
{/* Points down when closed, flips when the listbox opens. */}
<svg
viewBox="0 0 24 24"
aria-hidden="true"
focusable="false"
className="size-[var(--newt-space-5)] shrink-0 text-newt-text-primary transition-transform duration-slow ease-newt-out motion-reduce:transition-none group-data-[state=open]:rotate-180"
>
<path
fill="currentColor"
d="M5.3 9.3a1 1 0 0 1 1.4 0l5.3 5.29 5.3-5.3a1 1 0 1 1 1.4 1.42l-6 6a1 1 0 0 1-1.4 0l-6-6a1 1 0 0 1 0-1.42Z"
/>
</svg>
</SelectPrimitive.Trigger>
</div>
<SelectPrimitive.Portal container={container}>
<SelectPrimitive.Content
data-slot="select-menu-content"
position="popper"
side={side}
align={align}
sideOffset={4}
aria-label={label ?? DEFAULT_LABEL}
className={cn(contentClassName, contentClassNameProp)}
>
<SelectPrimitive.Viewport
className={cn(
/*
* A message select shows its scrollbar: the list is capped, so
* the bar is what says there is more of it below the fold.
* Radix hides the viewport's scrollbar with an attribute
* selector, so the two rules that bring it back repeat that
* attribute to outrank it.
*/
variant === "message"
? cn(
"max-h-[300px]",
"[&[data-radix-select-viewport]]:[scrollbar-width:thin] [scrollbar-color:var(--newt-bg-active)_transparent]",
"[&[data-radix-select-viewport]::-webkit-scrollbar]:block [&::-webkit-scrollbar]:w-2",
"[&::-webkit-scrollbar-track]:bg-transparent",
"[&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-solid [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-newt-bg-active [&::-webkit-scrollbar-thumb]:bg-clip-padding",
"[&::-webkit-scrollbar-thumb:hover]:bg-newt-text-muted [&::-webkit-scrollbar-thumb:hover]:bg-clip-padding"
)
: "max-h-80"
)}
>
{options.map((option, index) => {
const descriptionId = `${id}-description-${index}`
const state = { index, selected: option.value === selected }
return (
<SelectPrimitive.Item
key={option.value}
value={option.value}
disabled={option.disabled}
/* Typeahead matches the label, not the description. */
textValue={option.label}
aria-describedby={
option.description && !renderItem
? descriptionId
: undefined
}
className={itemClassName}
>
{renderItem ? (
/*
* A row drawn by the caller is the option's name as a
* whole: the text of everything in it, with no
* description id left dangling.
*/
<SelectPrimitive.ItemText asChild>
<div className="flex min-w-0 flex-1 items-center gap-[var(--newt-space-2)]">
{renderItem(option, state)}
</div>
</SelectPrimitive.ItemText>
) : (
<>
{option.icon || option.emoji ? (
<span
aria-hidden="true"
className="flex h-[18px] w-[18px] shrink-0 items-center justify-center text-[length:var(--newt-font-size-md)] leading-none text-newt-text-secondary"
>
{option.icon || option.emoji}
</span>
) : null}
<span className="flex min-w-0 flex-1 flex-col gap-[2px]">
<SelectPrimitive.ItemText className="truncate">
{option.label}
</SelectPrimitive.ItemText>
{option.description ? (
<span
id={descriptionId}
className="truncate text-[length:var(--newt-font-size-xs)] text-newt-text-muted"
>
{renderItemDescription
? renderItemDescription(option, state)
: option.description}
</span>
) : null}
</span>
</>
)}
{/*
* Only the settings menu ticks a row. In a message the
* choice is submitted rather than kept, so there is no
* standing selection for a tick to be about.
*/}
{variant === "settings" ? (
<SelectPrimitive.ItemIndicator className="ms-auto flex h-[18px] w-[18px] shrink-0 items-center justify-center rounded-[var(--newt-radius-full)] bg-newt-brand text-newt-on-accent">
<svg viewBox="0 0 24 24" className="h-[14px] w-[14px]">
<path
fill="none"
stroke="currentColor"
strokeWidth="3"
strokeLinecap="round"
strokeLinejoin="round"
d="m6 12.5 4 4 8-9"
/>
</svg>
</SelectPrimitive.ItemIndicator>
) : null}
</SelectPrimitive.Item>
)
})}
</SelectPrimitive.Viewport>
</SelectPrimitive.Content>
</SelectPrimitive.Portal>
</SelectPrimitive.Root>
)
}
)
SelectMenu.displayName = "SelectMenu"
export { SelectMenu }<script setup lang="ts">
import {
SelectContent,
SelectItem,
SelectItemIndicator,
SelectItemText,
SelectPortal,
SelectRoot,
SelectTrigger,
SelectViewport,
} from "reka-ui"
import type { HTMLAttributes, VNode } from "vue"
import { computed, ref, useId } from "vue"
import {
controlVariants,
controlWiring,
useFieldContext,
} from "~~/registry/bases/newt/ui/form-fields"
import { cn } from "@/lib/utils"
import {
SELECT_MENU_MAX_OPTIONS,
type SelectMenuAlign,
type SelectMenuOption,
type SelectMenuSide,
type SelectMenuSize,
type SelectMenuVariant,
} from "."
/*
* Reka Select owns the behaviour: open state, Escape and outside press, focus
* moving through the rows and back to the trigger, typeahead, and
* collision-aware placement. Only the look, the option model and the cap are
* newt/ui's.
*/
defineOptions({ inheritAttrs: false })
const {
class: className,
options: given,
variant = "settings",
defaultValue,
placeholder = "Make a selection",
disabled = false,
size,
/* `undefined` means "let the field decide", as on `Input`. */
invalid = undefined,
label,
container,
side = "bottom",
align = "start",
triggerClass,
contentClass,
} = defineProps<{
class?: HTMLAttributes["class"]
options: readonly SelectMenuOption[]
variant?: SelectMenuVariant
/** The value selected on first render, when no `v-model` is bound. */
defaultValue?: string
placeholder?: string
disabled?: boolean
/** The trigger height: `md` is 40px, `sm` 32px. */
size?: SelectMenuSize
/**
* Marks the menu as failing validation: sets `aria-invalid` and the danger
* edge on the trigger. Inside a `Field`, the field's `invalid` applies
* when this is left undefined, and the trigger is described by the
* field's help and error lines.
*/
invalid?: boolean
/**
* Accessible name of the trigger and the listbox. Outside a `Field` it
* defaults to "Select an option". Inside a `Field` the trigger takes its
* name from the field's `Label` unless you pass one.
*/
label?: string
/**
* The element the panel is portalled into. Defaults to `body`; pass the
* enclosing native `<dialog>` or a themed wrapper to keep it there.
*/
container?: string | HTMLElement
/**
* The side of the trigger the panel opens on. It flips to the opposite
* side when the preferred one has no room.
*/
side?: SelectMenuSide
/** How the panel lines up with the trigger along that side. */
align?: SelectMenuAlign
/** Classes merged onto the trigger button, to size or restyle it. */
triggerClass?: HTMLAttributes["class"]
/** Classes merged onto the panel. */
contentClass?: HTMLAttributes["class"]
}>()
defineSlots<{
/**
* Replaces what a row draws: the icon or emoji, the label and the
* description. The row itself and its tick stay the menu's.
*/
item?: (props: {
option: SelectMenuOption
index: number
selected: boolean
}) => VNode[]
/** Replaces the text of the description of an option that has one. */
"item-description"?: (props: {
option: SelectMenuOption
index: number
selected: boolean
}) => VNode[]
}>()
/*
* The cap is applied here rather than left to the caller: a row the platform
* would reject should not be one the keyboard can reach.
*/
const options = computed(() =>
variant === "message" ? given.slice(0, SELECT_MENU_MAX_OPTIONS) : given
)
const id = useId()
const emit = defineEmits<{ select: [value: string] }>()
/*
* The selection is mirrored here because the trigger draws more than the
* label of the chosen row: its icon and its note come from the option.
*/
const model = defineModel<string | undefined>({ default: undefined })
/*
* Without a bound v-model, defineModel keeps the pick locally. defaultValue
* shows only until the first pick, so a parent that clears its model clears
* the trigger too.
*/
const picked = ref(false)
const selected = computed(
() => model.value ?? (picked.value ? undefined : defaultValue)
)
const selectedOption = computed(() =>
options.value.find((option) => option.value === selected.value)
)
function onValueChange(next: string) {
picked.value = true
model.value = next
emit("select", next)
}
/*
* While the list is open, Reka hides everything outside it from assistive
* technology, the trigger included. A hidden element that still takes Tab is
* what an accessibility audit reports, so the trigger leaves the tab order
* until the list closes. Focus is held in the list meanwhile, and it returns
* to the trigger by script, which a negative tabindex allows.
*/
const open = ref(false)
/* Inside a `Field`, the trigger is the field's control. */
const field = useFieldContext()
const wiring = computed(() => controlWiring(field, { invalid }))
/* The name a menu gets when nothing else names it. */
const DEFAULT_LABEL = "Select an option"
/*
* An `aria-label` wins over the field's visible `Label`, so inside a field
* the default name stays off the trigger.
*/
const triggerLabel = computed(
() => label ?? (field === undefined ? DEFAULT_LABEL : undefined)
)
/*
* The trigger is the box of `Input`: the same height, edge, hover, inset
* focus ring, invalid edge and disabled dimming. An open list keeps the ring,
* because focus moves into the list while it is open. The chevron sits 12px
* from the outer edge, as on `Select`.
*/
const triggerClassName =
"group cursor-pointer justify-between gap-[var(--newt-space-2)] py-0 ps-[var(--newt-space-3)] pe-[calc(var(--newt-space-3)-1px)] text-start data-[state=open]:border-newt-focus-ring data-[state=open]:outline data-[state=open]:outline-2 data-[state=open]:outline-offset-[-2px] data-[state=open]:outline-newt-focus-ring data-[invalid]:data-[state=open]:border-newt-text-danger data-[invalid]:data-[state=open]:outline-newt-text-danger"
/*
* The panel: 4px off the trigger, 4px of padding around the rows, an 8px
* radius and a subtle edge on the floating surface. It fades in from 98%
* scale out of the side that touches the trigger, and plays that backwards
* on close; Reka keeps it mounted until the closing animation ends.
*/
const contentClassName =
"z-[110] box-border w-[var(--reka-select-trigger-width)] origin-[var(--reka-select-content-transform-origin)] rounded-[var(--newt-radius-md)] border border-solid border-newt-border-subtle bg-newt-bg-floating p-[var(--newt-space-1)] shadow-elevation-high data-[state=open]:animate-[newt-select-menu-in_var(--newt-duration-fast)_var(--newt-ease-overlay)] data-[state=closed]:animate-[newt-select-menu-out_var(--newt-duration-fast)_var(--newt-ease-overlay)] motion-reduce:data-[state=open]:animate-none motion-reduce:data-[state=closed]:animate-none"
/*
* A row is at least 40px tall, with 8px before the text and 4px after the
* tick, so the label lines up with the text of the trigger. The highlight
* arrives a little slower than it leaves, like a hover.
*/
const itemClassName =
"group/item box-border flex min-h-newt-control-md cursor-pointer select-none items-center gap-[var(--newt-space-2)] rounded-[var(--newt-radius-sm)] py-[calc(var(--newt-space-1)-1px)] pe-[var(--newt-space-1)] ps-[var(--newt-space-2)] text-[length:var(--newt-font-size-md)] leading-[var(--newt-line-height-tight)] text-newt-text-secondary outline-none transition-colors duration-instant ease-newt-in motion-reduce:transition-none data-[state=checked]:text-newt-text-primary data-[highlighted]:bg-newt-bg-hover data-[highlighted]:text-newt-text-primary data-[highlighted]:duration-base data-[highlighted]:ease-newt-out data-[disabled]:cursor-not-allowed data-[disabled]:text-newt-text-muted data-[disabled]:opacity-50"
</script>
<template>
<SelectRoot
:model-value="selected"
:disabled="disabled"
@update:model-value="onValueChange"
@update:open="open = $event"
>
<div
v-bind="$attrs"
:class="cn('relative w-full max-w-[400px]', className)"
>
<SelectTrigger
:id="wiring.id"
data-slot="select-menu-trigger"
:aria-label="triggerLabel"
:aria-invalid="wiring.ariaInvalid"
:aria-describedby="wiring.describedBy"
:data-invalid="wiring.invalid || undefined"
:tabindex="open ? -1 : undefined"
:class="cn(controlVariants({ size }), triggerClassName, triggerClass)"
>
<span
class="flex min-w-0 flex-1 items-center gap-[var(--newt-space-2)]"
>
<!-- An icon wins over an emoji when an option carries both. -->
<span
v-if="selectedOption?.icon || selectedOption?.emoji"
aria-hidden="true"
class="flex h-[18px] w-[18px] shrink-0 items-center justify-center text-[length:var(--newt-font-size-md)] leading-none text-newt-text-secondary"
>
<component :is="selectedOption.icon" v-if="selectedOption.icon" />
<template v-else>{{ selectedOption.emoji }}</template>
</span>
<span
:class="cn('truncate', !selectedOption && 'text-newt-text-muted')"
>
{{ selectedOption?.label ?? placeholder }}
</span>
<span
v-if="selectedOption?.note"
class="truncate text-newt-text-muted"
>
{{ selectedOption.note }}
</span>
</span>
<!-- Points down when closed, flips when the listbox opens. -->
<svg
viewBox="0 0 24 24"
aria-hidden="true"
focusable="false"
class="size-[var(--newt-space-5)] shrink-0 text-newt-text-primary transition-transform duration-slow ease-newt-out motion-reduce:transition-none group-data-[state=open]:rotate-180"
>
<path
fill="currentColor"
d="M5.3 9.3a1 1 0 0 1 1.4 0l5.3 5.29 5.3-5.3a1 1 0 1 1 1.4 1.42l-6 6a1 1 0 0 1-1.4 0l-6-6a1 1 0 0 1 0-1.42Z"
/>
</svg>
</SelectTrigger>
</div>
<SelectPortal :to="container">
<SelectContent
data-slot="select-menu-content"
position="popper"
:side="side"
:align="align"
:side-offset="4"
:aria-label="label ?? DEFAULT_LABEL"
:class="cn(contentClassName, contentClass)"
>
<!--
A message select shows its scrollbar: the list is capped, so the bar
is what says there is more of it below the fold. Reka hides the
viewport's scrollbar with an attribute selector, so the two rules
that bring it back repeat that attribute to outrank it.
-->
<SelectViewport
:class="
variant === 'message'
? cn(
'max-h-[300px]',
'[&[data-reka-select-viewport]]:[scrollbar-width:thin] [scrollbar-color:var(--newt-bg-active)_transparent]',
'[&[data-reka-select-viewport]::-webkit-scrollbar]:block [&::-webkit-scrollbar]:w-2',
'[&::-webkit-scrollbar-track]:bg-transparent',
'[&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-solid [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-newt-bg-active [&::-webkit-scrollbar-thumb]:bg-clip-padding',
'[&::-webkit-scrollbar-thumb:hover]:bg-newt-text-muted [&::-webkit-scrollbar-thumb:hover]:bg-clip-padding'
)
: 'max-h-80'
"
>
<!-- Typeahead matches the label, not the description. -->
<SelectItem
v-for="(option, index) of options"
:key="option.value"
:value="option.value"
:disabled="option.disabled"
:text-value="option.label"
:aria-describedby="
option.description && !$slots.item
? `${id}-description-${index}`
: undefined
"
:class="itemClassName"
>
<!--
A row drawn by the caller is the option's name as a whole: the
text of everything in it, with no description id left dangling.
-->
<SelectItemText
v-if="$slots.item"
as="div"
class="flex min-w-0 flex-1 items-center gap-[var(--newt-space-2)]"
>
<slot
name="item"
:option="option"
:index="index"
:selected="option.value === selected"
/>
</SelectItemText>
<template v-else>
<span
v-if="option.icon || option.emoji"
aria-hidden="true"
class="flex h-[18px] w-[18px] shrink-0 items-center justify-center text-[length:var(--newt-font-size-md)] leading-none text-newt-text-secondary"
>
<component :is="option.icon" v-if="option.icon" />
<template v-else>{{ option.emoji }}</template>
</span>
<span class="flex min-w-0 flex-1 flex-col gap-[2px]">
<SelectItemText class="truncate">
{{ option.label }}
</SelectItemText>
<span
v-if="option.description"
:id="`${id}-description-${index}`"
class="truncate text-[length:var(--newt-font-size-xs)] text-newt-text-muted"
>
<slot
name="item-description"
:option="option"
:index="index"
:selected="option.value === selected"
>
{{ option.description }}
</slot>
</span>
</span>
</template>
<!--
Only the settings menu ticks a row. In a message the choice is
submitted rather than kept, so there is no standing selection for
a tick to be about.
-->
<SelectItemIndicator
v-if="variant === 'settings'"
class="ms-auto flex h-[18px] w-[18px] shrink-0 items-center justify-center rounded-[var(--newt-radius-full)] bg-newt-brand text-newt-on-accent"
>
<svg viewBox="0 0 24 24" class="h-[14px] w-[14px]">
<path
fill="none"
stroke="currentColor"
stroke-width="3"
stroke-linecap="round"
stroke-linejoin="round"
d="m6 12.5 4 4 8-9"
/>
</svg>
</SelectItemIndicator>
</SelectItem>
</SelectViewport>
</SelectContent>
</SelectPortal>
</SelectRoot>
</template>
<style>
@keyframes newt-select-menu-in {
from {
opacity: 0;
scale: 0.98;
}
}
@keyframes newt-select-menu-out {
to {
opacity: 0;
scale: 0.98;
}
}
</style>import type { Component } from "vue"
export { default as SelectMenu } from "./SelectMenu.vue"
export interface SelectMenuOption {
value: string
label: string
/** Secondary line under the label. */
description?: string
/**
* Rendered before the label, in the row and on the trigger once chosen —
* what the client's device pickers use to tell a microphone from a monitor
* at a glance. The Vue analogue of React's node: pass a component, and the
* menu sizes it.
*/
icon?: Component
/**
* A unicode emoji drawn in the place of `icon`, for an option that has no
* component to pass. `icon` wins when an option carries both.
*/
emoji?: string
/**
* A muted aside after the label on the trigger, for what the option resolves
* to. It is the first thing allowed to truncate.
*/
note?: string
disabled?: boolean
}
/**
* What a select attached to a message may carry. The platform rejects a
* payload with more, so a menu that rendered them would be showing something
* that cannot exist.
*/
export const SELECT_MENU_MAX_OPTIONS = 25
/**
* Where the menu is: in a message, or in a settings panel. The two are not the
* same control — a message select caps its list, shows the scrollbar it needs
* to say the list continues, and marks nothing as chosen, because the choice
* is submitted rather than kept. A settings select has no cap and keeps a tick
* against the row that is in force.
*/
export type SelectMenuVariant = "settings" | "message"
/**
* The side of the trigger the panel opens on. The panel flips to the opposite
* side when the preferred one has no room.
*/
export type SelectMenuSide = "top" | "right" | "bottom" | "left"
/** The trigger height: `md` is 40px, `sm` 32px, as on `Input`. */
export type SelectMenuSize = "sm" | "md"
/** How the panel lines up with the trigger along its side. */
export type SelectMenuAlign = "start" | "center" | "end"Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Select Menu to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Select Menu to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { SelectMenu } from "@/components/ui/select-menu"
export function Example() {
return <SelectMenu />
}<script setup lang="ts">
import { SelectMenu } from "@/components/ui/select-menu"
</script>
<template>
<SelectMenu />
</template>Pass the choices as options. In React, control the value with value and onValueChange, or pass defaultValue. In Vue, bind v-model, or pass default-value.
Examples
Sizes
size sets the trigger height: md is 40px and sm is 32px, the same as Input and Button. The rows of the list keep their 40px height.
newt-select-menuimport { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
const OPTIONS = [
{ value: "auto", label: "Automatic" },
{ value: "720", label: "720p" },
{ value: "1080", label: "1080p" },
{ value: "source", label: "Source" },
]
/* `md` matches a 40px input or button; `sm` matches a 32px one. */
export default function SelectMenuSizesDemo() {
return (
<div className="flex w-full max-w-[400px] flex-col gap-[var(--newt-space-3)]">
<SelectMenu options={OPTIONS} defaultValue="auto" label="Resolution" />
<SelectMenu
size="sm"
options={OPTIONS}
defaultValue="1080"
label="Resolution, small"
/>
</div>
)
}<script setup lang="ts">
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
const OPTIONS: SelectMenuOption[] = [
{ value: "auto", label: "Automatic" },
{ value: "720", label: "720p" },
{ value: "1080", label: "1080p" },
{ value: "source", label: "Source" },
]
/* `md` matches a 40px input or button; `sm` matches a 32px one. */
</script>
<template>
<div class="flex w-full max-w-[400px] flex-col gap-[var(--newt-space-3)]">
<SelectMenu :options="OPTIONS" default-value="auto" label="Resolution" />
<SelectMenu
size="sm"
:options="OPTIONS"
default-value="1080"
label="Resolution, small"
/>
</div>
</template>Invalid
Inside a Field, the menu reads the field’s invalid. The trigger takes the danger edge and aria-invalid, and its aria-describedby lists the field’s help line and, while invalid, its error line. Outside a field, set invalid on the menu. Without a label, the trigger takes its name from the field’s Label. Pass label with the same text when the open list must say the same: label names the list as well as the trigger.
newt-select-menu"use client"
import * as React from "react"
import { Field, FieldHelp, Label } from "@/registry/bases/newt/ui/form-fields"
import { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
const OPTIONS = [
{ value: "eu", label: "Europe" },
{ value: "us-east", label: "US East" },
{ value: "us-west", label: "US West" },
{ value: "asia", label: "Asia Pacific" },
]
/*
* Inside a field, the menu reads `invalid` from it: the trigger takes the
* danger edge and `aria-invalid`, and lists the error line in
* `aria-describedby`. Picking a region clears the error.
*/
export default function SelectMenuInvalidDemo() {
const [region, setRegion] = React.useState<string>()
return (
<Field invalid={region === undefined} className="w-full max-w-[400px]">
<Label>Voice region</Label>
<SelectMenu
options={OPTIONS}
value={region}
onValueChange={setRegion}
label="Voice region"
placeholder="Choose a voice region"
/>
{region === undefined ? (
<FieldHelp variant="error">Pick a voice region to save.</FieldHelp>
) : (
<FieldHelp>Members join voice through this region.</FieldHelp>
)}
</Field>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Field, FieldHelp, Label } from "~~/registry/bases/newt/ui/form-fields"
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
const OPTIONS: SelectMenuOption[] = [
{ value: "eu", label: "Europe" },
{ value: "us-east", label: "US East" },
{ value: "us-west", label: "US West" },
{ value: "asia", label: "Asia Pacific" },
]
/*
* Inside a field, the menu reads `invalid` from it: the trigger takes the
* danger edge and `aria-invalid`, and lists the error line in
* `aria-describedby`. Picking a region clears the error.
*/
const region = ref<string>()
</script>
<template>
<Field :invalid="region === undefined" class="w-full max-w-[400px]">
<Label>Voice region</Label>
<SelectMenu
v-model="region"
:options="OPTIONS"
label="Voice region"
placeholder="Choose a voice region"
/>
<FieldHelp v-if="region === undefined" variant="error">
Pick a voice region to save.
</FieldHelp>
<FieldHelp v-else>Members join voice through this region.</FieldHelp>
</Field>
</template>Placeholder
With no value, the trigger shows the placeholder in muted text until an option is picked. Options need only a value and a label.
newt-select-menuimport { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
const OPTIONS = [
{ value: "auto", label: "Automatic" },
{ value: "eu", label: "Europe" },
{ value: "us-east", label: "US East" },
{ value: "us-west", label: "US West" },
{ value: "asia", label: "Asia Pacific" },
]
/* Nothing is chosen yet, so the trigger says what it is waiting for. */
export default function SelectMenuPlaceholderDemo() {
return (
<SelectMenu
options={OPTIONS}
label="Voice region"
placeholder="Choose a voice region"
/>
)
}<script setup lang="ts">
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
const OPTIONS: SelectMenuOption[] = [
{ value: "auto", label: "Automatic" },
{ value: "eu", label: "Europe" },
{ value: "us-east", label: "US East" },
{ value: "us-west", label: "US West" },
{ value: "asia", label: "Asia Pacific" },
]
/* Nothing is chosen yet, so the trigger says what it is waiting for. */
</script>
<template>
<SelectMenu
:options="OPTIONS"
label="Voice region"
placeholder="Choose a voice region"
/>
</template>Message variant
variant="message" is the select attached to a message. The list is capped at 25 options and its scrollbar is always visible. No row keeps a tick, because the choice is submitted and not kept.
newt-select-menuimport { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
const OPTIONS = [
{ value: "tank", label: "Tank", description: "Hold the front line" },
{ value: "healer", label: "Healer", description: "Keep the party standing" },
{ value: "ranged", label: "Ranged", description: "Damage from a distance" },
{ value: "melee", label: "Melee", description: "Damage up close" },
{ value: "support", label: "Support", description: "Buffs and utility" },
{ value: "scout", label: "Scout", description: "Find the route first" },
{ value: "crafter", label: "Crafter", description: "Gear for everyone" },
{ value: "caller", label: "Shot caller", description: "Lead the voice chat" },
{ value: "flex", label: "Flex", description: "Fill whatever is missing" },
{ value: "bench", label: "Bench", description: "On call if a slot opens" },
]
/*
* The select a message carries: the list is capped, its scrollbar is always
* drawn, and no row keeps a tick once the menu closes.
*/
export default function SelectMenuMessageDemo() {
return (
<SelectMenu
variant="message"
options={OPTIONS}
label="Raid role"
placeholder="Pick your role for tonight"
/>
)
}<script setup lang="ts">
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
const OPTIONS: SelectMenuOption[] = [
{ value: "tank", label: "Tank", description: "Hold the front line" },
{ value: "healer", label: "Healer", description: "Keep the party standing" },
{ value: "ranged", label: "Ranged", description: "Damage from a distance" },
{ value: "melee", label: "Melee", description: "Damage up close" },
{ value: "support", label: "Support", description: "Buffs and utility" },
{ value: "scout", label: "Scout", description: "Find the route first" },
{ value: "crafter", label: "Crafter", description: "Gear for everyone" },
{ value: "caller", label: "Shot caller", description: "Lead the voice chat" },
{ value: "flex", label: "Flex", description: "Fill whatever is missing" },
{ value: "bench", label: "Bench", description: "On call if a slot opens" },
]
/*
* The select a message carries: the list is capped, its scrollbar is always
* drawn, and no row keeps a tick once the menu closes.
*/
</script>
<template>
<SelectMenu
variant="message"
:options="OPTIONS"
label="Raid role"
placeholder="Pick your role for tonight"
/>
</template>Disabled
disabled locks the trigger while still showing the option in force, including its note.
newt-select-menuimport { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
const OPTIONS = [
{ value: "auto", label: "Automatic", note: "(Europe)" },
{ value: "eu", label: "Europe" },
{ value: "us-east", label: "US East" },
]
export default function SelectMenuDisabledDemo() {
return (
<div className="flex w-full max-w-[400px] flex-col gap-2">
<SelectMenu
options={OPTIONS}
defaultValue="auto"
label="Voice region"
disabled
/>
<p className="text-xs text-newt-text-muted">
Only the channel owner can change the voice region.
</p>
</div>
)
}<script setup lang="ts">
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
const OPTIONS: SelectMenuOption[] = [
{ value: "auto", label: "Automatic", note: "(Europe)" },
{ value: "eu", label: "Europe" },
{ value: "us-east", label: "US East" },
]
</script>
<template>
<div class="flex w-full max-w-[400px] flex-col gap-2">
<SelectMenu
model-value="auto"
:options="OPTIONS"
label="Voice region"
disabled
/>
<p class="text-xs text-newt-text-muted">
Only the channel owner can change the voice region.
</p>
</div>
</template>Opening above
side names the side of the trigger the panel opens on. A select under a message sits close to the composer, so side="top" opens it upwards. The panel still flips when that side has no room. align lines up a panel that is not as wide as the trigger.
triggerClassName (trigger-class in Vue) merges classes onto the trigger. contentClassName (content-class in Vue) merges classes onto the panel.
Each option carries an emoji: a unicode character drawn where the icon would be.
newt-select-menuimport { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
const OPTIONS = [
{ value: "news", label: "Announcements", emoji: "📣" },
{ value: "events", label: "Events", emoji: "🎉" },
{ value: "patches", label: "Patch notes", emoji: "🛠️" },
{ value: "games", label: "Game nights", emoji: "🎮" },
]
/*
* A select under a message sits close to the composer, so it opens upwards.
* Each option carries an emoji in place of an icon.
*/
export default function SelectMenuAboveDemo() {
return (
<SelectMenu
variant="message"
side="top"
options={OPTIONS}
label="Notification role"
placeholder="Choose what to hear about"
/>
)
}<script setup lang="ts">
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
const OPTIONS: SelectMenuOption[] = [
{ value: "news", label: "Announcements", emoji: "📣" },
{ value: "events", label: "Events", emoji: "🎉" },
{ value: "patches", label: "Patch notes", emoji: "🛠️" },
{ value: "games", label: "Game nights", emoji: "🎮" },
]
/*
* A select under a message sits close to the composer, so it opens upwards.
* Each option carries an emoji in place of an icon.
*/
</script>
<template>
<SelectMenu
variant="message"
side="top"
:options="OPTIONS"
label="Notification role"
placeholder="Choose what to hear about"
/>
</template>Rich description
renderItemDescription in React and the #item-description slot in Vue draw the description of an option. The option still needs a description: an option without one has no second line. Both receive the option, its index and whether it is selected.
newt-select-menu"use client"
import { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
const OPTIONS = [
{ value: "announcements", label: "announcements", description: "Community" },
{ value: "patch-notes", label: "patch-notes", description: "Community" },
{ value: "raid-planning", label: "raid-planning", description: "Guild" },
{ value: "loot-council", label: "loot-council", description: "Guild" },
]
/* The description names the category, so it leads with a folder glyph. */
export default function SelectMenuDescriptionDemo() {
return (
<SelectMenu
options={OPTIONS}
label="Channel"
placeholder="Choose a channel"
renderItemDescription={(option) => (
<>
<svg
viewBox="0 0 24 24"
aria-hidden="true"
className="me-1 inline h-3 w-3 align-[-1px]"
>
<path
fill="currentColor"
d="M3 6a2 2 0 0 1 2-2h4.2a2 2 0 0 1 1.4.6L12 6h7a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6Z"
/>
</svg>
{option.description}
</>
)}
/>
)
}<script setup lang="ts">
import {
SelectMenu,
type SelectMenuOption,
} from "~~/registry/bases/newt/ui/select-menu"
const OPTIONS: SelectMenuOption[] = [
{ value: "announcements", label: "announcements", description: "Community" },
{ value: "patch-notes", label: "patch-notes", description: "Community" },
{ value: "raid-planning", label: "raid-planning", description: "Guild" },
{ value: "loot-council", label: "loot-council", description: "Guild" },
]
/* The description names the category, so it leads with a folder glyph. */
</script>
<template>
<SelectMenu :options="OPTIONS" label="Channel" placeholder="Choose a channel">
<template #item-description="{ option }">
<svg
viewBox="0 0 24 24"
aria-hidden="true"
class="me-1 inline h-3 w-3 align-[-1px]"
>
<path
fill="currentColor"
d="M3 6a2 2 0 0 1 2-2h4.2a2 2 0 0 1 1.4.6L12 6h7a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6Z"
/></svg
>{{ option.description }}
</template>
</SelectMenu>
</template>Custom rows
renderItem in React and the #item slot in Vue draw the whole content of a row. The menu keeps the row itself, its highlight, its keyboard handling and its tick. The trigger still shows the label of the option, and typeahead still matches it.
The highlighted row has the hover background behind it, so muted text in a custom row stays readable without a class of its own. The row is group/item: style a part of it for the highlighted row with group-data-[highlighted]/item:.
newt-select-menu"use client"
import { SelectMenu } from "@/registry/bases/newt/ui/select-menu"
/* A role is an option plus what its row shows beside the name. */
const ROLES = [
{ value: "admin", label: "Admin", dot: "bg-newt-dnd", members: 2 },
{ value: "moderator", label: "Moderator", dot: "bg-newt-online", members: 5 },
{ value: "raider", label: "Raider", dot: "bg-newt-idle", members: 24 },
{ value: "guest", label: "Guest", dot: "bg-newt-offline", members: 112 },
]
/*
* The row is drawn here: a role dot, the name, and a member count at the end.
* The menu keeps the row itself, its highlight and its tick.
*/
export default function SelectMenuItemDemo() {
return (
<SelectMenu
options={ROLES}
defaultValue="raider"
label="Role"
renderItem={(option, { index }) => (
<>
<span
aria-hidden="true"
className={`h-3 w-3 shrink-0 rounded-full ${ROLES[index]?.dot ?? ""}`}
/>
<span className="min-w-0 flex-1 truncate">{option.label}</span>
<span className="shrink-0 text-xs text-newt-text-muted">
{ROLES[index]?.members} members
</span>
</>
)}
/>
)
}<script setup lang="ts">
import { SelectMenu } from "~~/registry/bases/newt/ui/select-menu"
/* A role is an option plus what its row shows beside the name. */
const ROLES = [
{ value: "admin", label: "Admin", dot: "bg-newt-dnd", members: 2 },
{ value: "moderator", label: "Moderator", dot: "bg-newt-online", members: 5 },
{ value: "raider", label: "Raider", dot: "bg-newt-idle", members: 24 },
{ value: "guest", label: "Guest", dot: "bg-newt-offline", members: 112 },
]
/*
* The row is drawn here: a role dot, the name, and a member count at the end.
* The menu keeps the row itself, its highlight and its tick.
*/
</script>
<template>
<SelectMenu :options="ROLES" default-value="raider" label="Role">
<template #item="{ option, index }">
<span
aria-hidden="true"
:class="['h-3 w-3 shrink-0 rounded-full', ROLES[index]?.dot]"
/>
<span class="min-w-0 flex-1 truncate">{{ option.label }}</span>
<span class="shrink-0 text-xs text-newt-text-muted">
{{ ROLES[index]?.members }} members
</span>
</template>
</SelectMenu>
</template>API reference
SelectMenu
options?SelectMenuOption[]
The choices: `{ value, label, description?, icon?, emoji?, note?, disabled? }`. A `value` must not be an empty string.
SelectMenuOption[]variant?"settings" | "message"
Where the menu is. `message` caps the list at 25 options, shows the scrollbar and ticks no row.
"settings" | "message""settings"value / v-model?string
The selected value, when you control it.
stringdefaultValue?string
The value selected on first render.
stringonValueChange?(value: string) => void
React only. Called with the chosen value. Vue emits `update:modelValue`, and `select` with the same value.
(value: string) => voidplaceholder?string
Shown on the trigger while nothing is selected.
string"Make a selection"size?"sm" | "md"
The trigger height: 32px or 40px.
"sm" | "md""md"invalid?boolean
Gives the trigger the danger edge and sets `aria-invalid`. Inside a `Field`, the field's `invalid` applies when this is not set.
booleanlabel?string
The accessible name of the trigger and the listbox. Inside a `Field`, the trigger takes its name from the field's `Label` when this is not set.
string"Select an option"disabled?boolean
Makes the trigger inert.
booleanfalsecontainer?HTMLElement
Element the panel is portalled into. Pass the enclosing native `<dialog>` or a themed wrapper to keep it there.
HTMLElementdocument.bodyside?"top" | "right" | "bottom" | "left"
The side of the trigger the panel opens on. The panel flips to the opposite side when this one has no room.
"top" | "right" | "bottom" | "left""bottom"align?"start" | "center" | "end"
How the panel lines up with the trigger along that side.
"start" | "center" | "end""start"triggerClassName / triggerClass?string
Classes merged onto the trigger button, to size or restyle it.
stringcontentClassName / contentClass?string
Classes merged onto the panel.
stringrenderItem / #item?(option, { index, selected }) => ReactNode / Vue slot
Draws the content of every row, in place of the icon, the label and the description.
(option, { index, selected }) => ReactNode / Vue slotrenderItemDescription / #item-description?(option, { index, selected }) => ReactNode / Vue slot
Draws the description of every option that has one.
(option, { index, selected }) => ReactNode / Vue slotIn React, icon is a node. In Vue, icon is a component. emoji is a unicode character in both, and icon wins when an option has both. The root also accepts the native attributes of a <div>, and a className (class in Vue) that is merged with the defaults.
The trigger carries data-slot="select-menu-trigger" and the panel carries data-slot="select-menu-content". Use them to find either part from a stylesheet or a test.
HTML and CSS
Load tokens.css, select-menu.css and select-menu.js with the markup. Add newt-select-menu--sm to the root for the 32px trigger. Set aria-invalid="true" on the trigger, or data-invalid on an enclosing newt-field, for the danger edge. Set data-side="top" on the root to open the panel above the trigger. Set data-align to center or end on the panel when it is not as wide as the trigger.
The panel carries data-state="open" or "closed". The script hides it when the closing animation ends, or at once when there is no animation.
Draw an emoji in a row with newt-select-menu__option-emoji. The script copies it to the trigger as newt-select-menu__emoji, and removes it when the chosen row has none. Mark the label of a row with newt-select-menu__option-label. The script copies that text to the trigger, so a row can hold more than its label.
Accessibility
The trigger is a combobox with aria-expanded and, while the panel is open, aria-controls pointing at it. The panel is a listbox, and both take their name from label. Each option carries aria-selected, and its description is linked with aria-describedby.
label and placeholder default to English. Translating labels lists every English default.
Enter, Space, Up and Down open the list. Focus moves to the selected option, or to the first option. Up and Down move through the options and skip disabled ones. Home and End jump to the ends. Typing a letter moves to the next option whose label starts with it. Enter or Space selects the focused option and returns focus to the trigger. Escape closes the list and returns focus to the trigger, and so does a press outside.
While the list is open, the page behind it does not scroll and does not take pointer events. Inside a drawer or a modal, Escape closes the list first and the dialog on the next press.
A row drawn with renderItem or #item names its option with all of its text, and no description is linked. Hide a decorative glyph in it with aria-hidden.
The list opens in 100ms. In Vue and HTML it also closes in 100ms; in React, Radix removes it at once. With prefers-reduced-motion: reduce, it appears and disappears at once, and the chevron and the row highlight change without a transition. Focus moves into the list as soon as it opens: the animation never delays it.
Auditing an open list
While the list is open, the primitive sets aria-hidden on everything outside it. A screen reader then stays inside the list. The trigger is outside the list, so it leaves the tab order with tabindex="-1" until the list closes. Focus still returns to it on close.
An axe audit of a page with an open list can still report aria-hidden-focus. The rule finds the other links and buttons of the page inside the hidden regions. This finding is expected. The list holds focus, Tab does not leave it, and the primitive restores the page when the list closes. Audit the page with the list closed, and audit the open panel on its own.
Testing
The list opens on pointerdown, and on Enter, Space, Up or Down. In Vue, a synthetic click does not open it. In a test, press a key on the focused trigger, or dispatch pointerdown.
Tokens
Select Menu reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-select-menu.
--newt-space-2--newt-control-md--newt-space-3--newt-border--newt-radius-md--newt-bg-input--newt-text-primary--newt-font-sans--newt-font-size-md--newt-font-weight-normal--newt-line-height-tight--newt-duration-instant--newt-ease-in--newt-control-sm--newt-border-strong--newt-duration-base--newt-ease-out--newt-focus-ring--newt-text-link--newt-text-danger--newt-danger--newt-text-secondary--newt-text-muted--newt-space-5--newt-duration-slow--newt-space-1--newt-border-subtle--newt-bg-floating--newt-shadow-elevation-high--newt-duration-fast--newt-ease-overlay--newt-dir--newt-bg-active--newt-radius-sm--newt-bg-base--newt-font-size-xs--newt-font-weight-semibold--newt-bg-hover--newt-radius-full--newt-brand