@lynx-js/lynx-ui-presence 0.0.0 → 3.131.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 +33 -0
- package/LICENSE +8 -0
- package/README.md +49 -0
- package/dist/Presence.d.ts +8 -0
- package/dist/Presence.d.ts.map +1 -0
- package/dist/Presence.jsx +35 -0
- package/dist/Presence.jsx.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +5 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.docs.d.ts +49 -0
- package/dist/types/index.docs.d.ts.map +1 -0
- package/dist/types/index.jsx +0 -0
- package/dist/usePresence.d.ts +8 -0
- package/dist/usePresence.d.ts.map +1 -0
- package/dist/usePresence.jsx +176 -0
- package/dist/usePresence.jsx.map +1 -0
- package/dist/usePresenceGroup.d.ts +20 -0
- package/dist/usePresenceGroup.d.ts.map +1 -0
- package/dist/usePresenceGroup.jsx +79 -0
- package/dist/usePresenceGroup.jsx.map +1 -0
- package/dist/useVisibilityFromPresence.d.ts +12 -0
- package/dist/useVisibilityFromPresence.d.ts.map +1 -0
- package/dist/useVisibilityFromPresence.jsx +14 -0
- package/dist/useVisibilityFromPresence.jsx.map +1 -0
- package/dist/utils.d.ts +24 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.jsx +45 -0
- package/dist/utils.jsx.map +1 -0
- package/package.json +46 -4
- package/src/Presence.tsx +64 -0
- package/src/index.tsx +17 -0
- package/src/types/index.docs.ts +72 -0
- package/src/types/index.ts +5 -0
- package/src/usePresence.tsx +322 -0
- package/src/usePresenceGroup.tsx +187 -0
- package/src/useVisibilityFromPresence.ts +39 -0
- package/src/utils.tsx +97 -0
- package/tsconfig.docs.json +7 -0
- package/tsconfig.json +9 -0
- /package/{index.js → dist/types/index.docs.jsx} +0 -0
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { PresenceAnimationStatus } from './types';
|
|
2
|
+
export declare enum PresenceState {
|
|
3
|
+
Initial = 0,
|
|
4
|
+
Entering = 1,
|
|
5
|
+
DelayedEntering = 2,// Special state for delaying the showAnimation. This is used to prevent the content from being layout before the showAnimation.
|
|
6
|
+
Entered = 3,
|
|
7
|
+
Leaving = 4,
|
|
8
|
+
Left = 5
|
|
9
|
+
}
|
|
10
|
+
export declare const resolveAnimationStatus: ({ state, enableDelay, grouped, }: {
|
|
11
|
+
state: PresenceState;
|
|
12
|
+
enableDelay: boolean;
|
|
13
|
+
grouped?: boolean;
|
|
14
|
+
}) => PresenceAnimationStatus;
|
|
15
|
+
interface PresenceClassVariantsProps {
|
|
16
|
+
state: PresenceState;
|
|
17
|
+
enableDelay: boolean;
|
|
18
|
+
className?: string;
|
|
19
|
+
transition?: boolean;
|
|
20
|
+
grouped?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare const presenceClassVariants: ({ state, enableDelay, className, transition, grouped, }: PresenceClassVariantsProps) => string;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAA;AAEtD,oBAAY,aAAa;IACvB,OAAO,IAAI;IACX,QAAQ,IAAI;IACZ,eAAe,IAAI,CAAE,gIAAgI;IACrJ,OAAO,IAAI;IACX,OAAO,IAAI;IACX,IAAI,IAAI;CACT;AAGD,eAAO,MAAM,sBAAsB,EAAE,CAAC,EACpC,KAAK,EACL,WAAW,EACX,OAAO,GACR,EAAE;IACD,KAAK,EAAE,aAAa,CAAA;IACpB,WAAW,EAAE,OAAO,CAAA;IACpB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB,KAAK,uBA+BL,CAAA;AAED,UAAU,0BAA0B;IAClC,KAAK,EAAE,aAAa,CAAA;IACpB,WAAW,EAAE,OAAO,CAAA;IACpB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,eAAO,MAAM,qBAAqB,GAAI,yDAMnC,0BAA0B,WAuB5B,CAAA"}
|
package/dist/utils.jsx
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { clsx } from "clsx";
|
|
2
|
+
var utils_PresenceState = /*#__PURE__*/ function(PresenceState) {
|
|
3
|
+
PresenceState[PresenceState["Initial"] = 0] = "Initial";
|
|
4
|
+
PresenceState[PresenceState["Entering"] = 1] = "Entering";
|
|
5
|
+
PresenceState[PresenceState["DelayedEntering"] = 2] = "DelayedEntering";
|
|
6
|
+
PresenceState[PresenceState["Entered"] = 3] = "Entered";
|
|
7
|
+
PresenceState[PresenceState["Leaving"] = 4] = "Leaving";
|
|
8
|
+
PresenceState[PresenceState["Left"] = 5] = "Left";
|
|
9
|
+
return PresenceState;
|
|
10
|
+
}({});
|
|
11
|
+
const resolveAnimationStatus = ({ state, enableDelay, grouped = false })=>{
|
|
12
|
+
const enteringStateWithDelay = enableDelay ? 2 : 1;
|
|
13
|
+
const isOpen = state === enteringStateWithDelay || 3 === state;
|
|
14
|
+
const groupedOpen = isOpen || 4 === state;
|
|
15
|
+
const isClose = 4 === state || 5 === state || enableDelay && 1 === state;
|
|
16
|
+
const groupedClosed = 5 === state;
|
|
17
|
+
return {
|
|
18
|
+
leaving: 4 === state,
|
|
19
|
+
entering: state === enteringStateWithDelay,
|
|
20
|
+
animating: 4 === state || state === enteringStateWithDelay,
|
|
21
|
+
open: grouped ? groupedOpen : isOpen,
|
|
22
|
+
closed: grouped ? groupedClosed : isClose
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const presenceClassVariants = ({ state, enableDelay, className, transition, grouped })=>{
|
|
26
|
+
const status = resolveAnimationStatus({
|
|
27
|
+
state,
|
|
28
|
+
enableDelay,
|
|
29
|
+
grouped
|
|
30
|
+
});
|
|
31
|
+
if (transition) return clsx(className, {
|
|
32
|
+
'ui-entering': status.entering,
|
|
33
|
+
'ui-leaving': status.leaving,
|
|
34
|
+
'ui-animating': status.animating,
|
|
35
|
+
'ui-open': status.open,
|
|
36
|
+
'ui-closed': status.closed
|
|
37
|
+
});
|
|
38
|
+
return clsx(className, {
|
|
39
|
+
'ui-open': status.open,
|
|
40
|
+
'ui-closed': status.closed
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
export { utils_PresenceState as PresenceState, presenceClassVariants, resolveAnimationStatus };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=utils.jsx.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.jsx","sources":["webpack://@lynx-js/lynx-ui-presence/./src/utils.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 { clsx } from 'clsx'\n\nimport type { PresenceAnimationStatus } from './types'\n\nexport enum PresenceState {\n Initial = 0,\n Entering = 1,\n DelayedEntering = 2, // Special state for delaying the showAnimation. This is used to prevent the content from being layout before the showAnimation.\n Entered = 3,\n Leaving = 4,\n Left = 5,\n}\n\n// Convert abstract state to clear animation status for users\nexport const resolveAnimationStatus: ({\n state,\n enableDelay,\n grouped,\n}: {\n state: PresenceState\n enableDelay: boolean\n grouped?: boolean\n}) => PresenceAnimationStatus = ({\n state,\n enableDelay,\n grouped = false,\n}) => {\n const enteringStateWithDelay = enableDelay\n ? PresenceState.DelayedEntering\n : PresenceState.Entering\n const isOpen = state === enteringStateWithDelay\n || state === PresenceState.Entered\n const groupedOpen = isOpen || state === PresenceState.Leaving\n\n const isClose = state === PresenceState.Leaving\n || state === PresenceState.Left\n || (enableDelay && state === PresenceState.Entering)\n const groupedClosed = state === PresenceState.Left\n return {\n leaving: state === PresenceState.Leaving,\n entering: state === enteringStateWithDelay,\n animating: state === PresenceState.Leaving\n || state === enteringStateWithDelay,\n\n // In grouped state, even the member has entered their animation(leaving) state, it has to remain open so it won't hide its children.\n // grouped: open = entering/delayedEntering/entered/leaving\n // normal: open = entering/delayedEntering/entered\n open: grouped ? groupedOpen : isOpen,\n // In grouped state, the grouped element has to wait all its member exit their animations.\n // grouped: closed = left\n // normal: closed = leaving/left\n closed: grouped ? groupedClosed : isClose,\n }\n}\n\ninterface PresenceClassVariantsProps {\n state: PresenceState\n enableDelay: boolean\n className?: string\n transition?: boolean\n grouped?: boolean\n}\n\nexport const presenceClassVariants = ({\n state,\n enableDelay,\n className,\n transition,\n grouped,\n}: PresenceClassVariantsProps) => {\n const status = resolveAnimationStatus({\n state,\n enableDelay,\n grouped,\n })\n if (transition) {\n return clsx(\n className,\n {\n 'ui-entering': status.entering,\n 'ui-leaving': status.leaving,\n 'ui-animating': status.animating,\n 'ui-open': status.open,\n 'ui-closed': status.closed,\n },\n )\n } else {\n return clsx(className, {\n 'ui-open': status.open,\n 'ui-closed': status.closed,\n })\n }\n}\n"],"names":["PresenceState","resolveAnimationStatus","state","enableDelay","grouped","enteringStateWithDelay","isOpen","groupedOpen","isClose","groupedClosed","presenceClassVariants","className","transition","status","clsx"],"mappings":";AAQO,IAAKA,sBAAaA,WAAAA,GAAAA,SAAbA,aAAa;;;;;;;WAAbA;;AAUL,MAAMC,yBAQmB,CAAC,EAC/BC,KAAK,EACLC,WAAW,EACXC,UAAU,KAAK,EAChB;IACC,MAAMC,yBAAyBF,cAAAA,IAAAA;IAG/B,MAAMG,SAASJ,UAAUG,0BACpBH,AAAU,MAAVA;IACL,MAAMK,cAAcD,UAAUJ,AAAU,MAAVA;IAE9B,MAAMM,UAAUN,AAAU,MAAVA,SACXA,AAAU,MAAVA,SACCC,eAAeD,AAAU,MAAVA;IACrB,MAAMO,gBAAgBP,AAAU,MAAVA;IACtB,OAAO;QACL,SAASA,AAAU,MAAVA;QACT,UAAUA,UAAUG;QACpB,WAAWH,AAAU,MAAVA,SACNA,UAAUG;QAKf,MAAMD,UAAUG,cAAcD;QAI9B,QAAQF,UAAUK,gBAAgBD;IACpC;AACF;AAUO,MAAME,wBAAwB,CAAC,EACpCR,KAAK,EACLC,WAAW,EACXQ,SAAS,EACTC,UAAU,EACVR,OAAO,EACoB;IAC3B,MAAMS,SAASZ,uBAAuB;QACpCC;QACAC;QACAC;IACF;IACA,IAAIQ,YACF,OAAOE,KACLH,WACA;QACE,eAAeE,OAAO,QAAQ;QAC9B,cAAcA,OAAO,OAAO;QAC5B,gBAAgBA,OAAO,SAAS;QAChC,WAAWA,OAAO,IAAI;QACtB,aAAaA,OAAO,MAAM;IAC5B;IAGF,OAAOC,KAAKH,WAAW;QACrB,WAAWE,OAAO,IAAI;QACtB,aAAaA,OAAO,MAAM;IAC5B;AAEJ"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lynx-js/lynx-ui-presence",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "3.131.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/lynx-family/lynx-ui.git",
|
|
7
|
+
"directory": "packages/lynx-ui-presence"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "The Lynx Authors"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.jsx",
|
|
16
|
+
"module": "./dist/index.jsx",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"src/**",
|
|
20
|
+
"CHANGELOG.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"tsconfig.docs.json",
|
|
23
|
+
"tsconfig.json",
|
|
24
|
+
"dist/**"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"clsx": "^2.1.1",
|
|
28
|
+
"@lynx-js/lynx-ui-common": "3.131.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@lynx-js/react": "0.113.0",
|
|
32
|
+
"@lynx-js/types": "3.6.0",
|
|
33
|
+
"@rslib/core": "^0.16.0",
|
|
34
|
+
"@types/react": "^18.3.8",
|
|
35
|
+
"typescript": "^5.5.3"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@lynx-js/react": ">=0.100.0",
|
|
39
|
+
"@lynx-js/types": "*",
|
|
40
|
+
"@types/react": "^18"
|
|
41
|
+
},
|
|
6
42
|
"publishConfig": {
|
|
7
43
|
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"jsnext:source": "./src/index.tsx",
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "rslib build",
|
|
48
|
+
"build:watch": "rslib build --watch",
|
|
49
|
+
"test": "vitest"
|
|
8
50
|
}
|
|
9
|
-
}
|
|
51
|
+
}
|
package/src/Presence.tsx
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { createContext, useState } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
PresenceAnimationStatus,
|
|
9
|
+
PresenceContextType,
|
|
10
|
+
PresenceProps,
|
|
11
|
+
} from './types'
|
|
12
|
+
import { usePresence } from './usePresence'
|
|
13
|
+
import { PresenceState, resolveAnimationStatus } from './utils'
|
|
14
|
+
|
|
15
|
+
export const PresenceContext = createContext<PresenceContextType>(null!)
|
|
16
|
+
|
|
17
|
+
export const renderPresenceChildren = (
|
|
18
|
+
props: {
|
|
19
|
+
children: PresenceProps['children']
|
|
20
|
+
status: PresenceAnimationStatus
|
|
21
|
+
},
|
|
22
|
+
) => {
|
|
23
|
+
const { children, status } = props
|
|
24
|
+
return typeof children === 'function'
|
|
25
|
+
? children(status)
|
|
26
|
+
: children
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function Presence(props: PresenceProps) {
|
|
30
|
+
const {
|
|
31
|
+
show,
|
|
32
|
+
forceMount,
|
|
33
|
+
state,
|
|
34
|
+
setPresenceState,
|
|
35
|
+
children,
|
|
36
|
+
enableDelay = false,
|
|
37
|
+
onOpen,
|
|
38
|
+
onClose,
|
|
39
|
+
debugLog,
|
|
40
|
+
} = props
|
|
41
|
+
const [internalState, setInternalState] = useState<PresenceState>(
|
|
42
|
+
PresenceState.Left,
|
|
43
|
+
)
|
|
44
|
+
const presence = usePresence({
|
|
45
|
+
show,
|
|
46
|
+
state: state ?? internalState,
|
|
47
|
+
setPresenceState: setPresenceState ?? setInternalState,
|
|
48
|
+
enableDelay,
|
|
49
|
+
onOpen,
|
|
50
|
+
onClose,
|
|
51
|
+
debugLog,
|
|
52
|
+
})
|
|
53
|
+
const { mount } = presence.controllers
|
|
54
|
+
const status = resolveAnimationStatus({
|
|
55
|
+
state: state ?? internalState,
|
|
56
|
+
enableDelay,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<PresenceContext.Provider value={presence}>
|
|
61
|
+
{(mount || forceMount) && renderPresenceChildren({ children, status })}
|
|
62
|
+
</PresenceContext.Provider>
|
|
63
|
+
)
|
|
64
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
export { Presence, PresenceContext, renderPresenceChildren } from './Presence'
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
PresenceState,
|
|
9
|
+
resolveAnimationStatus,
|
|
10
|
+
presenceClassVariants,
|
|
11
|
+
} from './utils'
|
|
12
|
+
|
|
13
|
+
export { usePresenceGroup } from './usePresenceGroup'
|
|
14
|
+
|
|
15
|
+
export { useVisibilityFromPresence } from './useVisibilityFromPresence'
|
|
16
|
+
|
|
17
|
+
export type * from './types'
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import type { ReactNode } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
AnimationEvent,
|
|
9
|
+
EventHandler,
|
|
10
|
+
TransitionEvent,
|
|
11
|
+
} from '@lynx-js/types'
|
|
12
|
+
|
|
13
|
+
import type { PresenceState } from '../utils'
|
|
14
|
+
|
|
15
|
+
export interface PresenceAnimationStatus {
|
|
16
|
+
open?: boolean
|
|
17
|
+
closed?: boolean
|
|
18
|
+
|
|
19
|
+
leaving?: boolean
|
|
20
|
+
entering?: boolean
|
|
21
|
+
animating?: boolean
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type PresenceChildrenType = (
|
|
25
|
+
status: PresenceAnimationStatus,
|
|
26
|
+
) => ReactNode
|
|
27
|
+
|
|
28
|
+
export interface PresenceProps {
|
|
29
|
+
show: boolean // The one and only entry for showing the presence content.
|
|
30
|
+
forceMount?: boolean // Force to mount the presence container event the content is not shown.
|
|
31
|
+
children?: ReactNode | PresenceChildrenType
|
|
32
|
+
state?: PresenceState // The core animation state of Presence.
|
|
33
|
+
setPresenceState?: (state: PresenceState) => void
|
|
34
|
+
enableDelay?: boolean // Adding 'DelayedEntering' state in PresenceState.
|
|
35
|
+
// Presence takes charge of sending the events.
|
|
36
|
+
onClose?: () => void
|
|
37
|
+
onOpen?: () => void
|
|
38
|
+
debugLog?: boolean
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface usePresenceProps {
|
|
42
|
+
show: boolean // The one and only entry for showing the presence content.
|
|
43
|
+
forceMount?: boolean // Force to mount the presence container event the content is not shown.
|
|
44
|
+
state: PresenceState // The core animation state of Presence.
|
|
45
|
+
setPresenceState: (state: PresenceState) => void
|
|
46
|
+
enableDelay?: boolean // Adding 'DelayedEntering' state in PresenceState.
|
|
47
|
+
// Presence takes charge of sending the events.
|
|
48
|
+
onOpen?: () => void
|
|
49
|
+
onClose?: () => void
|
|
50
|
+
debugLog?: boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface PresenceContextType {
|
|
54
|
+
controllers: {
|
|
55
|
+
state: PresenceState
|
|
56
|
+
mount: boolean
|
|
57
|
+
setPresenceState: (state: PresenceState) => void
|
|
58
|
+
// if the change of show is set by XTriggers and XCloseButtons, this change should be called by this method rather than pass thru the props of Presence.
|
|
59
|
+
// This method is used to make sure the controlled props and uncontrolled mode can be handled correctly.
|
|
60
|
+
// setUncontrolledShow: (show: boolean) => void
|
|
61
|
+
}
|
|
62
|
+
animationHandlers: {
|
|
63
|
+
handleKFStart: EventHandler<AnimationEvent>
|
|
64
|
+
handleKFCancel: EventHandler<AnimationEvent>
|
|
65
|
+
handleTransitionCancel: EventHandler<TransitionEvent>
|
|
66
|
+
handleKFEnd: EventHandler<AnimationEvent>
|
|
67
|
+
handleTransitionStart: EventHandler<TransitionEvent>
|
|
68
|
+
handleTransitionEnd: EventHandler<TransitionEvent>
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type usePresenceReturnType = PresenceContextType
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import { createContext, useEffect, useRef, useState } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import { delayFrames, log } from '@lynx-js/lynx-ui-common'
|
|
8
|
+
import type { AnimationEvent, TransitionEvent } from '@lynx-js/types'
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
PresenceAnimationStatus,
|
|
12
|
+
PresenceContextType,
|
|
13
|
+
PresenceProps,
|
|
14
|
+
usePresenceProps,
|
|
15
|
+
usePresenceReturnType,
|
|
16
|
+
} from './types'
|
|
17
|
+
import { PresenceState } from './utils'
|
|
18
|
+
|
|
19
|
+
export const PresenceContext = createContext<PresenceContextType>(null!)
|
|
20
|
+
|
|
21
|
+
export function usePresence(props: usePresenceProps): usePresenceReturnType {
|
|
22
|
+
const {
|
|
23
|
+
show,
|
|
24
|
+
state,
|
|
25
|
+
enableDelay = false,
|
|
26
|
+
setPresenceState,
|
|
27
|
+
onOpen,
|
|
28
|
+
onClose,
|
|
29
|
+
debugLog = false,
|
|
30
|
+
} = props
|
|
31
|
+
log(
|
|
32
|
+
debugLog,
|
|
33
|
+
`[lynx-ui-presence][usePresence] init, show: ${show}, state: ${state}, enableDelay: ${enableDelay}`,
|
|
34
|
+
)
|
|
35
|
+
const enteringStateWithDelay = enableDelay
|
|
36
|
+
? PresenceState.DelayedEntering
|
|
37
|
+
: PresenceState.Entering
|
|
38
|
+
|
|
39
|
+
const isTransitionAnimating = useRef<boolean>(false)
|
|
40
|
+
const isKFAnimating = useRef<boolean>(false)
|
|
41
|
+
// Track the initial render, to prevent effects from running on mount.
|
|
42
|
+
const isInitialRender = useRef(true)
|
|
43
|
+
const showRef = useRef(show)
|
|
44
|
+
// The core state to control the mounting and unmounting of the component.
|
|
45
|
+
const [mount, setMount] = useState<boolean>(false)
|
|
46
|
+
// These three id record the loop ID for the entering progress animation and show state to prevent race conditions.
|
|
47
|
+
const enteringLoopIdRef = useRef<number>(0)
|
|
48
|
+
const leavingLoopIdRef = useRef<number>(0)
|
|
49
|
+
const showScheduleIdRef = useRef(0)
|
|
50
|
+
// Two timing windows to wait for the animation to start before forcing a state change.
|
|
51
|
+
const enteringWaitFramesRef = useRef<number>(0)
|
|
52
|
+
const leavingWaitFramesRef = useRef<number>(0)
|
|
53
|
+
|
|
54
|
+
// The maximum number of frames to wait for an animation to start before forcing a state change.
|
|
55
|
+
const MAX_WAIT_FRAMES = 24
|
|
56
|
+
|
|
57
|
+
showRef.current = show
|
|
58
|
+
|
|
59
|
+
const notAnimating = () => (isKFAnimating.current === false
|
|
60
|
+
&& isTransitionAnimating.current === false)
|
|
61
|
+
|
|
62
|
+
const handleAnimationStart = () => {
|
|
63
|
+
log(
|
|
64
|
+
debugLog,
|
|
65
|
+
`[lynx-ui-presence][usePresence] handleAnimationStart state:${state}, show:${show}, isTransition:${isTransitionAnimating.current}, isKFAnimating:${isKFAnimating.current}`,
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const handleAnimationEnd = () => {
|
|
70
|
+
// still need to check if the other kind of animation is running.
|
|
71
|
+
if (
|
|
72
|
+
(state === PresenceState.Entering
|
|
73
|
+
|| state === PresenceState.DelayedEntering)
|
|
74
|
+
&& notAnimating()
|
|
75
|
+
) {
|
|
76
|
+
if (showRef.current) {
|
|
77
|
+
setPresenceState(PresenceState.Entered)
|
|
78
|
+
} else {
|
|
79
|
+
// Safeguard: entering animation may still fire after show turns false.
|
|
80
|
+
// Why not Left? Because Left will also cancel the ongoing leaving animation.
|
|
81
|
+
setPresenceState(PresenceState.Leaving)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (state === PresenceState.Leaving && notAnimating()) {
|
|
85
|
+
setPresenceState(PresenceState.Left)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ==== Animation events =====
|
|
90
|
+
const handleKFStart = () => {
|
|
91
|
+
isKFAnimating.current = true
|
|
92
|
+
enteringLoopIdRef.current += 1
|
|
93
|
+
leavingLoopIdRef.current += 1
|
|
94
|
+
log(
|
|
95
|
+
debugLog,
|
|
96
|
+
`[lynx-ui-presence][usePresence] KF start, loopId: ${leavingLoopIdRef.current}`,
|
|
97
|
+
)
|
|
98
|
+
handleAnimationStart()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const handleTransitionStart = () => {
|
|
102
|
+
isTransitionAnimating.current = true
|
|
103
|
+
enteringLoopIdRef.current += 1
|
|
104
|
+
leavingLoopIdRef.current += 1
|
|
105
|
+
log(
|
|
106
|
+
debugLog,
|
|
107
|
+
`[lynx-ui-presence][usePresence] Transition start, loopId: ${leavingLoopIdRef.current}`,
|
|
108
|
+
)
|
|
109
|
+
handleAnimationStart()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const handleKFEnd = (_e: AnimationEvent) => {
|
|
113
|
+
isKFAnimating.current = false
|
|
114
|
+
log(
|
|
115
|
+
debugLog,
|
|
116
|
+
`[lynx-ui-presence][usePresence] KF end, loopId: ${leavingLoopIdRef.current}`,
|
|
117
|
+
)
|
|
118
|
+
handleAnimationEnd()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const handleKFCancel = () => {
|
|
122
|
+
isKFAnimating.current = false
|
|
123
|
+
log(
|
|
124
|
+
debugLog,
|
|
125
|
+
`[lynx-ui-presence][usePresence] KF cancel, loopId: ${leavingLoopIdRef.current}`,
|
|
126
|
+
)
|
|
127
|
+
handleAnimationEnd()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const handleTransitionCancel = () => {
|
|
131
|
+
isTransitionAnimating.current = false
|
|
132
|
+
log(
|
|
133
|
+
debugLog,
|
|
134
|
+
`[lynx-ui-presence][usePresence] Transition cancel, loopId: ${leavingLoopIdRef.current}`,
|
|
135
|
+
)
|
|
136
|
+
handleAnimationEnd()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const handleTransitionEnd = (_e: TransitionEvent) => {
|
|
140
|
+
isTransitionAnimating.current = false
|
|
141
|
+
log(
|
|
142
|
+
debugLog,
|
|
143
|
+
`[lynx-ui-presence][usePresence] Transition end, loopId: ${leavingLoopIdRef.current}`,
|
|
144
|
+
)
|
|
145
|
+
handleAnimationEnd()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ==== state controller ====
|
|
149
|
+
const handleStateEntered = () => {
|
|
150
|
+
onOpen?.()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const handleStateLeft = () => {
|
|
154
|
+
if (!isInitialRender.current) {
|
|
155
|
+
onClose?.()
|
|
156
|
+
}
|
|
157
|
+
log(debugLog, '[lynx-ui-presence][usePresence] set mount=false (Left)')
|
|
158
|
+
setMount(false)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const handleStateLeaving = () => {
|
|
162
|
+
leavingWaitFramesRef.current = 0
|
|
163
|
+
leavingLoopIdRef.current += 1
|
|
164
|
+
const loopId = leavingLoopIdRef.current
|
|
165
|
+
log(
|
|
166
|
+
debugLog,
|
|
167
|
+
`[lynx-ui-presence][usePresence] leaving loop scheduled, loopId: ${loopId}`,
|
|
168
|
+
)
|
|
169
|
+
const tryLeft = () => {
|
|
170
|
+
// The loopId is inconsistent with the current animation state, means the animation started during the delay period, so we should stop this trying.
|
|
171
|
+
if (loopId !== leavingLoopIdRef.current) return
|
|
172
|
+
if (!notAnimating()) return
|
|
173
|
+
// timeout reached => treat as no animation
|
|
174
|
+
if (leavingWaitFramesRef.current >= MAX_WAIT_FRAMES) {
|
|
175
|
+
log(
|
|
176
|
+
debugLog,
|
|
177
|
+
`[lynx-ui-presence][usePresence] leaving timeout reached, loopId: ${loopId}, frames: ${leavingWaitFramesRef.current}`,
|
|
178
|
+
)
|
|
179
|
+
setPresenceState(PresenceState.Left)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
leavingWaitFramesRef.current += 1
|
|
183
|
+
delayFrames(1, tryLeft)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
delayFrames(1, tryLeft)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const handleStateEnteringWithDelay = () => {
|
|
190
|
+
enteringWaitFramesRef.current = 0
|
|
191
|
+
enteringLoopIdRef.current += 1
|
|
192
|
+
const loopId = enteringLoopIdRef.current
|
|
193
|
+
log(
|
|
194
|
+
debugLog,
|
|
195
|
+
`[lynx-ui-presence][usePresence] entering loop scheduled, loopId: ${loopId}`,
|
|
196
|
+
)
|
|
197
|
+
const tryEntered = () => {
|
|
198
|
+
// The loopId is inconsistent with the current animation state, means the animation started during the delay period, so we should stop this trying.
|
|
199
|
+
if (loopId !== enteringLoopIdRef.current) return
|
|
200
|
+
if (!notAnimating()) return
|
|
201
|
+
if (enteringWaitFramesRef.current >= MAX_WAIT_FRAMES) {
|
|
202
|
+
log(
|
|
203
|
+
debugLog,
|
|
204
|
+
`[lynx-ui-presence][usePresence] entering timeout reached, loopId: ${loopId}, frames: ${enteringWaitFramesRef.current}`,
|
|
205
|
+
)
|
|
206
|
+
setPresenceState(PresenceState.Entered)
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
enteringWaitFramesRef.current += 1
|
|
210
|
+
delayFrames(1, tryEntered)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
delayFrames(1, tryEntered)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
useEffect(() => {
|
|
217
|
+
log(
|
|
218
|
+
debugLog,
|
|
219
|
+
`[lynx-ui-presence][usePresence] state effect, state: ${state}, show: ${show}, enableDelay: ${enableDelay}, isTransitionAnimating: ${isTransitionAnimating.current}, isKFAnimating: ${isKFAnimating.current}`,
|
|
220
|
+
)
|
|
221
|
+
if (state === PresenceState.Entered) {
|
|
222
|
+
handleStateEntered()
|
|
223
|
+
}
|
|
224
|
+
if (state === PresenceState.Left) {
|
|
225
|
+
handleStateLeft()
|
|
226
|
+
}
|
|
227
|
+
if (state === PresenceState.Leaving) {
|
|
228
|
+
handleStateLeaving()
|
|
229
|
+
}
|
|
230
|
+
if (state === enteringStateWithDelay) {
|
|
231
|
+
handleStateEnteringWithDelay()
|
|
232
|
+
}
|
|
233
|
+
}, [state])
|
|
234
|
+
|
|
235
|
+
// ==== 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
|
+
const handleDismiss = () => {
|
|
263
|
+
if (
|
|
264
|
+
state === PresenceState.Entered || state === PresenceState.Entering
|
|
265
|
+
|| state === PresenceState.DelayedEntering
|
|
266
|
+
) {
|
|
267
|
+
log(
|
|
268
|
+
debugLog,
|
|
269
|
+
'[lynx-ui-presence][usePresence] show=false -> set PresenceState.Leaving',
|
|
270
|
+
)
|
|
271
|
+
setPresenceState(PresenceState.Leaving)
|
|
272
|
+
} else if (state === PresenceState.Initial && mount) {
|
|
273
|
+
log(
|
|
274
|
+
debugLog,
|
|
275
|
+
'[lynx-ui-presence][usePresence] show=false in Initial -> set mount=false',
|
|
276
|
+
)
|
|
277
|
+
setMount(false)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
useEffect(() => {
|
|
282
|
+
log(
|
|
283
|
+
debugLog,
|
|
284
|
+
`[lynx-ui-presence][usePresence] show effect show:${show}, enableDelay:${enableDelay}, state:${state}, mount:${mount}`,
|
|
285
|
+
)
|
|
286
|
+
showScheduleIdRef.current += 1
|
|
287
|
+
const scheduleId = showScheduleIdRef.current
|
|
288
|
+
if (show) {
|
|
289
|
+
handleShow(scheduleId)
|
|
290
|
+
} else {
|
|
291
|
+
handleDismiss()
|
|
292
|
+
}
|
|
293
|
+
}, [show, enableDelay])
|
|
294
|
+
|
|
295
|
+
useEffect(() => {
|
|
296
|
+
isInitialRender.current = false
|
|
297
|
+
}, [])
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
controllers: { state, mount, setPresenceState },
|
|
301
|
+
animationHandlers: {
|
|
302
|
+
handleKFStart,
|
|
303
|
+
handleKFCancel,
|
|
304
|
+
handleKFEnd,
|
|
305
|
+
handleTransitionStart,
|
|
306
|
+
handleTransitionCancel,
|
|
307
|
+
handleTransitionEnd,
|
|
308
|
+
},
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export const renderPresenceChildren = (
|
|
313
|
+
props: {
|
|
314
|
+
children: PresenceProps['children']
|
|
315
|
+
status: PresenceAnimationStatus
|
|
316
|
+
},
|
|
317
|
+
) => {
|
|
318
|
+
const { children, status } = props
|
|
319
|
+
return typeof children === 'function'
|
|
320
|
+
? children(status)
|
|
321
|
+
: children
|
|
322
|
+
}
|