Files
2nd/10_Wiki/Topics/AI_and_ML/모듈식 컴포넌트 (Modular Components).md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

6.9 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-모듈식-컴포넌트-modular-components 모듈식 컴포넌트 (Modular Components) 10_Wiki/Topics verified self
Modular Components
Composable UI
Component-Driven Architecture
none A 0.9 applied
architecture
ui
design-system
composition
2026-05-10 pending
language framework
typescript 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)

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

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)

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)

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)

: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

// 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

🤖 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 정리