Skip to content

useScroll ​

Hook that allows you to control scroll a element

sensors
test coverage
Last changed: last month

Installation ​

Library
CLI
Manual
typescript
import { useScroll } from '@siberiacancode/reactuse';

Usage ​

typescript
const scrolling = useScroll(ref, options);
// or
const scrolling = useScroll(ref, () => console.log('callback'));
// or
const { ref, scrolling } = useScroll(options);
// or
const { ref, scrolling } = useScroll(() => console.log('callback'));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
options.behavior?ScrollBehaviorautoThe behavior of scrolling
options.offset.left?number0The left offset for arrived states
options.offset.right?number0The right offset for arrived states
options.offset.top?number0The top offset for arrived states
options.offset.bottom?number0The bottom offset for arrived states
options.onScroll?(params: UseScrollCallbackParams, event: Event) => void-The callback function to be invoked on scroll
options.onStop?(event: Event) => void-The callback function to be invoked on scroll end

Returns

boolean

Parameters

NameTypeDefaultNote
callback?(params: UseScrollCallbackParams, event: Event) => void-The callback function to be invoked on scroll

Returns

boolean

Parameters

NameTypeDefaultNote
targetTarget-The target element to scroll
options.behavior?ScrollBehaviorautoThe behavior of scrolling
options.offset.left?number0The left offset for arrived states
options.offset.right?number0The right offset for arrived states
options.offset.top?number0The top offset for arrived states
options.offset.bottom?number0The bottom offset for arrived states
options.onScroll?(params: UseScrollCallbackParams, event: Event) => void-The callback function to be invoked on scroll
options.onStop?(event: Event) => void-The callback function to be invoked on scroll end

Returns

[StateRef<Target>, boolean]

Parameters

NameTypeDefaultNote
targetTarget-The target element to scroll
callback?(params: UseScrollCallbackParams, event: Event) => void-The callback function to be invoked on scroll

Returns

[StateRef<Target>, boolean]

Type declaration ​

typescript
import type { HookTarget } from '@/utils/helpers';

import type { StateRef } from '../useRefState/useRefState';

export interface UseScrollOptions {
  /** The on scroll callback */
  onScroll?: (params: UseScrollCallbackParams, event: Event) => void;

  /** The on end scroll callback */
  onStop?: (event: Event) => void;

  /** Offset arrived states by x pixels. */
  offset?: {
    left?: number;
    right?: number;
    top?: number;
    bottom?: number;
  };
}

export interface UseScrollCallbackParams {
  /** The element x position */
  x: number;
  /** The element y position */
  y: number;
  /** State of scroll arrived */
  arrived: {
    left: boolean;
    right: boolean;
    top: boolean;
    bottom: boolean;
  };
  /** State of scroll direction */
  directions: {
    left: boolean;
    right: boolean;
    top: boolean;
    bottom: boolean;
  };
}

export interface UseScroll {
  (target: HookTarget, callback?: (params: UseScrollCallbackParams, event: Event) => void): boolean;

  (target: HookTarget, options?: UseScrollOptions): boolean;

  <Target extends Element>(
    callback?: (params: UseScrollCallbackParams, event: Event) => void,
    target?: never
  ): {
    ref: StateRef<Target>;
    scrolling: boolean;
  };

  <Target extends Element>(
    options?: UseScrollOptions,
    target?: never
  ): {
    ref: StateRef<Target>;
    scrolling: boolean;
  };
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
E
Evgen41kk
Evgen41kk

Released under the MIT License.