Skip to content

Commit 4ff95d1

Browse files
committed
refactor(ToastRoot): integrate animation props from utility function
1 parent 11549d5 commit 4ff95d1

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

packages/react-ui/src/components/toast/toast-root.tsx

+23-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useEffectEvent, useElementTransitionStatus, useEventListener, usePrevio
33
import type { PrimitiveProps } from "../../primitives";
44
import { PopperAriaContext } from "../../primitives/popper/popper-aria-context";
55
import { PopperDispatchContext } from "../../primitives/popper/popper-dispatch-context";
6+
import { getPopperAnimationProps } from "../../primitives/popper/utils";
67
import { tx } from "../../utils";
78
import { Alert, type AlertProps } from "../alert/alert";
89
import { useToastComponent } from "./toast-context";
@@ -21,7 +22,16 @@ export type ToastRootProps = AlertProps & {
2122
};
2223

2324
export const ToastRoot = (props: PrimitiveProps<"div", ToastRootProps, "role" | "id">) => {
24-
const { priority = "high", onDismiss, color = "primary", variant = "soft", children, className, ...rest } = props;
25+
const {
26+
priority = "high",
27+
onDismiss,
28+
color = "primary",
29+
variant = "soft",
30+
children,
31+
className,
32+
style,
33+
...rest
34+
} = props;
2535

2636
const { id, duration, placement, dismiss, update, remove } = useToastComponent();
2737

@@ -57,8 +67,6 @@ export const ToastRoot = (props: PrimitiveProps<"div", ToastRootProps, "role" |
5767
useEventListener("blur", clear);
5868
useEventListener("focus", reset);
5969

60-
const translateStyle = placementTranslateStyles[placement?.split("-")[0] as keyof typeof placementTranslateStyles];
61-
6270
if (!isMounted) {
6371
return null;
6472
}
@@ -71,19 +79,24 @@ export const ToastRoot = (props: PrimitiveProps<"div", ToastRootProps, "role" |
7179
descriptionId,
7280
};
7381

82+
const translateStyle = placementTranslateStyles[placement?.split("-")[0] as keyof typeof placementTranslateStyles];
83+
84+
const animationProps = getPopperAnimationProps({
85+
status,
86+
duration: 300,
87+
transitionClassName: "transition-[opacity,translate]",
88+
defaultClassName: ["opacity-0", translateStyle.close],
89+
openClassName: ["opacity-100", translateStyle.open],
90+
});
91+
7492
return (
7593
<div
7694
role="status"
7795
aria-live={priority == "high" ? "assertive" : "polite"}
7896
aria-atomic="true"
7997
ref={setElement}
80-
className={tx(
81-
"flex flex-col items-center",
82-
"transition-[opacity,translate] duration-300",
83-
transitionEnable && status == "open" && ["opacity-100", translateStyle.open],
84-
transitionEnable && status == "close" && ["opacity-0", translateStyle.close],
85-
transitionEnable && status == "initial" && ["opacity-0", translateStyle.close],
86-
)}
98+
style={{ ...style, ...animationProps.style, viewTransitionName: `${id}` }}
99+
className={tx("flex flex-col items-center", transitionEnable && animationProps.className)}
87100
onFocus={clear}
88101
onBlur={reset}
89102
onMouseOver={clear}

0 commit comments

Comments
 (0)