테마
테마 (Theme)
하위 컴포넌트들을 테마링하는 헤드리스(마크업 없는) 컴포넌트예요. 자식 컴포넌트들의 슬롯 클래스와 props 기본값을 개별 수정 없이 한 번에 재정의할 수 있어요.
출처: Nuxt UI
본문
하위 컴포넌트들을 테마링하는 헤드리스 컴포넌트예요.
사용 방법 (Usage)
Theme 컴포넌트는 모든 자식 컴포넌트의 기본 슬롯 클래스와 props를 각각 수정하지 않고 재정의해요. 내부적으로 Vue의 provide/inject 메커니즘을 사용하므로 깊이와 무관하게 어디서든 재정의가 적용됩니다.
[!NOTE]
Theme 컴포넌트는 어떤 HTML 요소도 렌더링하지 않아요. 자식들에게 테마 재정의를 제공할 뿐입니다.
Nuxt:
[!TIP]
앱 수준의 테마 설정에는
app.config.ts파일을 사용하는 것을 권장해요.
Vue:
[!TIP]
앱 수준의 테마 설정에는
vite.config.ts파일을 사용하는 것을 권장해요.
슬롯 클래스 (Slot classes)
ui prop을 사용하면 하위 컴포넌트의 슬롯 클래스를 재정의할 수 있어요. 키는 컴포넌트 이름(camelCase)이고 값은 해당 슬롯 클래스 재정의입니다.
<template>
<UTheme
:ui="{
button: {
base: 'rounded-full'
}
}"
>
<div class="flex items-center gap-2">
<UButton label="Button" color="neutral" />
<UButton label="Button" color="neutral" variant="outline" />
<UButton label="Button" color="neutral" variant="subtle" />
</div>
</UTheme>
</template>
props 기본값 (Prop defaults) 4.8+
props prop을 사용하면 하위 컴포넌트의 어떤 prop의 기본값이든 재정의할 수 있어요. 각 키는 해당 컴포넌트 props의 일부로 매핑됩니다.
<template>
<UTheme
:props="{
button: { color: 'neutral', variant: 'subtle', size: 'lg' },
tooltip: { delayDuration: 0, arrow: true }
}"
>
<div class="flex items-center gap-2">
<UTooltip text="Inherits delayDuration from theme">
<UButton label="Hover me" />
</UTooltip>
<UButton label="With icon" icon="i-lucide-rocket" />
<UButton label="Square" icon="i-lucide-star" square />
</div>
</UTheme>
</template>
[!TIP]
컴포넌트에 명시적으로 전달된 prop(예:
<UButton color="primary" />)은 항상<UTheme :props>보다 우선해요. 테마 기본값은 prop이 명시적으로 전달되지 않았을 때만 적용됩니다.
예시 (Examples)
여러 컴포넌트 (Multiple components)
ui나 props에 서로 다른 키를 사용하면 여러 컴포넌트 유형을 한 번에 테마링할 수 있어요.
<template>
<UTheme
:props="{
button: { color: 'neutral', variant: 'outline', size: 'lg' },
input: { size: 'lg' },
select: { size: 'lg' }
}"
:ui="{
button: { base: 'rounded-full' },
input: { base: 'rounded-full' },
select: { base: 'rounded-full' }
}"
>
<div class="flex items-center gap-2">
<UButton label="Button" />
<UInput placeholder="Search..." />
<USelect placeholder="Select" :items="['Item 1', 'Item 2', 'Item 3']" />
</div>
</UTheme>
</template>
중첩 테마 (Nested themes)
여러 Theme 컴포넌트를 중첩해 재정의를 합성할 수 있어요. 가장 안쪽 Theme가 우선하며, 재정의되지 않은 키는 바깥 Theme에서 상속됩니다.
<template>
<UTheme
:ui="{
button: {
base: 'rounded-full'
}
}"
>
<div class="flex flex-col items-start gap-4 border border-muted p-4 rounded-lg">
<div class="flex items-center gap-2">
<UButton label="Outer theme" />
<UButton label="Outer theme" color="neutral" variant="outline" />
</div>
<UTheme
:ui="{
button: {
base: 'font-black uppercase'
}
}"
>
<div class="border border-muted p-4 rounded-lg">
<div class="flex items-center gap-2">
<UButton label="Inner theme" />
<UButton label="Inner theme" color="neutral" variant="outline" />
</div>
</div>
</UTheme>
</div>
</UTheme>
</template>
명시적 우선순위 (Explicit priority)
개별 컴포넌트에 어떤 prop(ui 포함)이든 명시적으로 설정하면 항상 Theme 컴포넌트보다 우선해요.
<template>
<UTheme
:ui="{
button: {
base: 'rounded-full'
}
}"
>
<div class="flex items-center gap-2">
<UButton label="Themed" />
<UButton label="Overridden" :ui="{ base: 'rounded-none' }" />
</div>
</UTheme>
</template>
깊은 전파 (Deep propagation)
재정의는 얼마나 깊게 중첩되어 있든 모든 하위 컴포넌트에 적용돼요.
<script setup lang="ts">
import MyButton from './MyButton.vue'
</script>
<template>
<UTheme
:ui="{
button: {
base: 'rounded-full'
}
}"
>
<UCard :ui="{ body: 'flex items-center gap-2 sm:flex-row flex-col' }">
<UButton label="Direct child" />
<MyButton />
</UCard>
</UTheme>
</template>
[!NOTE]
이 예시에서
MyButton은 내부적으로UButton을 렌더링하는 커스텀 컴포넌트예요. 테마 재정의는 전체 컴포넌트 트리를 통해 전파되므로 여전히 적용됩니다.
폼 컴포넌트 (Form components)
Theme 컴포넌트를 사용해 폼 컴포넌트 그룹 전체에 일관된 스타일을 적용할 수 있어요.
<script setup lang="ts">
import * as z from 'zod'
import type { FormSubmitEvent } from '@nuxt/ui'
const schema = z.object({
name: z.string().min(2, 'Too short'),
email: z.email('Invalid email'),
bio: z.string().optional()
})
type Schema = z.output<typeof schema>
const state = reactive<Partial<Schema>>({
name: 'John Doe',
email: 'john@example.com',
bio: undefined
})
const toast = useToast()
async function onSubmit(event: FormSubmitEvent<Schema>) {
toast.add({ title: 'Saved', description: 'Your profile has been updated.', color: 'success' })
console.log(event.data)
}
</script>
<template>
<UTheme
:props="{
input: { size: 'lg' },
textarea: { size: 'lg' }
}"
:ui="{
formField: {
root: 'flex max-sm:flex-col justify-between gap-4',
wrapper: 'w-full sm:max-w-xs'
}
}"
>
<UForm :schema="schema" :state="state" class="space-y-4 w-full" @submit="onSubmit">
<UFormField label="Name" name="name" description="Your public display name.">
<UInput v-model="state.name" />
</UFormField>
<UFormField label="Email" name="email" description="Used for notifications.">
<UInput v-model="state.email" type="email" />
</UFormField>
<UFormField label="Bio" name="bio" description="A short description about yourself.">
<UTextarea v-model="state.bio" placeholder="Tell us about yourself" />
</UFormField>
<div class="flex justify-end">
<UButton type="submit">
Save changes
</UButton>
</div>
</UForm>
</UTheme>
</template>
[!TIP]
<UFormField>,<UFieldGroup>,<UAvatarGroup>은size,color,highlight에 대해<UTheme :props>보다 우선해요. 검증 오류도 어떤 테마 값보다error색상을 강제합니다.
Prose 컴포넌트 (Prose components)
prose 네임스페이스를 사용해 타이포그래피 컴포넌트를 테마링할 수 있어요. 키는 prose 아래에 중첩됩니다(예: prose.p, prose.code).
<script setup lang="ts">
import { Markdown } from '@comark/vue'
const ui = {
prose: {
p: { base: 'my-2.5 text-sm/6' },
li: { base: 'my-0.5 text-sm/6' },
ul: { base: 'my-2.5' },
ol: { base: 'my-2.5' },
h1: { base: 'text-xl mb-4' },
h2: { base: 'text-lg mt-6 mb-3' },
h3: { base: 'text-base mt-4 mb-2' },
h4: { base: 'text-sm mt-3 mb-1.5' },
code: { base: 'text-xs' },
pre: { root: 'my-2.5', base: 'text-xs/5' },
table: { root: 'my-2.5' },
hr: { base: 'my-5' }
}
}
const value = `## Getting started
This is a paragraph with a **tighter typographic scale** applied through the \`Theme\` component.
- First item
- Second item
- Third item
> The spacing and font sizes are smaller than the defaults, making it ideal for compact content areas.`
</script>
<template>
<UTheme :ui="ui">
<Markdown :value="value" class="*:first:mt-0 *:last:mb-0" />
</UTheme>
</template>
API
Props
/**
* Props for the Theme component
*/
interface ThemeProps {
/**
* Per-component prop defaults that flow through `useComponentProps` to
* every descendant. Each key maps to a partial of that component's props.
*/
props?: ThemeDefaults | undefined;
/**
* Per-component slot class overrides (flat shorthand for `:props.<name>.ui`).
*/
ui?: ThemeUI | undefined;
}
Slots
/**
* Slots for the Theme component
*/
interface ThemeSlots {
default(): any;
}