@box/blueprint-web 18.8.0 → 18.9.1

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.
@@ -0,0 +1,112 @@
1
+ import { useState, useRef, useCallback, useEffect } from 'react';
2
+ import { noop } from './noop.js';
3
+ import { useEnhancedEffect } from './useEnhancedEffect.js';
4
+
5
+ /**
6
+ * Stamp this on the animated node together with `key={animationGeneration}`.
7
+ * A completion whose stamp does not match the current generation is ignored.
8
+ */
9
+ const ONE_SHOT_ANIMATION_GENERATION_ATTRIBUTE = 'data-bp-animation-generation';
10
+ const animationEndNoop = noop;
11
+ const readStampedAnimationGeneration = event => {
12
+ const {
13
+ currentTarget
14
+ } = event;
15
+ if (!(currentTarget instanceof Element)) {
16
+ return null;
17
+ }
18
+ const generation = currentTarget.getAttribute(ONE_SHOT_ANIMATION_GENERATION_ATTRIBUTE);
19
+ if (generation === null) {
20
+ return null;
21
+ }
22
+ return Number(generation);
23
+ };
24
+ /**
25
+ * Replays a CSS animation when `trigger` changes by `Object.is`.
26
+ *
27
+ * CSS will not restart while `data-state` stays `'playing'`, so this hook
28
+ * sets `'playing'` on a trigger change and returns to `'idle'` on
29
+ * `animationend`.
30
+ *
31
+ * Each accepted trigger change bumps `animationGeneration`. When remounting is
32
+ * safe, key the animated node with that value and set
33
+ * `data-bp-animation-generation` to the same number so an `animationend` from
34
+ * the previous node cannot idle the new one.
35
+ *
36
+ * If the trigger changes while already playing, the hook goes to `'idle'` first,
37
+ * then `'playing'` again. Use that path when the node must stay mounted (for
38
+ * example a text field that should keep focus and input). The idle update is
39
+ * flushed before paint; a follow-up effect sets `'playing'`.
40
+ *
41
+ * A same-value resubmit (for example the same validation error twice) does not
42
+ * change `trigger` by `Object.is`, so it will not replay. Pass a nonce as
43
+ * `trigger` when you need another play without a value change.
44
+ *
45
+ * A rerender that keeps the same `trigger` after idle does not auto-play.
46
+ */
47
+ function useOneShotAnimation(trigger, isEnabled) {
48
+ const [animationState, setAnimationState] = useState('idle');
49
+ const [animationGeneration, setAnimationGeneration] = useState(0);
50
+ const previousTriggerRef = useRef(trigger);
51
+ const animationGenerationRef = useRef(0);
52
+ const animationStateRef = useRef(animationState);
53
+ const restartAfterIdleRef = useRef(false);
54
+ animationStateRef.current = animationState;
55
+ const onAnimationEnd = useCallback(event => {
56
+ if (event.target !== event.currentTarget || animationState !== 'playing') {
57
+ return;
58
+ }
59
+ // This handler was created for a generation that a newer trigger replaced.
60
+ if (animationGeneration !== animationGenerationRef.current) {
61
+ return;
62
+ }
63
+ const stampedGeneration = readStampedAnimationGeneration(event);
64
+ // Unstamped callers (no keyed node) keep the previous completion behavior.
65
+ if (stampedGeneration !== null && stampedGeneration !== animationGenerationRef.current) {
66
+ return;
67
+ }
68
+ setAnimationState('idle');
69
+ }, [animationGeneration, animationState]);
70
+ useEnhancedEffect(() => {
71
+ if (!isEnabled) {
72
+ previousTriggerRef.current = trigger;
73
+ restartAfterIdleRef.current = false;
74
+ setAnimationState('idle');
75
+ return;
76
+ }
77
+ if (Object.is(previousTriggerRef.current, trigger)) {
78
+ return;
79
+ }
80
+ previousTriggerRef.current = trigger;
81
+ animationGenerationRef.current += 1;
82
+ setAnimationGeneration(animationGenerationRef.current);
83
+ if (animationStateRef.current === 'playing') {
84
+ restartAfterIdleRef.current = true;
85
+ setAnimationState('idle');
86
+ return;
87
+ }
88
+ restartAfterIdleRef.current = false;
89
+ setAnimationState('playing');
90
+ }, [isEnabled, trigger]);
91
+ useEffect(() => {
92
+ if (!restartAfterIdleRef.current || animationState !== 'idle') {
93
+ return;
94
+ }
95
+ restartAfterIdleRef.current = false;
96
+ setAnimationState('playing');
97
+ }, [animationState]);
98
+ if (!isEnabled) {
99
+ return {
100
+ animationState: 'idle',
101
+ animationGeneration,
102
+ onAnimationEnd: animationEndNoop
103
+ };
104
+ }
105
+ return {
106
+ animationState,
107
+ animationGeneration,
108
+ onAnimationEnd
109
+ };
110
+ }
111
+
112
+ export { ONE_SHOT_ANIMATION_GENERATION_ATTRIBUTE, useOneShotAnimation };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@box/blueprint-web",
3
- "version": "18.8.0",
3
+ "version": "18.9.1",
4
4
  "type": "module",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "publishConfig": {
@@ -55,7 +55,7 @@
55
55
  "dependencies": {
56
56
  "@ariakit/react": "0.4.21",
57
57
  "@ariakit/react-core": "0.4.21",
58
- "@box/blueprint-web-assets": "^5.13.7",
58
+ "@box/blueprint-web-assets": "^5.13.9",
59
59
  "@internationalized/date": "^3.12.0",
60
60
  "@radix-ui/react-accordion": "1.1.2",
61
61
  "@radix-ui/react-checkbox": "1.0.4",
@@ -85,7 +85,7 @@
85
85
  "devDependencies": {
86
86
  "@box/box-test-client": "^3.2.7",
87
87
  "@box/playwright-utils": "^2.8.5",
88
- "@box/storybook-utils": "^1.4.26",
88
+ "@box/storybook-utils": "^1.4.28",
89
89
  "@figma/code-connect": "1.4.4",
90
90
  "@playwright/test": "1.46.1",
91
91
  "@types/js-yaml": "^4.0.9",