@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
|
@@ -1,82 +1,159 @@
|
|
|
1
1
|
import { createResizeObserver } from "@solid-primitives/resize-observer"
|
|
2
|
-
import { onMount, Show, splitProps, type ComponentProps } from "solid-js"
|
|
2
|
+
import { createSignal, onCleanup, onMount, Show, splitProps, untrack, type ComponentProps, type JSX } from "solid-js"
|
|
3
3
|
import { createStore } from "solid-js/store"
|
|
4
|
+
import {
|
|
5
|
+
INITIAL_SCROLL_THUMB_STATE,
|
|
6
|
+
overflowChanged,
|
|
7
|
+
SCROLL_IDLE_ELAPSED,
|
|
8
|
+
SCROLL_IDLE_MS,
|
|
9
|
+
SCROLL_THUMB_DISPOSED,
|
|
10
|
+
SCROLLED,
|
|
11
|
+
selectDraggingAxis,
|
|
12
|
+
selectShowVerticalThumb,
|
|
13
|
+
selectThumbScrolling,
|
|
14
|
+
transitionScrollThumb,
|
|
15
|
+
type ScrollThumbEvent,
|
|
16
|
+
type ScrollThumbState,
|
|
17
|
+
} from "./scroll-view-machine"
|
|
4
18
|
import "./menu-scroll.css"
|
|
5
19
|
|
|
20
|
+
/**
|
|
21
|
+
* A scroll container for menus, popovers and dialog lists. It hides the native scrollbar and draws the shared thumb
|
|
22
|
+
* over the content, so rows keep equal start and end padding whether or not the list overflows. Touch devices and
|
|
23
|
+
* high-contrast modes keep the native scrollbar.
|
|
24
|
+
*/
|
|
6
25
|
export function MenuScroll(
|
|
7
|
-
props: Omit<ComponentProps<"div">, "ref" | "onScroll"> & {
|
|
26
|
+
props: Omit<ComponentProps<"div">, "ref" | "onScroll" | "onFocusIn"> & {
|
|
8
27
|
viewportClass?: string
|
|
9
28
|
viewportRef?: (element: HTMLDivElement) => void
|
|
29
|
+
onScroll?: JSX.EventHandler<HTMLDivElement, Event>
|
|
10
30
|
},
|
|
11
31
|
) {
|
|
12
|
-
const [local, rest] = splitProps(props, ["class", "children", "viewportClass", "viewportRef"])
|
|
32
|
+
const [local, rest] = splitProps(props, ["class", "children", "viewportClass", "viewportRef", "onScroll"])
|
|
13
33
|
|
|
14
|
-
const [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
overflow: false,
|
|
18
|
-
})
|
|
34
|
+
const [geometry, setGeometry] = createStore({ size: 0, start: 4 })
|
|
35
|
+
|
|
36
|
+
const [thumbState, setThumbState] = createSignal<ScrollThumbState>(INITIAL_SCROLL_THUMB_STATE)
|
|
19
37
|
|
|
20
38
|
let viewport!: HTMLDivElement
|
|
21
39
|
|
|
22
40
|
let content!: HTMLDivElement
|
|
23
41
|
|
|
42
|
+
let thumb: HTMLDivElement | undefined
|
|
43
|
+
|
|
44
|
+
let idleTimer: ReturnType<typeof setTimeout> | undefined
|
|
45
|
+
|
|
46
|
+
const onIdleElapsed = () => {
|
|
47
|
+
idleTimer = undefined
|
|
48
|
+
dispatch(SCROLL_IDLE_ELAPSED)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const dispatch = (event: ScrollThumbEvent) => {
|
|
52
|
+
const prev = untrack(thumbState)
|
|
53
|
+
const next = transitionScrollThumb(prev, event)
|
|
54
|
+
|
|
55
|
+
if (next.state !== prev) setThumbState(next.state)
|
|
56
|
+
|
|
57
|
+
for (const effect of next.effects) {
|
|
58
|
+
if (effect === "release-capture") {
|
|
59
|
+
if (prev.status === "dragging" && thumb?.hasPointerCapture(prev.pointerId))
|
|
60
|
+
thumb.releasePointerCapture(prev.pointerId)
|
|
61
|
+
continue
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
clearTimeout(idleTimer)
|
|
65
|
+
idleTimer = effect === "reset-idle-timer" ? setTimeout(onIdleElapsed, SCROLL_IDLE_MS) : undefined
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
24
69
|
const measure = () => {
|
|
25
70
|
const track = Math.max(0, viewport.clientHeight - 8)
|
|
26
71
|
|
|
27
|
-
const size = Math.min(track, Math.max(
|
|
72
|
+
const size = Math.min(track, Math.max(24, (viewport.clientHeight / Math.max(1, viewport.scrollHeight)) * track))
|
|
28
73
|
|
|
29
74
|
const max = viewport.scrollHeight - viewport.clientHeight
|
|
30
75
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
76
|
+
setGeometry("size", size)
|
|
77
|
+
setGeometry("start", 4 + (max > 0 ? (viewport.scrollTop / max) * (track - size) : 0))
|
|
78
|
+
dispatch(overflowChanged(max > 0, false))
|
|
34
79
|
}
|
|
35
80
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
81
|
+
const onPointerEnd = (event: PointerEvent) => dispatch({ type: "drag.ended", pointerId: event.pointerId })
|
|
82
|
+
|
|
83
|
+
onMount(() => createResizeObserver([viewport, content], measure))
|
|
84
|
+
|
|
85
|
+
onCleanup(() => dispatch(SCROLL_THUMB_DISPOSED))
|
|
40
86
|
|
|
41
87
|
return (
|
|
42
|
-
<div
|
|
88
|
+
<div
|
|
89
|
+
data-component="menu-scroll"
|
|
90
|
+
data-scrolling={selectThumbScrolling(thumbState()) ? "" : undefined}
|
|
91
|
+
data-dragging={selectDraggingAxis(thumbState()) === "vertical" ? "" : undefined}
|
|
92
|
+
class={local.class}
|
|
93
|
+
>
|
|
43
94
|
<div
|
|
44
|
-
{...rest}
|
|
45
|
-
ref={viewport}
|
|
46
95
|
data-slot="menu-scroll-viewport"
|
|
96
|
+
{...rest}
|
|
97
|
+
ref={(element) => {
|
|
98
|
+
viewport = element
|
|
99
|
+
local.viewportRef?.(element)
|
|
100
|
+
}}
|
|
47
101
|
data-scrollable
|
|
48
102
|
class={local.viewportClass}
|
|
49
|
-
onScroll={
|
|
103
|
+
onScroll={(event) => {
|
|
104
|
+
measure()
|
|
105
|
+
// Keyboard navigation scrolls without hovering, so the thumb also shows while the list scrolls.
|
|
106
|
+
dispatch(SCROLLED)
|
|
107
|
+
local.onScroll?.(event)
|
|
108
|
+
}}
|
|
109
|
+
// Kobalte menus focus items without scrolling and scroll their own content, which no longer overflows here.
|
|
110
|
+
onFocusIn={(event) => {
|
|
111
|
+
if (event.target !== viewport) event.target.scrollIntoView({ block: "nearest", inline: "nearest" })
|
|
112
|
+
}}
|
|
50
113
|
>
|
|
51
|
-
<div ref={content}>
|
|
114
|
+
<div ref={content} data-slot="menu-scroll-content">
|
|
115
|
+
{local.children}
|
|
116
|
+
</div>
|
|
52
117
|
</div>
|
|
53
|
-
<Show when={
|
|
118
|
+
<Show when={selectShowVerticalThumb(thumbState())}>
|
|
54
119
|
<div
|
|
120
|
+
ref={thumb}
|
|
55
121
|
data-slot="menu-scroll-thumb"
|
|
56
|
-
data-dragging={state.grab !== undefined ? "" : undefined}
|
|
57
122
|
aria-hidden="true"
|
|
58
|
-
style={{ height: `${
|
|
123
|
+
style={{ height: `${geometry.size}px`, transform: `translateY(${geometry.start}px)` }}
|
|
59
124
|
onPointerDown={(event) => {
|
|
60
125
|
if (event.button !== 0) return
|
|
61
126
|
event.preventDefault()
|
|
62
127
|
event.stopPropagation()
|
|
63
|
-
|
|
64
|
-
|
|
128
|
+
dispatch({
|
|
129
|
+
type: "drag.started",
|
|
130
|
+
axis: "vertical",
|
|
131
|
+
pointerId: event.pointerId,
|
|
132
|
+
grabOffset: event.clientY - event.currentTarget.getBoundingClientRect().top,
|
|
133
|
+
rtl: false,
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
if (event.isTrusted && selectDraggingAxis(untrack(thumbState)) === "vertical")
|
|
137
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
65
138
|
}}
|
|
66
139
|
onPointerMove={(event) => {
|
|
67
|
-
|
|
68
|
-
|
|
140
|
+
const drag = untrack(thumbState)
|
|
141
|
+
|
|
142
|
+
if (drag.status !== "dragging" || drag.pointerId !== event.pointerId) return
|
|
143
|
+
|
|
144
|
+
// A drag whose capture never started or was dropped without a `pointerup` ends at the first move without
|
|
145
|
+
// the button, instead of scrolling under a hovering pointer.
|
|
146
|
+
if (event.isTrusted && (event.buttons & 1) === 0) return onPointerEnd(event)
|
|
147
|
+
const travel = viewport.clientHeight - 8 - geometry.size
|
|
69
148
|
|
|
70
149
|
if (travel <= 0) return
|
|
71
150
|
viewport.scrollTop =
|
|
72
|
-
((event.clientY - viewport.getBoundingClientRect().top - 4 -
|
|
151
|
+
((event.clientY - viewport.getBoundingClientRect().top - 4 - drag.grabOffset) / travel) *
|
|
73
152
|
(viewport.scrollHeight - viewport.clientHeight)
|
|
74
153
|
}}
|
|
75
|
-
onPointerUp={
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}}
|
|
79
|
-
onLostPointerCapture={() => setState("grab", undefined)}
|
|
154
|
+
onPointerUp={onPointerEnd}
|
|
155
|
+
onPointerCancel={onPointerEnd}
|
|
156
|
+
onLostPointerCapture={onPointerEnd}
|
|
80
157
|
/>
|
|
81
158
|
</Show>
|
|
82
159
|
</div>
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
export type PopoverCloseReason = "escape" | "outside" | "control"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One popover's open state. `controlled` follows the `open` prop; `uncontrolled` owns it. A controlled popover that
|
|
5
|
+
* asked its owner to close stays open with `pendingDismiss` until the owner answers, so a refused close leaves no
|
|
6
|
+
* reason behind for a later close to read.
|
|
7
|
+
*/
|
|
8
|
+
export type PopoverState =
|
|
9
|
+
| {
|
|
10
|
+
readonly status: "closed"
|
|
11
|
+
readonly mode: "controlled" | "uncontrolled"
|
|
12
|
+
/** Why it closed, until Kobalte decides where focus returns. Focus stays put after an outside press. */
|
|
13
|
+
readonly closeReason: PopoverCloseReason | null
|
|
14
|
+
}
|
|
15
|
+
| { readonly status: "open"; readonly mode: "uncontrolled" }
|
|
16
|
+
| { readonly status: "open"; readonly mode: "controlled"; readonly pendingDismiss: PopoverCloseReason | null }
|
|
17
|
+
| { readonly status: "disposed" }
|
|
18
|
+
|
|
19
|
+
export type PopoverEvent =
|
|
20
|
+
/** The `open` prop, read again right after every `onOpenChange` call: a controlled `true` refuses a close. */
|
|
21
|
+
| { readonly type: "props.synced"; readonly open: boolean | undefined }
|
|
22
|
+
/** The shared listener dismissed this popover. */
|
|
23
|
+
| { readonly type: "dismiss"; readonly reason: "escape" | "outside" }
|
|
24
|
+
/** Kobalte asked to open or close, from the trigger or the close button. */
|
|
25
|
+
| { readonly type: "kobalte.changed"; readonly open: boolean }
|
|
26
|
+
| { readonly type: "close-auto-focus.handled" }
|
|
27
|
+
| { readonly type: "disposed" }
|
|
28
|
+
|
|
29
|
+
export type PopoverEffect = { readonly type: "notify.open"; readonly open: boolean }
|
|
30
|
+
|
|
31
|
+
export type PopoverTransition = {
|
|
32
|
+
readonly state: PopoverState
|
|
33
|
+
readonly effects: readonly PopoverEffect[]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const NO_EFFECTS: readonly PopoverEffect[] = Object.freeze([])
|
|
37
|
+
|
|
38
|
+
const NOTIFY_OPEN: readonly PopoverEffect[] = Object.freeze([Object.freeze({ type: "notify.open", open: true })])
|
|
39
|
+
|
|
40
|
+
const NOTIFY_CLOSED: readonly PopoverEffect[] = Object.freeze([Object.freeze({ type: "notify.open", open: false })])
|
|
41
|
+
|
|
42
|
+
export function createPopoverState(open: boolean | undefined, defaultOpen: boolean | undefined): PopoverState {
|
|
43
|
+
if (open === true) return { status: "open", mode: "controlled", pendingDismiss: null }
|
|
44
|
+
|
|
45
|
+
if (open === false) return { status: "closed", mode: "controlled", closeReason: null }
|
|
46
|
+
|
|
47
|
+
if (defaultOpen) return { status: "open", mode: "uncontrolled" }
|
|
48
|
+
|
|
49
|
+
return { status: "closed", mode: "uncontrolled", closeReason: null }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function transitionPopover(state: PopoverState, event: PopoverEvent): PopoverTransition {
|
|
53
|
+
if (state.status === "disposed") return { state, effects: NO_EFFECTS }
|
|
54
|
+
|
|
55
|
+
if (event.type === "disposed") return { state: { status: "disposed" }, effects: NO_EFFECTS }
|
|
56
|
+
|
|
57
|
+
if (event.type === "props.synced") return { state: syncProps(state, event.open), effects: NO_EFFECTS }
|
|
58
|
+
|
|
59
|
+
if (event.type === "close-auto-focus.handled") {
|
|
60
|
+
if (state.status !== "closed" || state.closeReason === null) return { state, effects: NO_EFFECTS }
|
|
61
|
+
|
|
62
|
+
return { state: { status: "closed", mode: state.mode, closeReason: null }, effects: NO_EFFECTS }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (event.type === "kobalte.changed" && event.open) {
|
|
66
|
+
if (state.status === "open") return { state, effects: NO_EFFECTS }
|
|
67
|
+
|
|
68
|
+
if (state.mode === "controlled") return { state, effects: NOTIFY_OPEN }
|
|
69
|
+
|
|
70
|
+
return { state: { status: "open", mode: "uncontrolled" }, effects: NOTIFY_OPEN }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// A dismiss or Kobalte close from here on.
|
|
74
|
+
if (state.status === "closed") return { state, effects: NO_EFFECTS }
|
|
75
|
+
|
|
76
|
+
const reason = event.type === "dismiss" ? event.reason : "control"
|
|
77
|
+
|
|
78
|
+
if (state.mode === "uncontrolled") {
|
|
79
|
+
return { state: { status: "closed", mode: "uncontrolled", closeReason: reason }, effects: NOTIFY_CLOSED }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Kobalte echoes a close it caused; the owner hears about it once.
|
|
83
|
+
if (state.pendingDismiss !== null) return { state, effects: NO_EFFECTS }
|
|
84
|
+
|
|
85
|
+
return { state: { status: "open", mode: "controlled", pendingDismiss: reason }, effects: NOTIFY_CLOSED }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function selectPopoverOpen(state: PopoverState) {
|
|
89
|
+
return state.status === "open"
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** An outside press already moved focus where the user wanted it, so closing does not pull it back to the trigger. */
|
|
93
|
+
export function selectPreventCloseAutoFocus(state: PopoverState) {
|
|
94
|
+
return state.status === "closed" && state.closeReason === "outside"
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function syncProps(state: Exclude<PopoverState, { status: "disposed" }>, open: boolean | undefined): PopoverState {
|
|
98
|
+
if (open === undefined) {
|
|
99
|
+
if (state.mode === "uncontrolled") return state
|
|
100
|
+
|
|
101
|
+
if (state.status === "open") return { status: "open", mode: "uncontrolled" }
|
|
102
|
+
|
|
103
|
+
return { status: "closed", mode: "uncontrolled", closeReason: state.closeReason }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (open) {
|
|
107
|
+
if (state.status === "open" && state.mode === "controlled" && state.pendingDismiss === null) return state
|
|
108
|
+
|
|
109
|
+
return { status: "open", mode: "controlled", pendingDismiss: null }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (state.status === "closed") {
|
|
113
|
+
if (state.mode === "controlled") return state
|
|
114
|
+
|
|
115
|
+
return { status: "closed", mode: "controlled", closeReason: state.closeReason }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
status: "closed",
|
|
120
|
+
mode: "controlled",
|
|
121
|
+
closeReason: state.mode === "controlled" ? (state.pendingDismiss ?? "control") : "control",
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** One open popover, and the popover it opened inside (portaled content is outside its parent in the DOM). */
|
|
126
|
+
export type PopoverStackEntry = {
|
|
127
|
+
readonly id: number
|
|
128
|
+
readonly parentId: number | null
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Every open popover, in the order they opened. One shared set of window listeners routes Escape and outside presses. */
|
|
132
|
+
export type PopoverStackState = { readonly entries: readonly PopoverStackEntry[] }
|
|
133
|
+
|
|
134
|
+
export type PopoverStackEvent =
|
|
135
|
+
| { readonly type: "layer.opened"; readonly id: number; readonly parentId: number | null }
|
|
136
|
+
| { readonly type: "layer.closed"; readonly id: number }
|
|
137
|
+
| { readonly type: "escape.pressed" }
|
|
138
|
+
/** A press or focus landed; `insideIds` are the open popovers whose trigger or content contains it. */
|
|
139
|
+
| { readonly type: "outside.interacted"; readonly insideIds: ReadonlySet<number> }
|
|
140
|
+
|
|
141
|
+
export type PopoverStackEffect = {
|
|
142
|
+
readonly type: "dismiss.layer"
|
|
143
|
+
readonly id: number
|
|
144
|
+
readonly reason: "escape" | "outside"
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export type PopoverStackTransition = {
|
|
148
|
+
readonly state: PopoverStackState
|
|
149
|
+
readonly effects: readonly PopoverStackEffect[]
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const NO_STACK_EFFECTS: readonly PopoverStackEffect[] = Object.freeze([])
|
|
153
|
+
|
|
154
|
+
export const EMPTY_POPOVER_STACK: PopoverStackState = Object.freeze({ entries: Object.freeze([]) })
|
|
155
|
+
|
|
156
|
+
export function transitionPopoverStack(state: PopoverStackState, event: PopoverStackEvent): PopoverStackTransition {
|
|
157
|
+
if (event.type === "layer.opened") {
|
|
158
|
+
if (state.entries.some((entry) => entry.id === event.id)) return { state, effects: NO_STACK_EFFECTS }
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
state: { entries: [...state.entries, { id: event.id, parentId: event.parentId }] },
|
|
162
|
+
effects: NO_STACK_EFFECTS,
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (event.type === "layer.closed") {
|
|
167
|
+
if (!state.entries.some((entry) => entry.id === event.id)) return { state, effects: NO_STACK_EFFECTS }
|
|
168
|
+
const entries = state.entries.filter((entry) => entry.id !== event.id)
|
|
169
|
+
|
|
170
|
+
return { state: entries.length === 0 ? EMPTY_POPOVER_STACK : { entries }, effects: NO_STACK_EFFECTS }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const top = state.entries.at(-1)
|
|
174
|
+
|
|
175
|
+
if (!top) return { state, effects: NO_STACK_EFFECTS }
|
|
176
|
+
|
|
177
|
+
if (event.type === "escape.pressed")
|
|
178
|
+
return { state, effects: [{ type: "dismiss.layer", id: top.id, reason: "escape" }] }
|
|
179
|
+
|
|
180
|
+
// A press inside a child keeps its ancestors open, even when the child's content is portaled outside them.
|
|
181
|
+
const kept = new Set<number>()
|
|
182
|
+
|
|
183
|
+
const keep = (id: number | null): void => {
|
|
184
|
+
if (id === null || kept.has(id)) return
|
|
185
|
+
kept.add(id)
|
|
186
|
+
keep(state.entries.find((entry) => entry.id === id)?.parentId ?? null)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
event.insideIds.forEach(keep)
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
state,
|
|
193
|
+
effects: state.entries
|
|
194
|
+
.filter((entry) => !kept.has(entry.id))
|
|
195
|
+
.reverse()
|
|
196
|
+
.map((entry) => ({ type: "dismiss.layer", id: entry.id, reason: "outside" }) as const),
|
|
197
|
+
}
|
|
198
|
+
}
|