Skip to main content
useScrollBlocker
@coinbase/cds-web@8.13.6
Block and unblock page scrolling.
Import
import { useScrollBlocker } from '@coinbase/cds-web/hooks/useScrollBlocker'
SourceView source code

Basic usage

Loading...
Live Code
function Example() {
  const blockScroll = useScrollBlocker();
  const [isBlocked, setIsBlocked] = useState(false);

  const toggleScroll = () => {
    setIsBlocked(!isBlocked);
    blockScroll(!isBlocked);
  };

  return (
    <VStack gap={3} alignItems="start">
      <TextHeadline>Scroll is currently {isBlocked ? 'blocked' : 'enabled'}</TextHeadline>
      <Button onClick={toggleScroll}>{isBlocked ? 'Enable Scroll' : 'Block Scroll'}</Button>
    </VStack>
  );
}

With Overlay

Loading...
Live Code
function Example() {
  const blockScroll = useScrollBlocker();
  const [isOpen, setIsOpen] = useState(false);

  const openModal = () => {
    setIsOpen(true);
    blockScroll(true);
  };

  const closeModal = () => {
    setIsOpen(false);
    blockScroll(false);
  };

  return (
    <VStack gap={3} alignItems="start">
      <Button onClick={openModal}>Open Overlay</Button>
      <Modal visible={isOpen} onRequestClose={closeModal}>
        <ModalHeader title="Modal Title" />
        <ModalBody>Modal Content</ModalBody>
      </Modal>
    </VStack>
  );
}

Is this page useful?

Coinbase Design is an open-source, adaptable system of guidelines, components, and tools that aid the best practices of user interface design for crypto products.