mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
feat(ui): animate segmented control with solid-motion and fix button hit-testing (#53825)
This commit is contained in:
committed by
GitHub
Unverified
parent
c28e9ec311
commit
6297cd5a0b
@@ -966,6 +966,7 @@
|
||||
"name": "@opencode/ui",
|
||||
"version": "2.0.24",
|
||||
"dependencies": {
|
||||
"@kitlangton/solid-motion": "0.2.2",
|
||||
"@kobalte/core": "catalog:",
|
||||
"@pierre/diffs": "catalog:",
|
||||
"@solid-primitives/event-listener": "catalog:",
|
||||
@@ -2018,6 +2019,8 @@
|
||||
|
||||
"@jsx-email/text": ["@jsx-email/text@1.0.2", "", { "peerDependencies": { "react": "^18.2.0" } }, "sha512-0zzwEwrKtY6tfjPJF0r3krKCDpP/ySYDvkn4+MvIFrIH5RZKmn3XDa5o/3hkbxMwpLn4MsXGIXn9XzMTaqTfUA=="],
|
||||
|
||||
"@kitlangton/solid-motion": ["@kitlangton/solid-motion@0.2.2", "", { "dependencies": { "motion-dom": "14.0.0", "motion-utils": "14.0.0" }, "peerDependencies": { "solid-js": "^1.8.0" } }, "sha512-Fp7T8gJta3aRGJakBPQ6WIWmF6U6TmWhT6sGNNgkBiJ8cOVfuKkF9yYXzhvgRtKpo7WfzxtNLku907dLHhTDtw=="],
|
||||
|
||||
"@kobalte/core": ["@kobalte/core@0.13.13", "", { "dependencies": { "@floating-ui/dom": "^1.5.1", "@internationalized/number": "^3.2.1", "@kobalte/utils": "^0.9.2", "@solid-primitives/props": "^3.1.8", "@solid-primitives/resize-observer": "^2.0.26", "solid-presence": "^0.2.0", "solid-prevent-scroll": "^0.1.11" }, "peerDependencies": { "solid-js": "^1.9.8" } }, "sha512-czBC+IQdOgJoW7DJjeh0rht7SPmTPgWsKg7Jo3RHMfwx028WxJtDylx9dJuJuD/GUae15+E4nYafu8LwuixFYQ=="],
|
||||
|
||||
"@kobalte/utils": ["@kobalte/utils@0.9.2", "", { "dependencies": { "@solid-primitives/event-listener": "^2.2.14", "@solid-primitives/keyed": "^1.2.0", "@solid-primitives/map": "^0.4.7", "@solid-primitives/media": "^2.2.4", "@solid-primitives/props": "^3.1.8", "@solid-primitives/refs": "^1.0.5", "@solid-primitives/utils": "^6.2.1" }, "peerDependencies": { "solid-js": "^1.8.8" } }, "sha512-jRVXr+zsVHxzDXRoh+CDeXzvCsFJ6uiHhqqNQ26Cw9ZsZ3D6nqPUBt1gGVtj2ZPmRL3a9Uk1v8D1aJ8/I12Dow=="],
|
||||
@@ -4790,9 +4793,9 @@
|
||||
|
||||
"motion": ["motion@12.34.5", "", { "dependencies": { "framer-motion": "^12.34.5", "tslib": "^2.4.0" }, "peerDependencies": { "@emotion/is-prop-valid": "*", "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0" }, "optionalPeers": ["@emotion/is-prop-valid", "react", "react-dom"] }, "sha512-N06NLJ9IeBHeielRqIvYvjPfXuRdyTxa+9++BgpGa+hY2D7TcMkI6QzV3jaRuv0aZRXgMa7cPy9YcBUBisPzAQ=="],
|
||||
|
||||
"motion-dom": ["motion-dom@12.43.0", "", { "dependencies": { "motion-utils": "^12.39.0" } }, "sha512-azKON4d9S65PEoFUiQTMTgPheEmzf2QngdRc50AKfJp9Q9mmcBVw22c8eMq9k8kxOFHdL7+WZY7N/5F/lwiDag=="],
|
||||
"motion-dom": ["motion-dom@14.0.0", "", { "dependencies": { "motion-utils": "14.0.0" } }, "sha512-aU3ApXo1yTpeUi3JU+jbDk2TELjxPE3MbBMUP7nZHlARb6RweJK+ojmLjpuB0YsY6zhbBl1YWGo8LMsI1/Llqw=="],
|
||||
|
||||
"motion-utils": ["motion-utils@12.39.0", "", {}, "sha512-8nadJAJjTtqRkmRF36FoJTrywK9nnFmnPwnSMyxaOCU7GDjN9RTMJIxx9De8ErM+vpPhMccr/6fo5WciyQLnMQ=="],
|
||||
"motion-utils": ["motion-utils@14.0.0", "", {}, "sha512-jBsyjVxTTRZNeuvd/v+btVXr90LQkYl4HhRu9T2ARc+PMnvkmjsjRVeAg4Qm10b6TlTw+kykWI8gu/6LuvpLSQ=="],
|
||||
|
||||
"mrmime": ["mrmime@2.0.1", "", {}, "sha512-Y3wQdFg2Va6etvQ5I82yUhGdsKrcYox6p7FfL1LbK2J4V01F9TGlepTIhnK24t7koZibmg82KGglhA1XK5IsLQ=="],
|
||||
|
||||
@@ -7462,6 +7465,10 @@
|
||||
|
||||
"minipass-pipeline/minipass/yallist": ["yallist@4.0.0", "", {}, "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="],
|
||||
|
||||
"motion/framer-motion/motion-dom": ["motion-dom@12.43.0", "", { "dependencies": { "motion-utils": "^12.39.0" } }, "sha512-azKON4d9S65PEoFUiQTMTgPheEmzf2QngdRc50AKfJp9Q9mmcBVw22c8eMq9k8kxOFHdL7+WZY7N/5F/lwiDag=="],
|
||||
|
||||
"motion/framer-motion/motion-utils": ["motion-utils@12.39.0", "", {}, "sha512-8nadJAJjTtqRkmRF36FoJTrywK9nnFmnPwnSMyxaOCU7GDjN9RTMJIxx9De8ErM+vpPhMccr/6fo5WciyQLnMQ=="],
|
||||
|
||||
"openid-client/lru-cache/yallist": ["yallist@4.0.0", "", {}, "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="],
|
||||
|
||||
"p-locate/p-limit/yocto-queue": ["yocto-queue@0.1.0", "", {}, "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="],
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
exact = true
|
||||
# Only install newly resolved package versions published at least 3 days ago.
|
||||
minimumReleaseAge = 259200
|
||||
minimumReleaseAgeExcludes = ["@ai-sdk/amazon-bedrock", "@ai-sdk/anthropic", "@brendonovich/vite-plugin-opencode", "@opencode/sdk", "@opencode-ai/pty", "@opencode-ai/pty-darwin-arm64", "@opencode-ai/pty-darwin-x64", "@opencode-ai/pty-linux-arm64-gnu", "@opencode-ai/pty-linux-arm64-musl", "@opencode-ai/pty-linux-x64-gnu", "@opencode-ai/pty-linux-x64-musl", "@opentui/core", "@opentui/core-darwin-arm64", "@opentui/core-darwin-x64", "@opentui/core-linux-arm64", "@opentui/core-linux-arm64-musl", "@opentui/core-linux-x64", "@opentui/core-linux-x64-musl", "@opentui/core-win32-arm64", "@opentui/core-win32-x64", "@opentui/keymap", "@opentui/solid", "opentui-spinner", "gitlab-ai-provider", "opencode-gitlab-auth", "@ff-labs/fff-node", "@ff-labs/fff-bun", "@ff-labs/fff-bin-darwin-arm64", "@ff-labs/fff-bin-darwin-x64", "@ff-labs/fff-bin-linux-arm64-gnu", "@ff-labs/fff-bin-linux-arm64-musl", "@ff-labs/fff-bin-linux-x64-gnu", "@ff-labs/fff-bin-linux-x64-musl", "@ff-labs/fff-bin-win32-arm64", "@ff-labs/fff-bin-win32-x64", "@pierre/diffs", "@pierre/theming", "app-builder-lib", "dmg-builder", "electron", "electron-builder", "electron-publish", "blume", "mermaid"]
|
||||
minimumReleaseAgeExcludes = ["@ai-sdk/amazon-bedrock", "@ai-sdk/anthropic", "@brendonovich/vite-plugin-opencode", "@kitlangton/solid-motion", "@opencode/sdk", "@opencode-ai/pty", "@opencode-ai/pty-darwin-arm64", "@opencode-ai/pty-darwin-x64", "@opencode-ai/pty-linux-arm64-gnu", "@opencode-ai/pty-linux-arm64-musl", "@opencode-ai/pty-linux-x64-gnu", "@opencode-ai/pty-linux-x64-musl", "@opentui/core", "@opentui/core-darwin-arm64", "@opentui/core-darwin-x64", "@opentui/core-linux-arm64", "@opentui/core-linux-arm64-musl", "@opentui/core-linux-x64", "@opentui/core-linux-x64-musl", "@opentui/core-win32-arm64", "@opentui/core-win32-x64", "@opentui/keymap", "@opentui/solid", "opentui-spinner", "gitlab-ai-provider", "opencode-gitlab-auth", "@ff-labs/fff-node", "@ff-labs/fff-bun", "@ff-labs/fff-bin-darwin-arm64", "@ff-labs/fff-bin-darwin-x64", "@ff-labs/fff-bin-linux-arm64-gnu", "@ff-labs/fff-bin-linux-arm64-musl", "@ff-labs/fff-bin-linux-x64-gnu", "@ff-labs/fff-bin-linux-x64-musl", "@ff-labs/fff-bin-win32-arm64", "@ff-labs/fff-bin-win32-x64", "@pierre/diffs", "@pierre/theming", "app-builder-lib", "dmg-builder", "electron", "electron-builder", "electron-publish", "blume", "mermaid"]
|
||||
|
||||
[test]
|
||||
root = "./do-not-run-tests-from-root"
|
||||
|
||||
@@ -109,6 +109,7 @@
|
||||
"vite-plugin-solid": "catalog:"
|
||||
},
|
||||
"dependencies": {
|
||||
"@kitlangton/solid-motion": "0.2.2",
|
||||
"@kobalte/core": "catalog:",
|
||||
"@pierre/diffs": "catalog:",
|
||||
"@solid-primitives/event-listener": "catalog:",
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
@import "../submit.css";
|
||||
|
||||
* {
|
||||
[data-component="button-v2"] {
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
text-rendering: geometricPrecision;
|
||||
}
|
||||
|
||||
[data-component="button-v2"] {
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
@@ -26,6 +22,10 @@
|
||||
outline: none;
|
||||
}
|
||||
|
||||
[data-component="button-v2"] > * {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
[data-component="button-v2"]:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
@import "../submit.css";
|
||||
|
||||
* {
|
||||
[data-component="icon-button-v2"] {
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
text-rendering: geometricPrecision;
|
||||
}
|
||||
|
||||
[data-component="icon-button-v2"] {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2"] {
|
||||
position: relative;
|
||||
isolation: isolate;
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
@@ -16,9 +18,28 @@
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 0 0 0.5px var(--v2-border-border-base);
|
||||
flex: none;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2-indicator"] {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
border-radius: 6px;
|
||||
background: var(--v2-background-bg-base);
|
||||
box-shadow: 0 0 0 0.5px var(--v2-border-border-strong);
|
||||
pointer-events: none;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
z-index: 0;
|
||||
will-change: transform, width;
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2-item"] {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
@@ -34,20 +55,26 @@
|
||||
background: transparent;
|
||||
box-shadow: none;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
font-style: normal;
|
||||
font-weight: 440;
|
||||
font-size: 13px;
|
||||
line-height: var(--line-height-compact);
|
||||
line-height: 100%;
|
||||
letter-spacing: -0.04px;
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-variation-settings: "slnt" 0;
|
||||
color: var(--v2-text-text-muted);
|
||||
transition:
|
||||
color 0.15s ease,
|
||||
background-color 0.12s ease,
|
||||
color 0.12s ease,
|
||||
box-shadow 0.12s ease;
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2-item"]:where(:hover:not(:disabled):not([data-pressed])) {
|
||||
color: var(--v2-text-text-base);
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2-item"]:where(:disabled) {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.45;
|
||||
@@ -60,12 +87,15 @@
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2-item"]:where([data-pressed]) {
|
||||
background: var(--v2-background-bg-base);
|
||||
color: var(--v2-text-text-base);
|
||||
box-shadow: 0 0 0 0.5px var(--v2-border-border-strong);
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2"]:not([data-ready]) [data-slot="segmented-control-v2-item"]:where([data-pressed]) {
|
||||
background: var(--v2-background-bg-base);
|
||||
box-shadow: 0 0 0 0.5px var(--v2-border-border-strong);
|
||||
}
|
||||
|
||||
[data-slot="segmented-control-v2-item-label"] {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
@@ -77,4 +107,7 @@
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
pointer-events: none;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { measureOffset, SEGMENTED_CONTROL_SPRING, type OffsetElement } from "./segmented-control"
|
||||
|
||||
describe("SegmentedControl", () => {
|
||||
test("uses snappier non-bouncy spring default", () => {
|
||||
expect(SEGMENTED_CONTROL_SPRING).toEqual({
|
||||
type: "spring",
|
||||
visualDuration: 0.15,
|
||||
bounce: 0,
|
||||
})
|
||||
})
|
||||
|
||||
test("measures button offset along offsetParent chain to root", () => {
|
||||
const root: OffsetElement = {
|
||||
offsetLeft: 0,
|
||||
offsetWidth: 232,
|
||||
offsetParent: null,
|
||||
getBoundingClientRect: () => ({ left: 0 }),
|
||||
}
|
||||
|
||||
const wrapper: OffsetElement = {
|
||||
offsetLeft: 8,
|
||||
offsetWidth: 216,
|
||||
offsetParent: root,
|
||||
getBoundingClientRect: () => ({ left: 8 }),
|
||||
}
|
||||
|
||||
const button: OffsetElement = {
|
||||
offsetLeft: 44,
|
||||
offsetWidth: 76,
|
||||
offsetParent: wrapper,
|
||||
getBoundingClientRect: () => ({ left: 52 }),
|
||||
}
|
||||
|
||||
expect(measureOffset(root, button)).toEqual({ x: 52, width: 76 })
|
||||
})
|
||||
|
||||
test("falls back to bounding client rect difference when offsetParent does not reach root", () => {
|
||||
const root: OffsetElement = {
|
||||
offsetLeft: 0,
|
||||
offsetWidth: 232,
|
||||
offsetParent: null,
|
||||
getBoundingClientRect: () => ({ left: 100 }),
|
||||
}
|
||||
|
||||
const button: OffsetElement = {
|
||||
offsetLeft: 0,
|
||||
offsetWidth: 64,
|
||||
offsetParent: null,
|
||||
getBoundingClientRect: () => ({ left: 164 }),
|
||||
}
|
||||
|
||||
expect(measureOffset(root, button)).toEqual({ x: 64, width: 64 })
|
||||
})
|
||||
})
|
||||
@@ -1,25 +1,37 @@
|
||||
import { animate, motion, useMotionValue, type Transition } from "@kitlangton/solid-motion"
|
||||
import {
|
||||
createContext,
|
||||
createEffect,
|
||||
createMemo,
|
||||
createSignal,
|
||||
mergeProps,
|
||||
onCleanup,
|
||||
onMount,
|
||||
Show,
|
||||
splitProps,
|
||||
useContext,
|
||||
type Accessor,
|
||||
type ComponentProps,
|
||||
type JSX,
|
||||
type ParentProps,
|
||||
} from "solid-js"
|
||||
import type { ComponentProps } from "solid-js"
|
||||
import "./segmented-control.css"
|
||||
|
||||
type OnChange = (value: string | null) => void
|
||||
|
||||
export const SEGMENTED_CONTROL_SPRING: Transition = {
|
||||
type: "spring",
|
||||
visualDuration: 0.15,
|
||||
bounce: 0,
|
||||
}
|
||||
|
||||
type SegmentedControlContextValue = {
|
||||
selected: Accessor<string | null>
|
||||
groupDisabled: Accessor<boolean>
|
||||
select: (value: string) => void
|
||||
clearIfAllowed: (value: string) => void
|
||||
focusNext: (from: HTMLButtonElement, direction: 1 | -1) => void
|
||||
registerItem: (value: string, el: HTMLButtonElement) => () => void
|
||||
}
|
||||
|
||||
const SegmentedControlContext = createContext<SegmentedControlContextValue>()
|
||||
@@ -32,6 +44,33 @@ function useSegmentedControlContext() {
|
||||
return ctx
|
||||
}
|
||||
|
||||
export type OffsetElement = {
|
||||
offsetLeft: number
|
||||
offsetWidth: number
|
||||
offsetParent: Element | OffsetElement | null
|
||||
getBoundingClientRect: () => { left: number }
|
||||
}
|
||||
|
||||
function isOffsetElement(node: Element | OffsetElement | null): node is OffsetElement {
|
||||
return node !== null && "offsetLeft" in node && "offsetParent" in node
|
||||
}
|
||||
|
||||
export function measureOffset(root: OffsetElement, button: OffsetElement) {
|
||||
let offset = 0
|
||||
let node: OffsetElement | null = button
|
||||
|
||||
while (node && node !== root) {
|
||||
offset += node.offsetLeft
|
||||
node = isOffsetElement(node.offsetParent) ? node.offsetParent : null
|
||||
}
|
||||
|
||||
if (node !== root) {
|
||||
offset = button.getBoundingClientRect().left - root.getBoundingClientRect().left
|
||||
}
|
||||
|
||||
return { x: offset, width: button.offsetWidth }
|
||||
}
|
||||
|
||||
export type SegmentedControlProps = Omit<ComponentProps<"div">, "onChange"> &
|
||||
ParentProps<{
|
||||
/** Selected value when controlled (including `null` when empty). Omit key for uncontrolled. */
|
||||
@@ -42,10 +81,12 @@ export type SegmentedControlProps = Omit<ComponentProps<"div">, "onChange"> &
|
||||
/** When true, clicking the active segment clears selection (`onChange(null)`). Default false. */
|
||||
allowDeselect?: boolean
|
||||
disabled?: boolean
|
||||
/** Motion transition for the active indicator pill. */
|
||||
transition?: Transition
|
||||
}>
|
||||
|
||||
export function SegmentedControl(props: SegmentedControlProps) {
|
||||
const isControlled = createMemo(() => Object.hasOwn(props as object, "value"))
|
||||
const isControlled = createMemo(() => Object.hasOwn(props, "value"))
|
||||
const merged = mergeProps({ allowDeselect: false, disabled: false }, props)
|
||||
|
||||
const [local, rest] = splitProps(merged, [
|
||||
@@ -57,13 +98,88 @@ export function SegmentedControl(props: SegmentedControlProps) {
|
||||
"onChange",
|
||||
"allowDeselect",
|
||||
"disabled",
|
||||
"transition",
|
||||
"ref",
|
||||
])
|
||||
|
||||
const [internal, setInternal] = createSignal<string | null>(local.defaultValue ?? null)
|
||||
const [layoutVersion, setLayoutVersion] = createSignal(0)
|
||||
const [ready, setReady] = createSignal(false)
|
||||
|
||||
const selected = createMemo(() => (isControlled() ? (local.value ?? null) : internal()))
|
||||
|
||||
const reducedMotion = () => globalThis.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches === true
|
||||
|
||||
const x = useMotionValue(0)
|
||||
const width = useMotionValue(0)
|
||||
|
||||
let rootEl: HTMLDivElement | undefined
|
||||
let observer: ResizeObserver | undefined
|
||||
const items = new Map<string, HTMLButtonElement>()
|
||||
let target = { x: 0, width: 0 }
|
||||
let lastSelected: string | null = null
|
||||
|
||||
const syncIndicator = (shouldAnimate: boolean) => {
|
||||
const current = selected()
|
||||
const button = current !== null ? items.get(current) : undefined
|
||||
|
||||
if (!rootEl || !button) {
|
||||
setReady(false)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const next = measureOffset(rootEl, button)
|
||||
|
||||
if (next.width === 0) return
|
||||
|
||||
target = next
|
||||
|
||||
if (!ready() || !shouldAnimate || reducedMotion()) {
|
||||
x.jump(next.x)
|
||||
width.jump(next.width)
|
||||
setReady(true)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const transition = local.transition ?? SEGMENTED_CONTROL_SPRING
|
||||
animate(x, next.x, transition)
|
||||
animate(width, next.width, transition)
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
if (!("ResizeObserver" in globalThis)) return
|
||||
|
||||
observer = new ResizeObserver(() => {
|
||||
const current = selected()
|
||||
const button = current !== null ? items.get(current) : undefined
|
||||
|
||||
if (!rootEl || !button) return
|
||||
|
||||
const next = measureOffset(rootEl, button)
|
||||
|
||||
if (next.width === 0) return
|
||||
|
||||
if (ready() && next.x === target.x && next.width === target.width) return
|
||||
|
||||
syncIndicator(false)
|
||||
})
|
||||
|
||||
if (rootEl) observer.observe(rootEl)
|
||||
|
||||
items.forEach((button) => observer?.observe(button))
|
||||
onCleanup(() => observer?.disconnect())
|
||||
})
|
||||
|
||||
createEffect(() => {
|
||||
const current = selected()
|
||||
layoutVersion()
|
||||
const shouldAnimate = ready() && lastSelected !== null && current !== null && current !== lastSelected
|
||||
lastSelected = current
|
||||
syncIndicator(shouldAnimate)
|
||||
})
|
||||
|
||||
const setSelected = (next: string | null) => {
|
||||
if (!isControlled()) setInternal(next)
|
||||
local.onChange?.(next)
|
||||
@@ -92,19 +208,32 @@ export function SegmentedControl(props: SegmentedControlProps) {
|
||||
next?.focus()
|
||||
}
|
||||
|
||||
const registerItem = (value: string, el: HTMLButtonElement) => {
|
||||
items.set(value, el)
|
||||
observer?.observe(el)
|
||||
setLayoutVersion((v) => v + 1)
|
||||
|
||||
return () => {
|
||||
if (items.get(value) === el) items.delete(value)
|
||||
observer?.unobserve(el)
|
||||
setLayoutVersion((v) => v + 1)
|
||||
}
|
||||
}
|
||||
|
||||
const ctx: SegmentedControlContextValue = {
|
||||
selected,
|
||||
groupDisabled: () => !!local.disabled,
|
||||
select,
|
||||
clearIfAllowed,
|
||||
focusNext,
|
||||
registerItem,
|
||||
}
|
||||
|
||||
const assignRef = (el: HTMLDivElement | undefined) => {
|
||||
const assignRef = (el: HTMLDivElement) => {
|
||||
rootEl = el
|
||||
const r = local.ref
|
||||
|
||||
if (typeof r === "function") (r as (el: HTMLDivElement | undefined) => void)(el)
|
||||
else if (r != null && typeof r === "object" && "value" in r) (r as { value: HTMLDivElement | undefined }).value = el
|
||||
if (r instanceof Function) r(el)
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -115,11 +244,15 @@ export function SegmentedControl(props: SegmentedControlProps) {
|
||||
role="group"
|
||||
data-component="segmented-control-v2"
|
||||
data-slot="segmented-control-v2"
|
||||
data-ready={ready() ? "" : undefined}
|
||||
classList={{
|
||||
...local.classList,
|
||||
[local.class ?? ""]: !!local.class,
|
||||
}}
|
||||
>
|
||||
<Show when={ready() && selected() !== null}>
|
||||
<motion.span data-slot="segmented-control-v2-indicator" aria-hidden="true" style={{ x, width }} />
|
||||
</Show>
|
||||
{local.children}
|
||||
</div>
|
||||
</SegmentedControlContext.Provider>
|
||||
@@ -134,9 +267,17 @@ export type SegmentedControlItemProps = Omit<ComponentProps<"button">, "type" |
|
||||
|
||||
function invokeButtonHandler<E extends Event>(
|
||||
handler: JSX.EventHandlerUnion<HTMLButtonElement, E> | undefined,
|
||||
e: E & { currentTarget: HTMLButtonElement },
|
||||
e: Parameters<JSX.EventHandler<HTMLButtonElement, E>>[0],
|
||||
) {
|
||||
if (typeof handler === "function") (handler as (ev: typeof e) => void)(e)
|
||||
if (handler instanceof Function) {
|
||||
handler(e)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (Array.isArray(handler)) {
|
||||
handler[0](handler[1], e)
|
||||
}
|
||||
}
|
||||
|
||||
export function SegmentedControlItem(props: SegmentedControlItemProps) {
|
||||
@@ -150,10 +291,18 @@ export function SegmentedControlItem(props: SegmentedControlItemProps) {
|
||||
"disabled",
|
||||
"onClick",
|
||||
"onKeyDown",
|
||||
"ref",
|
||||
])
|
||||
|
||||
const ctx = useSegmentedControlContext()
|
||||
|
||||
let buttonEl: HTMLButtonElement | undefined
|
||||
createEffect(() => {
|
||||
if (!buttonEl) return
|
||||
const cleanup = ctx.registerItem(local.value, buttonEl)
|
||||
onCleanup(cleanup)
|
||||
})
|
||||
|
||||
const pressed = createMemo(() => ctx.selected() === local.value)
|
||||
const disabled = createMemo(() => ctx.groupDisabled() || !!local.disabled)
|
||||
|
||||
@@ -162,27 +311,38 @@ export function SegmentedControlItem(props: SegmentedControlItemProps) {
|
||||
|
||||
if (e.defaultPrevented || disabled()) return
|
||||
|
||||
if (pressed()) ctx.clearIfAllowed(local.value)
|
||||
else ctx.select(local.value)
|
||||
if (pressed()) {
|
||||
ctx.clearIfAllowed(local.value)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx.select(local.value)
|
||||
}
|
||||
|
||||
const onKeyDown: JSX.EventHandlerUnion<HTMLButtonElement, KeyboardEvent> = (e) => {
|
||||
invokeButtonHandler(local.onKeyDown, e)
|
||||
|
||||
if (e.defaultPrevented || disabled()) return
|
||||
|
||||
const t = e.currentTarget
|
||||
const horizontal = t.closest(`[data-slot="segmented-control-v2"]`)?.matches(":dir(rtl)") ? -1 : 1
|
||||
|
||||
if (e.key === "ArrowRight") {
|
||||
e.preventDefault()
|
||||
ctx.focusNext(t, horizontal)
|
||||
} else if (e.key === "ArrowLeft") {
|
||||
e.preventDefault()
|
||||
ctx.focusNext(t, -horizontal as 1 | -1)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// accessibility stuff
|
||||
else if (e.key === "Home") {
|
||||
if (e.key === "ArrowLeft") {
|
||||
e.preventDefault()
|
||||
ctx.focusNext(t, horizontal === 1 ? -1 : 1)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (e.key === "Home") {
|
||||
e.preventDefault()
|
||||
const root = t.closest(`[data-slot="segmented-control-v2"]`)
|
||||
|
||||
@@ -191,7 +351,11 @@ export function SegmentedControlItem(props: SegmentedControlItemProps) {
|
||||
)
|
||||
|
||||
first?.focus()
|
||||
} else if (e.key === "End") {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (e.key === "End") {
|
||||
e.preventDefault()
|
||||
const root = t.closest(`[data-slot="segmented-control-v2"]`)
|
||||
|
||||
@@ -204,9 +368,17 @@ export function SegmentedControlItem(props: SegmentedControlItemProps) {
|
||||
}
|
||||
}
|
||||
|
||||
const assignRef = (el: HTMLButtonElement) => {
|
||||
buttonEl = el
|
||||
const r = local.ref
|
||||
|
||||
if (r instanceof Function) r(el)
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
{...rest}
|
||||
ref={assignRef}
|
||||
type="button"
|
||||
data-slot="segmented-control-v2-item"
|
||||
data-pressed={pressed() ? "" : undefined}
|
||||
@@ -223,3 +395,11 @@ export function SegmentedControlItem(props: SegmentedControlItemProps) {
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
export const SegmentedControlV2 = SegmentedControl
|
||||
|
||||
export const SegmentedControlItemV2 = SegmentedControlItem
|
||||
|
||||
export type SegmentedControlV2Props = SegmentedControlProps
|
||||
|
||||
export type SegmentedControlItemV2Props = SegmentedControlItemProps
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
* {
|
||||
[data-component="tabs-v2"] {
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
text-rendering: geometricPrecision;
|
||||
}
|
||||
|
||||
[data-component="tabs-v2"] {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
|
||||
Reference in New Issue
Block a user