Skip to content

Commit a3b25c2

Browse files
feat(react-ui-core): add swipe back ratio css var in other activity roots and add transitionend state in useStyleEffectSwipeBack() (#549)
1 parent 6286620 commit a3b25c2

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

extensions/react-ui-core/src/useStyleEffectOffset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function useStyleEffectOffset({
3232
$el.style.transform = "";
3333
$el.style.opacity = "";
3434

35-
listenOnce($el, "transitionend", () => {
35+
listenOnce($el, ["transitionend", "transitioncancel"], () => {
3636
$el.style.transition = "";
3737
});
3838
});

extensions/react-ui-core/src/useStyleEffectSwipeBack.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function useStyleEffectSwipeBack({
1616
onSwipeStart,
1717
onSwipeMove,
1818
onSwipeEnd,
19+
onTransitionEnd,
1920
}: {
2021
dimRef: React.RefObject<HTMLDivElement>;
2122
edgeRef: React.RefObject<HTMLDivElement>;
@@ -28,6 +29,7 @@ export function useStyleEffectSwipeBack({
2829
onSwipeStart?: () => void;
2930
onSwipeMove?: (args: { dx: number; ratio: number }) => void;
3031
onSwipeEnd?: (args: { swiped: boolean }) => void;
32+
onTransitionEnd?: (args: { swiped: boolean }) => void;
3133
}) {
3234
useStyleEffect({
3335
styleName: "swipe-back",
@@ -101,6 +103,11 @@ export function useStyleEffectSwipeBack({
101103
if (ref.current.parentElement?.style.display === "none") {
102104
ref.current.parentElement.style.display = "block";
103105
}
106+
107+
ref.current.parentElement?.style.setProperty(
108+
SWIPE_BACK_RATIO_CSS_VAR_NAME,
109+
String(ratio),
110+
);
104111
});
105112

106113
_rAFLock = false;
@@ -118,8 +125,6 @@ export function useStyleEffectSwipeBack({
118125
$paper.style.transform = `translateX(${swiped ? "100%" : "0"})`;
119126
$paper.style.transition = transitionDuration;
120127

121-
$appBarRef?.style.removeProperty(SWIPE_BACK_RATIO_CSS_VAR_NAME);
122-
123128
refs.forEach((ref) => {
124129
if (!ref.current) {
125130
return;
@@ -135,7 +140,7 @@ export function useStyleEffectSwipeBack({
135140

136141
resolve();
137142

138-
listenOnce($paper, "transitionend", () => {
143+
listenOnce($paper, ["transitionend", "transitioncancel"], () => {
139144
const _swiped =
140145
swiped ||
141146
getActivityTransitionState() === "exit-active" ||
@@ -145,6 +150,8 @@ export function useStyleEffectSwipeBack({
145150
$paper.style.overflowY = "";
146151
$paper.style.transform = "";
147152

153+
$appBarRef?.style.removeProperty(SWIPE_BACK_RATIO_CSS_VAR_NAME);
154+
148155
refs.forEach((ref, i) => {
149156
if (!ref.current) {
150157
return;
@@ -168,7 +175,13 @@ export function useStyleEffectSwipeBack({
168175
_cachedRef.parentElement.style.display;
169176
}
170177
}
178+
179+
ref.current.parentElement?.style.removeProperty(
180+
SWIPE_BACK_RATIO_CSS_VAR_NAME,
181+
);
171182
});
183+
184+
onTransitionEnd?.({ swiped });
172185
});
173186
});
174187
});
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
export function listenOnce<T extends HTMLElement>(
22
el: T,
3-
type: keyof HTMLElementEventMap,
3+
types: Array<keyof HTMLElementEventMap>,
44
cb: () => void,
55
) {
66
const listener = () => {
7-
el.removeEventListener(type, listener);
7+
types.forEach((type) => {
8+
el.removeEventListener(type, listener);
9+
});
810
cb();
911
};
1012

11-
el.addEventListener(type, listener);
13+
types.forEach((type) => {
14+
el.addEventListener(type, listener);
15+
});
1216
}

0 commit comments

Comments
 (0)