Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-ui-core): add swipe back ratio css var in other activity roots and add transitionend state in useStyleEffectSwipeBack() #549

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}
Loading