@opencode/ui 0.0.0-dev-735 → 0.0.0-dev-767
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/components/animated-number-machine.d.ts +95 -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/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/scroll-view-machine.d.ts +66 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- 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/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/theme/context.d.ts +20 -9
- package/dist/theme/machine.d.ts +174 -0
- package/package.json +3 -3
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- 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/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 +113 -83
- package/src/components/scroll-view-machine.ts +231 -0
- package/src/components/scroll-view.tsx +163 -116
- 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/data-display/line-comment/line-comment.tsx +154 -178
- package/src/data-display/line-comment/machine.ts +392 -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/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +55 -59
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.tsx +49 -27
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +118 -20
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/theme/context.tsx +134 -240
- package/src/theme/machine.ts +508 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/** The element a pan captures the pointer on: the preview's body for an image, the borrowed video for a video. */
|
|
2
|
+
export type PanTarget = "viewport" | "video"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The press under way. A `backdrop-press` began on the backdrop around the image and stayed put, so the click that ends
|
|
6
|
+
* it closes the preview. A pan captures the pointer, so its click lands on the backdrop wherever it began: `onBackdrop`
|
|
7
|
+
* says whether it began there, and `moved` whether it has moved, so the click that ends a moved pan neither closes the
|
|
8
|
+
* preview nor plays the video.
|
|
9
|
+
*/
|
|
10
|
+
export type PreviewZoomGesture =
|
|
11
|
+
| { readonly type: "idle" }
|
|
12
|
+
| { readonly type: "backdrop-press" }
|
|
13
|
+
| {
|
|
14
|
+
readonly type: "panning"
|
|
15
|
+
readonly pointerId: number
|
|
16
|
+
readonly target: PanTarget
|
|
17
|
+
readonly onBackdrop: boolean
|
|
18
|
+
readonly moved: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* `gliding` is an eased zoom under way on the frame loop. Scale and offsets live on springs in the driver, not here: a
|
|
23
|
+
* frame of a glide or a pan writes no state. A press does not stop a glide, so a third click of a double-click zoom
|
|
24
|
+
* leaves it running; the hand moving does.
|
|
25
|
+
*/
|
|
26
|
+
export type PreviewZoomState =
|
|
27
|
+
| { readonly status: "still" | "gliding"; readonly gesture: PreviewZoomGesture }
|
|
28
|
+
| { readonly status: "disposed" }
|
|
29
|
+
|
|
30
|
+
/** `zoomed` is whether the image shows above fit once the event's change applies. */
|
|
31
|
+
export type PreviewZoomEvent =
|
|
32
|
+
| {
|
|
33
|
+
readonly type: "pointer.down"
|
|
34
|
+
readonly pointerId: number
|
|
35
|
+
readonly button: number
|
|
36
|
+
/** `content` is anywhere in the preview but its body, such as the band around it or the controls. */
|
|
37
|
+
readonly target: PanTarget | "content"
|
|
38
|
+
readonly onBackdrop: boolean
|
|
39
|
+
readonly zoomed: boolean
|
|
40
|
+
}
|
|
41
|
+
| { readonly type: "pointer.moved"; readonly pointerId: number }
|
|
42
|
+
| { readonly type: "pointer.up"; readonly pointerId: number }
|
|
43
|
+
/** `pointercancel`, `lostpointercapture`, or a move with the primary button already up: the pan ends unreleased. */
|
|
44
|
+
| { readonly type: "pointer.cancel"; readonly pointerId: number }
|
|
45
|
+
| { readonly type: "backdrop.click"; readonly onBackdrop: boolean }
|
|
46
|
+
/** Pinch, wheel, drag or a resize set the view directly. */
|
|
47
|
+
| { readonly type: "follow"; readonly zoomed: boolean }
|
|
48
|
+
| { readonly type: "glide.start" }
|
|
49
|
+
| { readonly type: "glide.settled"; readonly zoomed: boolean }
|
|
50
|
+
| { readonly type: "disposed" }
|
|
51
|
+
|
|
52
|
+
export type PreviewZoomEffect =
|
|
53
|
+
| { readonly type: "pointer.capture"; readonly target: PanTarget; readonly pointerId: number }
|
|
54
|
+
| { readonly type: "pointer.release"; readonly target: PanTarget; readonly pointerId: number }
|
|
55
|
+
| { readonly type: "glide.loop.start" }
|
|
56
|
+
| { readonly type: "glide.loop.stop" }
|
|
57
|
+
/** Swallows the click that ends a moved pan over the video, so it does not play or pause it. */
|
|
58
|
+
| { readonly type: "video.suppress-click" }
|
|
59
|
+
| { readonly type: "dialog.close" }
|
|
60
|
+
|
|
61
|
+
export type PreviewZoomTransition = {
|
|
62
|
+
readonly state: PreviewZoomState
|
|
63
|
+
readonly effects: readonly PreviewZoomEffect[]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const IDLE: PreviewZoomGesture = Object.freeze({ type: "idle" })
|
|
67
|
+
|
|
68
|
+
const BACKDROP_PRESS: PreviewZoomGesture = Object.freeze({ type: "backdrop-press" })
|
|
69
|
+
|
|
70
|
+
const NONE: readonly PreviewZoomEffect[] = Object.freeze([])
|
|
71
|
+
|
|
72
|
+
export const INITIAL_PREVIEW_ZOOM_STATE: PreviewZoomState = Object.freeze({ status: "still", gesture: IDLE })
|
|
73
|
+
|
|
74
|
+
const DISPOSED: PreviewZoomState = Object.freeze({ status: "disposed" })
|
|
75
|
+
|
|
76
|
+
export function transitionPreviewZoom(state: PreviewZoomState, event: PreviewZoomEvent): PreviewZoomTransition {
|
|
77
|
+
if (state.status === "disposed") return unchanged(state)
|
|
78
|
+
const gliding = state.status === "gliding"
|
|
79
|
+
const pan = state.gesture.type === "panning" ? state.gesture : undefined
|
|
80
|
+
|
|
81
|
+
if (event.type === "disposed") {
|
|
82
|
+
return { state: DISPOSED, effects: [...release(pan), ...(gliding ? [{ type: "glide.loop.stop" } as const] : [])] }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (event.type === "pointer.down") {
|
|
86
|
+
// A second pointer leaves the pan alone; the same pointer pressing again means its release never arrived.
|
|
87
|
+
if (pan && pan.pointerId !== event.pointerId) return unchanged(state)
|
|
88
|
+
|
|
89
|
+
if (event.button === 0 && event.zoomed && event.target !== "content") {
|
|
90
|
+
return {
|
|
91
|
+
state: {
|
|
92
|
+
status: state.status,
|
|
93
|
+
gesture: {
|
|
94
|
+
type: "panning",
|
|
95
|
+
pointerId: event.pointerId,
|
|
96
|
+
target: event.target,
|
|
97
|
+
onBackdrop: event.onBackdrop,
|
|
98
|
+
moved: false,
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
effects: [{ type: "pointer.capture", target: event.target, pointerId: event.pointerId }],
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return gesture(state, event.button === 0 && event.onBackdrop ? BACKDROP_PRESS : IDLE)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (event.type === "pointer.moved") {
|
|
109
|
+
if (pan?.pointerId !== event.pointerId || pan.moved) return unchanged(state)
|
|
110
|
+
|
|
111
|
+
return gesture(state, { ...pan, onBackdrop: false, moved: true })
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (event.type === "pointer.up") {
|
|
115
|
+
if (pan?.pointerId !== event.pointerId) return unchanged(state)
|
|
116
|
+
|
|
117
|
+
if (pan.moved && pan.target === "video") {
|
|
118
|
+
return { state: { status: state.status, gesture: IDLE }, effects: [{ type: "video.suppress-click" }] }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return gesture(state, !pan.moved && pan.onBackdrop ? BACKDROP_PRESS : IDLE)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (event.type === "pointer.cancel") {
|
|
125
|
+
if (pan?.pointerId !== event.pointerId) return unchanged(state)
|
|
126
|
+
|
|
127
|
+
return { state: { status: state.status, gesture: IDLE }, effects: release(pan) }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (event.type === "backdrop.click") {
|
|
131
|
+
if (state.gesture.type !== "backdrop-press") return unchanged(state)
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
state: { status: state.status, gesture: IDLE },
|
|
135
|
+
effects: event.onBackdrop ? [{ type: "dialog.close" }] : NONE,
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// An eased zoom takes over from a pan, so a pan never outlives a zoom back to fit.
|
|
140
|
+
if (event.type === "glide.start") {
|
|
141
|
+
if (gliding && !pan) return unchanged(state)
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
state: { status: "gliding", gesture: pan ? IDLE : state.gesture },
|
|
145
|
+
effects: [...release(pan), ...(gliding ? [] : [{ type: "glide.loop.start" } as const])],
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (event.type === "glide.settled" && !gliding) return unchanged(state)
|
|
150
|
+
// The hand takes over from an eased zoom; a pan ends once the image is back at fit.
|
|
151
|
+
const ends = pan && !event.zoomed ? pan : undefined
|
|
152
|
+
|
|
153
|
+
if (!gliding && !ends) return unchanged(state)
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
state: { status: "still", gesture: ends ? IDLE : state.gesture },
|
|
157
|
+
effects: [...(gliding ? [{ type: "glide.loop.stop" } as const] : []), ...release(ends)],
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Whether a pan holds the pointer, which shows the grabbing cursor. */
|
|
162
|
+
export function selectDragging(state: PreviewZoomState) {
|
|
163
|
+
return state.status !== "disposed" && state.gesture.type === "panning"
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Whether `pointerId` is the pointer the pan under way follows. */
|
|
167
|
+
export function selectPanPointer(state: PreviewZoomState, pointerId: number) {
|
|
168
|
+
return state.status !== "disposed" && state.gesture.type === "panning" && state.gesture.pointerId === pointerId
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function gesture(state: Exclude<PreviewZoomState, { status: "disposed" }>, next: PreviewZoomGesture) {
|
|
172
|
+
if (state.gesture === next) return unchanged(state)
|
|
173
|
+
|
|
174
|
+
return { state: { status: state.status, gesture: next }, effects: NONE }
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function release(pan: Extract<PreviewZoomGesture, { type: "panning" }> | undefined): readonly PreviewZoomEffect[] {
|
|
178
|
+
if (!pan) return NONE
|
|
179
|
+
|
|
180
|
+
return [{ type: "pointer.release", target: pan.target, pointerId: pan.pointerId }]
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Every pointer move of a pan dispatches events that change nothing; answering them with the transition already built
|
|
184
|
+
// for that state keeps the hot path allocation-free.
|
|
185
|
+
let last: PreviewZoomTransition = Object.freeze({ state: DISPOSED, effects: NONE })
|
|
186
|
+
|
|
187
|
+
function unchanged(state: PreviewZoomState) {
|
|
188
|
+
if (last.state !== state) last = Object.freeze({ state, effects: NONE })
|
|
189
|
+
|
|
190
|
+
return last
|
|
191
|
+
}
|
|
@@ -59,26 +59,25 @@ export function detailScale(fitRatio: number) {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* The `object-view-box` that shows
|
|
63
|
-
* as a video under its controls. The inset is in the media's natural pixels.
|
|
62
|
+
* The `object-view-box` that shows the view at `scale`, `x` and `y` of media fitted at `size`, for media that must zoom
|
|
63
|
+
* inside its own box, such as a video under its controls. The inset is in the media's natural pixels. It takes the view
|
|
64
|
+
* as numbers, so painting a frame of a glide allocates nothing but the string.
|
|
64
65
|
*/
|
|
65
66
|
export function viewBox(
|
|
66
|
-
|
|
67
|
+
scale: number,
|
|
68
|
+
x: number,
|
|
69
|
+
y: number,
|
|
67
70
|
size: { width: number; height: number },
|
|
68
71
|
natural: { width: number; height: number },
|
|
69
72
|
) {
|
|
70
|
-
if (
|
|
73
|
+
if (scale <= 1) return "none"
|
|
71
74
|
|
|
72
|
-
const
|
|
75
|
+
const top = px(edge(y, size.height, scale) * natural.height)
|
|
76
|
+
const right = px(edge(-x, size.width, scale) * natural.width)
|
|
77
|
+
const bottom = px(edge(-y, size.height, scale) * natural.height)
|
|
78
|
+
const left = px(edge(x, size.width, scale) * natural.width)
|
|
73
79
|
|
|
74
|
-
|
|
75
|
-
edge(view.y, size.height) * natural.height,
|
|
76
|
-
edge(-view.x, size.width) * natural.width,
|
|
77
|
-
edge(-view.y, size.height) * natural.height,
|
|
78
|
-
edge(view.x, size.width) * natural.width,
|
|
79
|
-
]
|
|
80
|
-
|
|
81
|
-
return `inset(${inset.map((value) => `${Math.max(0, Math.round(value * 100) / 100)}px`).join(" ")})`
|
|
80
|
+
return `inset(${top} ${right} ${bottom} ${left})`
|
|
82
81
|
}
|
|
83
82
|
|
|
84
83
|
/** The zoom factor for one wheel event, bounded so a coarse mouse-wheel notch is a step rather than a jump. */
|
|
@@ -86,13 +85,22 @@ export function wheelZoomFactor(deltaY: number) {
|
|
|
86
85
|
return Math.exp(-clamp(deltaY, -30, 30) / 100)
|
|
87
86
|
}
|
|
88
87
|
|
|
89
|
-
|
|
88
|
+
/** Keeps one axis of a zoomed image of `size` covering a `viewport` it overflows, and centered in one it fits. */
|
|
89
|
+
export function clampAxis(offset: number, size: number, viewport: number) {
|
|
90
90
|
const limit = Math.max(0, (size - viewport) / 2)
|
|
91
91
|
|
|
92
92
|
// Adding zero turns -0 into 0.
|
|
93
93
|
return clamp(offset, -limit, limit) + 0
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
function edge(offset: number, fitted: number, scale: number) {
|
|
97
|
+
return 0.5 - 0.5 / scale - offset / (scale * fitted)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function px(value: number) {
|
|
101
|
+
return `${Math.max(0, Math.round(value * 100) / 100)}px`
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
function clamp(value: number, min: number, max: number) {
|
|
97
105
|
return Math.min(Math.max(value, min), max)
|
|
98
106
|
}
|
|
@@ -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
|
+
}
|