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

슬라이더

원문 보기 위키 갱신

슬라이더 (Slider)

범위 안에서 숫자 값을 선택하는 입력 컴포넌트예요. v-model로 값을 제어하고, 최소·최대·단계, 다중 썸, 방향, 툴팁 등을 지원합니다.

출처: Nuxt UI

본문

v-model 디렉티브를 사용해 Slider의 값을 제어해요.

<script setup lang="ts">
const value = ref(50)
</script>

<template>
  <USlider v-model="value" />
</template>

상태를 제어할 필요가 없을 때는 default-value prop으로 초기 값을 설정해 주세요.

<template>
  <USlider :default-value="50" />
</template>

[!TIP]

단일 썸 Slider에 이름을 지으려면 aria-label 또는 aria-labelledby를 사용해 주세요. 이 값은 slider 역할을 가진 썸 요소로 전달됩니다.

다중 썸 Slider의 썸은 위치에 따라 이름이 지어져 서로 구분됩니다. 썸이 두 개면 Minimum / Maximum, 세 개 이상이면 Value n of m으로 표시돼요. 이 이름들은 유지되며, aria-label은 root의 group 역할을 통해 Slider 전체를 가리켜 각 썸마다 반복되지 않습니다.

최소 / 최대 (Min / Max)

min과 max prop을 사용해 Slider의 최소·최대 값을 설정해요. 기본값은 0과 100입니다.

<template>
  <USlider :min="0" :max="50" :default-value="50" />
</template>

단계 (Step)

step prop을 사용해 Slider의 증가 값을 설정해요. 기본값은 1입니다.

<template>
  <USlider :step="10" :default-value="50" />
</template>

다중 (Multiple)

v-model 디렉티브나 default-value prop에 값 배열을 사용하면 범위 Slider를 만들 수 있어요.

<script setup lang="ts">
const value = ref([
  25,
  75
])
</script>

<template>
  <USlider v-model="value" />
</template>

min-steps-between-thumbs prop을 사용해 썸 사이의 최소 거리를 제한해요.

<script setup lang="ts">
const value = ref([
  25,
  50,
  75
])
</script>

<template>
  <USlider v-model="value" :min-steps-between-thumbs="10" />
</template>

방향 (Orientation)

orientation prop을 사용해 Slider의 방향을 바꿔요. 기본값은 horizontal입니다.

<template>
  <USlider orientation="vertical" :default-value="50" class="h-48" />
</template>

색상 (Color)

color prop을 사용해 Slider의 색상을 바꿔요.

<template>
  <USlider color="neutral" :default-value="50" />
</template>

크기 (Size)

size prop을 사용해 Slider의 크기를 바꿔요.

<template>
  <USlider size="xl" :default-value="50" />
</template>

툴팁 (Tooltip)

tooltip prop을 사용해 Slider 썸 주변에 현재 값을 표시하는 Tooltip을 보여줘요. true로 설정하면 기본 동작을 사용하고, 객체를 전달하면 Tooltip 컴포넌트의 어떤 속성이든 사용해 커스터마이즈할 수 있습니다.

<template>
  <USlider :default-value="50" tooltip />
</template>

비활성화 (Disabled)

disabled prop을 사용해 Slider를 비활성화해요.

<template>
  <USlider disabled :default-value="50" />
</template>

반전 (Inverted)

inverted prop을 사용해 Slider를 시각적으로 반전시켜요.

<template>
  <USlider inverted :default-value="25" />
</template>

API

Props

/**
 * Props for the Slider component
 */
interface SliderProps {
  /**
   * The element or component this component should render as.
   * @default 'div'
   */
  as?: any;
  /**
   * @default 'md'
   */
  size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
  /**
   * @default 'primary'
   */
  color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
  /**
   * The orientation of the slider.
   * @default 'horizontal'
   */
  orientation?: "horizontal" | "vertical" | undefined;
  /**
   * Display a tooltip around the slider thumbs with the current value.
   * `{ disableClosingTrigger: true }`{lang="ts-type"}
   * @default false
   */
  tooltip?: boolean | TooltipProps | undefined;
  /**
   * The value of the slider when initially rendered. Use when you do not need to control the state of the slider.
   */
  defaultValue?: number | number[] | undefined;
  ui?: { root?: SlotClass; track?: SlotClass; range?: SlotClass; thumb?: SlotClass; } | undefined;
  /**
   * The name of the field. Submitted with its owning form as part of a name/value pair.
   */
  name?: string | undefined;
  /**
   * When `true`, prevents the user from interacting with the slider.
   */
  disabled?: boolean | undefined;
  /**
   * Whether the slider is visually inverted.
   */
  inverted?: boolean | undefined;
  /**
   * The minimum value for the range.
   * @default 0
   */
  min?: number | undefined;
  /**
   * The maximum value for the range.
   * @default 100
   */
  max?: number | undefined;
  /**
   * The stepping interval.
   * @default 1
   */
  step?: number | undefined;
  /**
   * The minimum permitted steps between multiple thumbs.
   */
  minStepsBetweenThumbs?: number | undefined;
  modelValue?: T | undefined;
}

Emits

/**
 * Emitted events for the Slider component
 */
interface SliderEmits {
  change: (payload: [event: Event]) => void;
  update:modelValue: (payload: [value: T | undefined]) => void;
}

테마 (Theme)

export default defineAppConfig({
  ui: {
    slider: {
      slots: {
        root: 'relative flex items-center select-none touch-none',
        track: 'relative bg-accented overflow-hidden rounded-full grow',
        range: 'absolute rounded-full',
        thumb: 'rounded-full bg-default ring-2 focus-visible:outline-3 focus-visible:outline-offset-2'
      },
      variants: {
        color: {
          primary: {
            range: 'bg-primary',
            thumb: 'ring-primary outline-primary/25'
          },
          secondary: {
            range: 'bg-secondary',
            thumb: 'ring-secondary outline-secondary/25'
          },
          success: {
            range: 'bg-success',
            thumb: 'ring-success outline-success/25'
          },
          info: {
            range: 'bg-info',
            thumb: 'ring-info outline-info/25'
          },
          warning: {
            range: 'bg-warning',
            thumb: 'ring-warning outline-warning/25'
          },
          error: {
            range: 'bg-error',
            thumb: 'ring-error outline-error/25'
          },
          neutral: {
            range: 'bg-inverted',
            thumb: 'ring-inverted outline-inverted/25'
          }
        },
        size: {
          xs: {
            thumb: 'size-3'
          },
          sm: {
            thumb: 'size-3.5'
          },
          md: {
            thumb: 'size-4'
          },
          lg: {
            thumb: 'size-4.5'
          },
          xl: {
            thumb: 'size-5'
          }
        },
        orientation: {
          horizontal: {
            root: 'w-full',
            range: 'h-full'
          },
          vertical: {
            root: 'flex-col h-full',
            range: 'w-full'
          }
        },
        disabled: {
          true: {
            root: 'opacity-75 cursor-not-allowed'
          }
        }
      },
      compoundVariants: [
        {
          orientation: 'horizontal',
          size: 'xs',
          class: {
            track: 'h-[6px]'
          }
        },
        {
          orientation: 'horizontal',
          size: 'sm',
          class: {
            track: 'h-[7px]'
          }
        },
        {
          orientation: 'horizontal',
          size: 'md',
          class: {
            track: 'h-[8px]'
          }
        },
        {
          orientation: 'horizontal',
          size: 'lg',
          class: {
            track: 'h-[9px]'
          }
        },
        {
          orientation: 'horizontal',
          size: 'xl',
          class: {
            track: 'h-[10px]'
          }
        },
        {
          orientation: 'vertical',
          size: 'xs',
          class: {
            track: 'w-[6px]'
          }
        },
        {
          orientation: 'vertical',
          size: 'sm',
          class: {
            track: 'w-[7px]'
          }
        },
        {
          orientation: 'vertical',
          size: 'md',
          class: {
            track: 'w-[8px]'
          }
        },
        {
          orientation: 'vertical',
          size: 'lg',
          class: {
            track: 'w-[9px]'
          }
        },
        {
          orientation: 'vertical',
          size: 'xl',
          class: {
            track: 'w-[10px]'
          }
        }
      ],
      defaultVariants: {
        size: 'md',
        color: 'primary'
      }
    }
  }
})

변경 기록 (Changelog)

컴포넌트와 테마의 커밋 내역을 확인해 보세요.

더 알아보기 (Learn more)