@lynx-js/lynx-ui-presence 3.133.1 → 3.134.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @lynx-js/lynx-ui-presence
2
2
 
3
+ ## 3.134.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix presence edge cases for rapid visibility toggles during enter and exit transitions: ([#204](https://github.com/lynx-family/lynx-ui/pull/204))
8
+
9
+ - Recover when an enter interrupts an in-progress exit.
10
+ - Keep open and close callbacks balanced across interrupted transitions.
11
+ - Remove mounted containers when dismissal occurs before entering starts.
12
+ - Handle cancelled Dialog transitions during rapid visibility changes.
13
+
3
14
  ## 3.133.1
4
15
 
5
16
  ### Patch Changes
@@ -1 +1 @@
1
- {"version":3,"file":"usePresence.d.ts","sourceRoot":"","sources":["../src/usePresence.tsx"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,uBAAuB,EACvB,mBAAmB,EACnB,aAAa,EACb,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,SAAS,CAAA;AAGhB,eAAO,MAAM,eAAe,8CAA4C,CAAA;AAExE,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,qBAAqB,CAiS1E;AAED,eAAO,MAAM,sBAAsB,GACjC,OAAO;IACL,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,CAAA;IACnC,MAAM,EAAE,uBAAuB,CAAA;CAChC,8BAMF,CAAA"}
1
+ {"version":3,"file":"usePresence.d.ts","sourceRoot":"","sources":["../src/usePresence.tsx"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,uBAAuB,EACvB,mBAAmB,EACnB,aAAa,EACb,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,SAAS,CAAA;AAGhB,eAAO,MAAM,eAAe,8CAA4C,CAAA;AAExE,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,qBAAqB,CAoV1E;AAED,eAAO,MAAM,sBAAsB,GACjC,OAAO;IACL,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,CAAA;IACnC,MAAM,EAAE,uBAAuB,CAAA;CAChC,8BAMF,CAAA"}
@@ -9,6 +9,7 @@ function usePresence(props) {
9
9
  const isTransitionAnimating = useRef(false);
10
10
  const isKFAnimating = useRef(false);
11
11
  const isInitialRender = useRef(true);
12
+ const hasNotifiedOpen = useRef(false);
12
13
  const showRef = useRef(show);
13
14
  const [mount, setMount] = useState(false);
14
15
  const enteringLoopIdRef = useRef(0);
@@ -18,13 +19,47 @@ function usePresence(props) {
18
19
  const leavingWaitFramesRef = useRef(0);
19
20
  const MAX_WAIT_FRAMES = 24;
20
21
  showRef.current = show;
22
+ const cancelShowTimer = ()=>{
23
+ showScheduleIdRef.current += 1;
24
+ };
25
+ const cancelEnteringWait = ()=>{
26
+ enteringLoopIdRef.current += 1;
27
+ };
28
+ const cancelLeavingWait = ()=>{
29
+ leavingLoopIdRef.current += 1;
30
+ };
31
+ const scheduleShow = ()=>{
32
+ const scheduleId = showScheduleIdRef.current;
33
+ log(debugLog, '[lynx-ui-presence][usePresence] set mount=true');
34
+ setMount(true);
35
+ log(debugLog, '[lynx-ui-presence][usePresence] schedule set Entering in 8 frames');
36
+ delayFrames(8, ()=>{
37
+ if (scheduleId !== showScheduleIdRef.current || !showRef.current) return;
38
+ setPresenceState(PresenceState.Entering);
39
+ });
40
+ if (enableDelay) {
41
+ log(debugLog, '[lynx-ui-presence][usePresence] schedule set DelayedEntering in 16 frames');
42
+ delayFrames(16, ()=>{
43
+ if (scheduleId !== showScheduleIdRef.current || !showRef.current) return;
44
+ setPresenceState(PresenceState.DelayedEntering);
45
+ });
46
+ }
47
+ };
48
+ const restartShow = ()=>{
49
+ cancelShowTimer();
50
+ scheduleShow();
51
+ };
21
52
  const notAnimating = ()=>false === isKFAnimating.current && false === isTransitionAnimating.current;
22
53
  const handleAnimationStart = ()=>{
23
54
  log(debugLog, `[lynx-ui-presence][usePresence] handleAnimationStart state:${state}, show:${show}, isTransition:${isTransitionAnimating.current}, isKFAnimating:${isKFAnimating.current}`);
24
55
  };
25
56
  const handleAnimationEnd = ()=>{
26
57
  if ((state === PresenceState.Entering || state === PresenceState.DelayedEntering) && notAnimating()) showRef.current ? setPresenceState(PresenceState.Entered) : setPresenceState(PresenceState.Leaving);
27
- if (state === PresenceState.Leaving && notAnimating()) setPresenceState(PresenceState.Left);
58
+ if (state === PresenceState.Leaving && notAnimating()) {
59
+ cancelLeavingWait();
60
+ if (showRef.current) restartShow();
61
+ else setPresenceState(PresenceState.Left);
62
+ }
28
63
  };
29
64
  const handleKFStart = ()=>{
30
65
  isKFAnimating.current = true;
@@ -61,14 +96,25 @@ function usePresence(props) {
61
96
  handleAnimationEnd();
62
97
  };
63
98
  const handleStateEntered = ()=>{
99
+ if (hasNotifiedOpen.current) return;
100
+ hasNotifiedOpen.current = true;
64
101
  onOpen?.();
65
102
  };
66
103
  const handleStateLeft = ()=>{
67
- if (!isInitialRender.current) onClose?.();
104
+ if (showRef.current) {
105
+ log(debugLog, '[lynx-ui-presence][usePresence] skip mount=false because show=true (Left)');
106
+ restartShow();
107
+ return;
108
+ }
109
+ if (!isInitialRender.current && hasNotifiedOpen.current) {
110
+ hasNotifiedOpen.current = false;
111
+ onClose?.();
112
+ }
68
113
  log(debugLog, '[lynx-ui-presence][usePresence] set mount=false (Left)');
69
114
  setMount(false);
70
115
  };
71
116
  const handleStateLeaving = ()=>{
117
+ cancelEnteringWait();
72
118
  leavingWaitFramesRef.current = 0;
73
119
  leavingLoopIdRef.current += 1;
74
120
  const loopId = leavingLoopIdRef.current;
@@ -78,7 +124,8 @@ function usePresence(props) {
78
124
  if (!notAnimating()) return;
79
125
  if (leavingWaitFramesRef.current >= MAX_WAIT_FRAMES) {
80
126
  log(debugLog, `[lynx-ui-presence][usePresence] leaving timeout reached, loopId: ${loopId}, frames: ${leavingWaitFramesRef.current}`);
81
- setPresenceState(PresenceState.Left);
127
+ if (showRef.current) restartShow();
128
+ else setPresenceState(PresenceState.Left);
82
129
  return;
83
130
  }
84
131
  leavingWaitFramesRef.current += 1;
@@ -87,6 +134,7 @@ function usePresence(props) {
87
134
  delayFrames(1, tryLeft);
88
135
  };
89
136
  const handleStateEnteringWithDelay = ()=>{
137
+ cancelLeavingWait();
90
138
  enteringWaitFramesRef.current = 0;
91
139
  enteringLoopIdRef.current += 1;
92
140
  const loopId = enteringLoopIdRef.current;
@@ -96,7 +144,7 @@ function usePresence(props) {
96
144
  if (!notAnimating()) return;
97
145
  if (enteringWaitFramesRef.current >= MAX_WAIT_FRAMES) {
98
146
  log(debugLog, `[lynx-ui-presence][usePresence] entering timeout reached, loopId: ${loopId}, frames: ${enteringWaitFramesRef.current}`);
99
- setPresenceState(PresenceState.Entered);
147
+ showRef.current ? setPresenceState(PresenceState.Entered) : setPresenceState(PresenceState.Leaving);
100
148
  return;
101
149
  }
102
150
  enteringWaitFramesRef.current += 1;
@@ -113,37 +161,23 @@ function usePresence(props) {
113
161
  }, [
114
162
  state
115
163
  ]);
116
- const handleShow = (scheduleId)=>{
117
- log(debugLog, '[lynx-ui-presence][usePresence] set mount=true');
118
- setMount(true);
119
- log(debugLog, '[lynx-ui-presence][usePresence] schedule set Entering in 8 frames');
120
- delayFrames(8, ()=>{
121
- if (scheduleId !== showScheduleIdRef.current) return;
122
- setPresenceState(PresenceState.Entering);
123
- });
124
- if (enableDelay) {
125
- log(debugLog, '[lynx-ui-presence][usePresence] schedule set DelayedEntering in 16 frames');
126
- delayFrames(16, ()=>{
127
- if (scheduleId !== showScheduleIdRef.current) return;
128
- setPresenceState(PresenceState.DelayedEntering);
129
- });
130
- }
131
- };
132
164
  const handleDismiss = ()=>{
133
165
  if (state === PresenceState.Entered || state === PresenceState.Entering || state === PresenceState.DelayedEntering) {
134
166
  log(debugLog, '[lynx-ui-presence][usePresence] show=false -> set PresenceState.Leaving');
167
+ cancelEnteringWait();
135
168
  setPresenceState(PresenceState.Leaving);
136
- } else if (state === PresenceState.Initial && mount) {
137
- log(debugLog, '[lynx-ui-presence][usePresence] show=false in Initial -> set mount=false');
169
+ } else if ((state === PresenceState.Initial || state === PresenceState.Left) && mount) {
170
+ log(debugLog, '[lynx-ui-presence][usePresence] show=false before entering -> set mount=false');
138
171
  setMount(false);
139
172
  }
140
173
  };
141
174
  useEffect(()=>{
142
175
  log(debugLog, `[lynx-ui-presence][usePresence] show effect show:${show}, enableDelay:${enableDelay}, state:${state}, mount:${mount}`);
143
- showScheduleIdRef.current += 1;
144
- const scheduleId = showScheduleIdRef.current;
145
- if (show) handleShow(scheduleId);
146
- else handleDismiss();
176
+ if (show) restartShow();
177
+ else {
178
+ cancelShowTimer();
179
+ handleDismiss();
180
+ }
147
181
  }, [
148
182
  show,
149
183
  enableDelay
@@ -1 +1 @@
1
- {"version":3,"file":"usePresence.jsx","sources":["webpack://@lynx-js/lynx-ui-presence/./src/usePresence.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { createContext, useEffect, useRef, useState } from '@lynx-js/react'\n\nimport { delayFrames, log } from '@lynx-js/lynx-ui-common'\nimport type { AnimationEvent, TransitionEvent } from '@lynx-js/types'\n\nimport type {\n PresenceAnimationStatus,\n PresenceContextType,\n PresenceProps,\n usePresenceProps,\n usePresenceReturnType,\n} from './types'\nimport { PresenceState } from './utils'\n\nexport const PresenceContext = createContext<PresenceContextType>(null!)\n\nexport function usePresence(props: usePresenceProps): usePresenceReturnType {\n const {\n show,\n state,\n enableDelay = false,\n setPresenceState,\n onOpen,\n onClose,\n debugLog = false,\n } = props\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] init, show: ${show}, state: ${state}, enableDelay: ${enableDelay}`,\n )\n const enteringStateWithDelay = enableDelay\n ? PresenceState.DelayedEntering\n : PresenceState.Entering\n\n const isTransitionAnimating = useRef<boolean>(false)\n const isKFAnimating = useRef<boolean>(false)\n // Track the initial render, to prevent effects from running on mount.\n const isInitialRender = useRef(true)\n const showRef = useRef(show)\n // The core state to control the mounting and unmounting of the component.\n const [mount, setMount] = useState<boolean>(false)\n // These three id record the loop ID for the entering progress animation and show state to prevent race conditions.\n const enteringLoopIdRef = useRef<number>(0)\n const leavingLoopIdRef = useRef<number>(0)\n const showScheduleIdRef = useRef(0)\n // Two timing windows to wait for the animation to start before forcing a state change.\n const enteringWaitFramesRef = useRef<number>(0)\n const leavingWaitFramesRef = useRef<number>(0)\n\n // The maximum number of frames to wait for an animation to start before forcing a state change.\n const MAX_WAIT_FRAMES = 24\n\n showRef.current = show\n\n const notAnimating = () => (isKFAnimating.current === false\n && isTransitionAnimating.current === false)\n\n const handleAnimationStart = () => {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] handleAnimationStart state:${state}, show:${show}, isTransition:${isTransitionAnimating.current}, isKFAnimating:${isKFAnimating.current}`,\n )\n }\n\n const handleAnimationEnd = () => {\n // still need to check if the other kind of animation is running.\n if (\n (state === PresenceState.Entering\n || state === PresenceState.DelayedEntering)\n && notAnimating()\n ) {\n if (showRef.current) {\n setPresenceState(PresenceState.Entered)\n } else {\n // Safeguard: entering animation may still fire after show turns false.\n // Why not Left? Because Left will also cancel the ongoing leaving animation.\n setPresenceState(PresenceState.Leaving)\n }\n }\n if (state === PresenceState.Leaving && notAnimating()) {\n setPresenceState(PresenceState.Left)\n }\n }\n\n // ==== Animation events =====\n const handleKFStart = () => {\n isKFAnimating.current = true\n enteringLoopIdRef.current += 1\n leavingLoopIdRef.current += 1\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] KF start, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationStart()\n }\n\n const handleTransitionStart = () => {\n isTransitionAnimating.current = true\n enteringLoopIdRef.current += 1\n leavingLoopIdRef.current += 1\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] Transition start, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationStart()\n }\n\n const handleKFEnd = (_e: AnimationEvent) => {\n isKFAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] KF end, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n const handleKFCancel = () => {\n isKFAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] KF cancel, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n const handleTransitionCancel = () => {\n isTransitionAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] Transition cancel, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n const handleTransitionEnd = (_e: TransitionEvent) => {\n isTransitionAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] Transition end, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n // ==== state controller ====\n const handleStateEntered = () => {\n onOpen?.()\n }\n\n const handleStateLeft = () => {\n if (!isInitialRender.current) {\n onClose?.()\n }\n log(debugLog, '[lynx-ui-presence][usePresence] set mount=false (Left)')\n setMount(false)\n }\n\n const handleStateLeaving = () => {\n leavingWaitFramesRef.current = 0\n leavingLoopIdRef.current += 1\n const loopId = leavingLoopIdRef.current\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] leaving loop scheduled, loopId: ${loopId}`,\n )\n const tryLeft = () => {\n // The loopId is inconsistent with the current animation state, means the animation started during the delay period, so we should stop this trying.\n if (loopId !== leavingLoopIdRef.current) return\n if (!notAnimating()) return\n // timeout reached => treat as no animation\n if (leavingWaitFramesRef.current >= MAX_WAIT_FRAMES) {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] leaving timeout reached, loopId: ${loopId}, frames: ${leavingWaitFramesRef.current}`,\n )\n setPresenceState(PresenceState.Left)\n return\n }\n leavingWaitFramesRef.current += 1\n delayFrames(1, tryLeft)\n }\n\n delayFrames(1, tryLeft)\n }\n\n const handleStateEnteringWithDelay = () => {\n enteringWaitFramesRef.current = 0\n enteringLoopIdRef.current += 1\n const loopId = enteringLoopIdRef.current\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] entering loop scheduled, loopId: ${loopId}`,\n )\n const tryEntered = () => {\n // The loopId is inconsistent with the current animation state, means the animation started during the delay period, so we should stop this trying.\n if (loopId !== enteringLoopIdRef.current) return\n if (!notAnimating()) return\n if (enteringWaitFramesRef.current >= MAX_WAIT_FRAMES) {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] entering timeout reached, loopId: ${loopId}, frames: ${enteringWaitFramesRef.current}`,\n )\n setPresenceState(PresenceState.Entered)\n return\n }\n enteringWaitFramesRef.current += 1\n delayFrames(1, tryEntered)\n }\n\n delayFrames(1, tryEntered)\n }\n\n useEffect(() => {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] state effect, state: ${state}, show: ${show}, enableDelay: ${enableDelay}, isTransitionAnimating: ${isTransitionAnimating.current}, isKFAnimating: ${isKFAnimating.current}`,\n )\n if (state === PresenceState.Entered) {\n handleStateEntered()\n }\n if (state === PresenceState.Left) {\n handleStateLeft()\n }\n if (state === PresenceState.Leaving) {\n handleStateLeaving()\n }\n if (state === enteringStateWithDelay) {\n handleStateEnteringWithDelay()\n }\n }, [state])\n\n // ==== Show change part ====\n const handleShow = (scheduleId: number) => {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] set mount=true',\n )\n setMount(true)\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] schedule set Entering in 8 frames',\n )\n delayFrames(8, () => {\n if (scheduleId !== showScheduleIdRef.current) return\n setPresenceState(PresenceState.Entering)\n })\n if (enableDelay) {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] schedule set DelayedEntering in 16 frames',\n )\n delayFrames(16, () => {\n if (scheduleId !== showScheduleIdRef.current) return\n setPresenceState(PresenceState.DelayedEntering)\n })\n }\n }\n\n const handleDismiss = () => {\n if (\n state === PresenceState.Entered || state === PresenceState.Entering\n || state === PresenceState.DelayedEntering\n ) {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] show=false -> set PresenceState.Leaving',\n )\n setPresenceState(PresenceState.Leaving)\n } else if (state === PresenceState.Initial && mount) {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] show=false in Initial -> set mount=false',\n )\n setMount(false)\n }\n }\n\n useEffect(() => {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] show effect show:${show}, enableDelay:${enableDelay}, state:${state}, mount:${mount}`,\n )\n showScheduleIdRef.current += 1\n const scheduleId = showScheduleIdRef.current\n if (show) {\n handleShow(scheduleId)\n } else {\n handleDismiss()\n }\n }, [show, enableDelay])\n\n useEffect(() => {\n isInitialRender.current = false\n }, [])\n\n return {\n controllers: { state, mount, setPresenceState },\n animationHandlers: {\n handleKFStart,\n handleKFCancel,\n handleKFEnd,\n handleTransitionStart,\n handleTransitionCancel,\n handleTransitionEnd,\n },\n }\n}\n\nexport const renderPresenceChildren = (\n props: {\n children: PresenceProps['children']\n status: PresenceAnimationStatus\n },\n) => {\n const { children, status } = props\n return typeof children === 'function'\n ? children(status)\n : children\n}\n"],"names":["PresenceContext","createContext","usePresence","props","show","state","enableDelay","setPresenceState","onOpen","onClose","debugLog","log","enteringStateWithDelay","PresenceState","isTransitionAnimating","useRef","isKFAnimating","isInitialRender","showRef","mount","setMount","useState","enteringLoopIdRef","leavingLoopIdRef","showScheduleIdRef","enteringWaitFramesRef","leavingWaitFramesRef","MAX_WAIT_FRAMES","notAnimating","handleAnimationStart","handleAnimationEnd","handleKFStart","handleTransitionStart","handleKFEnd","_e","handleKFCancel","handleTransitionCancel","handleTransitionEnd","handleStateEntered","handleStateLeft","handleStateLeaving","loopId","tryLeft","delayFrames","handleStateEnteringWithDelay","tryEntered","useEffect","handleShow","scheduleId","handleDismiss","renderPresenceChildren","children","status"],"mappings":";;;AAkBO,MAAMA,kBAAkBC,cAAmC;AAE3D,SAASC,YAAYC,KAAuB;IACjD,MAAM,EACJC,IAAI,EACJC,KAAK,EACLC,cAAc,KAAK,EAC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ACd,IAAIK,eAAexB,kBAAkB,OAAO,EAAE;gBAC9CjB,iBAAiBM,cAAc,eAAe;YAChD;QACF;IACF;IAEA,MAAMoC,gBAAgB;QACpB,IACE5C,UAAUQ,cAAc,OAAO,IAAIR,UAAUQ,cAAc,QAAQ,IAChER,UAAUQ,cAAc,eAAe,EAC1C;YACAF,IACED,UACA;YAEFH,iBAAiBM,cAAc,OAAO;QACxC,OAAO,IAAIR,UAAUQ,cAAc,OAAO,IAAIM,OAAO;YACnDR,IACED,UACA;YAEFU,SAAS;QACX;IACF;IAEA0B,UAAU;QACRnC,IACED,UACA,CAAC,iDAAiD,EAAEN,KAAK,cAAc,EAAEE,YAAY,QAAQ,EAAED,MAAM,QAAQ,EAAEc,OAAO;QAExHK,kBAAkB,OAAO,IAAI;QAC7B,MAAMwB,aAAaxB,kBAAkB,OAAO;QAC5C,IAAIpB,MACF2C,WAAWC;aAEXC;IAEJ,GAAG;QAAC7C;QAAME;KAAY;IAEtBwC,UAAU;QACR7B,gBAAgB,OAAO,GAAG;IAC5B,GAAG,EAAE;IAEL,OAAO;QACL,aAAa;YAAEZ;YAAOc;YAAOZ;QAAiB;QAC9C,mBAAmB;YACjBwB;YACAI;YACAF;YACAD;YACAI;YACAC;QACF;IACF;AACF;AAEO,MAAMa,yBAAyB,CACpC/C;IAKA,MAAM,EAAEgD,QAAQ,EAAEC,MAAM,EAAE,GAAGjD;IAC7B,OAAO,AAAoB,cAApB,OAAOgD,WACVA,SAASC,UACTD;AACN"}
1
+ {"version":3,"file":"usePresence.jsx","sources":["webpack://@lynx-js/lynx-ui-presence/./src/usePresence.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { createContext, useEffect, useRef, useState } from '@lynx-js/react'\n\nimport { delayFrames, log } from '@lynx-js/lynx-ui-common'\nimport type { AnimationEvent, TransitionEvent } from '@lynx-js/types'\n\nimport type {\n PresenceAnimationStatus,\n PresenceContextType,\n PresenceProps,\n usePresenceProps,\n usePresenceReturnType,\n} from './types'\nimport { PresenceState } from './utils'\n\nexport const PresenceContext = createContext<PresenceContextType>(null!)\n\nexport function usePresence(props: usePresenceProps): usePresenceReturnType {\n const {\n show,\n state,\n enableDelay = false,\n setPresenceState,\n onOpen,\n onClose,\n debugLog = false,\n } = props\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] init, show: ${show}, state: ${state}, enableDelay: ${enableDelay}`,\n )\n const enteringStateWithDelay = enableDelay\n ? PresenceState.DelayedEntering\n : PresenceState.Entering\n\n const isTransitionAnimating = useRef<boolean>(false)\n const isKFAnimating = useRef<boolean>(false)\n // Track the initial render, to prevent effects from running on mount.\n const isInitialRender = useRef(true)\n const hasNotifiedOpen = useRef(false)\n const showRef = useRef(show)\n // The core state to control the mounting and unmounting of the component.\n const [mount, setMount] = useState<boolean>(false)\n // These three id record the loop ID for the entering progress animation and show state to prevent race conditions.\n const enteringLoopIdRef = useRef<number>(0)\n const leavingLoopIdRef = useRef<number>(0)\n const showScheduleIdRef = useRef(0)\n // Two timing windows to wait for the animation to start before forcing a state change.\n const enteringWaitFramesRef = useRef<number>(0)\n const leavingWaitFramesRef = useRef<number>(0)\n\n // The maximum number of frames to wait for an animation to start before forcing a state change.\n const MAX_WAIT_FRAMES = 24\n\n showRef.current = show\n\n const cancelShowTimer = () => {\n showScheduleIdRef.current += 1\n }\n\n const cancelEnteringWait = () => {\n enteringLoopIdRef.current += 1\n }\n\n const cancelLeavingWait = () => {\n leavingLoopIdRef.current += 1\n }\n\n const scheduleShow = () => {\n const scheduleId = showScheduleIdRef.current\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] set mount=true',\n )\n setMount(true)\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] schedule set Entering in 8 frames',\n )\n delayFrames(8, () => {\n if (scheduleId !== showScheduleIdRef.current || !showRef.current) return\n setPresenceState(PresenceState.Entering)\n })\n if (enableDelay) {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] schedule set DelayedEntering in 16 frames',\n )\n delayFrames(16, () => {\n if (scheduleId !== showScheduleIdRef.current || !showRef.current) {\n return\n }\n setPresenceState(PresenceState.DelayedEntering)\n })\n }\n }\n\n const restartShow = () => {\n cancelShowTimer()\n scheduleShow()\n }\n\n const notAnimating = () => (isKFAnimating.current === false\n && isTransitionAnimating.current === false)\n\n const handleAnimationStart = () => {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] handleAnimationStart state:${state}, show:${show}, isTransition:${isTransitionAnimating.current}, isKFAnimating:${isKFAnimating.current}`,\n )\n }\n\n const handleAnimationEnd = () => {\n // still need to check if the other kind of animation is running.\n if (\n (state === PresenceState.Entering\n || state === PresenceState.DelayedEntering)\n && notAnimating()\n ) {\n if (showRef.current) {\n setPresenceState(PresenceState.Entered)\n } else {\n // Safeguard: entering animation may still fire after show turns false.\n // Why not Left? Because Left will also cancel the ongoing leaving animation.\n setPresenceState(PresenceState.Leaving)\n }\n }\n if (state === PresenceState.Leaving && notAnimating()) {\n cancelLeavingWait()\n if (showRef.current) {\n restartShow()\n } else {\n setPresenceState(PresenceState.Left)\n }\n }\n }\n\n // ==== Animation events =====\n const handleKFStart = () => {\n isKFAnimating.current = true\n enteringLoopIdRef.current += 1\n leavingLoopIdRef.current += 1\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] KF start, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationStart()\n }\n\n const handleTransitionStart = () => {\n isTransitionAnimating.current = true\n enteringLoopIdRef.current += 1\n leavingLoopIdRef.current += 1\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] Transition start, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationStart()\n }\n\n const handleKFEnd = (_e: AnimationEvent) => {\n isKFAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] KF end, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n const handleKFCancel = () => {\n isKFAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] KF cancel, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n const handleTransitionCancel = () => {\n isTransitionAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] Transition cancel, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n const handleTransitionEnd = (_e: TransitionEvent) => {\n isTransitionAnimating.current = false\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] Transition end, loopId: ${leavingLoopIdRef.current}`,\n )\n handleAnimationEnd()\n }\n\n // ==== state controller ====\n const handleStateEntered = () => {\n if (hasNotifiedOpen.current) {\n return\n }\n hasNotifiedOpen.current = true\n onOpen?.()\n }\n\n const handleStateLeft = () => {\n if (showRef.current) {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] skip mount=false because show=true (Left)',\n )\n restartShow()\n return\n }\n if (!isInitialRender.current && hasNotifiedOpen.current) {\n hasNotifiedOpen.current = false\n onClose?.()\n }\n log(debugLog, '[lynx-ui-presence][usePresence] set mount=false (Left)')\n setMount(false)\n }\n\n const handleStateLeaving = () => {\n cancelEnteringWait()\n leavingWaitFramesRef.current = 0\n leavingLoopIdRef.current += 1\n const loopId = leavingLoopIdRef.current\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] leaving loop scheduled, loopId: ${loopId}`,\n )\n const tryLeft = () => {\n // The loopId is inconsistent with the current animation state, means the animation started during the delay period, so we should stop this trying.\n if (loopId !== leavingLoopIdRef.current) return\n if (!notAnimating()) return\n // timeout reached => treat as no animation\n if (leavingWaitFramesRef.current >= MAX_WAIT_FRAMES) {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] leaving timeout reached, loopId: ${loopId}, frames: ${leavingWaitFramesRef.current}`,\n )\n if (showRef.current) {\n restartShow()\n } else {\n setPresenceState(PresenceState.Left)\n }\n return\n }\n leavingWaitFramesRef.current += 1\n delayFrames(1, tryLeft)\n }\n\n delayFrames(1, tryLeft)\n }\n\n const handleStateEnteringWithDelay = () => {\n cancelLeavingWait()\n enteringWaitFramesRef.current = 0\n enteringLoopIdRef.current += 1\n const loopId = enteringLoopIdRef.current\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] entering loop scheduled, loopId: ${loopId}`,\n )\n const tryEntered = () => {\n // The loopId is inconsistent with the current animation state, means the animation started during the delay period, so we should stop this trying.\n if (loopId !== enteringLoopIdRef.current) return\n if (!notAnimating()) return\n if (enteringWaitFramesRef.current >= MAX_WAIT_FRAMES) {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] entering timeout reached, loopId: ${loopId}, frames: ${enteringWaitFramesRef.current}`,\n )\n if (showRef.current) {\n setPresenceState(PresenceState.Entered)\n } else {\n setPresenceState(PresenceState.Leaving)\n }\n return\n }\n enteringWaitFramesRef.current += 1\n delayFrames(1, tryEntered)\n }\n\n delayFrames(1, tryEntered)\n }\n\n useEffect(() => {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] state effect, state: ${state}, show: ${show}, enableDelay: ${enableDelay}, isTransitionAnimating: ${isTransitionAnimating.current}, isKFAnimating: ${isKFAnimating.current}`,\n )\n if (state === PresenceState.Entered) {\n handleStateEntered()\n }\n if (state === PresenceState.Left) {\n handleStateLeft()\n }\n if (state === PresenceState.Leaving) {\n handleStateLeaving()\n }\n if (state === enteringStateWithDelay) {\n handleStateEnteringWithDelay()\n }\n }, [state])\n\n // ==== Show change part ====\n const handleDismiss = () => {\n if (\n state === PresenceState.Entered || state === PresenceState.Entering\n || state === PresenceState.DelayedEntering\n ) {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] show=false -> set PresenceState.Leaving',\n )\n cancelEnteringWait()\n setPresenceState(PresenceState.Leaving)\n } else if (\n (state === PresenceState.Initial || state === PresenceState.Left) && mount\n ) {\n log(\n debugLog,\n '[lynx-ui-presence][usePresence] show=false before entering -> set mount=false',\n )\n setMount(false)\n }\n }\n\n useEffect(() => {\n log(\n debugLog,\n `[lynx-ui-presence][usePresence] show effect show:${show}, enableDelay:${enableDelay}, state:${state}, mount:${mount}`,\n )\n if (show) {\n restartShow()\n } else {\n cancelShowTimer()\n handleDismiss()\n }\n }, [show, enableDelay])\n\n useEffect(() => {\n isInitialRender.current = false\n }, [])\n\n return {\n controllers: { state, mount, setPresenceState },\n animationHandlers: {\n handleKFStart,\n handleKFCancel,\n handleKFEnd,\n handleTransitionStart,\n handleTransitionCancel,\n handleTransitionEnd,\n },\n }\n}\n\nexport const renderPresenceChildren = (\n props: {\n children: PresenceProps['children']\n status: PresenceAnimationStatus\n },\n) => {\n const { children, status } = props\n return typeof children === 'function'\n ? children(status)\n : children\n}\n"],"names":["PresenceContext","createContext","usePresence","props","show","state","enableDelay","setPresenceState","onOpen","onClose","debugLog","log","enteringStateWithDelay","PresenceState","isTransitionAnimating","useRef","isKFAnimating","isInitialRender","hasNotifiedOpen","showRef","mount","setMount","useState","enteringLoopIdRef","leavingLoopIdRef","showScheduleIdRef","enteringWaitFramesRef","leavingWaitFramesRef","MAX_WAIT_FRAMES","cancelShowTimer","cancelEnteringWait","cancelLeavingWait","scheduleShow","scheduleId","delayFrames","restartShow","notAnimating","handleAnimationStart","handleAnimationEnd","handleKFStart","handleTransitionStart","handleKFEnd","_e","handleKFCancel","handleTransitionCancel","handleTransitionEnd","handleStateEntered","handleStateLeft","handleStateLeaving","loopId","tryLeft","handleStateEnteringWithDelay","tryEntered","useEffect","handleDismiss","renderPresenceChildren","children","status"],"mappings":";;;AAkBO,MAAMA,kBAAkBC,cAAmC;AAE3D,SAASC,YAAYC,KAAuB;IACjD,MAAM,EACJC,IAAI,EACJC,KAAK,EACLC,cAAc,KAAK,EACnBC,gBAAgB,EAChBC,MAAM,EACNC,OAAO,EACPC,WAAW,KAAK,EACjB,GAAGP;IACJQ,IACED,UACA,CAAC,4CAA4C,EAAEN,KAAK,SAAS,EAAEC,MAAM,eAAe,EAAEC,aAAa;IAErG,MAAMM,yBAAyBN,cAC3BO,cAAc,e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;YACAI;YACAC;QACF;IACF;AACF;AAEO,MAAMU,yBAAyB,CACpCpD;IAKA,MAAM,EAAEqD,QAAQ,EAAEC,MAAM,EAAE,GAAGtD;IAC7B,OAAO,AAAoB,cAApB,OAAOqD,WACVA,SAASC,UACTD;AACN"}
@@ -1 +1 @@
1
- {"version":3,"file":"usePresenceGroup.d.ts","sourceRoot":"","sources":["../src/usePresenceGroup.tsx"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAEvC,eAAO,MAAM,eAAe,8CAA4C,CAAA;AAExE,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,OAAO,CAAA;IACb,UAAU,EAAE,OAAO,CAAA;IACnB,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAA;IAC9C,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,EACL,SAAS,GACT,oBAAoB,GACpB,KAAK,CAAC,SAAS,GAAG,oBAAoB,CAAC,CAAA;IAC3C,MAAM,CAAC,EAAE,MAAM,IAAI,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAA;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,MAAM,WAAW,0BAA0B;IACzC,cAAc,EAAE,SAAS,CAAA;IACzB,SAAS,EAAE,OAAO,CAAA;CACnB;AAGD,eAAO,MAAM,gBAAgB,EAAE,CAC7B,KAAK,EAAE,qBAAqB,KACzB,0BAiJJ,CAAA"}
1
+ {"version":3,"file":"usePresenceGroup.d.ts","sourceRoot":"","sources":["../src/usePresenceGroup.tsx"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAEvC,eAAO,MAAM,eAAe,8CAA4C,CAAA;AAExE,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,OAAO,CAAA;IACb,UAAU,EAAE,OAAO,CAAA;IACnB,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAA;IAC9C,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,EACL,SAAS,GACT,oBAAoB,GACpB,KAAK,CAAC,SAAS,GAAG,oBAAoB,CAAC,CAAA;IAC3C,MAAM,CAAC,EAAE,MAAM,IAAI,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAA;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,MAAM,WAAW,0BAA0B;IACzC,cAAc,EAAE,SAAS,CAAA;IACzB,SAAS,EAAE,OAAO,CAAA;CACnB;AAGD,eAAO,MAAM,gBAAgB,EAAE,CAC7B,KAAK,EAAE,qBAAqB,KACzB,0BAkJJ,CAAA"}
@@ -41,10 +41,7 @@ const usePresenceGroup = (props)=>{
41
41
  const onChildClose = useCallback(()=>{
42
42
  setMountingChildrenCount((prev)=>{
43
43
  const count = prev - 1;
44
- if (0 === count) {
45
- onClose?.();
46
- setMountView(false);
47
- }
44
+ if (0 === count) onClose?.();
48
45
  return count;
49
46
  });
50
47
  }, [
@@ -66,8 +63,10 @@ const usePresenceGroup = (props)=>{
66
63
  ]);
67
64
  useEffect(()=>{
68
65
  if (show) setMountView(true);
66
+ else if (stateGroup.every((state)=>state === PresenceState.Left)) setMountView(false);
69
67
  }, [
70
- show
68
+ show,
69
+ stateGroup
71
70
  ]);
72
71
  return {
73
72
  renderChildren,
@@ -1 +1 @@
1
- {"version":3,"file":"usePresenceGroup.jsx","sources":["webpack://@lynx-js/lynx-ui-presence/./src/usePresenceGroup.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport {\n createContext,\n useCallback,\n useEffect,\n useMemo,\n useState,\n} from '@lynx-js/react'\nimport type { ReactNode } from '@lynx-js/react'\n\nimport { Presence } from './Presence'\nimport type { PresenceChildrenType, PresenceContextType } from './types'\nimport { PresenceState } from './utils'\n\nexport const PresenceContext = createContext<PresenceContextType>(null!)\n\nexport interface usePresenceGroupProps {\n show: boolean\n forceMount: boolean\n setGroupState?: (state: PresenceState) => void\n enableDelay?: boolean\n children?:\n | ReactNode\n | PresenceChildrenType\n | Array<ReactNode | PresenceChildrenType>\n onOpen?: () => void\n onClose?: () => void\n debugLog?: boolean\n}\n\nexport interface usePresenceGroupReturnType {\n renderChildren: ReactNode\n mountView: boolean\n}\n// Manage a group of Presence components.\n// Wrap all child nodes with Presence and ensure that the onOpen and onClose events are triggered after all animations are completed.\nexport const usePresenceGroup: (\n props: usePresenceGroupProps,\n) => usePresenceGroupReturnType = (props) => {\n const {\n show,\n forceMount = false,\n children,\n enableDelay = false,\n onOpen,\n onClose,\n setGroupState,\n debugLog,\n } = props\n\n const [_, setMountingChildrenCount] = useState<number>(\n 0,\n )\n const [mountView, setMountView] = useState<boolean>(show)\n\n const childrenSize = Array.isArray(children)\n ? children.length\n : (children ? 1 : 0)\n const [stateGroup, setStateGroup] = useState<PresenceState[]>(\n Array.from({ length: childrenSize }, () => PresenceState.Left),\n )\n\n // Determine the overall state based on the stateGroup\n const getGroupState = (states: PresenceState[]): PresenceState => {\n // The state in Presence will stop the PresenceState from changing to DelayedEntering if the delay is disabled. No need to double check it in upper presence group.\n if (\n states.some(state => state === PresenceState.DelayedEntering)\n ) {\n return PresenceState.DelayedEntering\n }\n if (\n states.some(state => state === PresenceState.Entering)\n ) {\n return PresenceState.Entering\n }\n if (states.some(state => state === PresenceState.Leaving)) {\n return PresenceState.Leaving\n }\n if (states.every(state => state === PresenceState.Entered)) {\n return PresenceState.Entered\n }\n if (states.every(state => state === PresenceState.Left)) {\n return PresenceState.Left\n }\n return PresenceState.Left\n }\n\n const updateStateGroup = (index: number, newState: PresenceState) => {\n setStateGroup(prev => {\n const newStateGroup = [...prev]\n newStateGroup[index] = newState\n if (setGroupState) {\n setGroupState(getGroupState(newStateGroup))\n }\n return newStateGroup\n })\n }\n\n const onChildOpen = useCallback(() => {\n setMountingChildrenCount((prev) => {\n const count = prev + 1\n if (\n children\n && ((Array.isArray(children) && count === children.length)\n || (!Array.isArray(children) && count === 1))\n ) {\n onOpen?.()\n }\n return count\n })\n }, [children, onOpen])\n\n const onChildClose = useCallback(() => {\n setMountingChildrenCount((prev) => {\n const count = prev - 1\n if (count === 0) {\n onClose?.()\n setMountView(false)\n }\n return count\n })\n }, [onClose])\n\n const renderChildren = useMemo(() => {\n return Array.isArray(children)\n ? children.map(\n (child: ReactNode | PresenceChildrenType, index: number) => {\n return (\n <Presence\n key={index}\n show={show}\n forceMount={forceMount}\n state={stateGroup[index]}\n setPresenceState={(state: PresenceState) =>\n updateStateGroup(index, state)}\n enableDelay={enableDelay}\n onOpen={onChildOpen}\n onClose={onChildClose}\n debugLog={debugLog}\n >\n {child}\n </Presence>\n )\n },\n )\n : (children\n ? (\n <Presence\n show={show}\n forceMount={forceMount}\n state={stateGroup[0]}\n setPresenceState={(state: PresenceState) =>\n updateStateGroup(0, state)}\n enableDelay={enableDelay}\n onOpen={onChildOpen}\n onClose={onChildClose}\n debugLog={debugLog}\n >\n {children}\n </Presence>\n )\n : null)\n }, [\n show,\n children,\n stateGroup,\n onChildClose,\n onChildOpen,\n forceMount,\n enableDelay,\n debugLog,\n ])\n\n useEffect(() => {\n if (show) {\n setMountView(true)\n }\n }, [show])\n\n return {\n renderChildren,\n mountView,\n }\n}\n"],"names":["PresenceContext","createContext","usePresenceGroup","props","show","forceMount","children","enableDelay","onOpen","onClose","setGroupState","debugLog","_","setMountingChildrenCount","useState","mountView","setMountView","childrenSize","Array","stateGroup","setStateGroup","PresenceState","getGroupState","states","state","updateStateGroup","index","newState","prev","newStateGroup","onChildOpen","useCallback","count","onChildClose","renderChildren","useMemo","child","Presence","useEffect"],"mappings":";;;AAiBO,MAAMA,kBAAkBC,cAAmC;AAsB3D,MAAMC,mBAEqB,CAACC;IACjC,MAAM,EACJC,IAAI,EACJC,aAAa,KAAK,EAClBC,QAAQ,EACRC,cAAc,KAAK,EACnBC,MAAM,EACNC,OAAO,EACPC,aAAa,EACbC,QAAQ,EACT,GAAGR;IAEJ,MAAM,CAACS,GAAGC,yBAAyB,GAAGC,SACpC;IAEF,MAAM,CAACC,WAAWC,aAAa,GAAGF,SAAkBV;IAEpD,MAAMa,eAAeC,MAAM,OAAO,CAACZ,YAC/BA,SAAS,MAAM,GACdA,WAAW,IAAI;IACpB,MAAM,CAACa,YAAYC,cAAc,GAAGN,SAClCI,MAAM,IAAI,CAAC;QAAE,QAAQD;IAAa,GAAG,IAAMI,cAAc,IAAI;IAI/D,MAAMC,gBAAgB,CAACC;QAErB,IACEA,OAAO,IAAI,CAACC,CAAAA,QAASA,UAAUH,cAAc,eAAe,GAE5D,OAAOA,cAAc,eAAe;QAEtC,IACEE,OAAO,IAAI,CAACC,CAAAA,QAASA,UAAUH,cAAc,QAAQ,GAErD,OAAOA,cAAc,QAAQ;QAE/B,IAAIE,OAAO,IAAI,CAACC,CAAAA,QAASA,UAAUH,cAAc,OAAO,GACtD,OAAOA,cAAc,OAAO;QAE9B,IAAIE,OAAO,KAAK,CAACC,CAAAA,QAASA,UAAUH,cAAc,OAAO,GACvD,OAAOA,cAAc,OAAO;QAE1BE,OAAO,KAAK,CAACC,CAAAA,QAASA,UAAUH,cAAc,IAAI;QAGtD,OAAOA,cAAc,IAAI;IAC3B;IAEA,MAAMI,mBAAmB,CAACC,OAAeC;QACvCP,cAAcQ,CAAAA;YACZ,MAAMC,gBAAgB;mBAAID;aAAK;YAC/BC,aAAa,CAACH,MAAM,GAAGC;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pack://@lynx-js/lynx-ui-presence/./src/usePresenceGroup.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport {\n createContext,\n useCallback,\n useEffect,\n useMemo,\n useState,\n} from '@lynx-js/react'\nimport type { ReactNode } from '@lynx-js/react'\n\nimport { Presence } from './Presence'\nimport type { PresenceChildrenType, PresenceContextType } from './types'\nimport { PresenceState } from './utils'\n\nexport const PresenceContext = createContext<PresenceContextType>(null!)\n\nexport interface usePresenceGroupProps {\n show: boolean\n forceMount: boolean\n setGroupState?: (state: PresenceState) => void\n enableDelay?: boolean\n children?:\n | ReactNode\n | PresenceChildrenType\n | Array<ReactNode | PresenceChildrenType>\n onOpen?: () => void\n onClose?: () => void\n debugLog?: boolean\n}\n\nexport interface usePresenceGroupReturnType {\n renderChildren: ReactNode\n mountView: boolean\n}\n// Manage a group of Presence components.\n// Wrap all child nodes with Presence and ensure that the onOpen and onClose events are triggered after all animations are completed.\nexport const usePresenceGroup: (\n props: usePresenceGroupProps,\n) => usePresenceGroupReturnType = (props) => {\n const {\n show,\n forceMount = false,\n children,\n enableDelay = false,\n onOpen,\n onClose,\n setGroupState,\n debugLog,\n } = props\n\n const [_, setMountingChildrenCount] = useState<number>(\n 0,\n )\n const [mountView, setMountView] = useState<boolean>(show)\n\n const childrenSize = Array.isArray(children)\n ? children.length\n : (children ? 1 : 0)\n const [stateGroup, setStateGroup] = useState<PresenceState[]>(\n Array.from({ length: childrenSize }, () => PresenceState.Left),\n )\n\n // Determine the overall state based on the stateGroup\n const getGroupState = (states: PresenceState[]): PresenceState => {\n // The state in Presence will stop the PresenceState from changing to DelayedEntering if the delay is disabled. No need to double check it in upper presence group.\n if (\n states.some(state => state === PresenceState.DelayedEntering)\n ) {\n return PresenceState.DelayedEntering\n }\n if (\n states.some(state => state === PresenceState.Entering)\n ) {\n return PresenceState.Entering\n }\n if (states.some(state => state === PresenceState.Leaving)) {\n return PresenceState.Leaving\n }\n if (states.every(state => state === PresenceState.Entered)) {\n return PresenceState.Entered\n }\n if (states.every(state => state === PresenceState.Left)) {\n return PresenceState.Left\n }\n return PresenceState.Left\n }\n\n const updateStateGroup = (index: number, newState: PresenceState) => {\n setStateGroup(prev => {\n const newStateGroup = [...prev]\n newStateGroup[index] = newState\n if (setGroupState) {\n setGroupState(getGroupState(newStateGroup))\n }\n return newStateGroup\n })\n }\n\n const onChildOpen = useCallback(() => {\n setMountingChildrenCount((prev) => {\n const count = prev + 1\n if (\n children\n && ((Array.isArray(children) && count === children.length)\n || (!Array.isArray(children) && count === 1))\n ) {\n onOpen?.()\n }\n return count\n })\n }, [children, onOpen])\n\n const onChildClose = useCallback(() => {\n setMountingChildrenCount((prev) => {\n const count = prev - 1\n if (count === 0) {\n onClose?.()\n }\n return count\n })\n }, [onClose])\n\n const renderChildren = useMemo(() => {\n return Array.isArray(children)\n ? children.map(\n (child: ReactNode | PresenceChildrenType, index: number) => {\n return (\n <Presence\n key={index}\n show={show}\n forceMount={forceMount}\n state={stateGroup[index]}\n setPresenceState={(state: PresenceState) =>\n updateStateGroup(index, state)}\n enableDelay={enableDelay}\n onOpen={onChildOpen}\n onClose={onChildClose}\n debugLog={debugLog}\n >\n {child}\n </Presence>\n )\n },\n )\n : (children\n ? (\n <Presence\n show={show}\n forceMount={forceMount}\n state={stateGroup[0]}\n setPresenceState={(state: PresenceState) =>\n updateStateGroup(0, state)}\n enableDelay={enableDelay}\n onOpen={onChildOpen}\n onClose={onChildClose}\n debugLog={debugLog}\n >\n {children}\n </Presence>\n )\n : null)\n }, [\n show,\n children,\n stateGroup,\n onChildClose,\n onChildOpen,\n forceMount,\n enableDelay,\n debugLog,\n ])\n\n useEffect(() => {\n if (show) {\n setMountView(true)\n } else if (stateGroup.every(state => state === PresenceState.Left)) {\n setMountView(false)\n }\n }, [show, stateGroup])\n\n return {\n renderChildren,\n mountView,\n }\n}\n"],"names":["PresenceContext","createContext","usePresenceGroup","props","show","forceMount","children","enableDelay","onOpen","onClose","setGroupState","debugLog","_","setMountingChildrenCount","useState","mountView","setMountView","childrenSize","Array","stateGroup","setStateGroup","PresenceState","getGroupState","states","state","updateStateGroup","index","newState","prev","newStateGroup","onChildOpen","useCallback","count","onChildClose","renderChildren","useMemo","child","Presence","useEffect"],"mappings":";;;AAiBO,MAAMA,kBAAkBC,cAAmC;AAsB3D,MAAMC,mBAEqB,CAACC;IACjC,MAAM,EACJC,IAAI,EACJC,aAAa,KAAK,EAClBC,QAAQ,EACRC,cAAc,KAAK,EACnBC,MAAM,EACNC,OAAO,EACPC,aAAa,EACbC,QAAQ,EACT,GAAGR;IAEJ,MAAM,CAACS,GAAGC,yBAAyB,GAAGC,SACpC;IAEF,MAAM,CAACC,WAAWC,aAAa,GAAGF,SAAkBV;IAEpD,MAAMa,eAAeC,MAAM,OAAO,CAACZ,YAC/BA,SAAS,MAAM,GACdA,WAAW,IAAI;IACpB,MAAM,CAACa,YAAYC,cAAc,GAAGN,SAClCI,MAAM,IAAI,CAAC;QAAE,QAAQD;IAAa,GAAG,IAAMI,cAAc,IAAI;IAI/D,MAAMC,gBAAgB,CAACC;QAErB,IACEA,OAAO,IAAI,CAACC,CAAAA,QAASA,UAAUH,cAAc,eAAe,GAE5D,OAAOA,cAAc,eAAe;QAEtC,IACEE,OAAO,IAAI,CAACC,CAAAA,QAASA,UAAUH,cAAc,QAAQ,GAErD,OAAOA,cAAc,QAAQ;QAE/B,IAAIE,OAAO,IAAI,CAACC,CAAAA,QAASA,UAAUH,cAAc,OAAO,GACtD,OAAOA,cAAc,OAAO;QAE9B,IAAIE,OAAO,KAAK,CAACC,CAAAA,QAASA,UAAUH,cAAc,OAAO,GACvD,OAAOA,cAAc,OAAO;QAE1BE,OAAO,KAAK,CAACC,CAAAA,QAASA,UAAUH,cAAc,IAAI;QAGtD,OAAOA,cAAc,IAAI;IAC3B;IAEA,MAAMI,mBAAmB,CAACC,OAAeC;QACvCP,cAAcQ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,cAAc,IAAI,GAC/DL,aAAa;IAEjB,GAAG;QAACZ;QAAMe;KAAW;IAErB,OAAO;QACLe;QACAnB;IACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/lynx-ui-presence",
3
- "version": "3.133.1",
3
+ "version": "3.134.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/lynx-family/lynx-ui.git",
@@ -40,6 +40,7 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
40
40
  const isKFAnimating = useRef<boolean>(false)
41
41
  // Track the initial render, to prevent effects from running on mount.
42
42
  const isInitialRender = useRef(true)
43
+ const hasNotifiedOpen = useRef(false)
43
44
  const showRef = useRef(show)
44
45
  // The core state to control the mounting and unmounting of the component.
45
46
  const [mount, setMount] = useState<boolean>(false)
@@ -56,6 +57,52 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
56
57
 
57
58
  showRef.current = show
58
59
 
60
+ const cancelShowTimer = () => {
61
+ showScheduleIdRef.current += 1
62
+ }
63
+
64
+ const cancelEnteringWait = () => {
65
+ enteringLoopIdRef.current += 1
66
+ }
67
+
68
+ const cancelLeavingWait = () => {
69
+ leavingLoopIdRef.current += 1
70
+ }
71
+
72
+ const scheduleShow = () => {
73
+ const scheduleId = showScheduleIdRef.current
74
+ log(
75
+ debugLog,
76
+ '[lynx-ui-presence][usePresence] set mount=true',
77
+ )
78
+ setMount(true)
79
+ log(
80
+ debugLog,
81
+ '[lynx-ui-presence][usePresence] schedule set Entering in 8 frames',
82
+ )
83
+ delayFrames(8, () => {
84
+ if (scheduleId !== showScheduleIdRef.current || !showRef.current) return
85
+ setPresenceState(PresenceState.Entering)
86
+ })
87
+ if (enableDelay) {
88
+ log(
89
+ debugLog,
90
+ '[lynx-ui-presence][usePresence] schedule set DelayedEntering in 16 frames',
91
+ )
92
+ delayFrames(16, () => {
93
+ if (scheduleId !== showScheduleIdRef.current || !showRef.current) {
94
+ return
95
+ }
96
+ setPresenceState(PresenceState.DelayedEntering)
97
+ })
98
+ }
99
+ }
100
+
101
+ const restartShow = () => {
102
+ cancelShowTimer()
103
+ scheduleShow()
104
+ }
105
+
59
106
  const notAnimating = () => (isKFAnimating.current === false
60
107
  && isTransitionAnimating.current === false)
61
108
 
@@ -82,7 +129,12 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
82
129
  }
83
130
  }
84
131
  if (state === PresenceState.Leaving && notAnimating()) {
85
- setPresenceState(PresenceState.Left)
132
+ cancelLeavingWait()
133
+ if (showRef.current) {
134
+ restartShow()
135
+ } else {
136
+ setPresenceState(PresenceState.Left)
137
+ }
86
138
  }
87
139
  }
88
140
 
@@ -147,11 +199,24 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
147
199
 
148
200
  // ==== state controller ====
149
201
  const handleStateEntered = () => {
202
+ if (hasNotifiedOpen.current) {
203
+ return
204
+ }
205
+ hasNotifiedOpen.current = true
150
206
  onOpen?.()
151
207
  }
152
208
 
153
209
  const handleStateLeft = () => {
154
- if (!isInitialRender.current) {
210
+ if (showRef.current) {
211
+ log(
212
+ debugLog,
213
+ '[lynx-ui-presence][usePresence] skip mount=false because show=true (Left)',
214
+ )
215
+ restartShow()
216
+ return
217
+ }
218
+ if (!isInitialRender.current && hasNotifiedOpen.current) {
219
+ hasNotifiedOpen.current = false
155
220
  onClose?.()
156
221
  }
157
222
  log(debugLog, '[lynx-ui-presence][usePresence] set mount=false (Left)')
@@ -159,6 +224,7 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
159
224
  }
160
225
 
161
226
  const handleStateLeaving = () => {
227
+ cancelEnteringWait()
162
228
  leavingWaitFramesRef.current = 0
163
229
  leavingLoopIdRef.current += 1
164
230
  const loopId = leavingLoopIdRef.current
@@ -176,7 +242,11 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
176
242
  debugLog,
177
243
  `[lynx-ui-presence][usePresence] leaving timeout reached, loopId: ${loopId}, frames: ${leavingWaitFramesRef.current}`,
178
244
  )
179
- setPresenceState(PresenceState.Left)
245
+ if (showRef.current) {
246
+ restartShow()
247
+ } else {
248
+ setPresenceState(PresenceState.Left)
249
+ }
180
250
  return
181
251
  }
182
252
  leavingWaitFramesRef.current += 1
@@ -187,6 +257,7 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
187
257
  }
188
258
 
189
259
  const handleStateEnteringWithDelay = () => {
260
+ cancelLeavingWait()
190
261
  enteringWaitFramesRef.current = 0
191
262
  enteringLoopIdRef.current += 1
192
263
  const loopId = enteringLoopIdRef.current
@@ -203,7 +274,11 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
203
274
  debugLog,
204
275
  `[lynx-ui-presence][usePresence] entering timeout reached, loopId: ${loopId}, frames: ${enteringWaitFramesRef.current}`,
205
276
  )
206
- setPresenceState(PresenceState.Entered)
277
+ if (showRef.current) {
278
+ setPresenceState(PresenceState.Entered)
279
+ } else {
280
+ setPresenceState(PresenceState.Leaving)
281
+ }
207
282
  return
208
283
  }
209
284
  enteringWaitFramesRef.current += 1
@@ -233,32 +308,6 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
233
308
  }, [state])
234
309
 
235
310
  // ==== Show change part ====
236
- const handleShow = (scheduleId: number) => {
237
- log(
238
- debugLog,
239
- '[lynx-ui-presence][usePresence] set mount=true',
240
- )
241
- setMount(true)
242
- log(
243
- debugLog,
244
- '[lynx-ui-presence][usePresence] schedule set Entering in 8 frames',
245
- )
246
- delayFrames(8, () => {
247
- if (scheduleId !== showScheduleIdRef.current) return
248
- setPresenceState(PresenceState.Entering)
249
- })
250
- if (enableDelay) {
251
- log(
252
- debugLog,
253
- '[lynx-ui-presence][usePresence] schedule set DelayedEntering in 16 frames',
254
- )
255
- delayFrames(16, () => {
256
- if (scheduleId !== showScheduleIdRef.current) return
257
- setPresenceState(PresenceState.DelayedEntering)
258
- })
259
- }
260
- }
261
-
262
311
  const handleDismiss = () => {
263
312
  if (
264
313
  state === PresenceState.Entered || state === PresenceState.Entering
@@ -268,11 +317,14 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
268
317
  debugLog,
269
318
  '[lynx-ui-presence][usePresence] show=false -> set PresenceState.Leaving',
270
319
  )
320
+ cancelEnteringWait()
271
321
  setPresenceState(PresenceState.Leaving)
272
- } else if (state === PresenceState.Initial && mount) {
322
+ } else if (
323
+ (state === PresenceState.Initial || state === PresenceState.Left) && mount
324
+ ) {
273
325
  log(
274
326
  debugLog,
275
- '[lynx-ui-presence][usePresence] show=false in Initial -> set mount=false',
327
+ '[lynx-ui-presence][usePresence] show=false before entering -> set mount=false',
276
328
  )
277
329
  setMount(false)
278
330
  }
@@ -283,11 +335,10 @@ export function usePresence(props: usePresenceProps): usePresenceReturnType {
283
335
  debugLog,
284
336
  `[lynx-ui-presence][usePresence] show effect show:${show}, enableDelay:${enableDelay}, state:${state}, mount:${mount}`,
285
337
  )
286
- showScheduleIdRef.current += 1
287
- const scheduleId = showScheduleIdRef.current
288
338
  if (show) {
289
- handleShow(scheduleId)
339
+ restartShow()
290
340
  } else {
341
+ cancelShowTimer()
291
342
  handleDismiss()
292
343
  }
293
344
  }, [show, enableDelay])
@@ -118,7 +118,6 @@ export const usePresenceGroup: (
118
118
  const count = prev - 1
119
119
  if (count === 0) {
120
120
  onClose?.()
121
- setMountView(false)
122
121
  }
123
122
  return count
124
123
  })
@@ -177,8 +176,10 @@ export const usePresenceGroup: (
177
176
  useEffect(() => {
178
177
  if (show) {
179
178
  setMountView(true)
179
+ } else if (stateGroup.every(state => state === PresenceState.Left)) {
180
+ setMountView(false)
180
181
  }
181
- }, [show])
182
+ }, [show, stateGroup])
182
183
 
183
184
  return {
184
185
  renderChildren,