@opencode/ui 2.2.5 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/button/button.d.ts +2 -2
- package/dist/actions/icon-button/icon-button.d.ts +1 -1
- package/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/file-icon-machine.d.ts +40 -0
- package/dist/components/file-icon.d.ts +0 -2
- package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/components/list.d.ts +2 -6
- package/dist/components/menu-scroll.d.ts +9 -3
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/resize-handle.d.ts +2 -0
- package/dist/components/scroll-view-machine.d.ts +68 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/spinner.d.ts +11 -3
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/components/typewriter-machine.d.ts +62 -0
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/checkbox/checkbox.d.ts +6 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/select/select.d.ts +4 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/hooks/list-machine.d.ts +150 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/i18n/en.d.ts +5 -0
- package/dist/icons/catalog.d.ts +8 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/menu/menu.d.ts +5 -3
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
- package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/dist/theme/context.d.ts +38 -14
- package/dist/theme/machine.d.ts +174 -0
- package/dist/typography/text-shimmer/machine.d.ts +48 -0
- package/package.json +19 -3
- package/src/actions/button/button.tsx +11 -6
- package/src/actions/icon-button/icon-button.tsx +17 -4
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/file-icon-machine.ts +78 -0
- package/src/components/file-icon.tsx +96 -579
- package/src/components/file-icons/choose-icon-name.ts +574 -0
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/components/list.tsx +46 -137
- package/src/components/menu-scroll.css +19 -8
- package/src/components/menu-scroll.tsx +111 -34
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +116 -67
- package/src/components/scroll-view-machine.ts +244 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/spinner.css +18 -5
- package/src/components/spinner.tsx +28 -14
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/components/typewriter-machine.ts +134 -0
- package/src/components/typewriter.tsx +50 -39
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/data-display/line-comment/line-comment.tsx +157 -178
- package/src/data-display/line-comment/machine.ts +391 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/activity-ring/activity-ring.tsx +1 -1
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +55 -59
- package/src/forms/checkbox/checkbox-compact.css +169 -0
- package/src/forms/checkbox/checkbox.tsx +93 -31
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +4 -4
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.css +59 -17
- package/src/forms/select/select.tsx +67 -31
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/hooks/list-machine.ts +359 -0
- package/src/hooks/use-filtered-list.tsx +101 -112
- package/src/i18n/en.ts +6 -1
- package/src/icons/catalog.ts +8 -0
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +130 -22
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/overlays/dialog/dialog.tsx +7 -1
- package/src/overlays/mcp-sign-in/machine.ts +343 -0
- package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +157 -92
- package/src/styles/theme.css +10 -2
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/typography/text-shimmer/machine.ts +90 -0
- package/src/typography/text-shimmer/text-shimmer.css +71 -39
- package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every dropdown menu runs this machine, so each reachable state and event is a frozen module singleton, and an event
|
|
3
|
+
* that changes nothing returns the state it was given.
|
|
4
|
+
*/
|
|
5
|
+
export type MenuInputGesture = "pointer" | "keyboard" | "none"
|
|
6
|
+
|
|
7
|
+
/** How the menu opened. An open without an input gesture (a shortcut handler, an async action) is `programmatic`. */
|
|
8
|
+
export type MenuOpenModality = "pointer" | "keyboard" | "programmatic"
|
|
9
|
+
|
|
10
|
+
export type MenuState =
|
|
11
|
+
| { readonly status: "closed" }
|
|
12
|
+
| { readonly status: "open"; readonly modality: MenuOpenModality; readonly outsideInteracted: boolean }
|
|
13
|
+
/**
|
|
14
|
+
* Kobalte reports the close first and restores focus a task later, so the modality outlives the open state until
|
|
15
|
+
* that focus restore has run.
|
|
16
|
+
*/
|
|
17
|
+
| { readonly status: "closing"; readonly modality: MenuOpenModality; readonly outsideInteracted: boolean }
|
|
18
|
+
| { readonly status: "disposed" }
|
|
19
|
+
|
|
20
|
+
export type MenuEvent =
|
|
21
|
+
| { readonly type: "open.changed"; readonly open: boolean; readonly gesture: MenuInputGesture }
|
|
22
|
+
/** A press outside a non-modal menu, or a context menu anywhere, dismissed it and keeps focus where it landed. */
|
|
23
|
+
| { readonly type: "outside.interacted" }
|
|
24
|
+
| { readonly type: "close.autofocus" }
|
|
25
|
+
| { readonly type: "disposed" }
|
|
26
|
+
|
|
27
|
+
const MODALITIES: readonly MenuOpenModality[] = ["pointer", "keyboard", "programmatic"]
|
|
28
|
+
|
|
29
|
+
const GESTURES: readonly MenuInputGesture[] = ["pointer", "keyboard", "none"]
|
|
30
|
+
|
|
31
|
+
const CLOSED: MenuState = Object.freeze({ status: "closed" })
|
|
32
|
+
|
|
33
|
+
const DISPOSED: MenuState = Object.freeze({ status: "disposed" })
|
|
34
|
+
|
|
35
|
+
const OPEN = MODALITIES.flatMap((modality) =>
|
|
36
|
+
[false, true].map((outsideInteracted): MenuState => Object.freeze({ status: "open", modality, outsideInteracted })),
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
const CLOSING = MODALITIES.flatMap((modality) =>
|
|
40
|
+
[false, true].map(
|
|
41
|
+
(outsideInteracted): MenuState => Object.freeze({ status: "closing", modality, outsideInteracted }),
|
|
42
|
+
),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
const key = (modality: MenuOpenModality, outsideInteracted: boolean) =>
|
|
46
|
+
MODALITIES.indexOf(modality) * 2 + (outsideInteracted ? 1 : 0)
|
|
47
|
+
|
|
48
|
+
const open = (modality: MenuOpenModality, outsideInteracted: boolean) => OPEN[key(modality, outsideInteracted)]
|
|
49
|
+
|
|
50
|
+
const closing = (modality: MenuOpenModality, outsideInteracted: boolean) => CLOSING[key(modality, outsideInteracted)]
|
|
51
|
+
|
|
52
|
+
const OPEN_CHANGED = [false, true].flatMap((isOpen) =>
|
|
53
|
+
GESTURES.map((gesture): MenuEvent => Object.freeze({ type: "open.changed", open: isOpen, gesture })),
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
export const MENU_OUTSIDE_INTERACTED: MenuEvent = Object.freeze({ type: "outside.interacted" })
|
|
57
|
+
|
|
58
|
+
export const MENU_CLOSE_AUTOFOCUS: MenuEvent = Object.freeze({ type: "close.autofocus" })
|
|
59
|
+
|
|
60
|
+
export const MENU_DISPOSED: MenuEvent = Object.freeze({ type: "disposed" })
|
|
61
|
+
|
|
62
|
+
export function openChangedEvent(isOpen: boolean, gesture: MenuInputGesture) {
|
|
63
|
+
return OPEN_CHANGED[(isOpen ? GESTURES.length : 0) + GESTURES.indexOf(gesture)]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function createMenuState(initialOpen: boolean) {
|
|
67
|
+
return initialOpen ? open("programmatic", false) : CLOSED
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function transitionMenu(state: MenuState, event: MenuEvent): MenuState {
|
|
71
|
+
if (state.status === "disposed") return state
|
|
72
|
+
|
|
73
|
+
if (event.type === "disposed") return DISPOSED
|
|
74
|
+
|
|
75
|
+
if (event.type === "open.changed") {
|
|
76
|
+
if (event.open && state.status !== "open") {
|
|
77
|
+
return open(
|
|
78
|
+
event.gesture === "pointer" ? "pointer" : event.gesture === "keyboard" ? "keyboard" : "programmatic",
|
|
79
|
+
false,
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!event.open && state.status === "open") return closing(state.modality, state.outsideInteracted)
|
|
84
|
+
|
|
85
|
+
return state
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (event.type === "outside.interacted") {
|
|
89
|
+
if (state.status === "open" && !state.outsideInteracted) return open(state.modality, true)
|
|
90
|
+
|
|
91
|
+
if (state.status === "closing" && !state.outsideInteracted) return closing(state.modality, true)
|
|
92
|
+
|
|
93
|
+
return state
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (state.status === "closing") return CLOSED
|
|
97
|
+
|
|
98
|
+
return state
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Restore focus to the trigger without a focus ring: Escape counts as keyboard use, so Chromium would ring a trigger
|
|
103
|
+
* the pointer opened. After an outside press, focus stays where that press put it.
|
|
104
|
+
*/
|
|
105
|
+
export function selectMenuRestoreTriggerWithoutRing(state: MenuState) {
|
|
106
|
+
return (
|
|
107
|
+
(state.status === "closing" || state.status === "open") && state.modality === "pointer" && !state.outsideInteracted
|
|
108
|
+
)
|
|
109
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { DropdownMenu } from "@kobalte/core/dropdown-menu"
|
|
1
|
+
import { DropdownMenu, type DropdownMenuTriggerProps } from "@kobalte/core/dropdown-menu"
|
|
2
2
|
import { ContextMenu } from "@kobalte/core/context-menu"
|
|
3
|
-
import {
|
|
3
|
+
import type { PolymorphicProps } from "@kobalte/core/polymorphic"
|
|
4
4
|
import {
|
|
5
|
+
createComputed,
|
|
5
6
|
createContext,
|
|
7
|
+
createSignal,
|
|
8
|
+
onCleanup,
|
|
6
9
|
Show,
|
|
7
10
|
splitProps,
|
|
8
11
|
useContext,
|
|
@@ -11,7 +14,20 @@ import {
|
|
|
11
14
|
type ComponentProps,
|
|
12
15
|
type JSX,
|
|
13
16
|
type ParentProps,
|
|
17
|
+
type ValidComponent,
|
|
14
18
|
} from "solid-js"
|
|
19
|
+
import {
|
|
20
|
+
createMenuState,
|
|
21
|
+
MENU_CLOSE_AUTOFOCUS,
|
|
22
|
+
MENU_DISPOSED,
|
|
23
|
+
MENU_OUTSIDE_INTERACTED,
|
|
24
|
+
openChangedEvent,
|
|
25
|
+
selectMenuRestoreTriggerWithoutRing,
|
|
26
|
+
transitionMenu,
|
|
27
|
+
type MenuEvent,
|
|
28
|
+
type MenuInputGesture,
|
|
29
|
+
type MenuState,
|
|
30
|
+
} from "./machine"
|
|
15
31
|
import "./menu.css"
|
|
16
32
|
|
|
17
33
|
const ChevronRight: Component = () => (
|
|
@@ -42,13 +58,70 @@ const CheckMark: Component = () => (
|
|
|
42
58
|
|
|
43
59
|
export type MenuAppearance = "standard" | "compact"
|
|
44
60
|
|
|
45
|
-
|
|
61
|
+
type MenuContextValue = {
|
|
46
62
|
appearance: Accessor<MenuAppearance>
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
63
|
+
state: Accessor<MenuState>
|
|
64
|
+
modal: Accessor<boolean>
|
|
65
|
+
onOutsideInteract: () => void
|
|
66
|
+
onCloseAutoFocus: () => void
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Context menus restore focus through Kobalte alone, so they never run the dropdown machine.
|
|
70
|
+
const UNTRACKED_MENU: MenuContextValue = {
|
|
71
|
+
appearance: () => "compact",
|
|
72
|
+
state: () => createMenuState(false),
|
|
73
|
+
modal: () => true,
|
|
74
|
+
onOutsideInteract: () => {},
|
|
75
|
+
onCloseAutoFocus: () => {},
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const MenuAppearanceContext = createContext<MenuContextValue>(UNTRACKED_MENU)
|
|
79
|
+
|
|
80
|
+
// One set of window listeners serves every mounted menu. Capture runs before the trigger's own handlers, so while an
|
|
81
|
+
// input event dispatches, `gesture` names the input that may open a menu. It clears once that event's task ends, so a
|
|
82
|
+
// later open without input counts as programmatic.
|
|
83
|
+
let trackers = 0
|
|
84
|
+
|
|
85
|
+
let gesture: MenuInputGesture = "none"
|
|
86
|
+
|
|
87
|
+
let gestureReset: ReturnType<typeof setTimeout> | undefined
|
|
88
|
+
|
|
89
|
+
const trackGesture = (event: Event) => {
|
|
90
|
+
if (!event.isTrusted) return
|
|
91
|
+
// Keyboard activation fires a click with no press count.
|
|
92
|
+
gesture =
|
|
93
|
+
event.type === "keydown" || (event instanceof MouseEvent && event.type === "click" && event.detail === 0)
|
|
94
|
+
? "keyboard"
|
|
95
|
+
: "pointer"
|
|
96
|
+
|
|
97
|
+
// A microtask would run between listeners of this same dispatch, before the trigger's handler reads the gesture.
|
|
98
|
+
if (gestureReset !== undefined) return
|
|
99
|
+
gestureReset = setTimeout(() => {
|
|
100
|
+
gestureReset = undefined
|
|
101
|
+
gesture = "none"
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const GESTURE_EVENTS = ["pointerdown", "click", "keydown"] as const
|
|
106
|
+
|
|
107
|
+
const GESTURE_LISTENER_OPTIONS = { capture: true, passive: true }
|
|
108
|
+
|
|
109
|
+
function retainGestureTracker() {
|
|
110
|
+
trackers += 1
|
|
111
|
+
|
|
112
|
+
if (trackers === 1)
|
|
113
|
+
GESTURE_EVENTS.forEach((type) => window.addEventListener(type, trackGesture, GESTURE_LISTENER_OPTIONS))
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function releaseGestureTracker() {
|
|
117
|
+
trackers -= 1
|
|
118
|
+
|
|
119
|
+
if (trackers > 0) return
|
|
120
|
+
GESTURE_EVENTS.forEach((type) => window.removeEventListener(type, trackGesture, GESTURE_LISTENER_OPTIONS))
|
|
121
|
+
clearTimeout(gestureReset)
|
|
122
|
+
gestureReset = undefined
|
|
123
|
+
gesture = "none"
|
|
124
|
+
}
|
|
52
125
|
|
|
53
126
|
function useMenuContext() {
|
|
54
127
|
const ctx = useContext(MenuAppearanceContext)
|
|
@@ -245,16 +318,33 @@ function MenuSeparator(props: ComponentProps<typeof DropdownMenu.Separator>) {
|
|
|
245
318
|
)
|
|
246
319
|
}
|
|
247
320
|
|
|
321
|
+
// Kobalte drops `data-expanded` as the menu closes but returns focus to the trigger a task later, so for a frame the
|
|
322
|
+
// trigger is neither expanded nor focused. `data-closing` covers that gap for UI revealed by either.
|
|
323
|
+
function MenuTrigger<T extends ValidComponent = "button">(props: PolymorphicProps<T, DropdownMenuTriggerProps<T>>) {
|
|
324
|
+
const ctx = useMenuContext()
|
|
325
|
+
|
|
326
|
+
return <DropdownMenu.Trigger {...props} data-closing={ctx.state().status === "closing" ? "" : undefined} />
|
|
327
|
+
}
|
|
328
|
+
|
|
248
329
|
function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
|
|
249
330
|
const ctx = useMenuContext()
|
|
250
|
-
const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus"])
|
|
331
|
+
const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus", "onInteractOutside"])
|
|
251
332
|
|
|
252
|
-
//
|
|
253
|
-
|
|
333
|
+
// Kobalte's own rule for when an outside press keeps focus where it landed.
|
|
334
|
+
const onInteractOutside: NonNullable<typeof s.onInteractOutside> = (event) => {
|
|
335
|
+
s.onInteractOutside?.(event)
|
|
336
|
+
|
|
337
|
+
if (!event.defaultPrevented && (!ctx.modal() || event.detail.isContextMenu)) ctx.onOutsideInteract()
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// A menu returns focus to its trigger when it closes. Restore it without the ring after a pointer open and keep the
|
|
341
|
+
// ring for keyboard and programmatic opens.
|
|
254
342
|
const onCloseAutoFocus = (event: Event) => {
|
|
343
|
+
const restore = selectMenuRestoreTriggerWithoutRing(ctx.state())
|
|
344
|
+
ctx.onCloseAutoFocus()
|
|
255
345
|
s.onCloseAutoFocus?.(event)
|
|
256
346
|
|
|
257
|
-
if (event.defaultPrevented || !
|
|
347
|
+
if (event.defaultPrevented || !restore) return
|
|
258
348
|
event.preventDefault()
|
|
259
349
|
const active = document.activeElement
|
|
260
350
|
|
|
@@ -269,6 +359,7 @@ function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
|
|
|
269
359
|
<DropdownMenu.Content
|
|
270
360
|
{...r}
|
|
271
361
|
onCloseAutoFocus={onCloseAutoFocus}
|
|
362
|
+
onInteractOutside={onInteractOutside}
|
|
272
363
|
data-component="menu-v2-content"
|
|
273
364
|
data-appearance={ctx.appearance()}
|
|
274
365
|
classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
|
|
@@ -283,19 +374,36 @@ export interface MenuProps extends ComponentProps<typeof DropdownMenu> {
|
|
|
283
374
|
function MenuRoot(props: MenuProps) {
|
|
284
375
|
const [local, rest] = splitProps(props, ["appearance", "children", "onOpenChange"])
|
|
285
376
|
const appearance = () => local.appearance ?? "compact"
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
377
|
+
const initialOpen = !!props.open || (props.open === undefined && !!props.defaultOpen)
|
|
378
|
+
const [menuState, setMenuState] = createSignal(createMenuState(initialOpen))
|
|
379
|
+
const dispatch = (event: MenuEvent) => setMenuState((state) => transitionMenu(state, event))
|
|
380
|
+
|
|
381
|
+
retainGestureTracker()
|
|
382
|
+
onCleanup(() => {
|
|
383
|
+
releaseGestureTracker()
|
|
384
|
+
dispatch(MENU_DISPOSED)
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
// Kobalte does not report a controlled open it did not ask for, so a controlled menu follows `open` itself.
|
|
388
|
+
createComputed(() => {
|
|
389
|
+
if (props.open === undefined) return
|
|
390
|
+
dispatch(openChangedEvent(props.open, gesture))
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
const context = {
|
|
394
|
+
appearance,
|
|
395
|
+
state: menuState,
|
|
396
|
+
modal: () => props.modal ?? true,
|
|
397
|
+
onOutsideInteract: () => dispatch(MENU_OUTSIDE_INTERACTED),
|
|
398
|
+
onCloseAutoFocus: () => dispatch(MENU_CLOSE_AUTOFOCUS),
|
|
399
|
+
}
|
|
292
400
|
|
|
293
401
|
return (
|
|
294
|
-
<MenuAppearanceContext.Provider value={
|
|
402
|
+
<MenuAppearanceContext.Provider value={context}>
|
|
295
403
|
<DropdownMenu
|
|
296
404
|
{...rest}
|
|
297
405
|
onOpenChange={(open) => {
|
|
298
|
-
if (open)
|
|
406
|
+
if (props.open === undefined) dispatch(openChangedEvent(open, gesture))
|
|
299
407
|
local.onOpenChange?.(open)
|
|
300
408
|
}}
|
|
301
409
|
>
|
|
@@ -314,7 +422,7 @@ function MenuContextRoot(props: MenuContextProps) {
|
|
|
314
422
|
const appearance = () => local.appearance ?? "compact"
|
|
315
423
|
|
|
316
424
|
return (
|
|
317
|
-
<MenuAppearanceContext.Provider value={{
|
|
425
|
+
<MenuAppearanceContext.Provider value={{ ...UNTRACKED_MENU, appearance }}>
|
|
318
426
|
<ContextMenu {...rest}>{local.children}</ContextMenu>
|
|
319
427
|
</MenuAppearanceContext.Provider>
|
|
320
428
|
)
|
|
@@ -341,7 +449,7 @@ const MenuContext = Object.assign(MenuContextRoot, {
|
|
|
341
449
|
})
|
|
342
450
|
|
|
343
451
|
export const Menu = Object.assign(MenuRoot, {
|
|
344
|
-
Trigger:
|
|
452
|
+
Trigger: MenuTrigger,
|
|
345
453
|
Portal: DropdownMenu.Portal,
|
|
346
454
|
Content: MenuContent,
|
|
347
455
|
Item: MenuItem,
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export type IndicatorBox = {
|
|
2
|
+
readonly x: number
|
|
3
|
+
readonly width: number
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The pill behind the selected segment. It is `placed` only while the selected segment has a measurable box, so a
|
|
8
|
+
* selection that changes while the control is hidden (`display: none`) jumps to its segment once it shows instead
|
|
9
|
+
* of springing from the previous segment's stale position.
|
|
10
|
+
*/
|
|
11
|
+
export type SegmentedIndicatorState =
|
|
12
|
+
| { readonly status: "hidden" }
|
|
13
|
+
| { readonly status: "placed"; readonly value: string; readonly box: IndicatorBox }
|
|
14
|
+
| { readonly status: "disposed" }
|
|
15
|
+
|
|
16
|
+
export type SegmentedIndicatorEvent =
|
|
17
|
+
| {
|
|
18
|
+
readonly type: "measured"
|
|
19
|
+
readonly value: string | null
|
|
20
|
+
readonly box: IndicatorBox | null
|
|
21
|
+
readonly reducedMotion: boolean
|
|
22
|
+
}
|
|
23
|
+
| { readonly type: "disposed" }
|
|
24
|
+
|
|
25
|
+
export type SegmentedIndicatorEffect =
|
|
26
|
+
| { readonly type: "jump"; readonly box: IndicatorBox }
|
|
27
|
+
| { readonly type: "animate"; readonly box: IndicatorBox }
|
|
28
|
+
| { readonly type: "stop" }
|
|
29
|
+
|
|
30
|
+
export type SegmentedIndicatorTransition = {
|
|
31
|
+
readonly state: SegmentedIndicatorState
|
|
32
|
+
readonly effect: SegmentedIndicatorEffect | null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const HIDDEN_INDICATOR: SegmentedIndicatorState = Object.freeze({ status: "hidden" })
|
|
36
|
+
|
|
37
|
+
const DISPOSED_INDICATOR: SegmentedIndicatorState = Object.freeze({ status: "disposed" })
|
|
38
|
+
|
|
39
|
+
const STOP: SegmentedIndicatorEffect = Object.freeze({ type: "stop" })
|
|
40
|
+
|
|
41
|
+
export function transitionSegmentedIndicator(
|
|
42
|
+
state: SegmentedIndicatorState,
|
|
43
|
+
event: SegmentedIndicatorEvent,
|
|
44
|
+
): SegmentedIndicatorTransition {
|
|
45
|
+
if (state.status === "disposed") return { state, effect: null }
|
|
46
|
+
|
|
47
|
+
if (event.type === "disposed") {
|
|
48
|
+
return { state: DISPOSED_INDICATOR, effect: state.status === "placed" ? STOP : null }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (event.value === null || event.box === null || event.box.width <= 0) {
|
|
52
|
+
if (state.status === "hidden") return { state, effect: null }
|
|
53
|
+
|
|
54
|
+
return { state: HIDDEN_INDICATOR, effect: STOP }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const next = { status: "placed", value: event.value, box: event.box } as const
|
|
58
|
+
|
|
59
|
+
if (state.status === "hidden") return { state: next, effect: { type: "jump", box: event.box } }
|
|
60
|
+
|
|
61
|
+
if (state.value === event.value) {
|
|
62
|
+
if (state.box.x === event.box.x && state.box.width === event.box.width) return { state, effect: null }
|
|
63
|
+
|
|
64
|
+
return { state: next, effect: { type: "jump", box: event.box } }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { state: next, effect: { type: event.reducedMotion ? "jump" : "animate", box: event.box } }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function selectIndicatorReady(state: SegmentedIndicatorState) {
|
|
71
|
+
return state.status === "placed"
|
|
72
|
+
}
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
onMount,
|
|
10
10
|
Show,
|
|
11
11
|
splitProps,
|
|
12
|
+
untrack,
|
|
12
13
|
useContext,
|
|
13
14
|
type Accessor,
|
|
14
15
|
type ComponentProps,
|
|
@@ -17,6 +18,12 @@ import {
|
|
|
17
18
|
} from "solid-js"
|
|
18
19
|
import "./segmented-control.css"
|
|
19
20
|
import { prefersReducedMotion } from "../../motion"
|
|
21
|
+
import {
|
|
22
|
+
HIDDEN_INDICATOR,
|
|
23
|
+
selectIndicatorReady,
|
|
24
|
+
transitionSegmentedIndicator,
|
|
25
|
+
type SegmentedIndicatorEvent,
|
|
26
|
+
} from "./machine"
|
|
20
27
|
|
|
21
28
|
type OnChange = (value: string | null) => void
|
|
22
29
|
|
|
@@ -104,8 +111,7 @@ export function SegmentedControl(props: SegmentedControlProps) {
|
|
|
104
111
|
])
|
|
105
112
|
|
|
106
113
|
const [internal, setInternal] = createSignal<string | null>(local.defaultValue ?? null)
|
|
107
|
-
const [
|
|
108
|
-
const [ready, setReady] = createSignal(false)
|
|
114
|
+
const [indicator, setIndicator] = createSignal(HIDDEN_INDICATOR)
|
|
109
115
|
|
|
110
116
|
const selected = createMemo(() => (isControlled() ? (local.value ?? null) : internal()))
|
|
111
117
|
|
|
@@ -115,68 +121,62 @@ export function SegmentedControl(props: SegmentedControlProps) {
|
|
|
115
121
|
let rootEl: HTMLDivElement | undefined
|
|
116
122
|
let observer: ResizeObserver | undefined
|
|
117
123
|
const items = new Map<string, HTMLButtonElement>()
|
|
118
|
-
let target = { x: 0, width: 0 }
|
|
119
|
-
let lastSelected: string | null = null
|
|
120
124
|
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
125
|
+
const dispatch = (event: SegmentedIndicatorEvent) => {
|
|
126
|
+
const transition = transitionSegmentedIndicator(untrack(indicator), event)
|
|
127
|
+
setIndicator(transition.state)
|
|
128
|
+
const effect = transition.effect
|
|
124
129
|
|
|
125
|
-
if (
|
|
126
|
-
|
|
130
|
+
if (effect?.type === "stop") {
|
|
131
|
+
x.stop()
|
|
132
|
+
width.stop()
|
|
127
133
|
|
|
128
134
|
return
|
|
129
135
|
}
|
|
130
136
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
target = next
|
|
136
|
-
|
|
137
|
-
if (!ready() || !shouldAnimate || prefersReducedMotion()) {
|
|
138
|
-
x.jump(next.x)
|
|
139
|
-
width.jump(next.width)
|
|
140
|
-
setReady(true)
|
|
137
|
+
if (effect?.type === "jump") {
|
|
138
|
+
x.jump(effect.box.x)
|
|
139
|
+
width.jump(effect.box.width)
|
|
141
140
|
|
|
142
141
|
return
|
|
143
142
|
}
|
|
144
143
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
animate(
|
|
144
|
+
if (effect?.type !== "animate") return
|
|
145
|
+
const spring = local.transition ?? SEGMENTED_CONTROL_SPRING
|
|
146
|
+
animate(x, effect.box.x, spring)
|
|
147
|
+
animate(width, effect.box.width, spring)
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
observer = new ResizeObserver(() => {
|
|
154
|
-
const current = selected()
|
|
155
|
-
const button = current !== null ? items.get(current) : undefined
|
|
156
|
-
|
|
157
|
-
if (!rootEl || !button) return
|
|
158
|
-
|
|
159
|
-
const next = measureOffset(rootEl, button)
|
|
150
|
+
const sync = () => {
|
|
151
|
+
const current = untrack(selected)
|
|
152
|
+
const button = current !== null ? items.get(current) : undefined
|
|
160
153
|
|
|
161
|
-
|
|
154
|
+
dispatch({
|
|
155
|
+
type: "measured",
|
|
156
|
+
value: current,
|
|
157
|
+
box: rootEl && button ? measureOffset(rootEl, button) : null,
|
|
158
|
+
reducedMotion: prefersReducedMotion(),
|
|
159
|
+
})
|
|
160
|
+
}
|
|
162
161
|
|
|
163
|
-
|
|
162
|
+
onMount(() => {
|
|
163
|
+
if (!("ResizeObserver" in globalThis)) return
|
|
164
164
|
|
|
165
|
-
|
|
166
|
-
})
|
|
165
|
+
observer = new ResizeObserver(sync)
|
|
167
166
|
|
|
168
167
|
if (rootEl) observer.observe(rootEl)
|
|
169
168
|
|
|
170
169
|
items.forEach((button) => observer?.observe(button))
|
|
171
|
-
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
onCleanup(() => {
|
|
173
|
+
observer?.disconnect()
|
|
174
|
+
dispatch({ type: "disposed" })
|
|
172
175
|
})
|
|
173
176
|
|
|
174
177
|
createEffect(() => {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const shouldAnimate = ready() && lastSelected !== null && current !== null && current !== lastSelected
|
|
178
|
-
lastSelected = current
|
|
179
|
-
syncIndicator(shouldAnimate)
|
|
178
|
+
selected()
|
|
179
|
+
sync()
|
|
180
180
|
})
|
|
181
181
|
|
|
182
182
|
const setSelected = (next: string | null) => {
|
|
@@ -210,12 +210,12 @@ export function SegmentedControl(props: SegmentedControlProps) {
|
|
|
210
210
|
const registerItem = (value: string, el: HTMLButtonElement) => {
|
|
211
211
|
items.set(value, el)
|
|
212
212
|
observer?.observe(el)
|
|
213
|
-
|
|
213
|
+
sync()
|
|
214
214
|
|
|
215
215
|
return () => {
|
|
216
216
|
if (items.get(value) === el) items.delete(value)
|
|
217
217
|
observer?.unobserve(el)
|
|
218
|
-
|
|
218
|
+
sync()
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
|
|
@@ -243,13 +243,13 @@ export function SegmentedControl(props: SegmentedControlProps) {
|
|
|
243
243
|
role="group"
|
|
244
244
|
data-component="segmented-control-v2"
|
|
245
245
|
data-slot="segmented-control-v2"
|
|
246
|
-
data-ready={
|
|
246
|
+
data-ready={selectIndicatorReady(indicator()) ? "" : undefined}
|
|
247
247
|
classList={{
|
|
248
248
|
...local.classList,
|
|
249
249
|
[local.class ?? ""]: !!local.class,
|
|
250
250
|
}}
|
|
251
251
|
>
|
|
252
|
-
<Show when={
|
|
252
|
+
<Show when={selectIndicatorReady(indicator())}>
|
|
253
253
|
<motion.span data-slot="segmented-control-v2-indicator" aria-hidden="true" style={{ x, width }} />
|
|
254
254
|
</Show>
|
|
255
255
|
{local.children}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
Show,
|
|
7
7
|
children,
|
|
8
8
|
createEffect,
|
|
9
|
+
onCleanup,
|
|
9
10
|
splitProps,
|
|
10
11
|
} from "solid-js"
|
|
11
12
|
import { useDialogLayer } from "../../context/dialog"
|
|
@@ -113,7 +114,9 @@ export function Dialog(props: DialogProps) {
|
|
|
113
114
|
])
|
|
114
115
|
|
|
115
116
|
const layer = useDialogLayer()
|
|
117
|
+
let focusTimer: ReturnType<typeof setTimeout> | undefined
|
|
116
118
|
createEffect(() => layer?.setBackdropDismiss(!local.preventBackdropDismiss))
|
|
119
|
+
onCleanup(() => clearTimeout(focusTimer))
|
|
117
120
|
|
|
118
121
|
return (
|
|
119
122
|
<div
|
|
@@ -147,7 +150,10 @@ export function Dialog(props: DialogProps) {
|
|
|
147
150
|
// Kobalte attaches its focus trap after this event, so focusing now would leave the trap with no element
|
|
148
151
|
// to return to. Deferring, as Kobalte's own autofocus does, also lets children that focus themselves on
|
|
149
152
|
// mount keep that focus.
|
|
150
|
-
|
|
153
|
+
clearTimeout(focusTimer)
|
|
154
|
+
focusTimer = setTimeout(() => {
|
|
155
|
+
// A dialog that closed in the meantime must not pull focus back from where its close returned it.
|
|
156
|
+
if (layer?.closing() || !content.isConnected) return
|
|
151
157
|
const autofocus = content.querySelector<HTMLElement>("[autofocus]")
|
|
152
158
|
const active = document.activeElement
|
|
153
159
|
|