@moises.ai/design-system 4.20.8 → 4.20.10
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/index.js +2732 -2728
- package/package.json +1 -1
- package/src/components/PanControl/PanControl.jsx +134 -36
- package/src/components/PanControl/PanControl.stories.jsx +12 -2
package/package.json
CHANGED
|
@@ -23,14 +23,17 @@ export const PanControl = ({
|
|
|
23
23
|
minValue = -0.65,
|
|
24
24
|
maxValue = 0.65,
|
|
25
25
|
showTooltip = true,
|
|
26
|
+
tooltipValueMode = 'normalized',
|
|
26
27
|
tooltipDelayDuration = 0,
|
|
27
28
|
tooltipCloseDelay = 500,
|
|
28
29
|
}) => {
|
|
29
30
|
const containerRef = useRef(null)
|
|
31
|
+
const knobRef = useRef(null)
|
|
30
32
|
const [isDragging, setIsDragging] = useState(false)
|
|
31
33
|
const styleRef = useRef(null)
|
|
32
34
|
|
|
33
35
|
const startYRef = useRef(0)
|
|
36
|
+
const activePointerIdRef = useRef(null)
|
|
34
37
|
const pendingNormRef = useRef(value)
|
|
35
38
|
const rafRef = useRef(0)
|
|
36
39
|
const openTimeoutRef = useRef(null)
|
|
@@ -44,12 +47,43 @@ export const PanControl = ({
|
|
|
44
47
|
const [knobFocused, setKnobFocused] = useState(false)
|
|
45
48
|
const [isHovering, setIsHovering] = useState(false)
|
|
46
49
|
|
|
50
|
+
const clearOpenTimeout = useCallback(() => {
|
|
51
|
+
if (openTimeoutRef.current != null) {
|
|
52
|
+
clearTimeout(openTimeoutRef.current)
|
|
53
|
+
openTimeoutRef.current = null
|
|
54
|
+
}
|
|
55
|
+
}, [])
|
|
56
|
+
|
|
57
|
+
const clearCloseTimeout = useCallback(() => {
|
|
58
|
+
if (closeTimeoutRef.current != null) {
|
|
59
|
+
clearTimeout(closeTimeoutRef.current)
|
|
60
|
+
closeTimeoutRef.current = null
|
|
61
|
+
}
|
|
62
|
+
}, [])
|
|
63
|
+
|
|
64
|
+
const endDrag = useCallback(() => {
|
|
65
|
+
const pointerId = activePointerIdRef.current
|
|
66
|
+
const knob = knobRef.current
|
|
67
|
+
if (pointerId != null && knob?.hasPointerCapture?.(pointerId)) {
|
|
68
|
+
try {
|
|
69
|
+
knob.releasePointerCapture(pointerId)
|
|
70
|
+
} catch {
|
|
71
|
+
// Capture may already be gone (lostpointercapture / interrupt).
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
activePointerIdRef.current = null
|
|
75
|
+
setIsDragging(false)
|
|
76
|
+
}, [])
|
|
77
|
+
|
|
47
78
|
useEffect(() => {
|
|
48
79
|
if (disabled) {
|
|
80
|
+
endDrag()
|
|
81
|
+
clearOpenTimeout()
|
|
82
|
+
clearCloseTimeout()
|
|
49
83
|
setIsHovering(false)
|
|
50
84
|
setKnobFocused(false)
|
|
51
85
|
}
|
|
52
|
-
}, [disabled])
|
|
86
|
+
}, [disabled, endDrag, clearOpenTimeout, clearCloseTimeout])
|
|
53
87
|
|
|
54
88
|
useEffect(() => {
|
|
55
89
|
return () => {
|
|
@@ -59,17 +93,13 @@ export const PanControl = ({
|
|
|
59
93
|
}, [])
|
|
60
94
|
|
|
61
95
|
useEffect(() => {
|
|
62
|
-
if (isDragging)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
document.head.removeChild(styleRef.current)
|
|
70
|
-
styleRef.current = null
|
|
71
|
-
}
|
|
72
|
-
}
|
|
96
|
+
if (!isDragging) return undefined
|
|
97
|
+
|
|
98
|
+
const style = document.createElement('style')
|
|
99
|
+
style.textContent = '* { cursor: ns-resize !important; }'
|
|
100
|
+
document.head.appendChild(style)
|
|
101
|
+
styleRef.current = style
|
|
102
|
+
|
|
73
103
|
return () => {
|
|
74
104
|
if (styleRef.current) {
|
|
75
105
|
document.head.removeChild(styleRef.current)
|
|
@@ -82,6 +112,47 @@ export const PanControl = ({
|
|
|
82
112
|
}
|
|
83
113
|
}, [isDragging])
|
|
84
114
|
|
|
115
|
+
// Window-level end handlers: releasing outside the element, context menu,
|
|
116
|
+
// tab blur, visibility changes, and Escape must all clear the global ns-resize cursor.
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
if (!isDragging) return undefined
|
|
119
|
+
|
|
120
|
+
const isActivePointer = (e) =>
|
|
121
|
+
activePointerIdRef.current == null ||
|
|
122
|
+
e.pointerId === activePointerIdRef.current
|
|
123
|
+
|
|
124
|
+
const onPointerEnd = (e) => {
|
|
125
|
+
if (!isActivePointer(e)) return
|
|
126
|
+
endDrag()
|
|
127
|
+
}
|
|
128
|
+
const onInterrupt = () => endDrag()
|
|
129
|
+
const onKeyDown = (e) => {
|
|
130
|
+
if (e.key === 'Escape') {
|
|
131
|
+
e.preventDefault()
|
|
132
|
+
endDrag()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const onVisibilityChange = () => {
|
|
136
|
+
if (document.visibilityState !== 'visible') endDrag()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
window.addEventListener('pointerup', onPointerEnd)
|
|
140
|
+
window.addEventListener('pointercancel', onPointerEnd)
|
|
141
|
+
window.addEventListener('blur', onInterrupt)
|
|
142
|
+
window.addEventListener('contextmenu', onInterrupt)
|
|
143
|
+
window.addEventListener('keydown', onKeyDown)
|
|
144
|
+
document.addEventListener('visibilitychange', onVisibilityChange)
|
|
145
|
+
|
|
146
|
+
return () => {
|
|
147
|
+
window.removeEventListener('pointerup', onPointerEnd)
|
|
148
|
+
window.removeEventListener('pointercancel', onPointerEnd)
|
|
149
|
+
window.removeEventListener('blur', onInterrupt)
|
|
150
|
+
window.removeEventListener('contextmenu', onInterrupt)
|
|
151
|
+
window.removeEventListener('keydown', onKeyDown)
|
|
152
|
+
document.removeEventListener('visibilitychange', onVisibilityChange)
|
|
153
|
+
}
|
|
154
|
+
}, [isDragging, endDrag])
|
|
155
|
+
|
|
85
156
|
const scheduleEmit = useCallback(
|
|
86
157
|
(nextNormalized) => {
|
|
87
158
|
pendingNormRef.current = clampNorm(nextNormalized)
|
|
@@ -110,18 +181,29 @@ export const PanControl = ({
|
|
|
110
181
|
const onPointerDown = useCallback(
|
|
111
182
|
(e) => {
|
|
112
183
|
if (disabled) return
|
|
184
|
+
// Ignore secondary/auxiliary buttons (e.g. Mac trackpad two-finger click).
|
|
185
|
+
// Those can start drag without a reliable pointerup and leave cursor stuck.
|
|
186
|
+
if (e.button !== 0) return
|
|
113
187
|
e.preventDefault()
|
|
114
188
|
const target = e.currentTarget
|
|
189
|
+
activePointerIdRef.current = e.pointerId
|
|
115
190
|
target.setPointerCapture?.(e.pointerId)
|
|
191
|
+
clearCloseTimeout()
|
|
116
192
|
setIsDragging(true)
|
|
117
193
|
startYRef.current = e.clientY
|
|
118
194
|
},
|
|
119
|
-
[disabled],
|
|
195
|
+
[disabled, clearCloseTimeout],
|
|
120
196
|
)
|
|
121
197
|
|
|
122
198
|
const onPointerMove = useCallback(
|
|
123
199
|
(e) => {
|
|
124
200
|
if (!isDragging) return
|
|
201
|
+
if (
|
|
202
|
+
activePointerIdRef.current != null &&
|
|
203
|
+
e.pointerId !== activePointerIdRef.current
|
|
204
|
+
) {
|
|
205
|
+
return
|
|
206
|
+
}
|
|
125
207
|
const deltaY = startYRef.current - e.clientY
|
|
126
208
|
const next = value + deltaY * STEP_SIZE
|
|
127
209
|
scheduleEmit(next)
|
|
@@ -130,10 +212,26 @@ export const PanControl = ({
|
|
|
130
212
|
[isDragging, value, scheduleEmit],
|
|
131
213
|
)
|
|
132
214
|
|
|
133
|
-
const onPointerUp = useCallback(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
215
|
+
const onPointerUp = useCallback(
|
|
216
|
+
(e) => {
|
|
217
|
+
if (
|
|
218
|
+
activePointerIdRef.current != null &&
|
|
219
|
+
e.pointerId !== activePointerIdRef.current
|
|
220
|
+
) {
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
endDrag()
|
|
224
|
+
},
|
|
225
|
+
[endDrag],
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
const onContextMenu = useCallback(
|
|
229
|
+
(e) => {
|
|
230
|
+
e.preventDefault()
|
|
231
|
+
endDrag()
|
|
232
|
+
},
|
|
233
|
+
[endDrag],
|
|
234
|
+
)
|
|
137
235
|
|
|
138
236
|
const handleClick = useCallback(
|
|
139
237
|
(e) => {
|
|
@@ -196,27 +294,21 @@ export const PanControl = ({
|
|
|
196
294
|
}, [value, disabled, minValue, maxValue])
|
|
197
295
|
|
|
198
296
|
const panValueText = useMemo(() => {
|
|
199
|
-
const
|
|
200
|
-
|
|
297
|
+
const display =
|
|
298
|
+
tooltipValueMode === 'mapped'
|
|
299
|
+
? denormalize(value, minValue, maxValue)
|
|
300
|
+
: clampNorm(value)
|
|
301
|
+
const pct = Math.round(display * 100)
|
|
201
302
|
return `Pan: ${Math.abs(pct) < 1 ? 0 : pct}`
|
|
202
|
-
}, [value, minValue, maxValue])
|
|
203
|
-
|
|
204
|
-
const displayNorm = clampNorm(denormalize(value, minValue, maxValue))
|
|
205
|
-
const ariaPanPct = Math.round(displayNorm * 100)
|
|
303
|
+
}, [value, minValue, maxValue, tooltipValueMode])
|
|
206
304
|
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const clearCloseTimeout = () => {
|
|
215
|
-
if (closeTimeoutRef.current != null) {
|
|
216
|
-
clearTimeout(closeTimeoutRef.current)
|
|
217
|
-
closeTimeoutRef.current = null
|
|
218
|
-
}
|
|
219
|
-
}
|
|
305
|
+
const ariaPanPct = useMemo(() => {
|
|
306
|
+
const display =
|
|
307
|
+
tooltipValueMode === 'mapped'
|
|
308
|
+
? denormalize(value, minValue, maxValue)
|
|
309
|
+
: clampNorm(value)
|
|
310
|
+
return Math.round(display * 100)
|
|
311
|
+
}, [value, minValue, maxValue, tooltipValueMode])
|
|
220
312
|
|
|
221
313
|
const handlePointerEnter = () => {
|
|
222
314
|
if (disabled) return
|
|
@@ -234,6 +326,9 @@ export const PanControl = ({
|
|
|
234
326
|
}
|
|
235
327
|
|
|
236
328
|
const handlePointerLeave = () => {
|
|
329
|
+
// Keep tooltip visible while actively dragging, even if the pointer
|
|
330
|
+
// leaves the knob (common when adjusting vertically).
|
|
331
|
+
if (isDragging) return
|
|
237
332
|
clearOpenTimeout()
|
|
238
333
|
const { tooltipCloseDelay: delay } = tooltipTimingRef.current
|
|
239
334
|
if (delay <= 0) {
|
|
@@ -251,6 +346,7 @@ export const PanControl = ({
|
|
|
251
346
|
|
|
252
347
|
const pan = (
|
|
253
348
|
<div
|
|
349
|
+
ref={knobRef}
|
|
254
350
|
role="slider"
|
|
255
351
|
tabIndex={disabled ? -1 : 0}
|
|
256
352
|
aria-label="Pan"
|
|
@@ -268,8 +364,10 @@ export const PanControl = ({
|
|
|
268
364
|
onPointerMove={onPointerMove}
|
|
269
365
|
onPointerUp={onPointerUp}
|
|
270
366
|
onPointerCancel={onPointerUp}
|
|
367
|
+
onLostPointerCapture={onPointerUp}
|
|
271
368
|
onPointerEnter={handlePointerEnter}
|
|
272
369
|
onPointerLeave={handlePointerLeave}
|
|
370
|
+
onContextMenu={onContextMenu}
|
|
273
371
|
onFocus={() => setKnobFocused(true)}
|
|
274
372
|
onBlur={() => setKnobFocused(false)}
|
|
275
373
|
onKeyDown={handleKnobKeyDown}
|
|
@@ -71,7 +71,7 @@ export default {
|
|
|
71
71
|
minValue: {
|
|
72
72
|
control: { type: 'number', step: 0.01 },
|
|
73
73
|
description:
|
|
74
|
-
'Minimum mapped value (normalized -1
|
|
74
|
+
'Minimum mapped value for the visual arc (normalized -1 maps to this). Also used by tooltip when tooltipValueMode is "mapped".',
|
|
75
75
|
table: {
|
|
76
76
|
type: { summary: 'number' },
|
|
77
77
|
defaultValue: { summary: -0.65 },
|
|
@@ -80,12 +80,22 @@ export default {
|
|
|
80
80
|
maxValue: {
|
|
81
81
|
control: { type: 'number', step: 0.01 },
|
|
82
82
|
description:
|
|
83
|
-
'Maximum mapped value (normalized 1
|
|
83
|
+
'Maximum mapped value for the visual arc (normalized 1 maps to this). Also used by tooltip when tooltipValueMode is "mapped".',
|
|
84
84
|
table: {
|
|
85
85
|
type: { summary: 'number' },
|
|
86
86
|
defaultValue: { summary: 0.65 },
|
|
87
87
|
},
|
|
88
88
|
},
|
|
89
|
+
tooltipValueMode: {
|
|
90
|
+
control: 'select',
|
|
91
|
+
options: ['normalized', 'mapped'],
|
|
92
|
+
description:
|
|
93
|
+
'Tooltip/aria scale: "normalized" shows -100..100 from the control position; "mapped" shows the minValue/maxValue scale (e.g. ±65).',
|
|
94
|
+
table: {
|
|
95
|
+
type: { summary: "'normalized' | 'mapped'" },
|
|
96
|
+
defaultValue: { summary: 'normalized' },
|
|
97
|
+
},
|
|
98
|
+
},
|
|
89
99
|
showTooltip: {
|
|
90
100
|
control: 'boolean',
|
|
91
101
|
description: 'Shows the pan value tooltip on hover/focus/drag',
|