-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
create-presence.ts
164 lines (138 loc) · 5.38 KB
/
create-presence.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*!
* Portions of this file are based on code from radix-ui-primitives.
* MIT Licensed, Copyright (c) 2022 WorkOS.
*
* Credits to the Radix UI team:
* https://github.com/radix-ui/primitives/blob/21a7c97dc8efa79fecca36428eec49f187294085/packages/react/presence/src/Presence.tsx
* https://github.com/radix-ui/primitives/blob/21a7c97dc8efa79fecca36428eec49f187294085/packages/react/presence/src/useStateMachine.tsx
*/
import { Accessor, createEffect, createSignal, on, onCleanup } from "solid-js";
export interface CreatePresenceResult {
isPresent: Accessor<boolean>;
setRef: (el: HTMLElement) => void;
}
export function createPresence(present: Accessor<boolean>): CreatePresenceResult {
const [node, setNode] = createSignal<HTMLElement>();
let styles = {} as CSSStyleDeclaration;
let prevPresent = present();
let prevAnimationName = "none";
const [state, send] = createStateMachine(present() ? "mounted" : "unmounted", {
mounted: {
UNMOUNT: "unmounted",
ANIMATION_OUT: "unmountSuspended",
},
unmountSuspended: {
MOUNT: "mounted",
ANIMATION_END: "unmounted",
},
unmounted: {
MOUNT: "mounted",
},
});
createEffect(
on(state, state => {
const currentAnimationName = getAnimationName(styles);
prevAnimationName = state === "mounted" ? currentAnimationName : "none";
}),
);
createEffect(
on(present, present => {
if (prevPresent === present) {
return;
}
const currentAnimationName = getAnimationName(styles);
if (present) {
send("MOUNT");
//} else if (currentAnimationName === "none" || styles?.display === "none") {
// If there is no exit animation or the element is hidden, animations won't run, so we unmount instantly
} else if (styles?.display === "none") {
// If the element is hidden, animations won't run, so we unmount instantly
send("UNMOUNT");
} else {
/**
* When `present` changes to `false`, we check changes to animation-name to
* determine whether an animation has started. We chose this approach (reading
* computed styles) because there is no `animationrun` event and `animationstart`
* fires after `animation-delay` has expired which would be too late.
*/
const isAnimating = prevAnimationName !== currentAnimationName;
if (prevPresent && isAnimating) {
send("ANIMATION_OUT");
} else {
send("UNMOUNT");
}
}
prevPresent = present;
}),
);
createEffect(
on(node, node => {
if (node) {
/**
* Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`
* event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we
* make sure we only trigger ANIMATION_END for the currently active animation.
*/
const handleAnimationEnd = (event: AnimationEvent) => {
const currentAnimationName = getAnimationName(styles);
const isCurrentAnimation = currentAnimationName.includes(event.animationName);
if (event.target === node && isCurrentAnimation) {
send("ANIMATION_END");
}
};
const handleAnimationStart = (event: AnimationEvent) => {
if (event.target === node) {
// if animation occurred, store its name as the previous animation.
prevAnimationName = getAnimationName(styles);
}
};
node.addEventListener("animationstart", handleAnimationStart);
node.addEventListener("animationcancel", handleAnimationEnd);
node.addEventListener("animationend", handleAnimationEnd);
onCleanup(() => {
node.removeEventListener("animationstart", handleAnimationStart);
node.removeEventListener("animationcancel", handleAnimationEnd);
node.removeEventListener("animationend", handleAnimationEnd);
});
} else {
// Transition to the unmounted state if the node is removed prematurely.
// We avoid doing so during cleanup as the node may change but still exist.
send("ANIMATION_END");
}
}),
);
return {
isPresent: () => ["mounted", "unmountSuspended"].includes(state()),
setRef: (el: HTMLElement) => {
if (el) {
styles = getComputedStyle(el);
}
setNode(el);
},
};
}
/* -----------------------------------------------------------------------------------------------*/
function getAnimationName(styles?: CSSStyleDeclaration) {
return styles?.animationName || "none";
}
// https://fettblog.eu/typescript-union-to-intersection/
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any
? R
: never;
type Machine<S> = { [k: string]: { [k: string]: S } };
type MachineState<T> = keyof T;
type MachineEvent<T> = keyof UnionToIntersection<T[keyof T]>;
function createStateMachine<M>(
initialState: MachineState<M>,
machine: M & Machine<MachineState<M>>,
): [Accessor<MachineState<M>>, (event: MachineEvent<M>) => void] {
const reduce = (state: MachineState<M>, event: MachineEvent<M>): MachineState<M> => {
const nextState = (machine[state] as any)[event];
return nextState ?? state;
};
const [state, setState] = createSignal(initialState);
const send = (event: MachineEvent<M>) => {
setState(prev => reduce(prev, event));
};
return [state, send];
}