Skip to content

Commit

Permalink
fix storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
dottyy committed Jun 7, 2024
1 parent b87faa1 commit c285953
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/components/elements/Notification/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import Icon, { IconNames } from "@/components/extensive/Icon/Icon";
import Text from "../Text/Text";
import { TYPE_CLASSES } from "./constants/baseClasses";
import { TEXT_CLASSES } from "./constants/textClasses";
import withNotificationRoot from "./withNotificationRoot";

export interface NotificationProps extends React.HTMLAttributes<HTMLDivElement> {
type?: "success" | "error" | "warning";
message: string;
title: string;
open: boolean;
portalRootId?: string;
}

const Notification: FC<NotificationProps> = props => {
const { type = "default", message, className, title, open, ...rest } = props;
const t = useT();
const { type = "default", message, className, title, open, portalRootId = "notification-root", ...rest } = props;
const [openNotification, setOpenNotification] = useState(open);
const t = useT();

const textClasses = useMemo(() => (has(TEXT_CLASSES, type) ? TEXT_CLASSES[type] : TEXT_CLASSES.default), [type]);

const notificationClasses = useMemo(
() => (has(TYPE_CLASSES, type) ? TYPE_CLASSES[type] : TYPE_CLASSES.default),
[type]
Expand Down Expand Up @@ -76,8 +77,8 @@ const Notification: FC<NotificationProps> = props => {
<></>
)}
</div>,
document.body
document.getElementById(portalRootId)!
);
};

export default Notification;
export default withNotificationRoot(Notification);
20 changes: 20 additions & 0 deletions src/components/elements/Notification/withNotificationRoot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect } from "react";

const withNotificationRoot = (Component: React.ComponentType<any>) => {
useEffect(() => {
const notificationRootId = "notification-root";
let notificationRoot = document.getElementById(notificationRootId);
if (!notificationRoot) {
notificationRoot = document.createElement("div");
notificationRoot.setAttribute("id", notificationRootId);
document.body.appendChild(notificationRoot);
}
return () => {
document.body.removeChild(notificationRoot);
};
}, []);

return (props: any) => <Component {...props} portalRootId="notification-root" />;
};

export default withNotificationRoot;

0 comments on commit c285953

Please sign in to comment.