Skip to content

Commit 7052848

Browse files
authored
Fixing the AlertDialogPortal dark mode (#470)
1 parent e0af5db commit 7052848

File tree

4 files changed

+58
-29
lines changed

4 files changed

+58
-29
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
import { createContext } from 'react';
1+
import { createContext } from "react";
22

3-
export const AlertDialogContext = createContext({
3+
type AlertDialogContextType = {
4+
isOpen: boolean;
5+
handleOpenChange: (open: boolean) => void;
6+
rootClass: string;
7+
handleOverlayClick: () => void;
8+
};
49

10+
export const AlertDialogContext = createContext<AlertDialogContextType>({
11+
isOpen: false,
12+
handleOpenChange: () => {},
13+
rootClass: "",
14+
handleOverlayClick: () => {},
515
});
Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1-
import React from 'react';
2-
import Floater from '~/core/primitives/Floater';
1+
import React, { useContext } from "react";
2+
import Floater from "~/core/primitives/Floater";
3+
import { AlertDialogContext } from "../contexts/AlertDialogContext";
34

45
export type AlertDialogPortalProps = {
5-
children: React.ReactNode;
6-
}
6+
children: React.ReactNode;
7+
};
8+
9+
const AlertDialogPortal = ({ children }: AlertDialogPortalProps) => {
10+
const { rootClass } = useContext(AlertDialogContext);
11+
const rootElement = document.getElementsByClassName(
12+
rootClass
13+
)[0] as HTMLElement | null;
714

8-
const AlertDialogPortal = ({ children } : AlertDialogPortalProps) => {
9-
return (
10-
<Floater.Portal>
11-
{children}
12-
</Floater.Portal>
13-
);
15+
return (
16+
<Floater.Portal
17+
root={
18+
rootElement ||
19+
(() => {
20+
if (process.env.NODE_ENV === "development") {
21+
console.warn(
22+
`AlertDialog: No element found with class "${rootClass}". ` +
23+
"Falling back to document.body. Dark mode styling may not work correctly."
24+
);
25+
}
26+
return document.body;
27+
})()
28+
}
29+
>
30+
{children}
31+
</Floater.Portal>
32+
);
1433
};
1534

1635
export default AlertDialogPortal;

src/components/ui/AlertDialog/shards/AlertDialogRoot.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type AlertDialogRootProps = {
1414

1515
const COMPONENT_NAME = 'AlertDialog';
1616

17-
const AlertDialogRoot = ({ children, customRootClass = '', open, onOpenChange, onClickOutside } : AlertDialogRootProps) => {
17+
const AlertDialogRoot = ({ children, customRootClass = '', open, onOpenChange, onClickOutside=()=>{} } : AlertDialogRootProps) => {
1818
const { context: floaterContext } = Floater.useFloating();
1919
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
2020
const [isOpen, setIsOpen] = useState(open);
@@ -31,7 +31,7 @@ const AlertDialogRoot = ({ children, customRootClass = '', open, onOpenChange, o
3131
const props = { isOpen, handleOpenChange, floaterContext, rootClass, handleOverlayClick };
3232
return (
3333
<AlertDialogContext.Provider value={props}>
34-
<div className={rootClass}>
34+
<div className={rootClass} >
3535
{children}
3636
</div>
3737
</AlertDialogContext.Provider>
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
.rad-ui-alert-dialog-overlay {
2-
z-index: 50;
3-
background-color: rgba(0, 0, 0, 0.8);
1+
.rad-ui-alert-dialog-overlay {
2+
z-index: 50;
3+
background-color: rgba(0, 0, 0, 0.8);
44
}
55

66
.rad-ui-alert-dialog-content {
7-
padding: 16px;
8-
border-radius: 8px;
9-
background-color: white;
10-
color: black;
11-
width: 100%;
12-
max-width: 800px;
13-
margin: 10px auto;
14-
position: fixed;
15-
left: 50%;
16-
top: 50%;
17-
transform: translate(-50%, -50%);
18-
z-index: 50;
7+
padding: 16px;
8+
border-radius: 8px;
9+
background-color: var(--rad-ui-color-red-1000);
10+
color: var(--rad-ui-color-gray-50);
11+
width: 100%;
12+
max-width: 800px;
13+
margin: 10px auto;
14+
position: fixed;
15+
left: 50%;
16+
top: 50%;
17+
transform: translate(-50%, -50%);
18+
z-index: 50;
1919
}

0 commit comments

Comments
 (0)