Files
2nd/10_Wiki/Topics/AI_and_ML/모듈식 컴포넌트 (Modular Components).md
T
2026-05-10 22:08:15 +09:00

209 lines
6.9 KiB
Markdown

---
id: wiki-2026-0508-모듈식-컴포넌트-modular-components
title: 모듈식 컴포넌트 (Modular Components)
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Modular Components, Composable UI, Component-Driven Architecture]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [architecture, ui, design-system, composition]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: typescript
framework: React 19 / Web Components
---
# 모듈식 컴포넌트 (Modular Components)
## 매 한 줄
> **"매 single-responsibility의 재사용 가능한 unit을 composable interface로 묶어 system을 build"**. 2003 Brad Frost의 Atomic Design → 2013 React 도입 → 2020 design-token 기반 Headless UI (Radix, shadcn) → 2026 현재 Server Components + Web Components의 hybrid composition이 mainstream.
## 매 핵심
### 매 5 Properties of Modular Component
- **Single Responsibility**: 매 component 하나의 명확한 purpose.
- **Encapsulation**: 매 internal state / DOM 외부 격리.
- **Composability**: 매 children / slots 통한 nested 조합 가능.
- **Configurability via props**: 매 behavior 외부 inject (no hard-coded).
- **Testability in isolation**: 매 mock 없이 unit-test 가능.
### 매 Composition Patterns
- **Compound Components**: 매 `<Tabs><Tabs.List/><Tabs.Panel/></Tabs>` — context-shared.
- **Slots / Render Props**: 매 children 위치 customization.
- **Polymorphic `as` prop**: 매 `<Button as="a" href=...>` — element type swap.
- **Headless / Bring-Your-Own-Style**: 매 logic만 제공, style은 consumer 결정 (Radix).
### 매 응용
1. Atomic Design (Atoms → Molecules → Organisms → Templates).
2. Material UI / Chakra / shadcn-ui — design-system implementations.
3. Storybook — component-driven dev / docs.
4. Web Components (LitElement) — framework-agnostic, browser-native.
## 💻 패턴
### Compound Component (React)
```tsx
import { createContext, useContext, useState, ReactNode } from 'react';
const TabsCtx = createContext<{active: string; setActive: (v: string) => void} | null>(null);
export function Tabs({ defaultValue, children }: { defaultValue: string; children: ReactNode }) {
const [active, setActive] = useState(defaultValue);
return <TabsCtx.Provider value={{active, setActive}}>{children}</TabsCtx.Provider>;
}
Tabs.List = ({ children }: { children: ReactNode }) => <div role="tablist">{children}</div>;
Tabs.Trigger = ({ value, children }: { value: string; children: ReactNode }) => {
const ctx = useContext(TabsCtx)!;
return (
<button role="tab" aria-selected={ctx.active === value} onClick={() => ctx.setActive(value)}>
{children}
</button>
);
};
Tabs.Panel = ({ value, children }: { value: string; children: ReactNode }) => {
const ctx = useContext(TabsCtx)!;
return ctx.active === value ? <div role="tabpanel">{children}</div> : null;
};
```
### Polymorphic Component
```tsx
type AsProp<C extends React.ElementType> = { as?: C };
type PolymorphicProps<C extends React.ElementType, P = {}> =
P & AsProp<C> & Omit<React.ComponentPropsWithoutRef<C>, keyof (P & AsProp<C>)>;
export function Box<C extends React.ElementType = 'div'>(
{ as, children, ...rest }: PolymorphicProps<C, { variant?: 'card' | 'inline' }>
) {
const Tag = as || 'div';
return <Tag {...rest}>{children}</Tag>;
}
// usage: <Box as="a" href="/x">link</Box>
```
### Headless Hook (Disclosure)
```tsx
import { useState, useId, useCallback } from 'react';
export function useDisclosure(initial = false) {
const [open, setOpen] = useState(initial);
const id = useId();
return {
isOpen: open,
toggle: useCallback(() => setOpen(o => !o), []),
open: useCallback(() => setOpen(true), []),
close: useCallback(() => setOpen(false), []),
triggerProps: { 'aria-expanded': open, 'aria-controls': id },
panelProps: { id, hidden: !open },
};
}
```
### Web Component (Lit)
```ts
import { LitElement, html, css } from 'lit';
import { customElement, property } from 'lit/decorators.js';
@customElement('ag-button')
export class AgButton extends LitElement {
static styles = css`
button { padding: 0.5rem 1rem; border-radius: 0.375rem; }
:host([variant="primary"]) button { background: var(--ag-primary, #2563eb); color: white; }
`;
@property() variant: 'primary' | 'ghost' = 'primary';
render() {
return html`<button @click=${() => this.dispatchEvent(new CustomEvent('ag-click'))}>
<slot></slot>
</button>`;
}
}
```
### Design Token (CSS variables)
```css
:root {
--color-primary-50: #eff6ff;
--color-primary-500: #3b82f6;
--color-primary-900: #1e3a8a;
--space-1: 0.25rem;
--space-2: 0.5rem;
--radius-md: 0.375rem;
--shadow-sm: 0 1px 2px rgb(0 0 0 / 0.05);
}
.btn {
padding: var(--space-2) var(--space-2);
background: var(--color-primary-500);
border-radius: var(--radius-md);
box-shadow: var(--shadow-sm);
}
```
### React Server Component composition
```tsx
// app/dashboard/page.tsx — Server Component
import { Suspense } from 'react';
import { ChartCard } from '@/components/ChartCard';
import { ClientFilters } from './filters.client';
export default async function Page() {
const data = await fetchDashboardData(); // server-only
return (
<main>
<ClientFilters />
<Suspense fallback={<Skeleton />}>
<ChartCard data={data.revenue} />
</Suspense>
</main>
);
}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| 단일 React app | Compound + headless hooks |
| Cross-framework / micro-frontend | Web Components |
| Design system 배포 | Headless (Radix-style) + design tokens |
| SSR-heavy | RSC + selective Client Components |
| Mobile (RN) | Headless logic + platform-specific render |
**기본값**: Compound components + design tokens + Storybook isolation.
## 🔗 Graph
- 부모: [[Component_Architecture]] · [[Design_System]]
- 변형: [[Headless_UI]] · [[Compound_Components]] · [[Polymorphic_Components]]
- 응용: [[Storybook]] · [[Radix_UI]] · [[shadcn]] · [[Lit]]
- Adjacent: [[Atomic_Design]] · [[Design_Tokens]] · [[React_Server_Components]]
## 🤖 LLM 활용
**언제**: scaffold component variants, prop-type derivation, story / test generation, a11y attribute fill-in.
**언제 X**: 매 design decisions (token system shape, theming model) — 매 human design-system curator 영역.
## ❌ 안티패턴
- **God component**: 매 1000-line component, 다목적 props 30개.
- **Prop drilling 5+ levels**: 매 context / composition으로 refactor.
- **Style leakage**: 매 :global / inherited styles → encapsulation 깨짐.
- **Tight coupling to data fetch**: 매 component 내부에서 직접 fetch → reuse 불가능.
## 🧪 검증 / 중복
- Verified (Brad Frost *Atomic Design*, React docs, Radix UI specs).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — modular component 5-property + composition patterns + RSC 정리 |