feat(app): apply themes as arrow keys move through the theme picker

This commit is contained in:
vaprdev
2026-11-05 05:51:49 +00:00
Unverified
parent 5ea5c5e610
commit 239730eb39
4 changed files with 86 additions and 3 deletions
@@ -0,0 +1,48 @@
import { expect, story } from "../../stories/playwright/story"
story("selects the option arrow keys move to while the menu stays open", async ({ mount, page }) => {
const root = await mount("ui-select--select-on-navigate")
const current = root.getByTestId("select-on-navigate-current")
const listbox = page.getByRole("listbox")
await root.locator('[data-component="select-v2"]').click()
await expect(listbox).toBeVisible()
// Opening only highlights the current option.
await expect(current).toHaveText("Selected: Banana")
await page.keyboard.press("ArrowDown")
await expect(current).toHaveText("Selected: Cherry")
await expect(listbox).toBeVisible()
await page.keyboard.press("ArrowDown")
await expect(current).toHaveText("Selected: Date")
await page.keyboard.press("ArrowUp")
await expect(current).toHaveText("Selected: Cherry")
// Hover highlights without selecting.
await page.getByRole("option", { name: "Apple" }).hover()
await expect(page.getByRole("option", { name: "Apple" })).toHaveAttribute("data-highlighted", "")
await expect(current).toHaveText("Selected: Cherry")
await page.keyboard.press("Escape")
await expect(listbox).toBeHidden()
await expect(current).toHaveText("Selected: Cherry")
})
story("keeps the selection when the keyboard opens the menu", async ({ mount, page }) => {
const root = await mount("ui-select--select-on-navigate")
const current = root.getByTestId("select-on-navigate-current")
await root.locator('[data-component="select-v2"]').focus()
await page.keyboard.press("ArrowDown")
await expect(page.getByRole("listbox")).toBeVisible()
await expect(current).toHaveText("Selected: Banana")
await page.keyboard.press("ArrowDown")
await expect(current).toHaveText("Selected: Cherry")
await page.keyboard.press("Enter")
await expect(page.getByRole("listbox")).toBeHidden()
await expect(current).toHaveText("Selected: Cherry")
})
@@ -128,6 +128,7 @@ export const SettingsAppearance: Component = () => {
value={(option) => option.id}
label={(option) => option.name}
onSelect={appearance.theme.select}
selectOnNavigate
/>
</SettingsRow>
@@ -27,6 +27,7 @@ Single-select built on Kobalte with current menu styling. The trigger is inline
- \`groupBy\`: groups options; section headers use menu group label styling.
- \`placement\`, \`gutter\`, \`sameWidth\`, \`flip\`, \`slide\`, \`fitViewport\`: forwarded to Kobalte popper.
- \`invalid\`, \`disabled\`, \`numeric\`: match text input conventions.
- \`selectOnNavigate\`: arrow keys and typeahead select the option they move to while the menu stays open; hover only highlights.
`
export default {
@@ -98,6 +99,19 @@ export const Grouped = {
},
}
export const SelectOnNavigate = {
render: () => {
const [current, setCurrent] = createSignal("Banana")
return (
<div style={{ display: "grid", gap: "12px" }}>
<Select options={fruits} current={current()} onSelect={(v) => v !== null && setCurrent(v)} selectOnNavigate />
<span data-testid="select-on-navigate-current">Selected: {current()}</span>
</div>
)
},
}
export const Invalid = {
render: (args) => {
const [current, setCurrent] = createSignal(undefined)
+23 -3
View File
@@ -69,6 +69,8 @@ export type SelectProps<T> = Omit<
groupBy?: (x: T) => string
onSelect?: (value: T | null) => void
onHighlight?: (value: T | undefined) => void | (() => void)
/** Arrow keys and typeahead select the option they move to and keep the menu open. Pointer hover only highlights. */
selectOnNavigate?: boolean
invalid?: boolean
numeric?: boolean
/** `inline` is the borderless settings trigger; `small`, `base` and `large` are 24px, 28px and 32px input fields. */
@@ -94,6 +96,7 @@ export function Select<T>(props: SelectProps<T>) {
"groupBy",
"onSelect",
"onHighlight",
"selectOnNavigate",
"onOpenChange",
"children",
"invalid",
@@ -112,6 +115,8 @@ export function Select<T>(props: SelectProps<T>) {
])
const state: HighlightState = {}
// Kobalte focuses an item for both keyboard navigation and mouse hover; only the former selects.
let navigating = false
let scroller: HTMLDivElement | undefined
@@ -140,6 +145,14 @@ export function Select<T>(props: SelectProps<T>) {
state.key = key
}
const navigate = (item: T) => {
if (!local.selectOnNavigate || !navigating) return
if (local.current !== undefined && keyFor(local.current) === keyFor(item)) return
local.onSelect?.(item)
}
onCleanup(stop)
const grouped = createMemo(() => groupOptions(local.options, local.groupBy))
@@ -175,8 +188,14 @@ export function Select<T>(props: SelectProps<T>) {
{...itemProps}
data-component="menu-v2-item"
onPointerEnter={() => move(itemProps.item.rawValue)}
onPointerMove={() => move(itemProps.item.rawValue)}
onFocus={() => move(itemProps.item.rawValue)}
onPointerMove={() => {
navigating = false
move(itemProps.item.rawValue)
}}
onFocus={() => {
move(itemProps.item.rawValue)
navigate(itemProps.item.rawValue)
}}
>
<ItemLabel data-slot="menu-v2-item-content" as="span">
{local.children
@@ -197,6 +216,7 @@ export function Select<T>(props: SelectProps<T>) {
}}
onOpenChange={(open) => {
local.onOpenChange?.(open)
navigating = false
if (!open) stop()
}}
@@ -235,7 +255,7 @@ export function Select<T>(props: SelectProps<T>) {
<Portal>
<Content class={local.contentClass} data-component="menu-v2-content" data-slot="select-v2-content">
<MenuScroll viewportRef={(element) => (scroller = element)}>
<Listbox data-slot="select-v2-listbox" scrollRef={() => scroller} />
<Listbox data-slot="select-v2-listbox" scrollRef={() => scroller} onKeyDown={() => (navigating = true)} />
</MenuScroll>
</Content>
</Portal>