Skip to content

hunghg255/scrollbar-block

Repository files navigation

logo

A tiny library to block the scrollbar of the body element ✨

NPM Version NPM Downloads Minizip Contributors License

Live demo

Live Demo

Installation

NPM

Install

npm i scrollbar-block@latest

Use

import React, { useEffect, useState } from 'react';
import { scrollbarBlock } from 'scrollbar-block';

function Modal({ open, children }) {
  const dialogRef = React.useRef(null);
  const unlockScrollbars = React.useRef(null);

  useEffect(() => {
    const dialogNode = dialogRef.current;
    if (dialogNode) {
      if (open) {
        dialogNode.showModal();
        unlockScrollbars.current = lockScrollbars();
      } else {
        dialogNode.close();
        unlockScrollbars.current?.();
      }
    }
  }, [open]);

  return <dialog ref={dialogRef}>{children}</dialog>;
}

About

Buy Me A Coffee

Gia Hung – hung.hg

About

A tiny library to block the scrollbar of the body element ✨

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published