Skip to content

Commit

Permalink
patch: fix body locking unnecessarily (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonmarcello authored Dec 4, 2023
1 parent 14ff74c commit f603cd2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-kiwis-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wethegit/react-modal": patch
---

Fixes and issue where the modal would lock the body even though `appendToBody` wasn't set to `true`
3 changes: 0 additions & 3 deletions src/lib/components/modal-inner/modal-inner.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useEffect, useRef } from "react"
import { usePreventScroll } from "@wethegit/react-hooks"

import { classnames } from "../../../utils/classnames"

Expand All @@ -24,8 +23,6 @@ export function ModalInner({ children, className, ...props }: ModalInnerProps) {
const firstFocusableElement = useRef<HTMLDivElement>(null)
const lastFocusableElement = useRef<HTMLDivElement>(null)

usePreventScroll(true)

const focusStartingPosition = () => {
const element = firstFocusableElement.current
if (element) element.focus()
Expand Down
3 changes: 3 additions & 0 deletions src/lib/components/modal/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ReactDOM from "react-dom"
import { usePreventScroll } from "@wethegit/react-hooks"

import { ModalInner } from "../modal-inner"
import type { ModalInnerProps } from "../modal-inner"
Expand All @@ -15,6 +16,8 @@ export interface ModalProps extends ModalInnerProps {
}

export function Modal({ appendToBody, className, ...props }: ModalProps) {
usePreventScroll(appendToBody)

if (appendToBody) return ReactDOM.createPortal(<ModalInner {...props} />, document.body)
else
return (
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function CustomModal() {
</button>

{isOpen && (
<Modal>
<Modal appendToBody>
<ModalBackdrop onClick={toggle} className={styles.CustomModalOverlay} />
<ModalContent className={classnames([styles.CustomModalContent])}>
<button onClick={toggle} className={styles.CustomModalClose}>
Expand Down

0 comments on commit f603cd2

Please sign in to comment.