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

색상 선택기

원문 보기 위키 갱신

색상 선택기 (ColorPicker)

색상을 선택할 수 있는 컴포넌트예요. 다양한 색상 형식(hex, rgb, hsl, cmyk, lab)을 지원하며 크기 조절과 비활성화도 손쉽게 할 수 있어요.

출처: Nuxt UI

본문

색상을 선택하는 컴포넌트예요.

사용 방법 (Usage)

v-model 디렉티브를 사용하면 ColorPicker의 값을 제어할 수 있어요.

<script setup lang="ts">
const value = ref("#00C16A")
</script>

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

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

<template>
  <UColorPicker default-value="#00BCD4" />
</template>

RGB 형식 (RGB Format)

format prop을 사용하면 ColorPicker의 값을 rgb 형식으로 설정할 수 있어요.

<script setup lang="ts">
const value = ref("rgb(0, 193, 106)")
</script>

<template>
  <UColorPicker format="rgb" v-model="value" />
</template>

HSL 형식 (HSL Format)

format prop을 사용하면 ColorPicker의 값을 hsl 형식으로 설정할 수 있어요.

<script setup lang="ts">
const value = ref("hsl(153, 100%, 37.8%)")
</script>

<template>
  <UColorPicker format="hsl" v-model="value" />
</template>

CMYK 형식 (CMYK Format)

format prop을 사용하면 ColorPicker의 값을 cmyk 형식으로 설정할 수 있어요.

<script setup lang="ts">
const value = ref("cmyk(100%, 0%, 45.08%, 24.31%)")
</script>

<template>
  <UColorPicker format="cmyk" v-model="value" />
</template>

CIELab 형식 (CIELab Format)

format prop을 사용하면 ColorPicker의 값을 lab 형식으로 설정할 수 있어요.

<script setup lang="ts">
const value = ref("lab(68.88% -60.41% 32.55%)")
</script>

<template>
  <UColorPicker format="lab" v-model="value" />
</template>

스로틀 (Throttle)

throttle prop을 사용하면 ColorPicker의 스로틀 값을 설정할 수 있어요.

<script setup lang="ts">
const value = ref("#00C16A")
</script>

<template>
  <UColorPicker :throttle="100" v-model="value" />
</template>

사이즈 (Size)

size prop을 사용하면 ColorPicker의 크기를 설정할 수 있어요.

<template>
  <UColorPicker size="xl" />
</template>

비활성화 (Disabled)

disabled prop을 사용하면 ColorPicker를 비활성화할 수 있어요.

<template>
  <UColorPicker disabled />
</template>

예시 (Examples)

색상 선택기로 사용하기 (As a color chooser)

Button과 Popover 컴포넌트를 함께 사용하면 색상 선택기를 만들 수 있어요.

<script setup lang="ts">
const color = ref('#00C16A')

const chip = computed(() => ({ backgroundColor: color.value }))
</script>

<template>
  <UPopover>
    <UButton label="Choose color" color="neutral" variant="outline">
      <template #leading>
        <span :style="chip" class="size-3 rounded-full" />
      </template>
    </UButton>

    <template #content>
      <UColorPicker v-model="color" class="p-2" />
    </template>
  </UPopover>
</template>

API

Props

/**
 * Props for the ColorPicker component
 */
interface ColorPickerProps {
  /**
   * The element or component this component should render as.
   * @default 'div'
   */
  as?: any;
  /**
   * Throttle time in ms for the color picker
   * @default 50
   */
  throttle?: number | undefined;
  /**
   * Disable the color picker
   */
  disabled?: boolean | undefined;
  /**
   * The default value of the color picker
   * @default '#FFFFFF'
   */
  defaultValue?: string | undefined;
  /**
   * Format of the color
   * @default 'hex'
   */
  format?: "hex" | "rgb" | "hsl" | "cmyk" | "lab" | undefined;
  /**
   * @default 'md'
   */
  size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
  ui?: { root?: SlotClass; picker?: SlotClass; selector?: SlotClass; selectorBackground?: SlotClass; selectorThumb?: SlotClass; track?: SlotClass; trackThumb?: SlotClass; } | undefined;
  modelValue?: string | undefined;
}

Emits

/**
 * Emitted events for the ColorPicker component
 */
interface ColorPickerEmits {
  update:modelValue: (payload: [value: string | undefined]) => void;
}

테마 (Theme)

export default defineAppConfig({
  ui: {
    colorPicker: {
      slots: {
        root: 'data-[disabled]:opacity-75',
        picker: 'flex gap-4',
        selector: 'rounded-md touch-none',
        selectorBackground: 'w-full h-full relative rounded-md',
        selectorThumb: '-translate-y-1/2 -translate-x-1/2 absolute size-4 ring-2 ring-white rounded-full cursor-pointer data-[disabled]:cursor-not-allowed',
        track: 'w-[8px] relative rounded-md touch-none',
        trackThumb: 'absolute transform -translate-y-1/2 -translate-x-[4px] rtl:translate-x-[4px] size-4 rounded-full ring-2 ring-white cursor-pointer data-[disabled]:cursor-not-allowed'
      },
      variants: {
        size: {
          xs: {
            selector: 'w-38 h-38',
            track: 'h-38'
          },
          sm: {
            selector: 'w-40 h-40',
            track: 'h-40'
          },
          md: {
            selector: 'w-42 h-42',
            track: 'h-42'
          },
          lg: {
            selector: 'w-44 h-44',
            track: 'h-44'
          },
          xl: {
            selector: 'w-46 h-46',
            track: 'h-46'
          }
        }
      },
      defaultVariants: {
        size: 'md'
      }
    }
  }
})

변경 이력 (Changelog)

컴포넌트와 테마의 커밋 이력을 확인하세요.


더 알아보기 (Learn more)