스타일링 (Styling)
Bits UI 컴포넌트에 스타일을 적용하는 방법을 알아볼게요. Bits UI는 설계상 거의 스타일을 배포하지 않기 때문에, 컴포넌트를 스타일링할 때 완전한 자유를 누릴 수 있어요. CSS 프레임워크, 데이터 속성, 전역 클래스, 스코프 스타일, 그리고 상태 기반 스타일링까지 다양한 접근법을 하나씩 살펴볼게요.
출처: 문서
본문
Bits UI는 설계상 거의 스타일을 배포하지 않아서, 컴포넌트를 스타일링할 때 완전한 유연성을 제공해요. HTML 요소를 렌더링하는 각 컴포넌트는 스타일을 컴포넌트에 직접 적용할 수 있는 class prop과 style prop을 노출합니다.
스타일링 접근법
CSS 프레임워크
TailwindCSS나 UnoCSS 같은 CSS 프레임워크를 쓴다면, 클래스를 컴포넌트에 전달하기만 하면 돼요:
<script lang="ts">
import { Accordion } from "bits-ui";
</script>
<Accordion.Trigger class="h-12 w-full bg-blue-500 hover:bg-blue-600"
>Click me</Accordion.Trigger
>
데이터 속성
각 Bits UI 컴포넌트는 렌더링되는 요소에 특정 데이터 속성을 적용해요. 이 속성들은 애플리케이션 전반에서 스타일링할 때 신뢰할 수 있는 선택자를 제공합니다.
app.css
/* Target all Accordion.Trigger components */
[data-accordion-trigger] {
height: 3rem;
width: 100%;
background-color: #3182ce;
color: #fff;
}
레이아웃 컴포넌트에서 스타일시트를 import하면 돼요:
+layout.svelte
<script lang="ts">
import "../app.css";
let { children } = $props();
</script>
{@render children()}
이제 모든 Accordion.Trigger 컴포넌트에 스타일이 적용됩니다.
전역 클래스
다른 방법으로는 전역 클래스 이름을 사용할 수 있어요:
app.css
.accordion-trigger {
height: 3rem;
width: 100%;
background-color: #3182ce;
color: #fff;
}
레이아웃 컴포넌트에서 스타일시트를 import하면 돼요:
+layout.svelte
<script lang="ts">
import "../app.css";
let { children } = $props();
</script>
{@render children()}
컴포넌트와 함께 전역 클래스를 사용해요:
<script lang="ts">
import { Accordion } from "bits-ui";
</script>
<Accordion.Trigger class="accordion-trigger">Click me</Accordion.Trigger>
스코프 스타일
Svelte의 스코프 스타일을 쓰려면 child 스니펫으로 요소를 컴포넌트의 스코프 안으로 가져오면 돼요. 자세한 내용은 Child Snippet 문서를 참고하세요.
MyAccordionTrigger.svelte
<script lang="ts">
import { Accordion } from "bits-ui";
</script>
<Accordion.Trigger>
{#snippet child({ props })}
<button {...props} class="my-accordion-trigger"> Click me! </button>
{/snippet}
</Accordion.Trigger>
<style>
.my-accordion-trigger {
height: 3rem;
width: 100%;
background-color: #3182ce;
color: #fff;
}
</style>
Style Prop
요소를 렌더링하는 모든 Bits UI 컴포넌트는 문자열 또는 CSS 속성 객체 형태의 style prop을 받아들여요. 이 값들은 mergeProps 함수를 통해 내부 스타일과 병합됩니다.
<Accordion.Trigger style="background-color: #3182ce; color: white; padding: 1rem;">
Click me
</Accordion.Trigger>
<!-- Or using an object -->
<Accordion.Trigger
style={{ backgroundColor: "#3182ce", color: "white", padding: "1rem" }}
>
Click me
</Accordion.Trigger>
컴포넌트 상태 스타일링
Bits UI 컴포넌트는 데이터 속성과 CSS 변수를 통해 상태 정보를 노출해서, 컴포넌트 상태에 따라 동적인 스타일을 만들 수 있게 해줘요.
상태 데이터 속성
컴포넌트는 CSS에서 타겟팅할 수 있는 상태별 데이터 속성을 적용해요:
/* Style the Accordion.Trigger when open */
[data-accordion-trigger][data-state="open"] {
background-color: #f0f0f0;
font-weight: bold;
}
/* Style the Accordion.Trigger when closed */
[data-accordion-trigger][data-state="closed"] {
background-color: #ffffff;
}
/* Style disabled components */
[data-accordion-trigger][data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
각 컴포넌트의 API 레퍼런스에서 해당 데이터 속성을 확인할 수 있어요.
CSS 변수
Bits UI 컴포넌트는 내부 컴포넌트 값에 접근할 수 있는 CSS 변수를 노출할 수 있어요. 예를 들어 Select.Content가 앵커(기본적으로 customAnchor를 쓰지 않는 한 Select.Trigger)와 같은 너비가 되도록 하려면 --bits-select-anchor-width CSS 변수를 사용하면 됩니다:
[data-select-content] {
width: var(--bits-select-anchor-width);
min-width: var(--bits-select-anchor-width);
max-width: var(--bits-select-anchor-width);
}
각 컴포넌트가 제공하는 구체적인 CSS 변수는 API 레퍼런스에서 확인할 수 있어요.
예시: 아코디언 스타일링
다양한 상태를 가진 아코디언을 스타일링하는 예시를 볼게요:
<script lang="ts">
import { Accordion } from "bits-ui";
</script>
<Accordion.Root>
<Accordion.Item value="item-1">
<Accordion.Trigger>Section 1</Accordion.Trigger>
<Accordion.Content>Content for section 1</Accordion.Content>
</Accordion.Item>
<Accordion.Item value="item-2">
<Accordion.Trigger disabled>Section 2 (Disabled)</Accordion.Trigger>
<Accordion.Content>Content for section 2</Accordion.Content>
</Accordion.Item>
</Accordion.Root>
<style>
/* Base styles */
:global([data-accordion-item]) {
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
margin-bottom: 0.5rem;
}
/* Trigger styles based on state */
:global([data-accordion-trigger]) {
width: 100%;
padding: 1rem;
display: flex;
justify-content: space-between;
align-items: center;
}
:global([data-accordion-trigger][data-state="open"]) {
background-color: #f7fafc;
border-bottom: 1px solid #e2e8f0;
}
:global([data-accordion-trigger][data-disabled]) {
opacity: 0.5;
cursor: not-allowed;
}
/* Content styles */
:global([data-accordion-content]) {
padding: 1rem;
}
</style>
고급 스타일링 기법
마운트 관리 표면의 CSS 전환
애니메이션을 위해 마운트 수명 주기를 관리하는 컴포넌트는 애니메이션 표면에 일시적인 data-starting-style과 data-ending-style 속성을 노출해요. 팝업 콘텐츠나 오버레이처럼, 언마운트 중에도 닫기 애니메이션을 잃지 않으면서 등장·퇴장 전환을 원하는 부분에서 유용합니다.
[data-popover-content] {
opacity: 1;
transform: scale(1);
transition:
opacity 150ms ease,
transform 150ms ease;
}
[data-popover-content][data-starting-style],
[data-popover-content][data-ending-style] {
opacity: 0;
transform: scale(0.96);
}
데이터 속성과 CSS 변수 조합
데이터 속성과 CSS 변수를 조합하면 컴포넌트 상태에 따라 동적인 스타일을 만들 수 있어요. --bits-accordion-content-height 변수와 data-state 속성으로 아코디언 콘텐츠를 애니메이션하는 방법을 볼게요:
/* Basic transition animation */
[data-accordion-content] {
overflow: hidden;
transition: height 300ms ease-out;
height: 0;
}
[data-accordion-content][data-state="open"] {
height: var(--bits-accordion-content-height);
}
[data-accordion-content][data-state="closed"] {
height: 0;
}
커스텀 키프레임 애니메이션
더 세밀한 제어가 필요하다면 CSS 변수와 함께 키프레임 애니메이션을 사용해요:
/* Define keyframes for opening animation */
@keyframes accordionOpen {
0% {
height: 0;
opacity: 0;
}
80% {
height: var(--bits-accordion-content-height);
opacity: 0.8;
}
100% {
height: var(--bits-accordion-content-height);
opacity: 1;
}
}
/* Define keyframes for closing animation */
@keyframes accordionClose {
0% {
height: var(--bits-accordion-content-height);
opacity: 1;
}
20% {
height: var(--bits-accordion-content-height);
opacity: 0.8;
}
100% {
height: 0;
opacity: 0;
}
}
/* Apply animations based on state */
[data-accordion-content][data-state="open"] {
animation: accordionOpen 400ms cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
[data-accordion-content][data-state="closed"] {
animation: accordionClose 300ms cubic-bezier(0.7, 0, 0.84, 0) forwards;
}
예시: 애니메이션 아코디언
커스텀 전환을 가진 아코디언의 예시를 볼게요:
<script lang="ts">
import { Accordion } from "bits-ui";
</script>
<Accordion.Root type="single">
<Accordion.Item value="item-1">
<Accordion.Trigger>Section 1</Accordion.Trigger>
<Accordion.Content>Content for section 1</Accordion.Content>
</Accordion.Item>
<Accordion.Item value="item-2">
<Accordion.Trigger>Section 2</Accordion.Trigger>
<Accordion.Content>Content for section 2</Accordion.Content>
</Accordion.Item>
</Accordion.Root>
<style>
/* Base styles */
:global([data-accordion-item]) {
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
margin-bottom: 0.5rem;
}
/* Trigger styles based on state */
:global([data-accordion-trigger]) {
width: 100%;
padding: 1rem;
display: flex;
justify-content: space-between;
align-items: center;
}
:global([data-accordion-trigger][data-state="open"]) {
background-color: #f7fafc;
border-bottom: 1px solid #e2e8f0;
}
/* Content styles */
:global([data-accordion-content]) {
overflow: hidden;
transition: height 300ms ease-out;
}
/* Define keyframes for opening animation */
@keyframes -global-accordionOpen {
0% {
height: 0;
opacity: 0;
}
80% {
height: var(--bits-accordion-content-height);
opacity: 0.8;
}
100% {
height: var(--bits-accordion-content-height);
opacity: 1;
}
}
/* Define keyframes for closing animation */
@keyframes -global-accordionClose {
0% {
height: var(--bits-accordion-content-height);
opacity: 1;
}
20% {
height: var(--bits-accordion-content-height);
opacity: 0.8;
}
100% {
height: 0;
opacity: 0;
}
}
/* Apply animations based on state */
:global([data-accordion-content][data-state="open"]) {
animation: accordionOpen 400ms cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
:global([data-accordion-content][data-state="closed"]) {
animation: accordionClose 300ms cubic-bezier(0.7, 0, 0.84, 0) forwards;
}
</style>
더 알아보기 (Learn more)
데이터 속성과 CSS 변수를 효과적으로 쓰려면 아코디언의 API 레퍼런스에서 실제 속성과 변수를 확인하는 게 좋아요. 그리고 요소 자체를 완전히 교체하며 스타일을 입히고 싶다면 child 스니펫 문서를 이어서 보면 돼요.