Skip to content

Commit

Permalink
fixed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
impirios committed Oct 26, 2024
1 parent 193479c commit b1a3e1c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
7 changes: 2 additions & 5 deletions src/components/ui/AlertDialog/contexts/AlertDialogContext.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { createContext } from 'react';
import { FloatingContext } from '@floating-ui/react';
import { createContext } from "react";

type AlertDialogContextType = {
isOpen: boolean;
handleOpenChange: (open: boolean) => void;
floaterContext: FloatingContext;
rootClass: string;
handleOverlayClick: () => void;
};

export const AlertDialogContext = createContext<AlertDialogContextType>({
isOpen: false,
handleOpenChange: () => {},
floaterContext: {} as FloatingContext,
rootClass: '',
rootClass: "",
handleOverlayClick: () => {},
});
42 changes: 28 additions & 14 deletions src/components/ui/AlertDialog/shards/AlertDialogPortal.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import React, { useContext } from 'react';
import Floater from '~/core/primitives/Floater';
import { AlertDialogContext } from '../contexts/AlertDialogContext';
import React, { useContext } from "react";
import Floater from "~/core/primitives/Floater";
import { AlertDialogContext } from "../contexts/AlertDialogContext";

export type AlertDialogPortalProps = {
children: React.ReactNode;

}
children: React.ReactNode;
};

const AlertDialogPortal = ({ children } : AlertDialogPortalProps) => {
const {rootClass} = useContext(AlertDialogContext)
const rootElement = document.getElementsByClassName(rootClass)[0] as HTMLElement | null;
const AlertDialogPortal = ({ children }: AlertDialogPortalProps) => {
const { rootClass } = useContext(AlertDialogContext);
const rootElement = document.getElementsByClassName(
rootClass
)[0] as HTMLElement | null;

return (
<Floater.Portal root={rootElement || document.body}>
{children}
</Floater.Portal>
);
return (
<Floater.Portal
root={
rootElement ||
(() => {
if (process.env.NODE_ENV === "development") {
console.warn(
`AlertDialog: No element found with class "${rootClass}". ` +
"Falling back to document.body. Dark mode styling may not work correctly."
);
}
return document.body;
})()
}
>
{children}
</Floater.Portal>
);
};

export default AlertDialogPortal;
2 changes: 1 addition & 1 deletion src/components/ui/AlertDialog/shards/AlertDialogRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type AlertDialogRootProps = {

const COMPONENT_NAME = 'AlertDialog';

const AlertDialogRoot = ({ children, customRootClass = '', open, onOpenChange, onClickOutside } : AlertDialogRootProps) => {
const AlertDialogRoot = ({ children, customRootClass = '', open, onOpenChange, onClickOutside=()=>{} } : AlertDialogRootProps) => {
const { context: floaterContext } = Floater.useFloating();
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
const [isOpen, setIsOpen] = useState(open);
Expand Down

0 comments on commit b1a3e1c

Please sign in to comment.