문서 기반

UIInView

뷰포트 또는 스크롤 영역에서 요소가 보이는 시점을 감지합니다.

기본 사용법

스크롤로 사진 등장 효과 주기

미리보기 안에서 아래로 스크롤해 보세요. 사진 영역의 60%가 보이는 순간 사진이 위로 살짝 올라오며 선명해집니다. 이미 본 사진은 다시 내려와도 그대로 보입니다.

봄꽃 가까이 보기
연분홍 꽃잎이 여러 겹으로 포개진 모습
연분홍 꽃잎
작은 보라색 꽃들이 모여 핀 모습
보랏빛 꽃 무리
<script setup lang="ts">
import { UIInView } from "@connextable/ui";

const photos = [
  {
    src: "/lightbox/pink-flower.jpg",
    caption: "연분홍 꽃잎",
    alt: "연분홍 꽃잎이 여러 겹으로 포개진 모습",
  },
  {
    src: "/lightbox/purple-flowers.jpg",
    caption: "보랏빛 꽃 무리",
    alt: "작은 보라색 꽃들이 모여 핀 모습",
  },
];
</script>

<template>
  <section
    class="photo-story ui-scrollbar"
    aria-label="봄꽃 사진 모음"
    tabindex="0"
  >
    <header class="photo-story__intro">
      <strong>봄꽃 가까이 보기</strong>
    </header>
    <figure v-for="photo in photos" :key="photo.src">
      <UIInView v-slot="{ hasEntered }" :threshold="0.6" once fallback as-child>
        <div
          class="photo-story__frame"
          :data-revealed="hasEntered || undefined"
        >
          <img :src="photo.src" :alt="photo.alt" />
        </div>
      </UIInView>
      <figcaption>{{ photo.caption }}</figcaption>
    </figure>
  </section>
</template>

<style scoped>
.photo-story {
  box-sizing: border-box;
  inline-size: min(100%, 26rem);
  block-size: 16rem;
  overflow: auto;
  border: 1px solid var(--ui-color-border);
  border-radius: var(--ui-radius-md);
}

.photo-story:focus-visible {
  outline: var(--ui-focus-ring-width) solid var(--ui-color-focus);
  outline-offset: calc(-1 * var(--ui-focus-ring-width));
}

.photo-story__intro {
  box-sizing: border-box;
  display: grid;
  place-items: center;
  min-block-size: 9.5rem;
  padding: var(--ui-space-md);
  background: var(--ui-color-surface);
  text-align: center;
  font-size: 1.125rem;
}

.photo-story figure {
  margin: var(--ui-space-md);
}

.photo-story figcaption {
  margin-block-start: var(--ui-space-xs);
  color: var(--ui-color-text-muted);
  font-size: var(--ui-font-size-body-sm);
}

.photo-story__frame {
  aspect-ratio: 16 / 10;
  min-block-size: 10rem;
  overflow: hidden;
  border-radius: var(--ui-radius-sm);
  background: var(--ui-color-surface);
}

.photo-story__frame img {
  display: block;
  inline-size: 100%;
  block-size: 100%;
  object-fit: cover;
}

@media (prefers-reduced-motion: no-preference) {
  .photo-story__frame img {
    transition:
      opacity calc(var(--ui-motion-standard) * 3) var(--ui-ease-out),
      transform calc(var(--ui-motion-standard) * 3) var(--ui-ease-out);
  }

  .photo-story__frame:not([data-revealed]) img {
    opacity: 0.2;
    transform: translateY(1rem) scale(0.96);
  }
}
</style>

주요 API

속성과 모델

as / as-child

기본 요소는 div입니다. as-child로 슬롯의 단일 요소를 관찰 대상으로 사용합니다.

스크롤로 사진 등장 효과 주기 예제
root타입Element | Document | null | undefined

안쪽 스크롤 영역을 지정합니다. 생략하거나 null을 넘기면 문서 뷰포트를 관찰합니다.

root-margin타입string | undefined

관찰 영역을 늘리거나 줄입니다. 아래쪽 여백을 늘리면 요소가 보이기 전에 자료를 준비할 수 있습니다.

threshold타입number | undefined기본값0

보여야 하는 요소 면적의 비율입니다. 0부터 1 사이 값을 사용합니다.

스크롤로 사진 등장 효과 주기 예제
once타입boolean | undefined

요소가 처음 보이면 관찰을 멈춥니다. 화면에서 벗어나도 inView와 hasEntered가 true로 유지됩니다. 관찰 대상을 바꾸면 새로 관찰합니다.

스크롤로 사진 등장 효과 주기 예제

입력 상태

disabled / fallback

disabled를 켜면 관찰을 멈춥니다. IntersectionObserver를 지원하지 않는 환경에서 fallback을 켜면, 마운트한 뒤 요소가 보인 것으로 처리합니다.

스크롤로 사진 등장 효과 주기 예제

슬롯

#default타입(state: { inView: boolean; hasEntered: boolean; }) => unknown

inView로 요소가 보이는지, hasEntered로 지금까지 한 번이라도 보였는지 확인합니다.

스크롤로 사진 등장 효과 주기 예제

이벤트와 명령

@enter / @leave

요소가 보이면 enter, 보이지 않게 되면 leave로 관찰 결과를 전달합니다. fallback이 적용되면 enter에서 null을 받습니다.

전체 속성, 이벤트와 슬롯

UIInView

as
PrimitiveAs | undefined
as-child
boolean | undefined
root
Element | Document | null | undefined
root-margin
string | undefined
threshold
number | undefined
once
boolean | undefined
disabled
boolean | undefined
fallback
boolean | undefined
@enter
(entry: IntersectionObserverEntry | null) => any
@leave
(entry: IntersectionObserverEntry | null) => any
#default
(state: { inView: boolean; hasEntered: boolean; }) => unknown