@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,10 +1,19 @@
|
|
|
1
1
|
import { TextField as Kobalte } from "@kobalte/core/text-field"
|
|
2
|
-
import { createSignal, Show, splitProps } from "solid-js"
|
|
2
|
+
import { createSignal, onCleanup, Show, splitProps, untrack } from "solid-js"
|
|
3
3
|
import type { ComponentProps } from "solid-js"
|
|
4
4
|
import { useI18n } from "../context/i18n"
|
|
5
5
|
import { IconButton } from "@opencode/ui/icon-button"
|
|
6
6
|
import { Icon } from "@opencode/ui/icon"
|
|
7
7
|
import { Tooltip } from "@opencode/ui/tooltip"
|
|
8
|
+
import {
|
|
9
|
+
INITIAL_TEXT_FIELD_COPY_STATE,
|
|
10
|
+
selectTextFieldCopied,
|
|
11
|
+
selectTextFieldCopyIcon,
|
|
12
|
+
selectTextFieldCopyLabelKey,
|
|
13
|
+
transitionTextFieldCopy,
|
|
14
|
+
type TextFieldCopyEffect,
|
|
15
|
+
type TextFieldCopyEvent,
|
|
16
|
+
} from "./text-field-machine"
|
|
8
17
|
|
|
9
18
|
export interface TextFieldProps
|
|
10
19
|
extends ComponentProps<typeof Kobalte.Input>,
|
|
@@ -56,35 +65,49 @@ export function TextField(props: TextFieldProps) {
|
|
|
56
65
|
"multiline",
|
|
57
66
|
])
|
|
58
67
|
|
|
59
|
-
const [
|
|
68
|
+
const [copyState, setCopyState] = createSignal(INITIAL_TEXT_FIELD_COPY_STATE)
|
|
69
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
60
70
|
|
|
61
|
-
const
|
|
62
|
-
|
|
71
|
+
const dispatch = (event: TextFieldCopyEvent) => {
|
|
72
|
+
const next = transitionTextFieldCopy(untrack(copyState), event)
|
|
63
73
|
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
return i18n.t("ui.textField.copyToClipboard")
|
|
74
|
+
if (next.state !== untrack(copyState)) setCopyState(next.state)
|
|
75
|
+
next.effects.forEach(run)
|
|
67
76
|
}
|
|
68
77
|
|
|
69
|
-
const
|
|
70
|
-
if (
|
|
78
|
+
const run = (effect: TextFieldCopyEffect) => {
|
|
79
|
+
if (effect.type === "timer.clear") {
|
|
80
|
+
clearTimeout(timer)
|
|
81
|
+
timer = undefined
|
|
71
82
|
|
|
72
|
-
|
|
83
|
+
return
|
|
84
|
+
}
|
|
73
85
|
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
if (effect.type === "reset.schedule") {
|
|
87
|
+
timer = setTimeout(() => dispatch({ type: "reset.elapsed", attempt: effect.attempt }), effect.delayMs)
|
|
76
88
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
await navigator.clipboard.writeText(value)
|
|
80
|
-
setCopied(true)
|
|
81
|
-
setTimeout(() => setCopied(false), 2000)
|
|
82
|
-
}
|
|
89
|
+
return
|
|
90
|
+
}
|
|
83
91
|
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
timer = setTimeout(() => dispatch({ type: "copy.timed-out", attempt: effect.attempt }), effect.timeoutMs)
|
|
93
|
+
void writeClipboard(effect.value).then(
|
|
94
|
+
(written) => dispatch({ type: written ? "copy.succeeded" : "copy.failed", attempt: effect.attempt }),
|
|
95
|
+
() => dispatch({ type: "copy.failed", attempt: effect.attempt }),
|
|
96
|
+
)
|
|
86
97
|
}
|
|
87
98
|
|
|
99
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
100
|
+
|
|
101
|
+
const copied = () => selectTextFieldCopied(copyState())
|
|
102
|
+
const label = () => i18n.t(selectTextFieldCopyLabelKey(copyState(), local.copyKind))
|
|
103
|
+
|
|
104
|
+
const requestCopy = () =>
|
|
105
|
+
dispatch({
|
|
106
|
+
type: "copy.requested",
|
|
107
|
+
value: local.value ?? local.defaultValue ?? "",
|
|
108
|
+
enabled: !!local.copyable && !local.disabled,
|
|
109
|
+
})
|
|
110
|
+
|
|
88
111
|
return (
|
|
89
112
|
<Kobalte
|
|
90
113
|
data-component="input"
|
|
@@ -94,7 +117,7 @@ export function TextField(props: TextFieldProps) {
|
|
|
94
117
|
value={local.value}
|
|
95
118
|
onChange={local.onChange}
|
|
96
119
|
onKeyDown={local.onKeyDown}
|
|
97
|
-
onClick={
|
|
120
|
+
onClick={requestCopy}
|
|
98
121
|
required={local.required}
|
|
99
122
|
disabled={local.disabled}
|
|
100
123
|
readOnly={local.readOnly}
|
|
@@ -118,14 +141,19 @@ export function TextField(props: TextFieldProps) {
|
|
|
118
141
|
value={label()}
|
|
119
142
|
placement="top"
|
|
120
143
|
gutter={4}
|
|
121
|
-
forceOpen={copied()
|
|
144
|
+
forceOpen={copied() || undefined}
|
|
122
145
|
skipDelayDuration={0}
|
|
123
146
|
>
|
|
124
147
|
<IconButton
|
|
125
148
|
type="button"
|
|
126
|
-
icon={<Icon name={
|
|
149
|
+
icon={<Icon name={selectTextFieldCopyIcon(copyState(), local.copyKind)} />}
|
|
127
150
|
variant="ghost"
|
|
128
|
-
|
|
151
|
+
disabled={local.disabled}
|
|
152
|
+
onClick={(event) => {
|
|
153
|
+
// The field itself copies on click too; one click is one copy.
|
|
154
|
+
event.stopPropagation()
|
|
155
|
+
requestCopy()
|
|
156
|
+
}}
|
|
129
157
|
tabIndex={-1}
|
|
130
158
|
data-slot="input-copy-button"
|
|
131
159
|
aria-label={label()}
|
|
@@ -140,3 +168,29 @@ export function TextField(props: TextFieldProps) {
|
|
|
140
168
|
</Kobalte>
|
|
141
169
|
)
|
|
142
170
|
}
|
|
171
|
+
|
|
172
|
+
// `navigator.clipboard` exists only in secure contexts, so plain-HTTP LAN origins copy through a selected textarea.
|
|
173
|
+
async function writeClipboard(text: string) {
|
|
174
|
+
const written = await navigator.clipboard?.writeText(text).then(
|
|
175
|
+
() => true,
|
|
176
|
+
() => false,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if (written) return true
|
|
180
|
+
const active = document.activeElement instanceof HTMLElement ? document.activeElement : undefined
|
|
181
|
+
const textarea = document.createElement("textarea")
|
|
182
|
+
textarea.value = text
|
|
183
|
+
textarea.setAttribute("readonly", "")
|
|
184
|
+
textarea.setAttribute("aria-hidden", "true")
|
|
185
|
+
textarea.tabIndex = -1
|
|
186
|
+
textarea.style.position = "fixed"
|
|
187
|
+
textarea.style.opacity = "0"
|
|
188
|
+
textarea.style.pointerEvents = "none"
|
|
189
|
+
document.body.appendChild(textarea)
|
|
190
|
+
textarea.select()
|
|
191
|
+
const copied = document.execCommand("copy")
|
|
192
|
+
textarea.remove()
|
|
193
|
+
active?.focus({ preventScroll: true })
|
|
194
|
+
|
|
195
|
+
return copied
|
|
196
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/** Fonts that never settle must not keep the reveal from animating. */
|
|
2
|
+
export const TEXT_REVEAL_READY_TIMEOUT_MS = 1_000
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `measuring` waits for fonts and one painted frame before transitions turn on, so the first text never animates in.
|
|
6
|
+
* It only moves forward, so its results match on the phase. `swapping` holds a replaced text in place for one frame,
|
|
7
|
+
* then the wipe runs. `leaving` is always text that was on screen, so text replaced within one frame never wipes out.
|
|
8
|
+
*/
|
|
9
|
+
export type TextRevealState =
|
|
10
|
+
| { readonly status: "measuring"; readonly current: string | undefined; readonly widthPx: number }
|
|
11
|
+
| {
|
|
12
|
+
readonly status: "idle"
|
|
13
|
+
readonly issued: number
|
|
14
|
+
readonly current: string | undefined
|
|
15
|
+
readonly leaving: string | undefined
|
|
16
|
+
readonly widthPx: number
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
readonly status: "swapping"
|
|
20
|
+
readonly attempt: number
|
|
21
|
+
readonly current: string | undefined
|
|
22
|
+
readonly leaving: string | undefined
|
|
23
|
+
readonly widthPx: number
|
|
24
|
+
}
|
|
25
|
+
| { readonly status: "disposed" }
|
|
26
|
+
|
|
27
|
+
type Measured = { readonly widthPx: number; readonly growOnly: boolean }
|
|
28
|
+
|
|
29
|
+
export type TextRevealEvent =
|
|
30
|
+
| { readonly type: "text.changed"; readonly text: string | undefined }
|
|
31
|
+
| ({ readonly type: "width.measured" } & Measured)
|
|
32
|
+
| ({ readonly type: "fonts.settled" } & Measured)
|
|
33
|
+
| ({ readonly type: "ready.settled" } & Measured)
|
|
34
|
+
| ({ readonly type: "ready.timed-out" } & Measured)
|
|
35
|
+
| ({ readonly type: "swap.settled"; readonly attempt: number } & Measured)
|
|
36
|
+
| { readonly type: "disposed" }
|
|
37
|
+
|
|
38
|
+
export type TextRevealEffect =
|
|
39
|
+
| { readonly type: "measure.entering" }
|
|
40
|
+
| { readonly type: "ready.frame" }
|
|
41
|
+
| { readonly type: "swap.frame"; readonly attempt: number }
|
|
42
|
+
| { readonly type: "cancel.frame" }
|
|
43
|
+
| { readonly type: "cancel.ready-timer" }
|
|
44
|
+
|
|
45
|
+
export type TextRevealTransition = {
|
|
46
|
+
readonly state: TextRevealState
|
|
47
|
+
readonly effects: readonly TextRevealEffect[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const NO_EFFECTS: readonly TextRevealEffect[] = Object.freeze([])
|
|
51
|
+
|
|
52
|
+
const MEASURE: TextRevealEffect = Object.freeze({ type: "measure.entering" })
|
|
53
|
+
|
|
54
|
+
const READY_FRAME: TextRevealEffect = Object.freeze({ type: "ready.frame" })
|
|
55
|
+
|
|
56
|
+
const CANCEL_FRAME: TextRevealEffect = Object.freeze({ type: "cancel.frame" })
|
|
57
|
+
|
|
58
|
+
const CANCEL_READY_TIMER: TextRevealEffect = Object.freeze({ type: "cancel.ready-timer" })
|
|
59
|
+
|
|
60
|
+
const DISPOSED: TextRevealState = Object.freeze({ status: "disposed" })
|
|
61
|
+
|
|
62
|
+
export function createTextRevealState(text: string | undefined): TextRevealState {
|
|
63
|
+
return { status: "measuring", current: text, widthPx: 0 }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function transitionTextReveal(state: TextRevealState, event: TextRevealEvent): TextRevealTransition {
|
|
67
|
+
if (state.status === "disposed") return { state, effects: NO_EFFECTS }
|
|
68
|
+
|
|
69
|
+
if (event.type === "disposed") {
|
|
70
|
+
if (state.status === "measuring") return { state: DISPOSED, effects: [CANCEL_FRAME, CANCEL_READY_TIMER] }
|
|
71
|
+
|
|
72
|
+
return { state: DISPOSED, effects: state.status === "swapping" ? [CANCEL_FRAME] : NO_EFFECTS }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (event.type === "text.changed") return changeText(state, event.text)
|
|
76
|
+
|
|
77
|
+
if (state.status === "measuring" && event.type === "fonts.settled") {
|
|
78
|
+
return { state: { ...state, widthPx: widen(state.widthPx, event) }, effects: [READY_FRAME] }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Fonts that settle after the ready timeout still widen the track to the real text.
|
|
82
|
+
if (event.type === "width.measured" || event.type === "fonts.settled") {
|
|
83
|
+
const widthPx = widen(state.widthPx, event)
|
|
84
|
+
|
|
85
|
+
return { state: widthPx === state.widthPx ? state : { ...state, widthPx }, effects: NO_EFFECTS }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (state.status === "measuring" && (event.type === "ready.settled" || event.type === "ready.timed-out")) {
|
|
89
|
+
return {
|
|
90
|
+
state: {
|
|
91
|
+
status: "idle",
|
|
92
|
+
issued: 0,
|
|
93
|
+
current: state.current,
|
|
94
|
+
leaving: undefined,
|
|
95
|
+
widthPx: widen(state.widthPx, event),
|
|
96
|
+
},
|
|
97
|
+
effects: [CANCEL_FRAME, CANCEL_READY_TIMER],
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (state.status === "swapping" && event.type === "swap.settled" && event.attempt === state.attempt) {
|
|
102
|
+
return {
|
|
103
|
+
state: {
|
|
104
|
+
status: "idle",
|
|
105
|
+
issued: state.attempt,
|
|
106
|
+
current: state.current,
|
|
107
|
+
leaving: state.leaving,
|
|
108
|
+
widthPx: widen(state.widthPx, event),
|
|
109
|
+
},
|
|
110
|
+
effects: NO_EFFECTS,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return { state, effects: NO_EFFECTS }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Transitions turn on once the first text has painted. */
|
|
118
|
+
export function selectTextRevealReady(state: TextRevealState) {
|
|
119
|
+
return state.status === "idle" || state.status === "swapping"
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function selectTextRevealSwapping(state: TextRevealState) {
|
|
123
|
+
return state.status === "swapping"
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function selectTextRevealEntering(state: TextRevealState) {
|
|
127
|
+
return (state.status === "disposed" ? undefined : state.current) ?? "\u00A0"
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function selectTextRevealLeaving(state: TextRevealState) {
|
|
131
|
+
return (state.status === "idle" || state.status === "swapping" ? state.leaving : undefined) ?? "\u00A0"
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function selectTextRevealTrackWidth(state: TextRevealState, truncate: boolean | undefined) {
|
|
135
|
+
if (truncate) return "100%"
|
|
136
|
+
|
|
137
|
+
return state.status !== "disposed" && state.widthPx > 0 ? `${state.widthPx}px` : "auto"
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function changeText(
|
|
141
|
+
state: Exclude<TextRevealState, { status: "disposed" }>,
|
|
142
|
+
text: string | undefined,
|
|
143
|
+
): TextRevealTransition {
|
|
144
|
+
if (text === state.current) return { state, effects: NO_EFFECTS }
|
|
145
|
+
|
|
146
|
+
// Before the first paint nothing is on screen to wipe out, so the new text just replaces the old.
|
|
147
|
+
if (state.status === "measuring") return { state: { ...state, current: text }, effects: [MEASURE] }
|
|
148
|
+
|
|
149
|
+
if (state.status === "idle" && state.current !== undefined && text?.startsWith(state.current)) {
|
|
150
|
+
return { state: { ...state, current: text }, effects: [MEASURE] }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Changing back to the text still on screen cancels the swap that never painted.
|
|
154
|
+
if (state.status === "swapping" && text === state.leaving) {
|
|
155
|
+
return {
|
|
156
|
+
state: { status: "idle", issued: state.attempt, current: text, leaving: undefined, widthPx: state.widthPx },
|
|
157
|
+
effects: [CANCEL_FRAME, MEASURE],
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (state.status === "swapping") {
|
|
162
|
+
const attempt = state.attempt + 1
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
state: { ...state, attempt, current: text },
|
|
166
|
+
effects: [CANCEL_FRAME, { type: "swap.frame", attempt }],
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const attempt = state.issued + 1
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
state: { status: "swapping", attempt, current: text, leaving: state.current, widthPx: state.widthPx },
|
|
174
|
+
effects: [{ type: "swap.frame", attempt }],
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function widen(prev: number, measured: Measured) {
|
|
179
|
+
if (measured.widthPx <= 0) return prev
|
|
180
|
+
|
|
181
|
+
if (measured.growOnly && prev > 0 && measured.widthPx <= prev) return prev
|
|
182
|
+
|
|
183
|
+
return measured.widthPx
|
|
184
|
+
}
|
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { createEffect,
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return `${fallback}
|
|
1
|
+
import { createEffect, createSignal, onCleanup, onMount, untrack } from "solid-js"
|
|
2
|
+
import {
|
|
3
|
+
createTextRevealState,
|
|
4
|
+
selectTextRevealEntering,
|
|
5
|
+
selectTextRevealLeaving,
|
|
6
|
+
selectTextRevealReady,
|
|
7
|
+
selectTextRevealSwapping,
|
|
8
|
+
selectTextRevealTrackWidth,
|
|
9
|
+
TEXT_REVEAL_READY_TIMEOUT_MS,
|
|
10
|
+
transitionTextReveal,
|
|
11
|
+
type TextRevealEffect,
|
|
12
|
+
type TextRevealEvent,
|
|
13
|
+
} from "./text-reveal-machine"
|
|
14
|
+
|
|
15
|
+
// A number gets the unit; a string is already a CSS value.
|
|
16
|
+
const css = (value: number | string | undefined, fallback: number, unit: "px" | "ms") => {
|
|
17
|
+
if (value === undefined) return `${fallback}${unit}`
|
|
18
|
+
|
|
19
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- discriminates this prop's own `number | string` union
|
|
20
|
+
return typeof value === "number" ? `${value}${unit}` : value
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
const pct = (value: number | undefined, fallback: number) => {
|
|
@@ -36,125 +39,125 @@ export function TextReveal(props: {
|
|
|
36
39
|
growOnly?: boolean
|
|
37
40
|
truncate?: boolean
|
|
38
41
|
}) {
|
|
39
|
-
const [
|
|
40
|
-
cur: props.text,
|
|
41
|
-
old: undefined as string | undefined,
|
|
42
|
-
width: "auto",
|
|
43
|
-
ready: false,
|
|
44
|
-
swapping: false,
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
const cur = () => state.cur
|
|
48
|
-
const old = () => state.old
|
|
49
|
-
const width = () => state.width
|
|
50
|
-
const ready = () => state.ready
|
|
51
|
-
const swapping = () => state.swapping
|
|
42
|
+
const [machine, setMachine] = createSignal(createTextRevealState(props.text))
|
|
52
43
|
let inRef: HTMLSpanElement | undefined
|
|
53
44
|
let outRef: HTMLSpanElement | undefined
|
|
54
45
|
let rootRef: HTMLSpanElement | undefined
|
|
55
46
|
let frame: number | undefined
|
|
47
|
+
let readyTimer: ReturnType<typeof setTimeout> | undefined
|
|
56
48
|
|
|
57
49
|
const win = () => inRef?.scrollWidth ?? 0
|
|
58
50
|
const wout = () => outRef?.scrollWidth ?? 0
|
|
51
|
+
const growOnly = () => props.growOnly ?? true
|
|
52
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- probing for a browser global; SSR has no frames
|
|
53
|
+
const animates = typeof requestAnimationFrame === "function"
|
|
59
54
|
|
|
60
|
-
const
|
|
61
|
-
|
|
55
|
+
const dispatch = (event: TextRevealEvent) => {
|
|
56
|
+
const next = transitionTextReveal(untrack(machine), event)
|
|
62
57
|
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
// Commit first: the effects measure the text nodes this state renders.
|
|
59
|
+
if (next.state !== untrack(machine)) setMachine(next.state)
|
|
60
|
+
next.effects.forEach(run)
|
|
61
|
+
}
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
const cancelFrame = () => {
|
|
64
|
+
if (frame !== undefined) cancelAnimationFrame(frame)
|
|
65
|
+
frame = undefined
|
|
66
|
+
}
|
|
68
67
|
|
|
69
|
-
|
|
68
|
+
const onFrame = (finish: () => void) => {
|
|
69
|
+
cancelFrame()
|
|
70
|
+
|
|
71
|
+
if (!animates) return finish()
|
|
72
|
+
frame = requestAnimationFrame(() => {
|
|
73
|
+
frame = undefined
|
|
74
|
+
finish()
|
|
75
|
+
})
|
|
70
76
|
}
|
|
71
77
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
(
|
|
75
|
-
(next, prev) => {
|
|
76
|
-
if (next === prev) return
|
|
77
|
-
|
|
78
|
-
if (typeof next === "string" && typeof prev === "string" && next.startsWith(prev)) {
|
|
79
|
-
setState("cur", next)
|
|
80
|
-
widen(win())
|
|
81
|
-
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
setState("swapping", true)
|
|
86
|
-
setState("old", prev)
|
|
87
|
-
setState("cur", next)
|
|
88
|
-
|
|
89
|
-
if (typeof requestAnimationFrame !== "function") {
|
|
90
|
-
widen(Math.max(win(), wout()))
|
|
91
|
-
rootRef?.offsetHeight
|
|
92
|
-
setState("swapping", false)
|
|
93
|
-
|
|
94
|
-
return
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (frame !== undefined && typeof cancelAnimationFrame === "function") cancelAnimationFrame(frame)
|
|
98
|
-
frame = requestAnimationFrame(() => {
|
|
99
|
-
widen(Math.max(win(), wout()))
|
|
100
|
-
rootRef?.offsetHeight
|
|
101
|
-
setState("swapping", false)
|
|
102
|
-
frame = undefined
|
|
103
|
-
})
|
|
104
|
-
},
|
|
105
|
-
),
|
|
106
|
-
)
|
|
78
|
+
const run = (effect: TextRevealEffect) => {
|
|
79
|
+
if (effect.type === "measure.entering") {
|
|
80
|
+
dispatch({ type: "width.measured", widthPx: win(), growOnly: growOnly() })
|
|
107
81
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const fonts = typeof document !== "undefined" ? document.fonts : undefined
|
|
82
|
+
return
|
|
83
|
+
}
|
|
111
84
|
|
|
112
|
-
if (
|
|
113
|
-
|
|
85
|
+
if (effect.type === "ready.frame") {
|
|
86
|
+
onFrame(() => dispatch({ type: "ready.settled", widthPx: win(), growOnly: growOnly() }))
|
|
114
87
|
|
|
115
88
|
return
|
|
116
89
|
}
|
|
117
90
|
|
|
118
|
-
if (
|
|
119
|
-
|
|
91
|
+
if (effect.type === "swap.frame") {
|
|
92
|
+
onFrame(() => {
|
|
93
|
+
const widthPx = Math.max(win(), wout())
|
|
94
|
+
rootRef?.offsetHeight
|
|
95
|
+
dispatch({ type: "swap.settled", attempt: effect.attempt, widthPx, growOnly: growOnly() })
|
|
96
|
+
})
|
|
120
97
|
|
|
121
98
|
return
|
|
122
99
|
}
|
|
123
100
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
101
|
+
if (effect.type === "cancel.frame") {
|
|
102
|
+
cancelFrame()
|
|
103
|
+
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
clearTimeout(readyTimer)
|
|
108
|
+
readyTimer = undefined
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
createEffect(() => {
|
|
112
|
+
const text = props.text
|
|
113
|
+
untrack(() => dispatch({ type: "text.changed", text }))
|
|
128
114
|
})
|
|
129
115
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
116
|
+
onMount(() => {
|
|
117
|
+
dispatch({ type: "width.measured", widthPx: win(), growOnly: growOnly() })
|
|
118
|
+
|
|
119
|
+
if (!animates) {
|
|
120
|
+
dispatch({ type: "ready.settled", widthPx: win(), growOnly: growOnly() })
|
|
121
|
+
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
readyTimer = setTimeout(
|
|
126
|
+
() => dispatch({ type: "ready.timed-out", widthPx: win(), growOnly: growOnly() }),
|
|
127
|
+
TEXT_REVEAL_READY_TIMEOUT_MS,
|
|
128
|
+
)
|
|
129
|
+
const fonts = typeof document !== "undefined" ? document.fonts : undefined
|
|
130
|
+
const settle = () => dispatch({ type: "fonts.settled", widthPx: win(), growOnly: growOnly() })
|
|
131
|
+
|
|
132
|
+
if (!fonts) return settle()
|
|
133
|
+
void fonts.ready.finally(settle)
|
|
133
134
|
})
|
|
134
135
|
|
|
136
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
137
|
+
|
|
135
138
|
return (
|
|
136
139
|
<span
|
|
137
140
|
ref={rootRef}
|
|
138
141
|
data-component="text-reveal"
|
|
139
|
-
data-ready={
|
|
140
|
-
data-swapping={
|
|
142
|
+
data-ready={selectTextRevealReady(machine()) ? "true" : "false"}
|
|
143
|
+
data-swapping={selectTextRevealSwapping(machine()) ? "true" : "false"}
|
|
141
144
|
data-truncate={props.truncate ? "true" : "false"}
|
|
142
145
|
class={props.class}
|
|
143
146
|
aria-label={props.text ?? ""}
|
|
144
147
|
style={{
|
|
145
|
-
"--text-reveal-duration":
|
|
148
|
+
"--text-reveal-duration": css(props.duration, 450, "ms"),
|
|
146
149
|
"--text-reveal-edge": pct(props.edge, 17),
|
|
147
|
-
"--text-reveal-travel":
|
|
150
|
+
"--text-reveal-travel": css(props.travel, 0, "px"),
|
|
148
151
|
"--text-reveal-spring": props.spring ?? "cubic-bezier(0.34, 1.08, 0.64, 1)",
|
|
149
152
|
"--text-reveal-spring-soft": props.springSoft ?? "cubic-bezier(0.34, 1, 0.64, 1)",
|
|
150
153
|
}}
|
|
151
154
|
>
|
|
152
|
-
<span data-slot="text-reveal-track" style={{ width: props.truncate
|
|
155
|
+
<span data-slot="text-reveal-track" style={{ width: selectTextRevealTrackWidth(machine(), props.truncate) }}>
|
|
153
156
|
<span data-slot="text-reveal-entering" ref={inRef}>
|
|
154
|
-
{
|
|
157
|
+
{selectTextRevealEntering(machine())}
|
|
155
158
|
</span>
|
|
156
159
|
<span data-slot="text-reveal-leaving" ref={outRef}>
|
|
157
|
-
{
|
|
160
|
+
{selectTextRevealLeaving(machine())}
|
|
158
161
|
</span>
|
|
159
162
|
</span>
|
|
160
163
|
</span>
|