useScrollLock

Tags:

A React Hook that helps to lock/unlock the scroll behavior of an element.

Demo

Source
isLocked:
false

Usage

See API for more details.

Source

Click links below to view source on GitHub.

API

const [isLocked, setLocked, actions] = useScrollLock(elementTarget, initialState)

ElementTarget

ElementTarget is a union type that represents various kinds of elements that can be targeted.

See ElementTarget or ElementTarget Types for more details.

InitialState

A boolean value to determine the initial state of the scroll lock.

Returns

export type UseScrollLockReturnsActions = {
  /**
   * Lock the scroll
   */
  lock(): void
  /**
   * Unlock the scroll
   */
  unlock(): void
  /**
   * Toggle the scroll lock
   */
  toggle(): void
}

export type UseScrollLockReturns = [
  /**
   * Whether the scroll is locked
   */
  isLocked: boolean,
  /**
   * Set the scroll lock
   */
  setLocked: ReactSetState<boolean>,
  /**
   * Actions to lock/unlock the scroll
   */
  UseScrollLockReturnsActions,
]