스타일링 (Styling)
Kobalte 컴포넌트는 스타일이 없는(unstyled) 상태로 제공되어서 시각적 표현을 완전히 커스터마이즈할 수 있어요. 선호하는 스타일링 솔루션(vanilla CSS, Tailwind, CSS-in-JS 라이브러리 등)을 자유롭게 사용해요.
출처: 문서
본문
Kobalte 컴포넌트는 스타일이 없는 상태로 제공되므로, 시각적 표현(look and feel)을 완전히 커스터마이즈할 수 있어요. 선호하는 스타일링 솔루션(vanilla CSS, Tailwind, CSS-in-JS 라이브러리 등)을 가져다 쓰면 돼요.
컴포넌트 파트 스타일링하기 (Styling a component part)
모든 컴포넌트와 그 파트는 class prop을 받아요. 이 class는 DOM 요소에 그대로 전달됩니다. 여러분이 제공한 class를 대상으로 컴포넌트 파트를 스타일링할 수 있어요.
import { Popover as KPopover } from "@kobalte/core/popover";
import "./style.css";
export const Popover = () => {
return (
<KPopover>
<KPopover.Trigger class="popover__trigger">Open</KPopover.Trigger>
<KPopover.Content class="popover__content">...</KPopover.Content>
</KPopover>
);
};
.popover__trigger {
/* The popover trigger style. */
}
.popover__content {
/* The popover content style. */
}
상태 스타일링하기 (Styling a state)
컴포넌트나 그 파트가 여러 상태를 가질 수 있을 때, Kobalte는 해당 상태를 나타내는 data-* 속성을 자동으로 붙여줘요. 예를 들어 popover의 트리거는 다음 상태를 가질 수 있어요:
data-expanded— popover가 펼쳐졌을 때 (expanded).data-disabled— popover가 비활성화되었을 때 (disabled).
Kobalte가 추가한 data-* 속성을 대상으로 컴포넌트 상태를 스타일링할 수 있어요.
.popover__trigger[data-disabled] {
/* The popover trigger style when disabled. */
}
TailwindCSS 플러그인 사용하기 (Using the TailwindCSS plugin)
TailwindCSS v3를 사용 중이라면, @kobalte/tailwindcss 플러그인을 통해 ui-expanded:* 같은 수정자(modifier)로 Kobalte의 data-* 속성을 대상으로 삼을 수 있어요. Tailwind v4부터는 data 속성이 기본적으로 지원되므로 플러그인이 더는 필요하지 않고, 대신 그대로 직접 사용하면 돼요.
설치 (Installation)
npm i @kobalte/tailwindcss
pnpm add @kobalte/tailwindcss
yarn add @kobalte/tailwindcss
bun add @kobalte/tailwindcss
deno add npm:@kobalte/tailwindcss
사용법 (Usage)
tailwind.config.js에 플러그인을 추가해요:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [],
theme: {
extend: {},
},
plugins: [
// default prefix is "ui"
require("@kobalte/tailwindcss"),
// or with a custom prefix:
require("@kobalte/tailwindcss")({ prefix: "kb" }),
],
};
컴포넌트를 스타일링해요:
import { Popover as KPopover } from "@kobalte/core/popover";
export const Popover = () => (
<KPopover>
<KPopover.Trigger class="inline-flex px-4 py-2 rounded ui-disabled:bg-slate-100">
Open
</KPopover.Trigger>
<KPopover.Content class="flex p-4 rounded bg-white">...</KPopover.Content>
</KPopover>
);
다음 수정자들을 사용할 수 있어요:
| Modifier | CSS Selector |
|---|---|
| ui-valid | &[data-valid] |
| ui-invalid | &[data-invalid] |
| ui-required | &[data-required] |
| ui-disabled | &[data-disabled] |
| ui-readonly | &[data-readonly] |
| ui-checked | &[data-checked] |
| ui-indeterminate | &[data-indeterminate] |
| ui-selected | &[data-selected] |
| ui-pressed | &[data-pressed] |
| ui-expanded | &[data-expanded] |
| ui-highlighted | &[data-highlighted] |
| ui-current | &[data-current] |
ui-not-* 형태의 역(inverse) 수정자, 그리고 ui-group-*와 ui-peer-* 형태의 그룹 및 피어(peer) 수정자도 사용할 수 있어요.
Vanilla Extract 플러그인 사용하기 (Using the Vanilla Extract plugin)
Vanilla Extract를 사용 중이라면, @kobalte/vanilla-extract 플러그인으로 Kobalte의 data-* 속성을 대상으로 삼을 수 있어요.
설치 (Installation)
npm i @kobalte/vanilla-extract
pnpm add @kobalte/vanilla-extract
yarn add @kobalte/vanilla-extract
bun add @kobalte/vanilla-extract
deno add npm:@kobalte/vanilla-extract
사용법 (Usage)
componentStateStyles 유틸리티 함수를 사용해서 Kobalte 컴포넌트의 data-* 속성을 대상으로 하는 vanilla-extract 스타일을 만들 수 있어요.
import { componentStateStyles } from "@kobalte/vanilla-extract";
import { style } from "@vanilla-extract/css";
const button = style([
{
background: "blue",
padding: "2px 6px",
},
componentStateStyles({
disabled: {
opacity: 0.4,
},
invalid: {
backgroundColor: "red",
not: {
backgroundColor: "yellow",
},
},
}),
componentStateStyles(
{
invalid: {
backgroundColor: "red",
},
},
{ parentSelector: "[data-theme=dark]" },
),
]);
그런 다음 컴포넌트에 스타일을 적용해요:
import { Button } from "@kobalte/core/button";
import { button } from "./styles.css.ts";
export const MyButton = () => <Button class={button}>...</Button>;
UnoCSS와 함께 사용하기 (Usage with UnoCSS)
커뮤니티가 만든 UnoCSS preset을 사용하면 TailwindCSS 플러그인과 동일한 동작을 얻을 수 있어요.
컴포넌트 확장하기 (Extending a component)
컴포넌트 확장은 일반적인 SolidJS 컴포넌트를 확장하는 방식과 동일해요.
import { Popover as KPopover } from "@kobalte/core/popover";
import { ComponentProps } from "solid-js";
export const PopoverTrigger = (props: ComponentProps<typeof KPopover.Trigger>) => {
return <KPopover.Trigger {...props} />;
};
더 고급 타입 제어와 추가 props가 필요하다면 Polymorphism Guide를 참고하세요.