Skip to content

Commit

Permalink
fix: account for SSR
Browse files Browse the repository at this point in the history
fix: ModalProps interface
  • Loading branch information
gsong committed Aug 16, 2024
1 parent 35bc461 commit 611d2e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gsong/react-modal-dialog",
"version": "1.0.1",
"version": "1.0.3",
"description": "No-frills modal <dialog> for React",
"license": "MIT",
"repository": "github:gsong/react-modal-dialog",
Expand Down
25 changes: 18 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import * as React from "react";

export const useModal = () => {
const originalStyles = React.useRef<OriginalStyles>({
overflow: document.documentElement.style.overflow,
scrollbarGutter: document.documentElement.style.scrollbarGutter,
overflow: "",
scrollbarGutter: "",
});
const ref = React.useRef<HTMLDialogElement>(null);

React.useEffect(() => {
originalStyles.current = {
overflow: document.documentElement.style.overflow,
scrollbarGutter: document.documentElement.style.scrollbarGutter,
};
}, []);

const openModal = React.useCallback(
(
{ disableBodyScroll: _disableBodyScroll } = { disableBodyScroll: true },
Expand Down Expand Up @@ -67,13 +74,17 @@ export const useModal = () => {
};

const disableBodyScroll = () => {
document.documentElement.style.overflow = "hidden";
document.documentElement.style.scrollbarGutter = "stable";
if (typeof window !== "undefined") {
document.documentElement.style.overflow = "hidden";
document.documentElement.style.scrollbarGutter = "stable";
}
};

const enableBodyScroll = ({ overflow, scrollbarGutter }: OriginalStyles) => {
document.documentElement.style.overflow = overflow;
document.documentElement.style.scrollbarGutter = scrollbarGutter;
if (typeof window !== "undefined") {
document.documentElement.style.overflow = overflow;
document.documentElement.style.scrollbarGutter = scrollbarGutter;
}
};

const clickedInCurrentTarget = (
Expand All @@ -90,7 +101,7 @@ interface OriginalStyles {
scrollbarGutter: string;
}

interface ModalProps {
interface ModalProps extends React.HTMLAttributes<HTMLDialogElement> {
allowDismiss?: boolean;
onDismiss?: (event: React.MouseEvent<HTMLDialogElement, MouseEvent>) => void;
onCancel?: (event: React.SyntheticEvent<HTMLDialogElement, Event>) => void;
Expand Down

0 comments on commit 611d2e0

Please # to comment.