Skip to content

Commit

Permalink
feat(react-ui-core): add swipe back ratio css var in other activity r…
Browse files Browse the repository at this point in the history
…oots and add `transitionend` state in `useStyleEffectSwipeBack()` (#549)
  • Loading branch information
tonyfromundefined committed Dec 18, 2024
1 parent 6286620 commit 48693e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-schools-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackflow/react-ui-core": patch
---

feat(react-ui-core): add swipe back ratio css var in other activity roots and add `transitionend` state in `useStyleEffectSwipeBack()`
2 changes: 1 addition & 1 deletion extensions/react-ui-core/src/useStyleEffectOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function useStyleEffectOffset({
$el.style.transform = "";
$el.style.opacity = "";

listenOnce($el, "transitionend", () => {
listenOnce($el, ["transitionend", "transitioncancel"], () => {
$el.style.transition = "";
});
});
Expand Down
19 changes: 16 additions & 3 deletions extensions/react-ui-core/src/useStyleEffectSwipeBack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function useStyleEffectSwipeBack({
onSwipeStart,
onSwipeMove,
onSwipeEnd,
onTransitionEnd,
}: {
dimRef: React.RefObject<HTMLDivElement>;
edgeRef: React.RefObject<HTMLDivElement>;
Expand All @@ -28,6 +29,7 @@ export function useStyleEffectSwipeBack({
onSwipeStart?: () => void;
onSwipeMove?: (args: { dx: number; ratio: number }) => void;
onSwipeEnd?: (args: { swiped: boolean }) => void;
onTransitionEnd?: (args: { swiped: boolean }) => void;
}) {
useStyleEffect({
styleName: "swipe-back",
Expand Down Expand Up @@ -101,6 +103,11 @@ export function useStyleEffectSwipeBack({
if (ref.current.parentElement?.style.display === "none") {
ref.current.parentElement.style.display = "block";
}

ref.current.parentElement?.style.setProperty(
SWIPE_BACK_RATIO_CSS_VAR_NAME,
String(ratio),
);
});

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

$appBarRef?.style.removeProperty(SWIPE_BACK_RATIO_CSS_VAR_NAME);

refs.forEach((ref) => {
if (!ref.current) {
return;
Expand All @@ -135,7 +140,7 @@ export function useStyleEffectSwipeBack({

resolve();

listenOnce($paper, "transitionend", () => {
listenOnce($paper, ["transitionend", "transitioncancel"], () => {
const _swiped =
swiped ||
getActivityTransitionState() === "exit-active" ||
Expand All @@ -145,6 +150,8 @@ export function useStyleEffectSwipeBack({
$paper.style.overflowY = "";
$paper.style.transform = "";

$appBarRef?.style.removeProperty(SWIPE_BACK_RATIO_CSS_VAR_NAME);

refs.forEach((ref, i) => {
if (!ref.current) {
return;
Expand All @@ -168,7 +175,13 @@ export function useStyleEffectSwipeBack({
_cachedRef.parentElement.style.display;
}
}

ref.current.parentElement?.style.removeProperty(
SWIPE_BACK_RATIO_CSS_VAR_NAME,
);
});

onTransitionEnd?.({ swiped });
});
});
});
Expand Down
10 changes: 7 additions & 3 deletions extensions/react-ui-core/src/utils/listenOnce.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export function listenOnce<T extends HTMLElement>(
el: T,
type: keyof HTMLElementEventMap,
types: Array<keyof HTMLElementEventMap>,
cb: () => void,
) {
const listener = () => {
el.removeEventListener(type, listener);
types.forEach((type) => {
el.removeEventListener(type, listener);
});
cb();
};

el.addEventListener(type, listener);
types.forEach((type) => {
el.addEventListener(type, listener);
});
}

0 comments on commit 48693e7

Please sign in to comment.