Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/friendly-balloons-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/toast": patch
---

- Use LazyMotion and AnimatePresence in ToastProvider to support exit animations.
31 changes: 18 additions & 13 deletions packages/components/toast/src/toast-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {ToastOptions, ToastQueue, useToastQueue} from "@react-stately/toast";
import {useProviderContext} from "@heroui/system";
import {AnimatePresence, LazyMotion} from "framer-motion";

import {RegionProps, ToastRegion} from "./toast-region";
import {ToastProps, ToastPlacement} from "./use-toast";

const loadFeatures = () => import("framer-motion").then((res) => res.domMax);

let globalToastQueue: ToastQueue<ToastProps> | null = null;

interface ToastProviderProps {
Expand Down Expand Up @@ -37,20 +40,22 @@ export const ToastProvider = ({
const globalContext = useProviderContext();
const disableAnimation = disableAnimationProp ?? globalContext?.disableAnimation ?? false;

if (toastQueue.visibleToasts.length == 0) {
return null;
}

return (
<ToastRegion
disableAnimation={disableAnimation}
maxVisibleToasts={maxVisibleToasts}
placement={placement}
toastOffset={toastOffset}
toastProps={toastProps}
toastQueue={toastQueue}
{...regionProps}
/>
<LazyMotion features={loadFeatures}>
<AnimatePresence>
{toastQueue.visibleToasts.length > 0 ? (
<ToastRegion
disableAnimation={disableAnimation}
maxVisibleToasts={maxVisibleToasts}
placement={placement}
toastOffset={toastOffset}
toastProps={toastProps}
toastQueue={toastQueue}
{...regionProps}
/>
) : null}
</AnimatePresence>
</LazyMotion>
);
};

Expand Down
30 changes: 12 additions & 18 deletions packages/components/toast/src/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import {
SuccessIcon,
WarningIcon,
} from "@heroui/shared-icons";
import {AnimatePresence, m, LazyMotion} from "framer-motion";
import {m} from "framer-motion";
import {cloneElement, isValidElement} from "react";
import {Spinner} from "@heroui/spinner";

import {UseToastProps, useToast} from "./use-toast";

const loadFeatures = () => import("framer-motion").then((res) => res.domMax);

export interface ToastProps extends UseToastProps {}

const iconMap = {
Expand Down Expand Up @@ -108,21 +106,17 @@ const Toast = forwardRef<"div", ToastProps>((props, ref) => {
{disableAnimation ? (
toastContent
) : (
<LazyMotion features={loadFeatures}>
<AnimatePresence>
<m.div {...getMotionDivProps()}>
<m.div
key={"inner-div"}
animate={{opacity: 1}}
exit={{opacity: 0}}
initial={{opacity: 0}}
transition={{duration: 0.25, ease: "easeOut", delay: 0.1}}
>
{toastContent}
</m.div>
</m.div>
</AnimatePresence>
</LazyMotion>
<m.div {...getMotionDivProps()}>
<m.div
key={"inner-div"}
animate={{opacity: 1}}
exit={{opacity: 0}}
initial={{opacity: 0}}
transition={{duration: 0.25, ease: "easeOut", delay: 0.1}}
>
{toastContent}
</m.div>
</m.div>
)}
</>
);
Expand Down