R
fn

useScrollLock

v0.0.15testeddemo

Lock scrolling of an element by toggling `overflow: hidden`, preserving the element's prior inline overflow and handling iOS `touchmove`. Returns a writable boolean ref — set it to lock/unlock, read it for state.

Example

ts
const el = useTemplateRef<HTMLElement>('el');
const isLocked = useScrollLock(el);
isLocked.value = true; // lock
isLocked.value = false; // unlock

Demo

Loading demo…

Signature

ts
export function useScrollLock(
  element: MaybeComputedElementRef,
  initialState = false,
  options: UseScrollLockOptions ={ ... }

Parameters

ParameterTypeDescription
elementMaybeComputedElementRefThe element (or template ref / getter) to lock.
initialState?booleanWhether the element starts locked. Defaults to `false`.
options?UseScrollLockOptionsConfigurable `window` / `navigator` (mainly for SSR & testing).

Returns

UseScrollLockReturnA writable boolean ref; `true` while locked.