-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
31 additions
and
20 deletions.
There are no files selected for viewing
7 changes: 2 additions & 5 deletions
7
src/components/ui/AlertDialog/contexts/AlertDialogContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/components/ui/AlertDialog/shards/AlertDialogPortal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters