-
Notifications
You must be signed in to change notification settings - Fork 323
/
EditableSpan.tsx
311 lines (273 loc) · 9.31 KB
/
EditableSpan.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/** @file A text `<span>` which turns into an `input` when desired. */
import * as React from 'react'
import CrossIcon from '#/assets/cross.svg'
import TickIcon from '#/assets/tick.svg'
import { Button, Form, Input, Text, Underlay } from '#/components/AriaComponents'
import * as textProvider from '#/providers/TextProvider'
import * as tailwindMerge from '#/utilities/tailwindMerge'
import { useInteractOutside } from '#/components/aria'
import { useAutoFocus } from '#/hooks/autoFocusHooks'
import { useMeasure } from '#/hooks/measureHooks'
import { AnimatePresence, motion, type Variants } from 'framer-motion'
import { useLayoutEffect } from 'react'
import type { z } from 'zod'
// eslint-disable-next-line no-restricted-syntax
const MotionText = motion(Text)
/**
* Props for {@link EditableSpan}.
*/
export interface EditableSpanProps {
readonly 'data-testid'?: string
readonly className?: string
readonly editable?: boolean
readonly onSubmit: (value: string) => Promise<void>
readonly onCancel: () => void
readonly children: string
/**
* Additional schema to validate the value.
*/
readonly schema?: (schema: z.ZodType<string>) => z.ZodType<string>
}
/** A `<span>` that can turn into an `<input type="text">`. */
export default function EditableSpan(props: EditableSpanProps) {
const { className = '', editable = false, children } = props
return (
<AnimatePresence initial={false}>
{editable && <EditForm {...props} />}
{!editable && (
<MotionText
className={tailwindMerge.twJoin('min-w-0', className)}
testId={props['data-testid']}
truncate="1"
initial={{ opacity: 0, y: 5 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 5 }}
>
{children}
</MotionText>
)}
</AnimatePresence>
)
}
/**
* Props for {@link EditForm}.
*/
interface EditFormProps extends EditableSpanProps {}
const CONTAINER_VARIANTS: Variants = {
hidden: {
opacity: 0,
transition: {
staggerChildren: 1,
},
},
visible: {
opacity: 1,
transition: {
staggerChildren: 1,
},
},
}
const CHILD_VARIANTS: Variants = {
hidden: { opacity: 0, x: 5 },
visible: { opacity: 1, x: 0 },
}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const TRANSITION_OPTIONS = { stiffness: 300, damping: 150, mass: 1 }
/**
* Edit form for {@link EditableSpan}.
*/
function EditForm(props: EditFormProps) {
const { className = '', children, onSubmit, onCancel, schema } = props
const { getText } = textProvider.useText()
const formRef = React.useRef<HTMLFormElement | null>(null)
const inputRef = React.useRef<HTMLInputElement | null>(null)
const form = Form.useForm({
schema: (z) => {
const baseValueSchema = z.string().min(1).trim()
const baseSchema = z.object({ value: baseValueSchema })
if (schema != null) {
return baseSchema.merge(z.object({ value: schema(baseValueSchema) }))
}
return baseSchema
},
defaultValues: { value: children },
onSubmit: ({ value }) => onSubmit(value),
})
useInteractOutside({ ref: formRef, onInteractOutside: onCancel })
useAutoFocus({ ref: inputRef })
const { error } = Form.useFieldState({ name: 'value', form })
const formErrors = Form.useFormError({ form })
const errorMessage = (() => {
if (error != null) {
return error
}
if (formErrors.length > 0) {
return formErrors
.filter(({ type }) => type === 'error')
.map(({ message }) => message)
.join('\n')
}
return null
})()
const hasError = errorMessage != null
return (
<form
ref={formRef}
className="relative flex grow gap-1.5"
data-testid="editable-span-form"
{...form.formProps}
>
<Form.Provider form={form}>
<div className="flex flex-1 flex-shrink-0 basis-full items-center">
<Input
inputRef={inputRef}
name="value"
variant="custom"
size="custom"
rounded="none"
testId={props['data-testid']}
className={tailwindMerge.twJoin('flex-shrink-0 flex-grow basis-0', className)}
type="text"
aria-label={getText('editNameShortcut')}
// we don't want the display the default error message
error={null}
onContextMenu={(event) => {
event.stopPropagation()
}}
onKeyDown={(event) => {
if (event.key === 'Escape') {
event.preventDefault()
onCancel()
}
event.stopPropagation()
}}
/>
<AnimatePresence>
{hasError && <ErrorMessage message={errorMessage} formRef={formRef} />}
</AnimatePresence>
<AnimatePresence>
<motion.div
variants={CONTAINER_VARIANTS}
initial="hidden"
animate="visible"
exit="hidden"
className="ml-1 flex w-auto flex-none basis-0 items-center gap-1.5"
>
{form.formState.isDirty && (
<motion.div
variants={CHILD_VARIANTS}
transition={TRANSITION_OPTIONS}
initial="hidden"
animate="visible"
exit="hidden"
>
<Form.Submit
size="medium"
variant="icon"
icon={TickIcon}
aria-label={getText('confirmEdit')}
children={null}
/>
</motion.div>
)}
<motion.div
variants={CHILD_VARIANTS}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
transition={{ ...TRANSITION_OPTIONS, delay: 0.25 }}
initial="hidden"
animate="visible"
exit="hidden"
>
<Button
size="medium"
variant="icon"
icon={CrossIcon}
aria-label={getText('cancelEdit')}
onPress={onCancel}
children={null}
/>
</motion.div>
</motion.div>
</AnimatePresence>
</div>
</Form.Provider>
</form>
)
}
/**
* Props for {@link ErrorMessage}.
*/
interface ErrorMessageProps {
readonly message: string
readonly formRef: React.RefObject<HTMLFormElement>
}
/**
* Error message for {@link EditableSpan}.
*/
function ErrorMessage(props: ErrorMessageProps) {
const { message, formRef } = props
const [measureFormRef, formRect] = useMeasure({ useRAF: false })
const offset = 12
const crossOffset = 36
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const outlineWidth = crossOffset + 10
useLayoutEffect(() => {
measureFormRef(formRef.current)
}, [measureFormRef, formRef])
if (formRect == null) {
return null
}
return (
<div
className="pointer-events-none absolute"
style={{
width: formRect.width + outlineWidth,
height: formRect.height + offset,
transform: `translateX(-${crossOffset}px)`,
}}
>
<motion.div
layout
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
initial={{ opacity: 0, scaleX: 0.99 }}
animate={{ opacity: 1, scaleX: 1 }}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
exit={{ opacity: 0, scaleX: 0.99 }}
className="pointer-events-none absolute h-full w-full rounded-4xl border-[2px] border-danger"
data-testid="error-message-outline"
/>
<motion.div
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
initial={{ opacity: 0, x: -4 }}
animate={{ opacity: 1, x: 0 }}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
exit={{ x: -4, opacity: 0 }}
data-testid="error-message-container"
className="absolute bottom-0 right-0 top-0 z-1"
>
<Underlay
className="pointer-events-auto flex h-full max-w-[512px] items-center rounded-3xl rounded-l-none bg-danger pl-1.5 pr-2.5"
style={{ transform: `translateX(100%)` }}
>
<MotionText
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
initial={{ filter: 'blur(8px)', opacity: 0, x: -12 }}
animate={{ filter: 'blur(0px)', opacity: 1, x: 0 }}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
exit={{ filter: 'blur(8px)', opacity: 0, x: -12 }}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
transition={{ stiffness: 550, damping: 150, mass: 4 }}
testId="error-message-text"
variant="body"
truncate="1"
color="invert"
>
{message}
</MotionText>
<div className="absolute bottom-0 left-0 aspect-square w-5 -translate-x-full [background:radial-gradient(circle_at_0%_0%,_transparent_70%,_var(--color-danger)_70%)]" />
<div className="absolute left-0 top-0 aspect-square w-5 -translate-x-full [background:radial-gradient(circle_at_0%_100%,_transparent_70%,_var(--color-danger)_70%)]" />
</Underlay>
</motion.div>
</div>
)
}