Skip to content

Files

Latest commit

a0ba145 Β· Jan 9, 2020

History

History
35 lines (28 loc) Β· 648 Bytes

useLockBodyScroll.md

File metadata and controls

35 lines (28 loc) Β· 648 Bytes

πŸ‹ useLockBodyScroll

Locks the scrolling - used for things like modals

Usage

import { useLockBodyScroll } from "react-recipes";

function App() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    isOpen && (
      <Modal title="The title" onClose={() => setIsOpen(false)}>
        Great modal content!
      </Modal>
    )
  );
}

function Modal({ title, children, onClose }) {
  // Call hook to lock body scroll
  useLockBodyScroll();

  return (
    <div className="modal-overlay" onClick={onClose}>
      <div className="modal">
        <h2>{title}</h2>
        <p>{children}</p>
      </div>
    </div>
  );
}