@astryxdesign/core 0.7.7-canary.ae97fa8 → 0.7.7-canary.b377b21
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/AlertDialog/AlertDialog.d.ts +4 -1
- package/dist/AlertDialog/AlertDialog.d.ts.map +1 -1
- package/dist/AlertDialog/AlertDialog.js +36 -6
- package/dist/BottomSheet/BottomSheetPanel.d.ts.map +1 -1
- package/dist/BottomSheet/BottomSheetPanel.js +22 -3
- package/dist/BottomSheet/useSheetGestures.d.ts.map +1 -1
- package/dist/BottomSheet/useSheetGestures.js +3 -1
- package/dist/Button/Button.d.ts.map +1 -1
- package/dist/Button/Button.js +17 -1
- package/dist/CheckboxInput/CheckboxInput.d.ts.map +1 -1
- package/dist/CheckboxInput/CheckboxInput.js +13 -2
- package/dist/ComplexSelector/ComplexSelector.d.ts +4 -1
- package/dist/ComplexSelector/ComplexSelector.d.ts.map +1 -1
- package/dist/ComplexSelector/ComplexSelector.js +14 -4
- package/dist/ContextMenu/ContextMenu.js +2 -2
- package/dist/Dialog/Dialog.d.ts.map +1 -1
- package/dist/Dialog/Dialog.js +7 -31
- package/dist/Field/Field.js +2 -2
- package/dist/HoverCard/HoverCard.d.ts +4 -3
- package/dist/HoverCard/HoverCard.d.ts.map +1 -1
- package/dist/HoverCard/useHoverCard.d.ts +5 -4
- package/dist/HoverCard/useHoverCard.d.ts.map +1 -1
- package/dist/HoverCard/useHoverCard.js +17 -31
- package/dist/Icon/Icon.d.ts +5 -1
- package/dist/Icon/Icon.d.ts.map +1 -1
- package/dist/Icon/Icon.js +5 -2
- package/dist/Layer/usePressPin.d.ts +17 -5
- package/dist/Layer/usePressPin.d.ts.map +1 -1
- package/dist/Layer/usePressPin.js +62 -12
- package/dist/Lightbox/Lightbox.d.ts.map +1 -1
- package/dist/Lightbox/Lightbox.js +18 -0
- package/dist/PowerSearch/PowerSearch.d.ts +36 -3
- package/dist/PowerSearch/PowerSearch.d.ts.map +1 -1
- package/dist/PowerSearch/PowerSearch.js +433 -127
- package/dist/PowerSearch/PowerSearchBottomSheet.d.ts +100 -0
- package/dist/PowerSearch/PowerSearchBottomSheet.d.ts.map +1 -0
- package/dist/PowerSearch/PowerSearchBottomSheet.js +1913 -0
- package/dist/PowerSearch/PowerSearchEditPopover.d.ts +32 -1
- package/dist/PowerSearch/PowerSearchEditPopover.d.ts.map +1 -1
- package/dist/PowerSearch/PowerSearchEditPopover.js +17 -6
- package/dist/PowerSearch/PowerSearchValueEditor.d.ts +38 -3
- package/dist/PowerSearch/PowerSearchValueEditor.d.ts.map +1 -1
- package/dist/PowerSearch/PowerSearchValueEditor.js +170 -61
- package/dist/PowerSearch/formatFilterValue.d.ts +7 -0
- package/dist/PowerSearch/formatFilterValue.d.ts.map +1 -1
- package/dist/PowerSearch/formatFilterValue.js +11 -1
- package/dist/PowerSearch/formatTokenValue.d.ts +28 -0
- package/dist/PowerSearch/formatTokenValue.d.ts.map +1 -0
- package/dist/PowerSearch/formatTokenValue.js +160 -0
- package/dist/PowerSearch/powerSearchSheetUtils.d.ts +65 -0
- package/dist/PowerSearch/powerSearchSheetUtils.d.ts.map +1 -0
- package/dist/PowerSearch/powerSearchSheetUtils.js +168 -0
- package/dist/RadioList/RadioList.d.ts.map +1 -1
- package/dist/RadioList/RadioList.js +3 -2
- package/dist/RadioList/RadioListItem.d.ts +1 -1
- package/dist/RadioList/RadioListItem.d.ts.map +1 -1
- package/dist/RadioList/RadioListItem.js +5 -3
- package/dist/Resizable/ResizeHandle.d.ts +2 -1
- package/dist/Resizable/ResizeHandle.d.ts.map +1 -1
- package/dist/Resizable/ResizeHandle.js +99 -77
- package/dist/SegmentedControl/useSegmentedSelectionMotion.d.ts.map +1 -1
- package/dist/SegmentedControl/useSegmentedSelectionMotion.js +3 -3
- package/dist/Slider/sliderTouchController.js +2 -2
- package/dist/Spinner/Spinner.d.ts +1 -1
- package/dist/Spinner/Spinner.d.ts.map +1 -1
- package/dist/Spinner/Spinner.js +60 -58
- package/dist/Switch/useSwitchTouch.js +4 -4
- package/dist/Table/BaseTable.d.ts.map +1 -1
- package/dist/Table/BaseTable.js +29 -8
- package/dist/TextInput/TextInput.d.ts +2 -2
- package/dist/TextInput/TextInput.d.ts.map +1 -1
- package/dist/TextInput/TextInput.js +10 -7
- package/dist/Toast/Toast.d.ts +11 -1
- package/dist/Toast/Toast.d.ts.map +1 -1
- package/dist/Toast/Toast.js +25 -15
- package/dist/Toast/ToastViewport.d.ts.map +1 -1
- package/dist/Toast/ToastViewport.js +94 -34
- package/dist/Toast/types.d.ts +9 -3
- package/dist/Toast/types.d.ts.map +1 -1
- package/dist/Tooltip/useTooltip.d.ts +19 -1
- package/dist/Tooltip/useTooltip.d.ts.map +1 -1
- package/dist/Tooltip/useTooltip.js +34 -53
- package/dist/Typeahead/TypeaheadItem.d.ts +2 -1
- package/dist/Typeahead/TypeaheadItem.d.ts.map +1 -1
- package/dist/animator/createAnimator.d.ts +98 -0
- package/dist/animator/createAnimator.d.ts.map +1 -0
- package/dist/animator/createAnimator.js +259 -0
- package/dist/astryx.css +35 -6
- package/dist/gestures/PressTracker.d.ts.map +1 -1
- package/dist/gestures/PressTracker.js +4 -3
- package/dist/hooks/actionGroupTracker.d.ts +4 -1
- package/dist/hooks/actionGroupTracker.d.ts.map +1 -1
- package/dist/hooks/actionGroupTracker.js +13 -3
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/pressGroupContext.d.ts +6 -0
- package/dist/hooks/pressGroupContext.d.ts.map +1 -0
- package/dist/hooks/pressGroupContext.js +20 -0
- package/dist/hooks/useActionGroupTracking.d.ts +53 -0
- package/dist/hooks/useActionGroupTracking.d.ts.map +1 -0
- package/dist/hooks/useActionGroupTracking.js +174 -0
- package/dist/hooks/useAnimator.d.ts +41 -0
- package/dist/hooks/useAnimator.d.ts.map +1 -0
- package/dist/hooks/useAnimator.js +70 -0
- package/dist/hooks/useAttachment.d.ts +1 -1
- package/dist/hooks/useAttachment.js +1 -1
- package/dist/hooks/useInputStatusIcon.d.ts +1 -1
- package/dist/hooks/useInputStatusIcon.d.ts.map +1 -1
- package/dist/hooks/useInputStatusIcon.js +2 -2
- package/dist/hooks/useLongPress.d.ts +6 -2
- package/dist/hooks/useLongPress.d.ts.map +1 -1
- package/dist/hooks/useLongPress.js +95 -15
- package/dist/i18n/generated-english/PowerSearch/PowerSearchBottomSheet.generated.d.ts +8 -0
- package/dist/i18n/generated-english/PowerSearch/PowerSearchBottomSheet.generated.d.ts.map +1 -0
- package/dist/i18n/generated-english/PowerSearch/PowerSearchBottomSheet.generated.js +42 -0
- package/dist/i18n/generated-english/PowerSearch/PowerSearchValueEditor.generated.d.ts.map +1 -1
- package/dist/i18n/generated-english/PowerSearch/PowerSearchValueEditor.generated.js +6 -0
- package/dist/i18n/generated-english/PowerSearch/formatTokenValue.generated.d.ts +8 -0
- package/dist/i18n/generated-english/PowerSearch/formatTokenValue.generated.d.ts.map +1 -0
- package/dist/i18n/generated-english/PowerSearch/formatTokenValue.generated.js +14 -0
- package/dist/i18n/generated-locales/en.generated.d.ts.map +1 -1
- package/dist/i18n/generated-locales/en.generated.js +30 -0
- package/dist/i18n/generated-locales/pseudo.generated.d.ts.map +1 -1
- package/dist/i18n/generated-locales/pseudo.generated.js +30 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/internal/indicatorPress.stylex.d.ts +20 -0
- package/dist/internal/indicatorPress.stylex.d.ts.map +1 -0
- package/dist/internal/indicatorPress.stylex.js +26 -0
- package/dist/motion/animator.d.ts +14 -14
- package/dist/motion/animator.d.ts.map +1 -1
- package/dist/motion/animator.js +41 -18
- package/dist/motion/uikit/alert.d.ts +41 -0
- package/dist/motion/uikit/alert.d.ts.map +1 -0
- package/dist/motion/uikit/alert.js +43 -0
- package/dist/privateAttributes/pressGroupItem.stylex.d.ts +15 -0
- package/dist/privateAttributes/pressGroupItem.stylex.d.ts.map +1 -0
- package/dist/privateAttributes/pressGroupItem.stylex.js +23 -0
- package/dist/utils/focusReturn.d.ts +15 -0
- package/dist/utils/focusReturn.d.ts.map +1 -1
- package/dist/utils/focusReturn.js +28 -0
- package/dist/utils/interactionOverlay.stylex.d.ts +3 -2
- package/dist/utils/interactionOverlay.stylex.d.ts.map +1 -1
- package/dist/utils/interactionOverlay.stylex.js +3 -2
- package/dist/utils/pressFeedback.d.ts.map +1 -1
- package/dist/utils/pressFeedback.js +19 -6
- package/dist/utils/triggerFocus.d.ts +36 -0
- package/dist/utils/triggerFocus.d.ts.map +1 -0
- package/dist/utils/triggerFocus.js +65 -0
- package/locales/en.json +120 -0
- package/locales/pseudo.json +90 -0
- package/package.json +9 -4
- package/src/AlertDialog/AlertDialog.doc.mjs +2 -2
- package/src/AlertDialog/AlertDialog.tsx +37 -5
- package/src/BottomSheet/BottomSheetPanel.tsx +33 -3
- package/src/BottomSheet/useSheetGestures.ts +3 -1
- package/src/Button/Button.tsx +26 -4
- package/src/CheckboxInput/CheckboxInput.doc.mjs +18 -0
- package/src/CheckboxInput/CheckboxInput.tsx +36 -9
- package/src/ComplexSelector/ComplexSelector.tsx +20 -5
- package/src/ContextMenu/ContextMenu.tsx +2 -2
- package/src/Dialog/Dialog.tsx +10 -31
- package/src/Field/Field.tsx +2 -2
- package/src/HoverCard/HoverCard.doc.mjs +4 -2
- package/src/HoverCard/HoverCard.tsx +4 -3
- package/src/HoverCard/useHoverCard.doc.mjs +178 -36
- package/src/HoverCard/useHoverCard.tsx +46 -52
- package/src/Icon/Icon.doc.mjs +49 -8
- package/src/Icon/Icon.tsx +15 -7
- package/src/Layer/usePressPin.ts +81 -15
- package/src/Lightbox/Lightbox.tsx +18 -0
- package/src/PowerSearch/PowerSearch.doc.mjs +54 -5
- package/src/PowerSearch/PowerSearch.tsx +609 -155
- package/src/PowerSearch/PowerSearchBottomSheet.tsx +2515 -0
- package/src/PowerSearch/PowerSearchEditPopover.tsx +28 -6
- package/src/PowerSearch/PowerSearchValueEditor.tsx +203 -38
- package/src/PowerSearch/formatFilterValue.ts +18 -1
- package/src/PowerSearch/formatTokenValue.ts +215 -0
- package/src/PowerSearch/powerSearchSheetUtils.ts +224 -0
- package/src/RadioList/RadioList.tsx +3 -2
- package/src/RadioList/RadioListItem.tsx +5 -3
- package/src/Resizable/Resizable.doc.mjs +5 -4
- package/src/Resizable/ResizeHandle.tsx +167 -109
- package/src/SegmentedControl/useSegmentedSelectionMotion.ts +6 -3
- package/src/Slider/sliderTouchController.ts +2 -2
- package/src/Spinner/Spinner.tsx +77 -63
- package/src/Switch/useSwitchTouch.ts +4 -4
- package/src/Table/BaseTable.tsx +46 -12
- package/src/TextInput/TextInput.tsx +11 -10
- package/src/Toast/Toast.doc.mjs +4 -3
- package/src/Toast/Toast.tsx +52 -32
- package/src/Toast/ToastViewport.tsx +121 -35
- package/src/Toast/types.ts +9 -3
- package/src/Tooltip/useTooltip.doc.mjs +24 -0
- package/src/Tooltip/useTooltip.tsx +61 -56
- package/src/Typeahead/TypeaheadItem.doc.mjs +6 -3
- package/src/Typeahead/TypeaheadItem.tsx +2 -1
- package/src/animator/createAnimator.doc.mjs +153 -0
- package/src/animator/createAnimator.ts +324 -0
- package/src/gestures/PressTracker.ts +4 -3
- package/src/gestures/README.md +7 -1
- package/src/hooks/actionGroupTracker.ts +20 -5
- package/src/hooks/index.ts +2 -0
- package/src/hooks/pressGroupContext.ts +21 -0
- package/src/hooks/useActionGroupTracking.ts +224 -0
- package/src/hooks/useAnimator.doc.mjs +109 -0
- package/src/hooks/useAnimator.ts +67 -0
- package/src/hooks/useAttachment.ts +1 -1
- package/src/hooks/useInputStatusIcon.tsx +3 -2
- package/src/hooks/useLongPress.doc.mjs +2 -2
- package/src/hooks/useLongPress.ts +136 -15
- package/src/i18n/generated-english/PowerSearch/PowerSearchBottomSheet.generated.ts +43 -0
- package/src/i18n/generated-english/PowerSearch/PowerSearchValueEditor.generated.ts +6 -0
- package/src/i18n/generated-english/PowerSearch/formatTokenValue.generated.ts +15 -0
- package/src/i18n/generated-locales/en.generated.ts +41 -0
- package/src/i18n/generated-locales/pseudo.generated.ts +42 -0
- package/src/index.ts +13 -0
- package/src/internal/indicatorPress.stylex.ts +24 -0
- package/src/motion/README.md +8 -5
- package/src/motion/animator.ts +61 -29
- package/src/motion/uikit/alert.ts +43 -0
- package/src/privateAttributes/pressGroupItem.stylex.ts +23 -0
- package/src/utils/focusReturn.ts +30 -0
- package/src/utils/interactionOverlay.stylex.ts +3 -2
- package/src/utils/pressFeedback.ts +19 -6
- package/src/utils/triggerFocus.ts +80 -0
|
@@ -5,14 +5,16 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @file useTooltip.tsx
|
|
7
7
|
* @input Uses useLayer, usePressPin, React hooks
|
|
8
|
-
* @output Exports useTooltip hook with stable trigger refs for hover/focus/tap tooltips
|
|
8
|
+
* @output Exports useTooltip hook with stable trigger refs for hover/focus/tap tooltips
|
|
9
|
+
* and imperative show() and hide() for an app-built toggle trigger,
|
|
9
10
|
* and the module-only useTooltipWithTriggerBehavior that InfoTip's press-to-pin uses
|
|
10
11
|
* @position Layer hook; builds on useLayer for tooltip behavior
|
|
11
12
|
*
|
|
12
13
|
* SYNC: When modified, update:
|
|
13
14
|
* - /packages/core/src/Tooltip/index.ts
|
|
14
15
|
*/
|
|
15
|
-
import { useCallback, useEffect, useRef } from 'react';
|
|
16
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
17
|
+
import { createTriggerFocusGate } from "../utils/triggerFocus.js";
|
|
16
18
|
import * as stylex from '@stylexjs/stylex';
|
|
17
19
|
import { useLayerInternal } from "../Layer/useLayer.js";
|
|
18
20
|
import { useAnnounceRenderedText } from "../hooks/useAnnounceRenderedText.js";
|
|
@@ -52,28 +54,6 @@ const styles = {
|
|
|
52
54
|
* Touch trigger behavior for tooltips
|
|
53
55
|
*/
|
|
54
56
|
|
|
55
|
-
/**
|
|
56
|
-
* Check if an element is naturally focusable
|
|
57
|
-
*/
|
|
58
|
-
function isFocusable(element) {
|
|
59
|
-
// Elements with explicit tabindex
|
|
60
|
-
if (element.hasAttribute('tabindex')) {
|
|
61
|
-
return element.tabIndex >= 0;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Naturally focusable elements
|
|
65
|
-
const focusableTags = ['A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA'];
|
|
66
|
-
if (focusableTags.includes(element.tagName)) {
|
|
67
|
-
return !element.disabled;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Elements with contenteditable
|
|
71
|
-
if (element.isContentEditable) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
57
|
/**
|
|
78
58
|
* Hook for tooltip behavior with hover/focus triggers.
|
|
79
59
|
*
|
|
@@ -187,6 +167,9 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
187
167
|
isHeldOpen: isPressHeldOpen,
|
|
188
168
|
attach: attachPressPin,
|
|
189
169
|
unpin: unpinPress,
|
|
170
|
+
hold: holdPress,
|
|
171
|
+
release: releasePress,
|
|
172
|
+
isAppHeld,
|
|
190
173
|
isPinShown,
|
|
191
174
|
touch: {
|
|
192
175
|
isTouchPointerRef,
|
|
@@ -289,25 +272,11 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
289
272
|
scheduleHide();
|
|
290
273
|
}, [isTouchPointerRef, layerId, scheduleHide]);
|
|
291
274
|
|
|
292
|
-
// Under `auto`, whether focus counts is decided when it arrives,
|
|
293
|
-
//
|
|
294
|
-
|
|
295
|
-
// by definition; focus inside it counts only while the trigger is
|
|
296
|
-
// focusable too, so a plain wrapper still ignores its children's focus.
|
|
297
|
-
const isFocusForTrigger = useCallback(e => {
|
|
298
|
-
if (focusTrigger === 'always') {
|
|
299
|
-
return true;
|
|
300
|
-
}
|
|
301
|
-
const triggerEl = e.currentTarget;
|
|
302
|
-
return triggerEl != null && (e.target === triggerEl || isFocusable(triggerEl));
|
|
303
|
-
}, [focusTrigger]);
|
|
304
|
-
|
|
305
|
-
// Whether the trigger's current focus counted when it arrived, so its blur
|
|
306
|
-
// hides the tooltip even if the trigger has since left the tab order.
|
|
307
|
-
const focusCountedRef = useRef(false);
|
|
275
|
+
// Under `auto`, whether focus counts is decided when it arrives, and a
|
|
276
|
+
// focus that counted still hides on blur (utils/triggerFocus.ts).
|
|
277
|
+
const [focusGate] = useState(createTriggerFocusGate);
|
|
308
278
|
const handleFocusIn = useCallback(e => {
|
|
309
|
-
|
|
310
|
-
if (!isEnabled || !focusCountedRef.current) {
|
|
279
|
+
if (!focusGate.focusIn(e, focusTrigger) || !isEnabled) {
|
|
311
280
|
return;
|
|
312
281
|
}
|
|
313
282
|
// A tap focuses the trigger it activates, and `:focus-visible` does not
|
|
@@ -326,18 +295,17 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
326
295
|
}
|
|
327
296
|
clearTimeouts();
|
|
328
297
|
showLayer();
|
|
329
|
-
}, [
|
|
298
|
+
}, [focusGate, focusTrigger, isEnabled, isTouchInteraction, clearTimeouts, showLayer]);
|
|
330
299
|
|
|
331
300
|
// A pinned tooltip's focus-leave close is the pin's own: it listens on the
|
|
332
301
|
// trigger whatever `focusTrigger` is. This schedules the unpinned hide,
|
|
333
302
|
// which a held tooltip skips.
|
|
334
303
|
const handleFocusOut = useCallback(e => {
|
|
335
|
-
if (!
|
|
304
|
+
if (!focusGate.focusOut(e, focusTrigger)) {
|
|
336
305
|
return;
|
|
337
306
|
}
|
|
338
|
-
focusCountedRef.current = false;
|
|
339
307
|
scheduleHide();
|
|
340
|
-
}, [
|
|
308
|
+
}, [focusGate, focusTrigger, scheduleHide]);
|
|
341
309
|
|
|
342
310
|
// Pressing the trigger hides its own tooltip: once the control is activated
|
|
343
311
|
// the hint has served its purpose, and a tooltip lingering over a
|
|
@@ -347,9 +315,10 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
347
315
|
// A touch press is a different gesture (it may be the only way to open the
|
|
348
316
|
// tooltip at all), so the touch path answers it first.
|
|
349
317
|
const handlePointerDown = useCallback(event => {
|
|
350
|
-
// A
|
|
351
|
-
//
|
|
352
|
-
|
|
318
|
+
// A toggle trigger's press pins or closes through its click. The app's
|
|
319
|
+
// hide() closes a held tooltip. Neither path hides here or waits for a
|
|
320
|
+
// tap verdict.
|
|
321
|
+
if (isToggleTrigger(triggerRef.current) || isAppHeld()) {
|
|
353
322
|
return;
|
|
354
323
|
}
|
|
355
324
|
if (handleTouchPointerDown(event)) {
|
|
@@ -360,7 +329,7 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
360
329
|
}
|
|
361
330
|
clearTimeouts();
|
|
362
331
|
hideLayer();
|
|
363
|
-
}, [isToggleTrigger, handleTouchPointerDown, isOpen, clearTimeouts, hideLayer]);
|
|
332
|
+
}, [isToggleTrigger, isAppHeld, handleTouchPointerDown, isOpen, clearTimeouts, hideLayer]);
|
|
364
333
|
|
|
365
334
|
// Interaction ref that handles event listeners only
|
|
366
335
|
const interactionRef = useCallback(el => {
|
|
@@ -384,7 +353,7 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
384
353
|
el.addEventListener('pointerdown', handlePointerDown);
|
|
385
354
|
|
|
386
355
|
// Focus listeners unless focus never opens the tooltip; `auto` decides
|
|
387
|
-
// per event (
|
|
356
|
+
// per event (utils/triggerFocus.ts).
|
|
388
357
|
if (focusTrigger !== 'never') {
|
|
389
358
|
el.addEventListener('focusin', handleFocusIn);
|
|
390
359
|
el.addEventListener('focusout', handleFocusOut);
|
|
@@ -425,9 +394,11 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
425
394
|
showLayer();
|
|
426
395
|
} else {
|
|
427
396
|
clearTimeouts();
|
|
397
|
+
// A pin or app hold ends when the consumer closes the tooltip.
|
|
398
|
+
unpinPress();
|
|
428
399
|
hideLayer();
|
|
429
400
|
}
|
|
430
|
-
}, [isOpen, clearTimeouts, showLayer, hideLayer]);
|
|
401
|
+
}, [isOpen, clearTimeouts, showLayer, hideLayer, unpinPress]);
|
|
431
402
|
|
|
432
403
|
// Dismiss on Escape (WCAG 1.4.13 — dismissible) through the shared layer
|
|
433
404
|
// stack. A visible tip is the top-most layer, so it takes the press and
|
|
@@ -507,12 +478,22 @@ export function useTooltipWithTriggerBehavior(options = {}) {
|
|
|
507
478
|
children: children
|
|
508
479
|
}), renderProps);
|
|
509
480
|
}, [renderLayer, placement, alignment, popoverXstyle, cancelHide, scheduleHide, contentRefs]);
|
|
481
|
+
const show = useCallback(() => {
|
|
482
|
+
clearTimeouts();
|
|
483
|
+
holdPress();
|
|
484
|
+
}, [clearTimeouts, holdPress]);
|
|
485
|
+
const hide = useCallback(() => {
|
|
486
|
+
clearTimeouts();
|
|
487
|
+
releasePress();
|
|
488
|
+
}, [clearTimeouts, releasePress]);
|
|
510
489
|
return {
|
|
511
490
|
ref,
|
|
512
491
|
positionRef: layerRef,
|
|
513
492
|
interactionRef,
|
|
514
493
|
anchorId: layerAnchorId,
|
|
515
494
|
describedBy: layerId,
|
|
516
|
-
renderTooltip
|
|
495
|
+
renderTooltip,
|
|
496
|
+
show,
|
|
497
|
+
hide
|
|
517
498
|
};
|
|
518
499
|
}
|
|
@@ -31,7 +31,8 @@ export interface TypeaheadItemProps<T extends SearchableItem = SearchableItem> e
|
|
|
31
31
|
*/
|
|
32
32
|
isDisabled?: boolean;
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* This prop has no effect. Set `item.auxiliaryData.group` on search-source
|
|
35
|
+
* items to group results in Typeahead or BaseTypeahead.
|
|
35
36
|
*/
|
|
36
37
|
group?: string;
|
|
37
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TypeaheadItem.d.ts","sourceRoot":"","sources":["../../src/Typeahead/TypeaheadItem.tsx"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,EAAC,KAAK,SAAS,EAAC,MAAM,OAAO,CAAC;AAQ5C,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AAQ5C,MAAM,WAAW,kBAAkB,CACjC,CAAC,SAAS,cAAc,GAAG,cAAc,CACzC,SAAQ,SAAS,CAAC,cAAc,CAAC;IACjC,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAChC;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;IAER;;OAEG;IACH,IAAI,CAAC,EAAE,SAAS,CAAC;IAEjB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAErB
|
|
1
|
+
{"version":3,"file":"TypeaheadItem.d.ts","sourceRoot":"","sources":["../../src/Typeahead/TypeaheadItem.tsx"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,EAAC,KAAK,SAAS,EAAC,MAAM,OAAO,CAAC;AAQ5C,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AAQ5C,MAAM,WAAW,kBAAkB,CACjC,CAAC,SAAS,cAAc,GAAG,cAAc,CACzC,SAAQ,SAAS,CAAC,cAAc,CAAC;IACjC,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAChC;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;IAER;;OAEG;IACH,IAAI,CAAC,EAAE,SAAS,CAAC;IAEjB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAErB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AA6CD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,cAAc,EAAE,EACtD,GAAG,EACH,IAAI,EACJ,IAAI,EACJ,WAAW,EACX,UAAkB,EAClB,KAAK,EAAE,MAAM,EACb,MAAM,EACN,SAAS,EACT,KAAK,EACL,GAAG,IAAI,EACR,EAAE,kBAAkB,CAAC,CAAC,CAAC,qBAyBvB;yBApCe,aAAa"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A channel the animator drives. `x` and `y` translate the element (px).
|
|
3
|
+
* `scale` scales it about its transform origin (1 is its size). `rotate`
|
|
4
|
+
* turns it (degrees, clockwise), and `opacity` fades it (0 to 1).
|
|
5
|
+
*/
|
|
6
|
+
export type AnimatorChannel = 'x' | 'y' | 'scale' | 'rotate' | 'opacity';
|
|
7
|
+
/**
|
|
8
|
+
* Values by channel. A velocity uses the same keys, in units per second:
|
|
9
|
+
* px/s, scale per second, deg/s, opacity per second.
|
|
10
|
+
*/
|
|
11
|
+
export type AnimatorValues = Partial<Record<AnimatorChannel, number>>;
|
|
12
|
+
/** Every channel's value (or velocity), as `get` and `getVelocity` read it. */
|
|
13
|
+
export type AnimatorSnapshot = Readonly<Record<AnimatorChannel, number>>;
|
|
14
|
+
/** Options of `AnimatorHandle.animate`. */
|
|
15
|
+
export interface AnimatorAnimateOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Each channel's velocity at the start, such as a drag's release velocity.
|
|
18
|
+
* A channel without one keeps the
|
|
19
|
+
* velocity it has on screen, so an `animate` that interrupts another
|
|
20
|
+
* carries on smoothly.
|
|
21
|
+
*/
|
|
22
|
+
velocity?: AnimatorValues;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A release velocity for `AnimatorHandle.decay`, per channel, in its units
|
|
26
|
+
* per second. That is px/s for `x` and `y`, and degrees per second for
|
|
27
|
+
* `rotate`. `scale` and `opacity` take their units per second.
|
|
28
|
+
*/
|
|
29
|
+
export type AnimatorDecayVelocity = AnimatorValues;
|
|
30
|
+
/**
|
|
31
|
+
* Animates one element. Each method acts on the channels it is given and
|
|
32
|
+
* leaves the others where they are.
|
|
33
|
+
*/
|
|
34
|
+
export interface AnimatorHandle {
|
|
35
|
+
/**
|
|
36
|
+
* Springs the given channels to `target` on the `springs.stateChange`
|
|
37
|
+
* motion role, from wherever they are on screen. The promise resolves when
|
|
38
|
+
* they arrive, or as soon as a later `animate`, `decay`, `set` or `stop`
|
|
39
|
+
* takes any of them over.
|
|
40
|
+
*/
|
|
41
|
+
animate(target: AnimatorValues, options?: AnimatorAnimateOptions): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Coasts the given channels from a release velocity, slowing as a scroll
|
|
44
|
+
* view's momentum does. It resolves when they stop, or when another call
|
|
45
|
+
* takes them over. The distance follows from the velocity alone: there are
|
|
46
|
+
* no bounds, except that opacity stops at 0 or 1.
|
|
47
|
+
*/
|
|
48
|
+
decay(velocity: AnimatorDecayVelocity): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Puts the given channels at these values at once, ending their motion:
|
|
51
|
+
* the per-frame write of a drag.
|
|
52
|
+
*/
|
|
53
|
+
set(values: AnimatorValues): void;
|
|
54
|
+
/** Every channel's value on screen now. */
|
|
55
|
+
get(): AnimatorSnapshot;
|
|
56
|
+
/** Every channel's velocity on screen now, in units per second. */
|
|
57
|
+
getVelocity(): AnimatorSnapshot;
|
|
58
|
+
/** Halts every channel where it is on screen. */
|
|
59
|
+
stop(): void;
|
|
60
|
+
}
|
|
61
|
+
/** What `createAnimator` returns: the handle, and `destroy`. */
|
|
62
|
+
export interface Animator extends AnimatorHandle {
|
|
63
|
+
/**
|
|
64
|
+
* Stops every motion and lets go of the element; the element keeps the
|
|
65
|
+
* values it shows. The animator does nothing after this.
|
|
66
|
+
*/
|
|
67
|
+
destroy(): void;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* An animator whose element is attached later, through `attach`. It is the
|
|
71
|
+
* one construction `createAnimator` and `useAnimator` share, so the hook has
|
|
72
|
+
* no behavior of its own. Not exported from the package.
|
|
73
|
+
*/
|
|
74
|
+
export declare function createAttachableAnimator(): {
|
|
75
|
+
readonly animator: Animator;
|
|
76
|
+
readonly attach: (element: HTMLElement | null) => void;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Animates `element` with the motion Core uses. `animate` springs channels to
|
|
80
|
+
* a target on the state-change motion, and `decay` coasts them from a release
|
|
81
|
+
* velocity. `set` jumps them, `get` and `getVelocity` read them, `stop` halts
|
|
82
|
+
* them, and `destroy` lets the element go. Under Reduce Motion every call is
|
|
83
|
+
* planned. A move becomes a short cross-fade on a touch screen, and it lands
|
|
84
|
+
* at once with a mouse.
|
|
85
|
+
*
|
|
86
|
+
* Use one animator per element: two on one element overwrite each other.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```
|
|
90
|
+
* const card = document.querySelector('.card');
|
|
91
|
+
* const animator = createAnimator(card);
|
|
92
|
+
* await animator.animate({x: 0, rotate: 0}, {velocity: {x: -900}});
|
|
93
|
+
* animator.decay({x: 2400});
|
|
94
|
+
* animator.destroy();
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare function createAnimator(element: HTMLElement): Animator;
|
|
98
|
+
//# sourceMappingURL=createAnimator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createAnimator.d.ts","sourceRoot":"","sources":["../../src/animator/createAnimator.ts"],"names":[],"mappings":"AA+BA;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,GAAG,GAAG,GAAG,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEzE;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC;AAEtE,+EAA+E;AAC/E,MAAM,MAAM,gBAAgB,GAAG,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC;AAEzE,2CAA2C;AAC3C,MAAM,WAAW,sBAAsB;IACrC;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B;AAED;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG,cAAc,CAAC;AAEnD;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;OAKG;IACH,OAAO,CACL,MAAM,EAAE,cAAc,EACtB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtD;;;OAGG;IACH,GAAG,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI,CAAC;IAClC,2CAA2C;IAC3C,GAAG,IAAI,gBAAgB,CAAC;IACxB,mEAAmE;IACnE,WAAW,IAAI,gBAAgB,CAAC;IAChC,iDAAiD;IACjD,IAAI,IAAI,IAAI,CAAC;CACd;AAED,gEAAgE;AAChE,MAAM,WAAW,QAAS,SAAQ,cAAc;IAC9C;;;OAGG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB;AAqFD;;;;GAIG;AACH,wBAAgB,wBAAwB,IAAI;IAC1C,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,KAAK,IAAI,CAAC;CACxD,CA+FA;AAKD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,GAAG,QAAQ,CAI7D"}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file createAnimator.ts
|
|
7
|
+
* @input An element; the internal motion animator (motion/animator.ts), the
|
|
8
|
+
* `springs.stateChange` motion role and the momentum coast (motion/coast.ts)
|
|
9
|
+
* @output The public animator: `createAnimator`, the `AnimatorHandle` it
|
|
10
|
+
* returns, and their value types
|
|
11
|
+
* @position Public entry point, re-exported from the package root. The
|
|
12
|
+
* `useAnimator` hook wraps it. Importing it does no DOM work.
|
|
13
|
+
*
|
|
14
|
+
* SYNC: When modified, update:
|
|
15
|
+
* - /packages/core/src/animator/createAnimator.test.ts
|
|
16
|
+
* - /packages/core/src/animator/createAnimator.doc.mjs
|
|
17
|
+
* - /packages/core/src/hooks/useAnimator.ts
|
|
18
|
+
*/
|
|
19
|
+
import { createMotionAnimator, SETTLE_THRESHOLD } from "../motion/animator.js";
|
|
20
|
+
import { coast } from "../motion/coast.js";
|
|
21
|
+
import { solveDecay } from "../motion/decay.js";
|
|
22
|
+
import { registerSpringsTable, springs } from "../motion/uikit/springs.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A channel the animator drives. `x` and `y` translate the element (px).
|
|
26
|
+
* `scale` scales it about its transform origin (1 is its size). `rotate`
|
|
27
|
+
* turns it (degrees, clockwise), and `opacity` fades it (0 to 1).
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Values by channel. A velocity uses the same keys, in units per second:
|
|
32
|
+
* px/s, scale per second, deg/s, opacity per second.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/** Every channel's value (or velocity), as `get` and `getVelocity` read it. */
|
|
36
|
+
|
|
37
|
+
/** Options of `AnimatorHandle.animate`. */
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A release velocity for `AnimatorHandle.decay`, per channel, in its units
|
|
41
|
+
* per second. That is px/s for `x` and `y`, and degrees per second for
|
|
42
|
+
* `rotate`. `scale` and `opacity` take their units per second.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Animates one element. Each method acts on the channels it is given and
|
|
47
|
+
* leaves the others where they are.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/** What `createAnimator` returns: the handle, and `destroy`. */
|
|
51
|
+
|
|
52
|
+
const CHANNELS = ['x', 'y', 'scale', 'rotate', 'opacity'];
|
|
53
|
+
|
|
54
|
+
/** The values of the channels the public API names, and nothing else. */
|
|
55
|
+
function motionValues(values) {
|
|
56
|
+
const out = {};
|
|
57
|
+
if (values == null) {
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
for (const channel of CHANNELS) {
|
|
61
|
+
const value = values[channel];
|
|
62
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
63
|
+
out[channel] = value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* A coast from `from` at `velocity` on the momentum decay a scroll view
|
|
71
|
+
* uses, ending at rest. The theme does not change this decay. An opacity
|
|
72
|
+
* coast stops at 0 or 1.
|
|
73
|
+
*/
|
|
74
|
+
function momentum(from, velocity, threshold, isUnit) {
|
|
75
|
+
const glide = solveDecay({
|
|
76
|
+
from,
|
|
77
|
+
velocity
|
|
78
|
+
});
|
|
79
|
+
const edge = !isUnit ? null : glide.rest < 0 ? 0 : glide.rest > 1 ? 1 : null;
|
|
80
|
+
const hit = edge == null ? null : glide.timeToReach(edge);
|
|
81
|
+
const duration = hit ?? glide.settleTime(threshold);
|
|
82
|
+
const end = edge ?? glide.rest;
|
|
83
|
+
return {
|
|
84
|
+
duration,
|
|
85
|
+
at: t => t >= duration ? end : glide.value(t),
|
|
86
|
+
velocity: t => t >= duration ? 0 : glide.velocity(t)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function snapshot(read) {
|
|
90
|
+
return {
|
|
91
|
+
x: read('x'),
|
|
92
|
+
y: read('y'),
|
|
93
|
+
scale: read('scale'),
|
|
94
|
+
rotate: read('rotate'),
|
|
95
|
+
opacity: read('opacity')
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Resolves once every motion of one call has finished, or as soon as any of
|
|
101
|
+
* them is interrupted (`finished` resolves `false`). A later call that takes
|
|
102
|
+
* over one channel ends the whole call where it stopped, as one `animate`
|
|
103
|
+
* does. The other channels move on.
|
|
104
|
+
*/
|
|
105
|
+
async function settledOrTakenOver(moving) {
|
|
106
|
+
if (moving.length === 0) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
await new Promise(resolve => {
|
|
110
|
+
let left = moving.length;
|
|
111
|
+
for (const finished of moving) {
|
|
112
|
+
void finished.then(completed => {
|
|
113
|
+
left -= 1;
|
|
114
|
+
if (!completed || left === 0) {
|
|
115
|
+
resolve();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* An animator whose element is attached later, through `attach`. It is the
|
|
124
|
+
* one construction `createAnimator` and `useAnimator` share, so the hook has
|
|
125
|
+
* no behavior of its own. Not exported from the package.
|
|
126
|
+
*/
|
|
127
|
+
export function createAttachableAnimator() {
|
|
128
|
+
const motion = createMotionAnimator(null);
|
|
129
|
+
// Calls do nothing while no element is attached, before the ref attaches
|
|
130
|
+
// or after it lets go. They also do nothing after destroy, and animate and
|
|
131
|
+
// decay then resolve at once.
|
|
132
|
+
let element = null;
|
|
133
|
+
let destroyed = false;
|
|
134
|
+
const inactive = () => destroyed || element == null;
|
|
135
|
+
const animator = {
|
|
136
|
+
async animate(target, options) {
|
|
137
|
+
const to = motionValues(target);
|
|
138
|
+
if (inactive() || Object.keys(to).length === 0) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// The role registry is written on first use, never on import.
|
|
142
|
+
registerSpringsTable();
|
|
143
|
+
const velocity = motionValues(options?.velocity);
|
|
144
|
+
await motion.animate(to, {
|
|
145
|
+
spring: springs.stateChange,
|
|
146
|
+
...(Object.keys(velocity).length > 0 ? {
|
|
147
|
+
velocity
|
|
148
|
+
} : {})
|
|
149
|
+
}).finished;
|
|
150
|
+
},
|
|
151
|
+
async decay(velocity) {
|
|
152
|
+
if (inactive()) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const given = motionValues(velocity);
|
|
156
|
+
// The translate axes coast as a throw, which Reduce Motion lets
|
|
157
|
+
// continue. The others coast as a transition, which it replaces.
|
|
158
|
+
const translate = coast(motion, {
|
|
159
|
+
x: given.x,
|
|
160
|
+
y: given.y
|
|
161
|
+
});
|
|
162
|
+
const others = {};
|
|
163
|
+
for (const channel of ['scale', 'rotate', 'opacity']) {
|
|
164
|
+
const v0 = given[channel];
|
|
165
|
+
if (v0 != null) {
|
|
166
|
+
const property = channel === 'scale' ? 'scaleX' : channel;
|
|
167
|
+
others[channel] = momentum(motion.get(property), v0, SETTLE_THRESHOLD[property], channel === 'opacity');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const moving = [];
|
|
171
|
+
if (Object.keys(translate).length > 0) {
|
|
172
|
+
moving.push(motion.decay(translate).finished);
|
|
173
|
+
}
|
|
174
|
+
if (Object.keys(others).length > 0) {
|
|
175
|
+
moving.push(motion.play(others).finished);
|
|
176
|
+
}
|
|
177
|
+
await settledOrTakenOver(moving);
|
|
178
|
+
},
|
|
179
|
+
set(values) {
|
|
180
|
+
if (!inactive()) {
|
|
181
|
+
motion.set(motionValues(values));
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
get() {
|
|
185
|
+
return snapshot(channel => motion.get(channel));
|
|
186
|
+
},
|
|
187
|
+
getVelocity() {
|
|
188
|
+
return snapshot(channel => motion.getVelocity(channel));
|
|
189
|
+
},
|
|
190
|
+
stop() {
|
|
191
|
+
if (!inactive()) {
|
|
192
|
+
motion.stop();
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
destroy() {
|
|
196
|
+
if (!destroyed) {
|
|
197
|
+
destroyed = true;
|
|
198
|
+
// Halts every channel where it shows, so the element keeps those
|
|
199
|
+
// values and nothing reads as moving, then lets go of it.
|
|
200
|
+
motion.stop();
|
|
201
|
+
motion.dispose();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
return {
|
|
206
|
+
animator,
|
|
207
|
+
attach: next => {
|
|
208
|
+
if (destroyed || next === element) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (element != null) {
|
|
212
|
+
// The old node takes its values with it, so the next node, or a
|
|
213
|
+
// remount, starts at rest.
|
|
214
|
+
motion.attach(null);
|
|
215
|
+
motion.stop();
|
|
216
|
+
motion.set(motionValues(REST));
|
|
217
|
+
}
|
|
218
|
+
element = next;
|
|
219
|
+
motion.attach(next);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** A new or remounted element starts from these values. */
|
|
225
|
+
const REST = {
|
|
226
|
+
x: 0,
|
|
227
|
+
y: 0,
|
|
228
|
+
scale: 1,
|
|
229
|
+
rotate: 0,
|
|
230
|
+
opacity: 1
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Animates `element` with the motion Core uses. `animate` springs channels to
|
|
235
|
+
* a target on the state-change motion, and `decay` coasts them from a release
|
|
236
|
+
* velocity. `set` jumps them, `get` and `getVelocity` read them, `stop` halts
|
|
237
|
+
* them, and `destroy` lets the element go. Under Reduce Motion every call is
|
|
238
|
+
* planned. A move becomes a short cross-fade on a touch screen, and it lands
|
|
239
|
+
* at once with a mouse.
|
|
240
|
+
*
|
|
241
|
+
* Use one animator per element: two on one element overwrite each other.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```
|
|
245
|
+
* const card = document.querySelector('.card');
|
|
246
|
+
* const animator = createAnimator(card);
|
|
247
|
+
* await animator.animate({x: 0, rotate: 0}, {velocity: {x: -900}});
|
|
248
|
+
* animator.decay({x: 2400});
|
|
249
|
+
* animator.destroy();
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
export function createAnimator(element) {
|
|
253
|
+
const {
|
|
254
|
+
animator,
|
|
255
|
+
attach
|
|
256
|
+
} = createAttachableAnimator();
|
|
257
|
+
attach(element);
|
|
258
|
+
return animator;
|
|
259
|
+
}
|