시작하기 (Getting Started)
Kobalte를 설치하고 첫 컴포넌트를 만들어 보는 방법을 안내해요. Kobalte는 SolidJS용 접근성 높은 UI 컴포넌트 라이브러리이므로, 프로젝트에 SolidJS가 이미 설정되어 있어야 해요.
출처: 문서
본문
설치 (Installation)
다음 명령을 실행해서 Kobalte를 설치해요.
npm i @kobalte/core
pnpm add @kobalte/core
yarn add @kobalte/core
bun add @kobalte/core
deno add npm:@kobalte/core
컴포넌트 사용하기 (Using the components)
아래 예시는 Kobalte로 Popover 컴포넌트를 만드는 방법을 보여줘요.
import { Popover } from "@kobalte/core/popover";
import { CrossIcon } from "some-icon-library";
import "./style.css";
function App() {
<Popover>
<Popover.Trigger class="popover__trigger">Learn more</Popover.Trigger>
<Popover.Portal>
<Popover.Content class="popover__content">
<Popover.Arrow />
<div class="popover__header">
<Popover.Title class="popover__title">About Kobalte</Popover.Title>
<Popover.CloseButton class="popover__close-button">
<CrossIcon />
</Popover.CloseButton>
</div>
<Popover.Description class="popover__description">
A UI toolkit for building accessible web apps and design systems with SolidJS.
</Popover.Description>
</Popover.Content>
</Popover.Portal>
</Popover>;
}
.popover__trigger {
appearance: none;
display: inline-flex;
justify-content: center;
align-items: center;
height: 40px;
width: auto;
outline: none;
border: none;
border-radius: 6px;
padding: 0 16px;
background-color: hsl(200 98% 39%);
color: white;
font-size: 16px;
line-height: 0;
transition: 250ms background-color;
}
.popover__trigger:hover {
background-color: hsl(201 96% 32%);
}
.popover__trigger:focus-visible {
outline: 2px solid hsl(200 98% 39%);
outline-offset: 2px;
}
.popover__trigger:active {
background-color: hsl(201 90% 27%);
}
.popover__content {
z-index: 50;
max-width: min(calc(100vw - 16px), 380px);
border: 1px solid hsl(240 5% 84%);
border-radius: 6px;
padding: 12px;
background-color: white;
box-shadow:
0 10px 15px -3px rgb(0 0 0 / 0.1),
0 4px 6px -4px rgb(0 0 0 / 0.1);
}
.popover__header {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 6px;
}
.popover__close-button {
height: 16px;
width: 16px;
color: hsl(240 5% 34%);
}
.popover__title {
font-size: 16px;
font-weight: 500;
color: hsl(240 6% 10%);
}
.popover__description {
font-size: 14px;
color: hsl(240 5% 26%);
}
몇 줄의 코드만으로 완전히 접근성 있는 Popover 컴포넌트를 구현했어요. 이 컴포넌트는:
- WAI-ARIA Dialog 디자인 패턴을 따릅니다.
- 제어(controlled) 또는 비제어(uncontrolled) 방식으로 사용할 수 있어요.
- 선택적으로 방향을 가리키는 화살표(arrow)를 렌더링할 수 있어요.
- 포커스 관리를 완전히 지원하며 커스터마이즈할 수 있어요.