mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
feat(ui): add input field appearances and surfaces to select (#61500)
This commit is contained in:
committed by
GitHub
Unverified
parent
fe04656465
commit
774a3dd510
@@ -69,6 +69,62 @@ for (const control of ["text-input", "textarea"]) {
|
||||
})
|
||||
}
|
||||
|
||||
story("select fields match the text input surfaces with inset feedback", async ({ mount, page }) => {
|
||||
const root = await mount("ui-select--surfaces")
|
||||
|
||||
const fields = root.locator('[data-component="select-v2"]')
|
||||
|
||||
await page.addStyleTag({
|
||||
content: `[data-component="select-v2"] {
|
||||
--v2-background-bg-base: rgb(20, 30, 40);
|
||||
--v2-background-bg-layer-01: rgb(50, 60, 70);
|
||||
--v2-overlay-simple-overlay-hover: rgba(10, 20, 30, 0.1);
|
||||
--v2-elevation-input: 0px 0px 0px 3px rgb(1, 2, 3);
|
||||
--v2-elevation-button-neutral: 0px 0px 0px 7px rgb(4, 5, 6);
|
||||
}`,
|
||||
})
|
||||
|
||||
const base = fields.nth(0)
|
||||
|
||||
const invisible = fields.nth(2)
|
||||
|
||||
await expect(base).toHaveCSS("background-color", "rgb(20, 30, 40)")
|
||||
await expect(fields.nth(1)).toHaveCSS("background-color", "rgb(50, 60, 70)")
|
||||
await expect(base).toHaveCSS("box-shadow", "rgb(1, 2, 3) 0px 0px 0px 3px")
|
||||
await expect(base).toHaveCSS("background-image", "none")
|
||||
await expect(invisible).toHaveCSS("background-color", "rgba(0, 0, 0, 0)")
|
||||
await expect(invisible).toHaveCSS("box-shadow", "none")
|
||||
|
||||
await base.hover()
|
||||
await expect
|
||||
.poll(() => base.evaluate((element) => getComputedStyle(element).boxShadow))
|
||||
.toContain("rgb(1, 2, 3) 0px 0px 0px 3px")
|
||||
await expect(base).toHaveCSS("background-color", "rgb(20, 30, 40)")
|
||||
|
||||
await invisible.hover()
|
||||
await expect(invisible).toHaveCSS("background-color", "rgba(10, 20, 30, 0.1)")
|
||||
await expect(invisible).toHaveCSS("box-shadow", "none")
|
||||
|
||||
const invalid = root.locator("[data-invalid]")
|
||||
|
||||
await expect(invalid).toHaveCSS("outline-width", "1px")
|
||||
await expect(invalid).toHaveCSS("outline-offset", "-1px")
|
||||
await expect(invalid).not.toHaveCSS("outline-color", "rgba(0, 0, 0, 0)")
|
||||
})
|
||||
|
||||
story("select fields come in the text input sizes and default to the inline trigger", async ({ mount }) => {
|
||||
const root = await mount("ui-select--appearances")
|
||||
|
||||
const fields = root.locator('[data-component="select-v2"]')
|
||||
|
||||
for (const [index, height] of [24, 28, 32].entries()) {
|
||||
await expect(fields.nth(index)).toHaveCSS("height", `${height}px`)
|
||||
}
|
||||
|
||||
await expect(fields.nth(3)).toHaveAttribute("data-appearance", "inline")
|
||||
await expect(fields.nth(3)).toHaveCSS("box-shadow", "none")
|
||||
})
|
||||
|
||||
story(
|
||||
"text inputs keep clear and copy actions equally sized and inset across sizes and directions",
|
||||
async ({ mount, page }) => {
|
||||
|
||||
@@ -32,10 +32,9 @@
|
||||
border: 0;
|
||||
border-radius: 6px;
|
||||
outline: 1px solid transparent;
|
||||
outline-offset: 0;
|
||||
background:
|
||||
linear-gradient(180deg, var(--v2-alpha-light-2) 0%, var(--v2-alpha-light-0) 100%), var(--v2-background-bg-base);
|
||||
box-shadow: var(--v2-elevation-button-neutral);
|
||||
outline-offset: -1px;
|
||||
background: var(--v2-background-bg-base);
|
||||
box-shadow: var(--v2-elevation-input);
|
||||
flex: none;
|
||||
align-self: stretch;
|
||||
user-select: none;
|
||||
@@ -45,21 +44,59 @@
|
||||
box-shadow 85ms ease-out;
|
||||
}
|
||||
|
||||
[data-component="select-v2"]:where(:hover):not([data-disabled], [data-invalid]):not(:focus-within):not(
|
||||
[data-expanded]
|
||||
) {
|
||||
background:
|
||||
linear-gradient(0deg, var(--v2-overlay-simple-overlay-hover), var(--v2-overlay-simple-overlay-hover)),
|
||||
linear-gradient(180deg, var(--v2-alpha-light-2) 0%, var(--v2-alpha-light-0) 100%), var(--v2-background-bg-base);
|
||||
[data-component="select-v2"][data-appearance="small"] {
|
||||
height: 24px;
|
||||
}
|
||||
|
||||
[data-component="select-v2"]:where([data-expanded]):not([data-disabled], [data-invalid]) {
|
||||
background:
|
||||
linear-gradient(0deg, var(--v2-overlay-simple-overlay-hover), var(--v2-overlay-simple-overlay-hover)),
|
||||
linear-gradient(180deg, var(--v2-alpha-light-2) 0%, var(--v2-alpha-light-0) 100%), var(--v2-background-bg-base);
|
||||
[data-component="select-v2"][data-appearance="large"] {
|
||||
height: 32px;
|
||||
padding-inline: 0 8px;
|
||||
}
|
||||
|
||||
[data-component="select-v2"][data-surface="layer-1"] {
|
||||
background: var(--v2-background-bg-layer-01);
|
||||
}
|
||||
|
||||
[data-component="select-v2"][data-surface="invisible"] {
|
||||
background: transparent;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
/* An inset overlay keeps the surface colour underneath, the same hover the text input uses. */
|
||||
@media (hover: hover) {
|
||||
[data-component="select-v2"]:where(:hover):not(
|
||||
[data-disabled],
|
||||
[data-invalid],
|
||||
[data-expanded],
|
||||
[data-appearance="inline"],
|
||||
[data-surface="invisible"]
|
||||
):not(:focus-within) {
|
||||
box-shadow:
|
||||
inset 0 0 0 100vmax var(--v2-overlay-simple-overlay-hover),
|
||||
var(--v2-elevation-input);
|
||||
}
|
||||
|
||||
[data-component="select-v2"][data-surface="invisible"]:where(:hover):not(
|
||||
[data-disabled],
|
||||
[data-invalid],
|
||||
[data-expanded]
|
||||
):not(:focus-within) {
|
||||
background: var(--v2-overlay-simple-overlay-hover);
|
||||
}
|
||||
}
|
||||
|
||||
[data-component="select-v2"]:where([data-expanded]):not([data-disabled], [data-invalid], [data-appearance="inline"]) {
|
||||
box-shadow:
|
||||
inset 0 0 0 100vmax var(--v2-overlay-simple-overlay-hover),
|
||||
var(--v2-elevation-input);
|
||||
outline-color: transparent;
|
||||
}
|
||||
|
||||
[data-component="select-v2"][data-surface="invisible"]:where([data-expanded]):not([data-disabled], [data-invalid]) {
|
||||
background: var(--v2-overlay-simple-overlay-hover);
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
[data-component="select-v2"]:where(:focus-within):not([data-disabled], [data-invalid]):not([data-expanded]) {
|
||||
outline-color: var(--v2-border-border-focus);
|
||||
box-shadow: none;
|
||||
|
||||
@@ -16,9 +16,11 @@ const cities: { city: string; region: Region }[] = [
|
||||
]
|
||||
|
||||
const docs = `### Overview
|
||||
Single-select built on Kobalte with an inline trigger and current menu styling.
|
||||
Single-select built on Kobalte with current menu styling. The trigger is inline by default, or an input field that matches the text input.
|
||||
|
||||
### API
|
||||
- \`appearance\`: \`inline\` (default) is the borderless trigger; \`small\`, \`base\` and \`large\` are 24px, 28px and 32px input fields.
|
||||
- \`surface\`: \`base\` (default), \`layer-1\` or \`invisible\` for input fields; invisible gains a fill on hover.
|
||||
- \`placeholder\`: Shown in the trigger when nothing is selected (same idea as text inputs).
|
||||
- \`options\`, \`current\`, \`onSelect\`: controlled selection (\`current\` is the selected option object).
|
||||
- \`value\` / \`label\`: accessors when options are not plain strings.
|
||||
@@ -126,6 +128,43 @@ export const Disabled = {
|
||||
),
|
||||
}
|
||||
|
||||
export const Appearances = {
|
||||
render: () => (
|
||||
<div style={{ display: "grid", gap: "20px", width: "280px" }}>
|
||||
<Select appearance="small" placeholder="Small (24px)" options={fruits} onSelect={() => {}} />
|
||||
<Select appearance="base" placeholder="Base (28px)" options={fruits} current="Banana" onSelect={() => {}} />
|
||||
<Select appearance="large" placeholder="Large (32px)" options={fruits} current="Cherry" onSelect={() => {}} />
|
||||
<Select placeholder="Inline (default)" options={fruits} current="Date" onSelect={() => {}} />
|
||||
</div>
|
||||
),
|
||||
}
|
||||
|
||||
export const Surfaces = {
|
||||
render: () => (
|
||||
<div style={{ display: "grid", gap: "20px", width: "280px" }}>
|
||||
<Select appearance="base" placeholder="Base (default)" options={fruits} onSelect={() => {}} />
|
||||
<Select appearance="base" surface="layer-1" placeholder="Layer 1" options={fruits} onSelect={() => {}} />
|
||||
<Select appearance="base" surface="invisible" placeholder="Invisible" options={fruits} onSelect={() => {}} />
|
||||
<Select
|
||||
appearance="base"
|
||||
surface="invisible"
|
||||
placeholder="Invisible, invalid"
|
||||
options={fruits}
|
||||
invalid
|
||||
onSelect={() => {}}
|
||||
/>
|
||||
<Select
|
||||
appearance="base"
|
||||
surface="invisible"
|
||||
placeholder="Invisible, disabled"
|
||||
options={fruits}
|
||||
disabled
|
||||
onSelect={() => {}}
|
||||
/>
|
||||
</div>
|
||||
),
|
||||
}
|
||||
|
||||
export const WithField = {
|
||||
parameters: { frameHeight: "500px" },
|
||||
render: (args) => {
|
||||
|
||||
@@ -70,11 +70,17 @@ export type SelectProps<T> = Omit<
|
||||
onHighlight?: (value: T | undefined) => void | (() => void)
|
||||
invalid?: boolean
|
||||
numeric?: boolean
|
||||
/** `inline` is the borderless settings trigger; `small`, `base` and `large` are 24px, 28px and 32px input fields. */
|
||||
appearance?: "inline" | "small" | "base" | "large"
|
||||
/** Background surface of an input field; invisible gains a fill on hover. */
|
||||
surface?: "base" | "layer-1" | "invisible"
|
||||
children?: (item: T) => JSX.Element
|
||||
valueClass?: string
|
||||
contentClass?: string
|
||||
}
|
||||
|
||||
type HighlightState = { key?: string; cleanup?: void | (() => void) }
|
||||
|
||||
export function Select<T>(props: SelectProps<T>) {
|
||||
const [local, others] = splitProps(props, [
|
||||
"class",
|
||||
@@ -91,6 +97,8 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
"children",
|
||||
"invalid",
|
||||
"numeric",
|
||||
"appearance",
|
||||
"surface",
|
||||
"disabled",
|
||||
"valueClass",
|
||||
"contentClass",
|
||||
@@ -102,7 +110,7 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
"fitViewport",
|
||||
])
|
||||
|
||||
const state: { key?: string; cleanup?: void | (() => void) } = {}
|
||||
const state: HighlightState = {}
|
||||
|
||||
const stop = () => {
|
||||
state.cleanup?.()
|
||||
@@ -110,7 +118,7 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
state.key = undefined
|
||||
}
|
||||
|
||||
const keyFor = (item: T) => (local.value ? local.value(item) : String(item as string))
|
||||
const keyFor = (item: T) => (local.value ? local.value(item) : String(item))
|
||||
|
||||
const move = (item: T | undefined) => {
|
||||
if (!local.onHighlight) return
|
||||
@@ -148,8 +156,8 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
fitViewport={local.fitViewport ?? false}
|
||||
value={local.current}
|
||||
options={grouped()}
|
||||
optionValue={(x) => (local.value ? local.value(x) : String(x as string))}
|
||||
optionTextValue={(x) => (local.label ? local.label(x) : String(x as string))}
|
||||
optionValue={(x) => (local.value ? local.value(x) : String(x))}
|
||||
optionTextValue={(x) => (local.label ? local.label(x) : String(x))}
|
||||
optionGroupChildren="options"
|
||||
placeholder={local.placeholder}
|
||||
sectionComponent={(sectionProps) => (
|
||||
@@ -172,7 +180,7 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
? local.children(itemProps.item.rawValue)
|
||||
: local.label
|
||||
? local.label(itemProps.item.rawValue)
|
||||
: String(itemProps.item.rawValue as string)}
|
||||
: String(itemProps.item.rawValue)}
|
||||
</ItemLabel>
|
||||
<ItemIndicator data-slot="menu-v2-item-indicator" forceMount>
|
||||
<CheckSmall />
|
||||
@@ -180,7 +188,7 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
</Item>
|
||||
)}
|
||||
onChange={(next) => {
|
||||
const v = next == null ? null : Array.isArray(next) ? ((next[0] as T) ?? null) : (next as T)
|
||||
const v = next == null ? null : Array.isArray(next) ? (next[0] ?? null) : next
|
||||
local.onSelect?.(v)
|
||||
stop()
|
||||
}}
|
||||
@@ -195,7 +203,8 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
aria-label={props["aria-label"]}
|
||||
aria-labelledby={props["aria-labelledby"]}
|
||||
data-component="select-v2"
|
||||
data-appearance="inline"
|
||||
data-appearance={local.appearance ?? "inline"}
|
||||
data-surface={local.appearance && local.appearance !== "inline" ? (local.surface ?? "base") : undefined}
|
||||
data-invalid={local.invalid ? "" : undefined}
|
||||
data-numeric={local.numeric ? "" : undefined}
|
||||
disabled={local.disabled}
|
||||
@@ -212,7 +221,7 @@ export function Select<T>(props: SelectProps<T>) {
|
||||
|
||||
if (local.label && selected != null) return local.label(selected)
|
||||
|
||||
return selected != null ? (selected as string) : ""
|
||||
return selected != null ? String(selected) : ""
|
||||
}}
|
||||
</Value>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user