@opencode/ui 2.2.4 → 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 +12 -0
- 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/line-comment.d.ts +9 -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/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/text-input/text-input.d.ts +4 -2
- package/dist/forms/textarea/textarea.d.ts +1 -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 +70 -0
- package/src/components/menu-scroll.tsx +161 -0
- 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 +196 -132
- 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/text-input/text-input.css +30 -16
- package/src/forms/text-input/text-input.tsx +6 -2
- package/src/forms/textarea/textarea.css +13 -8
- package/src/forms/textarea/textarea.tsx +3 -1
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +75 -66
- 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/styles/tokens/theme.css +13 -0
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/theme/themes/oc-2.json +2 -0
- package/src/theme/v2/mapping.ts +12 -8
- 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,161 @@
|
|
|
1
|
+
import { createResizeObserver } from "@solid-primitives/resize-observer"
|
|
2
|
+
import { createSignal, onCleanup, onMount, Show, splitProps, untrack, type ComponentProps, type JSX } from "solid-js"
|
|
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"
|
|
18
|
+
import "./menu-scroll.css"
|
|
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
|
+
*/
|
|
25
|
+
export function MenuScroll(
|
|
26
|
+
props: Omit<ComponentProps<"div">, "ref" | "onScroll" | "onFocusIn"> & {
|
|
27
|
+
viewportClass?: string
|
|
28
|
+
viewportRef?: (element: HTMLDivElement) => void
|
|
29
|
+
onScroll?: JSX.EventHandler<HTMLDivElement, Event>
|
|
30
|
+
},
|
|
31
|
+
) {
|
|
32
|
+
const [local, rest] = splitProps(props, ["class", "children", "viewportClass", "viewportRef", "onScroll"])
|
|
33
|
+
|
|
34
|
+
const [geometry, setGeometry] = createStore({ size: 0, start: 4 })
|
|
35
|
+
|
|
36
|
+
const [thumbState, setThumbState] = createSignal<ScrollThumbState>(INITIAL_SCROLL_THUMB_STATE)
|
|
37
|
+
|
|
38
|
+
let viewport!: HTMLDivElement
|
|
39
|
+
|
|
40
|
+
let content!: HTMLDivElement
|
|
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
|
+
|
|
69
|
+
const measure = () => {
|
|
70
|
+
const track = Math.max(0, viewport.clientHeight - 8)
|
|
71
|
+
|
|
72
|
+
const size = Math.min(track, Math.max(24, (viewport.clientHeight / Math.max(1, viewport.scrollHeight)) * track))
|
|
73
|
+
|
|
74
|
+
const max = viewport.scrollHeight - viewport.clientHeight
|
|
75
|
+
|
|
76
|
+
setGeometry("size", size)
|
|
77
|
+
setGeometry("start", 4 + (max > 0 ? (viewport.scrollTop / max) * (track - size) : 0))
|
|
78
|
+
dispatch(overflowChanged(max > 0, false))
|
|
79
|
+
}
|
|
80
|
+
|
|
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))
|
|
86
|
+
|
|
87
|
+
return (
|
|
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
|
+
>
|
|
94
|
+
<div
|
|
95
|
+
data-slot="menu-scroll-viewport"
|
|
96
|
+
{...rest}
|
|
97
|
+
ref={(element) => {
|
|
98
|
+
viewport = element
|
|
99
|
+
local.viewportRef?.(element)
|
|
100
|
+
}}
|
|
101
|
+
data-scrollable
|
|
102
|
+
class={local.viewportClass}
|
|
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
|
+
}}
|
|
113
|
+
>
|
|
114
|
+
<div ref={content} data-slot="menu-scroll-content">
|
|
115
|
+
{local.children}
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
<Show when={selectShowVerticalThumb(thumbState())}>
|
|
119
|
+
<div
|
|
120
|
+
ref={thumb}
|
|
121
|
+
data-slot="menu-scroll-thumb"
|
|
122
|
+
aria-hidden="true"
|
|
123
|
+
style={{ height: `${geometry.size}px`, transform: `translateY(${geometry.start}px)` }}
|
|
124
|
+
onPointerDown={(event) => {
|
|
125
|
+
if (event.button !== 0) return
|
|
126
|
+
event.preventDefault()
|
|
127
|
+
event.stopPropagation()
|
|
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)
|
|
138
|
+
}}
|
|
139
|
+
onPointerMove={(event) => {
|
|
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
|
|
148
|
+
|
|
149
|
+
if (travel <= 0) return
|
|
150
|
+
viewport.scrollTop =
|
|
151
|
+
((event.clientY - viewport.getBoundingClientRect().top - 4 - drag.grabOffset) / travel) *
|
|
152
|
+
(viewport.scrollHeight - viewport.clientHeight)
|
|
153
|
+
}}
|
|
154
|
+
onPointerUp={onPointerEnd}
|
|
155
|
+
onPointerCancel={onPointerEnd}
|
|
156
|
+
onLostPointerCapture={onPointerEnd}
|
|
157
|
+
/>
|
|
158
|
+
</Show>
|
|
159
|
+
</div>
|
|
160
|
+
)
|
|
161
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -1,14 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { CloseButton, Content, Description, Portal, Root, Title, Trigger } from "@kobalte/core/popover"
|
|
2
|
+
import {
|
|
3
|
+
ComponentProps,
|
|
4
|
+
JSXElement,
|
|
5
|
+
ParentProps,
|
|
6
|
+
Show,
|
|
7
|
+
createComputed,
|
|
8
|
+
createContext,
|
|
9
|
+
createEffect,
|
|
10
|
+
createSignal,
|
|
11
|
+
onCleanup,
|
|
12
|
+
splitProps,
|
|
13
|
+
untrack,
|
|
14
|
+
useContext,
|
|
15
|
+
ValidComponent,
|
|
16
|
+
} from "solid-js"
|
|
5
17
|
import { useI18n } from "../context/i18n"
|
|
6
18
|
import { Icon } from "@opencode/ui/icon"
|
|
7
19
|
import { IconButton } from "@opencode/ui/icon-button"
|
|
20
|
+
import {
|
|
21
|
+
createPopoverState,
|
|
22
|
+
EMPTY_POPOVER_STACK,
|
|
23
|
+
selectPopoverOpen,
|
|
24
|
+
selectPreventCloseAutoFocus,
|
|
25
|
+
transitionPopover,
|
|
26
|
+
transitionPopoverStack,
|
|
27
|
+
type PopoverEvent,
|
|
28
|
+
type PopoverStackEvent,
|
|
29
|
+
} from "./popover-machine"
|
|
8
30
|
|
|
9
31
|
export interface PopoverProps<T extends ValidComponent = "div">
|
|
10
32
|
extends ParentProps,
|
|
11
|
-
Omit<ComponentProps<typeof
|
|
33
|
+
Omit<ComponentProps<typeof Root>, "children"> {
|
|
12
34
|
trigger?: JSXElement
|
|
13
35
|
triggerAs?: T
|
|
14
36
|
triggerProps?: ComponentProps<T>
|
|
@@ -20,6 +42,68 @@ export interface PopoverProps<T extends ValidComponent = "div">
|
|
|
20
42
|
portal?: boolean
|
|
21
43
|
}
|
|
22
44
|
|
|
45
|
+
type PopoverLayer = {
|
|
46
|
+
trigger: () => HTMLElement | undefined
|
|
47
|
+
content: () => HTMLElement | undefined
|
|
48
|
+
dismiss: (reason: "escape" | "outside") => void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Every open popover shares one set of window listeners, so Escape closes only the newest popover and a press inside a
|
|
52
|
+
// portaled child keeps its parent open.
|
|
53
|
+
let stack = EMPTY_POPOVER_STACK
|
|
54
|
+
|
|
55
|
+
let nextLayerID = 0
|
|
56
|
+
|
|
57
|
+
const layers = new Map<number, PopoverLayer>()
|
|
58
|
+
|
|
59
|
+
const PopoverParentContext = createContext<number | null>(null)
|
|
60
|
+
|
|
61
|
+
function dispatchStack(event: PopoverStackEvent) {
|
|
62
|
+
const previous = stack
|
|
63
|
+
const transition = transitionPopoverStack(previous, event)
|
|
64
|
+
stack = transition.state
|
|
65
|
+
|
|
66
|
+
if (previous.entries.length === 0 && stack.entries.length > 0) {
|
|
67
|
+
window.addEventListener("keydown", onStackKeyDown, true)
|
|
68
|
+
window.addEventListener("pointerdown", onStackInteract, true)
|
|
69
|
+
window.addEventListener("focusin", onStackInteract, true)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (previous.entries.length > 0 && stack.entries.length === 0) {
|
|
73
|
+
window.removeEventListener("keydown", onStackKeyDown, true)
|
|
74
|
+
window.removeEventListener("pointerdown", onStackInteract, true)
|
|
75
|
+
window.removeEventListener("focusin", onStackInteract, true)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
transition.effects.forEach((effect) => layers.get(effect.id)?.dismiss(effect.reason))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function onStackKeyDown(event: KeyboardEvent) {
|
|
82
|
+
// A dialog opened above the popover already took this Escape.
|
|
83
|
+
if (event.key !== "Escape" || event.defaultPrevented) return
|
|
84
|
+
const owner = event.target instanceof Element ? event.target.closest("[data-owns-escape]") : null
|
|
85
|
+
|
|
86
|
+
// A control inside the popover that cancels its own edit on Escape, such as a comment editor, keeps it open.
|
|
87
|
+
if (owner && ![...layers.values()].some((layer) => layer.trigger() === owner || layer.content() === owner)) return
|
|
88
|
+
dispatchStack({ type: "escape.pressed" })
|
|
89
|
+
event.preventDefault()
|
|
90
|
+
event.stopPropagation()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function onStackInteract(event: Event) {
|
|
94
|
+
const target = event.target
|
|
95
|
+
|
|
96
|
+
if (!(target instanceof Node)) return
|
|
97
|
+
|
|
98
|
+
const insideIds = new Set(
|
|
99
|
+
[...layers].flatMap(([id, layer]) =>
|
|
100
|
+
layer.trigger()?.contains(target) || layer.content()?.contains(target) ? [id] : [],
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
dispatchStack({ type: "outside.interacted", insideIds })
|
|
105
|
+
}
|
|
106
|
+
|
|
23
107
|
export function Popover<T extends ValidComponent = "div">(props: PopoverProps<T>) {
|
|
24
108
|
const i18n = useI18n()
|
|
25
109
|
|
|
@@ -40,99 +124,65 @@ export function Popover<T extends ValidComponent = "div">(props: PopoverProps<T>
|
|
|
40
124
|
"modal",
|
|
41
125
|
])
|
|
42
126
|
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
127
|
+
const id = ++nextLayerID
|
|
128
|
+
const parentId = useContext(PopoverParentContext)
|
|
129
|
+
const [popover, setPopover] = createSignal(createPopoverState(local.open, local.defaultOpen))
|
|
130
|
+
let triggerRef: HTMLElement | undefined
|
|
131
|
+
let contentRef: HTMLElement | undefined
|
|
132
|
+
|
|
133
|
+
const dispatch = (event: PopoverEvent) => {
|
|
134
|
+
const transition = transitionPopover(untrack(popover), event)
|
|
135
|
+
setPopover(transition.state)
|
|
136
|
+
transition.effects.forEach((effect) => {
|
|
137
|
+
local.onOpenChange?.(effect.open)
|
|
138
|
+
// A controlled owner that kept `open` refuses the close, which clears the pending dismiss reason.
|
|
139
|
+
dispatch({ type: "props.synced", open: untrack(() => local.open) })
|
|
140
|
+
})
|
|
56
141
|
}
|
|
57
142
|
|
|
58
|
-
|
|
59
|
-
if (next) setState("dismiss", null)
|
|
60
|
-
|
|
61
|
-
if (local.onOpenChange) local.onOpenChange(next)
|
|
62
|
-
|
|
63
|
-
if (controlled()) return
|
|
64
|
-
setState("uncontrolledOpen", next)
|
|
65
|
-
}
|
|
143
|
+
createComputed(() => dispatch({ type: "props.synced", open: local.open }))
|
|
66
144
|
|
|
67
145
|
createEffect(() => {
|
|
68
|
-
if (!
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return false
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const close = (reason: "escape" | "outside") => {
|
|
83
|
-
setState("dismiss", reason)
|
|
84
|
-
onOpenChange(false)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const onKeyDown = (event: KeyboardEvent) => {
|
|
88
|
-
if (event.key !== "Escape") return
|
|
89
|
-
close("escape")
|
|
90
|
-
event.preventDefault()
|
|
91
|
-
event.stopPropagation()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const onPointerDown = (event: PointerEvent) => {
|
|
95
|
-
const target = event.target
|
|
96
|
-
|
|
97
|
-
if (!(target instanceof Node)) return
|
|
98
|
-
|
|
99
|
-
if (inside(target)) return
|
|
100
|
-
close("outside")
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const onFocusIn = (event: FocusEvent) => {
|
|
104
|
-
const target = event.target
|
|
105
|
-
|
|
106
|
-
if (!(target instanceof Node)) return
|
|
107
|
-
|
|
108
|
-
if (inside(target)) return
|
|
109
|
-
close("outside")
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
makeEventListener(window, "keydown", onKeyDown, { capture: true })
|
|
113
|
-
makeEventListener(window, "pointerdown", onPointerDown, { capture: true })
|
|
114
|
-
makeEventListener(window, "focusin", onFocusIn, { capture: true })
|
|
146
|
+
if (!selectPopoverOpen(popover())) return
|
|
147
|
+
layers.set(id, {
|
|
148
|
+
trigger: () => triggerRef,
|
|
149
|
+
content: () => contentRef,
|
|
150
|
+
dismiss: (reason) => dispatch({ type: "dismiss", reason }),
|
|
151
|
+
})
|
|
152
|
+
dispatchStack({ type: "layer.opened", id, parentId })
|
|
153
|
+
onCleanup(() => {
|
|
154
|
+
dispatchStack({ type: "layer.closed", id })
|
|
155
|
+
layers.delete(id)
|
|
156
|
+
})
|
|
115
157
|
})
|
|
116
158
|
|
|
159
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
160
|
+
|
|
117
161
|
const content = () => (
|
|
118
|
-
<
|
|
119
|
-
ref={(el: HTMLElement | undefined) =>
|
|
162
|
+
<Content
|
|
163
|
+
ref={(el: HTMLElement | undefined) => {
|
|
164
|
+
contentRef = el
|
|
165
|
+
}}
|
|
120
166
|
data-component="popover-content"
|
|
167
|
+
data-owns-escape={selectPopoverOpen(popover()) ? "" : undefined}
|
|
121
168
|
classList={{
|
|
122
169
|
...local.classList,
|
|
123
170
|
[local.class ?? ""]: !!local.class,
|
|
124
171
|
}}
|
|
125
172
|
style={local.style}
|
|
173
|
+
onEscapeKeyDown={preventDismiss}
|
|
174
|
+
onPointerDownOutside={preventDismiss}
|
|
175
|
+
onFocusOutside={preventDismiss}
|
|
126
176
|
onCloseAutoFocus={(event: Event) => {
|
|
127
|
-
if (
|
|
128
|
-
|
|
177
|
+
if (selectPreventCloseAutoFocus(untrack(popover))) event.preventDefault()
|
|
178
|
+
dispatch({ type: "close-auto-focus.handled" })
|
|
129
179
|
}}
|
|
130
180
|
>
|
|
131
|
-
{/* <
|
|
181
|
+
{/* <Arrow data-slot="popover-arrow" /> */}
|
|
132
182
|
<Show when={local.title}>
|
|
133
183
|
<div data-slot="popover-header">
|
|
134
|
-
<
|
|
135
|
-
<
|
|
184
|
+
<Title data-slot="popover-title">{local.title}</Title>
|
|
185
|
+
<CloseButton
|
|
136
186
|
data-slot="popover-close-button"
|
|
137
187
|
as={IconButton}
|
|
138
188
|
icon={<Icon name="close" />}
|
|
@@ -142,25 +192,43 @@ export function Popover<T extends ValidComponent = "div">(props: PopoverProps<T>
|
|
|
142
192
|
</div>
|
|
143
193
|
</Show>
|
|
144
194
|
<Show when={local.description}>
|
|
145
|
-
<
|
|
195
|
+
<Description data-slot="popover-description">{local.description}</Description>
|
|
146
196
|
</Show>
|
|
147
|
-
<div data-slot="popover-body">
|
|
148
|
-
|
|
197
|
+
<div data-slot="popover-body">
|
|
198
|
+
<PopoverParentContext.Provider value={id}>{local.children}</PopoverParentContext.Provider>
|
|
199
|
+
</div>
|
|
200
|
+
</Content>
|
|
149
201
|
)
|
|
150
202
|
|
|
151
203
|
return (
|
|
152
|
-
<
|
|
153
|
-
|
|
154
|
-
|
|
204
|
+
<Root
|
|
205
|
+
gutter={4}
|
|
206
|
+
{...rest}
|
|
207
|
+
open={selectPopoverOpen(popover())}
|
|
208
|
+
onOpenChange={(open) => dispatch({ type: "kobalte.changed", open })}
|
|
209
|
+
modal={local.modal ?? false}
|
|
210
|
+
>
|
|
211
|
+
<Trigger
|
|
212
|
+
ref={(el: HTMLElement) => {
|
|
213
|
+
triggerRef = el
|
|
214
|
+
}}
|
|
155
215
|
as={local.triggerAs ?? "div"}
|
|
156
216
|
data-slot="popover-trigger"
|
|
217
|
+
data-owns-escape={selectPopoverOpen(popover()) ? "" : undefined}
|
|
218
|
+
// SAFETY: `triggerProps` are `ComponentProps<T>` for `triggerAs`, the component Kobalte renders; Kobalte's
|
|
219
|
+
// polymorphic props cannot follow the generic `T`.
|
|
157
220
|
{...(local.triggerProps as any)}
|
|
158
221
|
>
|
|
159
222
|
{local.trigger}
|
|
160
|
-
</
|
|
223
|
+
</Trigger>
|
|
161
224
|
<Show when={local.portal ?? true} fallback={content()}>
|
|
162
|
-
<
|
|
225
|
+
<Portal>{content()}</Portal>
|
|
163
226
|
</Show>
|
|
164
|
-
</
|
|
227
|
+
</Root>
|
|
165
228
|
)
|
|
166
229
|
}
|
|
230
|
+
|
|
231
|
+
// The shared window listeners decide when a popover dismisses; Kobalte only closes it from the trigger or close button.
|
|
232
|
+
function preventDismiss(event: Event) {
|
|
233
|
+
event.preventDefault()
|
|
234
|
+
}
|