본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

use-scroll-into-view

원문 보기 위키 갱신

특정 엘리먼트를 뷰포트 안으로 부드럽게 스크롤하는 훅이에요. 애니메이션 설정, 이징, 부모 노드 스크롤을 지원해요.

출처: 문서

본문

사용법 (Usage)

useScrollIntoView 훅은 대상 엘리먼트로 부드럽게 스크롤해요. scrollIntoView 함수를 호출하면 애니메이션이 시작돼요.

import { useScrollIntoView } from '@mantine/hooks';
import { Button, Text, Group, Box } from '@mantine/core';

function Demo() {
  const { scrollIntoView, targetRef } = useScrollIntoView({
    offset: 60,
  });

  return (

          scrollIntoView({
            alignment: 'center',
          })
        }
      >
        Scroll to target

      Hello there

  );
}

API

훅은 설정 객체로 구성돼요.

  • onScrollFinish – 스크롤 애니메이션이 끝난 후 호출할 함수
  • onScrollCancel – 사용자 상호작용으로 스크롤 애니메이션이 취소될 때 호출할 함수
  • easing – 커스텀 수학 이징 함수
  • duration – 스크롤 애니메이션의 밀리초 단위 지속 시간
  • axis – 스크롤 축
  • cancelable – 사용자의 스크롤로 애니메이션이 중단될 수 있는지 여부
  • offset – 가장 가까운 가장자리와 엘리먼트 사이의 추가 거리
  • isList – Select, Carousel처럼 대상이 여러 개인 스크롤 목록에서 콘텐츠가 점프하는 것을 방지하는 표시

훅은 다음을 포함하는 객체를 반환해요.

  • scrollIntoView – 스크롤 애니메이션을 시작하는 함수
  • cancel – 스크롤 애니메이션을 중지하는 함수
  • scrolling – 스크롤 애니메이션이 진행 중인지 나타내는 boolean
  • targetRef – 대상 HTML 노드의 ref
  • scrollableRef – 스크롤 가능한 부모 HTML 엘리먼트의 ref. 사용하지 않으면 document 엘리먼트를 사용해요.

반환된 scrollIntoView 함수는 선택적 인수인 alignment를 하나 받아요. alignment는 현재 축을 기준으로 부모에 대한 대상 엘리먼트의 정렬 방식이에요.

import { useScrollIntoView } from '@mantine/hooks';

const { scrollIntoView } = useScrollIntoView();

scrollIntoView({ alignment: 'center' });

이징 (Easing)

훅은 애니메이션 흐름을 제어하기 위해 커스텀 easing 수학 함수를 받아요. 0과 1 사이의 숫자인 t 인수를 사용해요.

기본 이징은 easeInOutQuad이며 자세한 내용은 여기에서 확인할 수 있어요. 다른 인기 있는 예시는 easings.net에서 확인할 수 있어요.

import { useScrollIntoView } from '@mantine/hooks';

useScrollIntoView({
  easing: (t) => (t

부모 노드 (Parent node)

기본적으로 훅은 스크롤 가능한 부모를 찾기 위해 scrollableRef를 사용해요. 명시적으로 지정하지 않으면 document 엘리먼트를 스크롤해요.

import { useScrollIntoView } from '@mantine/hooks';
import { Button, Text, Group, Paper, Box } from '@mantine/core';

function Demo() {
  const { scrollIntoView, targetRef, scrollableRef } = useScrollIntoView();

  return (

            Scroll me into view

       scrollIntoView()}>Scroll to target

  );
}

X 축 스크롤 (Scroll X axis)

axis: 'x'로 설정하면 가로 방향으로 스크롤해요.

import { useScrollIntoView } from '@mantine/hooks';
import { Button, Text, Group, Paper, Box } from '@mantine/core';

function Demo() {
  const { scrollIntoView, targetRef, scrollableRef } = useScrollIntoView({ axis: 'x' });

  return (

            Scroll me into view

       scrollIntoView()}>Scroll to target

  );
}

정의 (Definition)

interface UseScrollIntoViewAnimation {
  /** Target element alignment relatively to parent based on current axis */
  alignment?: 'start' | 'end' | 'center';
}

interface UseScrollIntoViewOptions {
  /** Callback fired after scroll */
  onScrollFinish?: () => void;

  /** Callback fired when scroll animation is canceled by user interaction */
  onScrollCancel?: () => void;

  /** Duration of scroll in milliseconds */
  duration?: number;

  /** Axis of scroll */
  axis?: 'x' | 'y';

  /** Custom mathematical easing function */
  easing?: (t: number) => number;

  /** Additional distance between nearest edge and element */
  offset?: number;

  /** Indicator if animation may be interrupted by user scrolling */
  cancelable?: boolean;

  /** Prevents content jumping in scrolling lists with multiple targets */
  isList?: boolean;
}

export interface UseScrollIntoViewReturnValue {
  scrollableRef: React.RefObject;
  targetRef: React.RefObject;
  scrollIntoView: (params?: UseScrollIntoViewAnimation) => void;
  cancel: () => void;
  scrolling: boolean;
}

function useScrollIntoView(
  options?: UseScrollIntoViewOptions,
): UseScrollIntoViewReturnValue

내보내는 타입 (Exported types)

UseScrollIntoViewOptions와 UseScrollIntoViewReturnValue 타입은 @mantine/hooks 패키지에서 내보내져요. 애플리케이션에서 임포트할 수 있어요.

import type { UseScrollIntoViewOptions, UseScrollIntoViewReturnValue } from '@mantine/hooks';

더 알아보기 (Learn more)