๐Ÿ”ฅ

Lit 3 & Web Components ์น˜ํŠธ์‹œํŠธ

๊ตฌ๊ธ€(Google) ์˜คํ”ˆ์†Œ์Šค ๊ธฐ๋ฐ˜ ์ดˆ๊ฒฝ๋Ÿ‰ ์›น ํ‘œ์ค€ ์ปดํฌ๋„ŒํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Lit v3.2+ ์™„์ „ ๊ฐ€์ด๋“œ: ์‹œ์Šคํ…œ ์š”๊ตฌ์‚ฌ์–‘, @customElement์™€ @property ๋ฐ˜์‘ํ˜• ์ƒํƒœ, html/css ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๋ฆฌํ„ฐ๋Ÿด, Shadow DOM ์บก์Аํ™”, ์ƒ๋ช…์ฃผ๊ธฐ(Lifecycle), Controller ์žฌ์‚ฌ์šฉ ํŒจํ„ด ๋ฐ ํ”„๋กœ๋•์…˜ ๋ฒˆ๋“ค๋ง

--views
๋‹จ์ˆ˜:
#lit#lit-html#lit-element#web-components#custom-elements#shadow-dom#reactive-properties#vite#typescript#frontend์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-11-07

1. ํ•„์ˆ˜ ์‚ฌ์ „ ์š”๊ฑด & ์‹œ์Šคํ…œ ์š”๊ตฌ์‚ฌ์–‘ (Prerequisites & System Requirements)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
Node.js ๋Ÿฐํƒ€์ž„Node.js v18.18.0+ (LTS)Node.js v22.x LTS ๋˜๋Š” Bun v1.2+์ตœ์‹  ES2022 ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ฐ ๋ชจ๋“ˆ ํ•ด์„๊ธฐ ์ง€์›
ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €npm v9.0+ / pnpm v8.0+pnpm v10.0+ ๋˜๋Š” bun์ดˆ๊ณ ์† ๋ชจ๋“ˆ ์บ์‹ฑ ๋ฐ ๋ชจ๋…ธ๋ ˆํฌ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ ์ตœ์ ํ™”
์šด์˜์ฒด์ œ (OS)macOS 12+, Windows 10/11, Linux (Ubuntu 20.04+)macOS 14+ (Apple Silicon), Windows 11, Linuxํฌ๋กœ์Šค ํ”Œ๋žซํผ ํ‘œ์ค€ ์›น ๋ธŒ๋ผ์šฐ์ € ๋Ÿฐํƒ€์ž„ ํ˜ธํ™˜
๋ธŒ๋ผ์šฐ์ € ์ง€์›Chrome 90+, Safari 14+, Edge 90+, Firefox 90+์ตœ์‹  ์ƒ์šฉ ๋ชจ๋˜ ๋ธŒ๋ผ์šฐ์ €W3C Custom Elements v1 & Shadow DOM v1 ๋„ค์ดํ‹ฐ๋ธŒ ์ง€์›
๋ฉ”๋ชจ๋ฆฌ (RAM)์ตœ์†Œ 4 GB ์ด์ƒ8 GB ~ 16 GB ์ด์ƒVite ๊ฐœ๋ฐœ ์„œ๋ฒ„ HMR ๋ฐ TypeScript ํƒ€์ž… ์ฒด์ปค ๋ฒ„ํผ ํ™•๋ณด
๋””์Šคํฌ (Storage)1 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„5 GB ์ด์ƒ์˜ ๊ณ ์† NVMe SSDnode_modules ๋ฐ ๋ฒˆ๋“ค ๋นŒ๋“œ ์•„ํ‹ฐํŒฉํŠธ ๋ณด๊ด€ ๊ณต๊ฐ„

[!IMPORTANT] ๊ณต์‹ ๋ฒค๋” ์ตœ์‹  ๋ฒ„์ „ ์ •๋ณด (Google / Lit Core Team):

  • Lit ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ: Lit v3.2+ (์•ˆ์ •ํ™” ์ตœ์‹  ๋ฆด๋ฆฌ์Šค, ๊ตฌ๋ฒ„์ „ lit-element / lit-html์„ ํ†ตํ•ฉํ•œ ๊ณต์‹ 3.x ์„ธ๋Œ€)
  • ์ดˆ๊ฒฝ๋Ÿ‰ ๋ฒˆ๋“ค ์‚ฌ์ด์ฆˆ: ์ฝ”์–ด ๋Ÿฐํƒ€์ž„ ํฌ๊ธฐ๊ฐ€ ๋‹จ **~5.2 KB (minified + brotli/gzip)**๋กœ, React(40KB+)๋‚˜ Angular์— ๋น„ํ•ด ๊ทน๋‹จ์ ์œผ๋กœ ๊ฐ€๋ณ๊ณ  ์˜์กด์„ฑ์ด ์—†์Šต๋‹ˆ๋‹ค.
  • W3C ์›น ํ‘œ์ค€ 100% ์ค€์ˆ˜: ํŠน์ • ํ”„๋ ˆ์ž„์›Œํฌ(React, Vue, Angular, Svelte)์— ์ข…์†๋˜์ง€ ์•Š๊ณ , ๋ธŒ๋ผ์šฐ์ € ๋„ค์ดํ‹ฐ๋ธŒ ๊ธฐ์ˆ ์ธ Custom Elements v1, Shadow DOM v1, **HTML <template>**์„ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ž‘์„ฑ๋œ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ชจ๋“  ํ”„๋ ˆ์ž„์›Œํฌ๋‚˜ ๋ฐ”๋‹๋ผ JS/HTML ํ”„๋กœ์ ํŠธ ์–ด๋””์„œ๋“  ํ‘œ์ค€ HTML ํƒœ๊ทธ(<my-counter></my-counter>)์ฒ˜๋Ÿผ ์ฆ‰์‹œ ์‚ฝ์ž…ํ•˜์—ฌ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿš€ 1. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ ํŒจํ‚ค์ง€ ์„ค์น˜

๊ณต์‹ Vite ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜์—ฌ TypeScript ๊ธฐ๋ฐ˜ Lit 3 ํ”„๋กœ์ ํŠธ๋ฅผ ์‹ ์†ํ•˜๊ฒŒ ๋ถ€ํŠธ์ŠคํŠธ๋žฉํ•ฉ๋‹ˆ๋‹ค.

# [1] Vite๋ฅผ ํ†ตํ•œ Lit + TypeScript ํ”„๋กœ์ ํŠธ ์Šค์บํด๋”ฉ
npm create vite@latest my-lit-app -- --template lit-ts

# ๋˜๋Š” pnpm ์‚ฌ์šฉ (๊ถŒ์žฅ)
pnpm create vite my-lit-app --template lit-ts

# [2] ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋™ ๋ฐ ์ข…์†์„ฑ ์„ค์น˜
cd my-lit-app
pnpm install

# [3] Lit ์ตœ์‹  ์•ˆ์ •ํ™” ์ฝ”์–ด ํŒจํ‚ค์ง€ ์„ค์น˜ ํ™•์ธ
pnpm add lit@^3.2.1

# [4] ๊ฐœ๋ฐœ ์„œ๋ฒ„ ์‹คํ–‰
pnpm run dev
# Vite ๋กœ์ปฌ ์„œ๋ฒ„ ์‹คํ–‰ ํ™•์ธ: http://localhost:5173

TypeScript ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ(experimentalDecorators)๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋„๋ก tsconfig.json์„ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "experimentalDecorators": true,
    "useDefineForClassFields": false,
    "strict": true,
    "lib": ["ES2022", "DOM", "DOM.Iterable"]
  },
  "include": ["src"]
}

2. ๋ฉ˜ํƒˆ ๋ชจ๋ธ & ์•„ํ‚คํ…์ฒ˜ ๋‹ค์ด์–ด๊ทธ๋žจ (Mental Model & Architecture)

Lit ์ปดํฌ๋„ŒํŠธ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ํ‘œ์ค€ HTMLElement๋ฅผ ์ƒ์†ํ•˜๋Š” LitElement ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค. ์ƒํƒœ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋น„๋™๊ธฐ ๋งˆ์ดํฌ๋กœํƒœ์Šคํฌ ๋ Œ๋” ํ๋ฅผ ํ†ตํ•ด ์ตœ์ ํ™”๋œ DOM ํŒจ์น˜๋งŒ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

+-----------------------------------------------------------------------------------+
|                           Lit 3 Component Lifecycle Architecture                  |
+-----------------------------------------------------------------------------------+
                                                                                     
  [1] ์‚ฌ์šฉ์ž ์ƒํ˜ธ์ž‘์šฉ ๋˜๋Š” ์™ธ๋ถ€ ์†์„ฑ ๋ณ€๊ฒฝ (Property / Attribute Change)                     
       |                                                                            
       v                                                                            
  +-------------------------------------------------------------------------------+ 
  |  @property() / @state() Setter ํ˜ธ์ถœ                                           | 
  |  - oldValue !== newValue ๊ฒ€์‚ฌ (hasChanged)                                      | 
  |  - requestUpdate() ํŠธ๋ฆฌ๊ฑฐ                                                      | 
  +-------------------------------------------------------------------------------+ 
       |                                                                            
       v                                                                            
  [2] ๋น„๋™๊ธฐ ๋งˆ์ดํฌ๋กœํƒœ์Šคํฌ ๋ฐฐ์น˜ ํ (Microtask Batching Queue)                              
  +-------------------------------------------------------------------------------+ 
  |  ๋™์ผ ํ”„๋ ˆ์ž„ ๋‚ด ์—ฌ๋Ÿฌ ์†์„ฑ์ด ์—ฐ์† ๋ณ€๊ฒฝ๋˜์–ด๋„ ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€ํ•˜๊ณ                | 
  |  ๋‹จ ํ•œ ๋ฒˆ์˜ update()๋กœ ์ผ๊ด„ ํ†ตํ•ฉ ์ฒ˜๋ฆฌ                                            | 
  +-------------------------------------------------------------------------------+ 
       |                                                                            
       v                                                                            
  [3] ๋ Œ๋”๋ง ๋ฐ Shadow DOM ์ฃผ์ž… (Render & Shadow DOM)                                
  +-------------------------------------------------------------------------------+ 
  |  render() ๋ฉ”์„œ๋“œ ์‹คํ–‰ -> lit-html TemplateResult ๋ฐ˜ํ™˜                           | 
  |  - html`<div>${this.count}</div>`                                             | 
  |  - ๊ฐ€์ƒ DOM(VDOM) ์—†์ด ๋ณ€๊ฒฝ๋œ ํ‘œํ˜„์‹ ๋…ธ๋“œ๋งŒ ๋ธŒ๋ผ์šฐ์ € ๋„ค์ดํ‹ฐ๋ธŒ ํŒŒํŠธ๋กœ ์ง์ ‘ ๊ณ ์† ํŒจ์น˜    | 
  |  - css`...` ์Šคํƒ€์ผ์ด ShadowRoot์— CSSStyleSheet(Constructable Sheets)๋กœ ์บก์Аํ™” | 
  +-------------------------------------------------------------------------------+ 
       |                                                                            
       v                                                                            
  [4] ์™„๋ฃŒ ์•Œ๋ฆผ (updated / updateComplete Promise Resolve)                           
  +-------------------------------------------------------------------------------+ 
  |  DOM ๋ฐ˜์˜ ์™„๋ฃŒ -> await el.updateComplete ๋Œ€๊ธฐ ๊ฐ€๋Šฅ                              | 
  +-------------------------------------------------------------------------------+ 

3. ๋‹จ๊ณ„๋ณ„ ์‹ค์Šต (Step-by-Step Walkthrough)

[์ดˆ๋ณด์ž 1๋‹จ๊ณ„] ํ—ฌ๋กœ์›”๋“œ: ์นด์šดํ„ฐ ์ปดํฌ๋„ŒํŠธ

์™„์ „ํ•œ ์ž์ฒด ๊ฒฉ๋ฆฌ(Shadow DOM)์™€ ๋ฐ˜์‘ํ˜• ์ƒํƒœ๋ฅผ ๊ฐ–์ถ˜ ์ฒซ ๋ฒˆ์งธ Lit 3 ์ปค์Šคํ…€ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

// src/my-counter.ts
import { LitElement, html, css } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';

/**
 * @customElement('my-counter')
 * ๋ธŒ๋ผ์šฐ์ €์˜ window.customElements.define('my-counter', MyCounter)๋ฅผ ์ž๋™ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
 */
@customElement('my-counter')
export class MyCounter extends LitElement {
  // ์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€ HTML ์†์„ฑ(Attribute)๊ณผ ์ž๋™ ๋™๊ธฐํ™”๋˜๋Š” ํผ๋ธ”๋ฆญ ํ”„๋กœํผํ‹ฐ
  @property({ type: String })
  heading = 'Lit 3 ์นด์šดํ„ฐ';

  // ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ๋งŒ ๊ด€๋ฆฌ๋˜๋Š” ํ”„๋ผ์ด๋น— ๋ฐ˜์‘ํ˜• ์ƒํƒœ
  @state()
  private count = 0;

  // Shadow DOM์— ์บก์Аํ™”๋˜์–ด ์™ธ๋ถ€ CSS์™€ ์ ˆ๋Œ€ ์ถฉ๋Œํ•˜์ง€ ์•Š๋Š” ๊ณ ์„ฑ๋Šฅ ์Šคํƒ€์ผ
  static styles = css`
    :host {
      display: block;
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
      padding: 1.5rem;
      border-radius: 12px;
      border: 1px solid #e2e8f0;
      background: #ffffff;
      box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
      max-width: 320px;
      margin: 1rem auto;
      text-align: center;
    }

    h3 {
      margin: 0 0 1rem;
      color: #1e293b;
      font-size: 1.25rem;
    }

    .display {
      font-size: 2.5rem;
      font-weight: 800;
      color: #3b82f6;
      margin-bottom: 1rem;
      font-variant-numeric: tabular-nums;
    }

    .btn-group {
      display: flex;
      gap: 0.5rem;
      justify-content: center;
    }

    button {
      padding: 0.5rem 1rem;
      border: none;
      border-radius: 6px;
      background: #0ea5e9;
      color: white;
      font-weight: 600;
      cursor: pointer;
      transition: background 0.2s ease, transform 0.1s ease;
    }

    button:hover {
      background: #0284c7;
    }

    button:active {
      transform: scale(0.96);
    }
  `;

  private increment() {
    this.count += 1;
    // ์ปค์Šคํ…€ ์ด๋ฒคํŠธ(CustomEvent) ๋””์ŠคํŒจ์น˜: ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋‚˜ ์™ธ๋ถ€ ํ”„๋ ˆ์ž„์›Œํฌ์™€ ํ†ต์‹ 
    this.dispatchEvent(
      new CustomEvent('count-changed', {
        detail: { count: this.count },
        bubbles: true,
        composed: true, // Shadow DOM ๊ฒฝ๊ณ„๋ฅผ ํ†ต๊ณผํ•˜์—ฌ ์™ธ๋ถ€ ๋ฌธ์„œ๋กœ ์ „ํŒŒ
      })
    );
  }

  private decrement() {
    if (this.count > 0) {
      this.count -= 1;
      this.dispatchEvent(
        new CustomEvent('count-changed', {
          detail: { count: this.count },
          bubbles: true,
          composed: true,
        })
      );
    }
  }

  // ์„ ์–ธํ˜• ๋ Œ๋”๋ง ํ…œํ”Œ๋ฆฟ
  render() {
    return html`
      <h3>${this.heading}</h3>
      <div class="display">${this.count}</div>
      <div class="btn-group">
        <button @click=${this.decrement} aria-label="๊ฐ์†Œ">-1</button>
        <button @click=${this.increment} aria-label="์ฆ๊ฐ€">+1</button>
      </div>
    `;
  }
}

// TypeScript ์ „์—ญ JSX/HTML ํƒ€์ž… ๋“ฑ๋ก (ํƒ€ ํ”„๋ ˆ์ž„์›Œํฌ ์—ฐ๋™ ์‹œ ์ž๋™์™„์„ฑ ์ง€์›)
declare global {
  interface HTMLElementTagNameMap {
    'my-counter': MyCounter;
  }
}

HTML์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• (index.html):

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <title>Lit 3 ์‹œ์ž‘ํ•˜๊ธฐ</title>
    <script type="module" src="/src/my-counter.ts"></script>
  </head>
  <body>
    <!-- ํ‘œ์ค€ HTML ํƒœ๊ทธ์ฒ˜๋Ÿผ ์†์„ฑ์„ ์ „๋‹ฌํ•˜์—ฌ ์ฆ‰์‹œ ๋ Œ๋”๋ง -->
    <my-counter heading="์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ˆ˜๋Ÿ‰"></my-counter>

    <script>
      const counterEl = document.querySelector('my-counter');
      // Lit์ด ๋ฐฉ์ถœํ•œ ๋„ค์ดํ‹ฐ๋ธŒ ์ปค์Šคํ…€ ์ด๋ฒคํŠธ ์ฒญ์ทจ
      counterEl.addEventListener('count-changed', (e) => {
        console.log('[์ด๋ฒคํŠธ ์ˆ˜์‹ ] ํ˜„์žฌ ์นด์šดํŠธ:', e.detail.count);
      });
    </script>
  </body>
</html>

[2๋‹จ๊ณ„] ํ•ต์‹ฌ ๊ธฐ๋Šฅ: ๋ฐ˜์‘ํ˜• ํ”„๋กœํผํ‹ฐ, ์ง€์‹œ๋ฌธ(Directives) ๋ฐ ์Šฌ๋กฏ(Slots)

Lit 3์—์„œ ๋ชฉ๋ก ๋ Œ๋”๋ง(map), ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง(when), ์Šคํƒ€์ผ ๋ฐ”์ธ๋”ฉ(styleMap), ๊ทธ๋ฆฌ๊ณ  ์™ธ๋ถ€ ์ฝ˜ํ…์ธ  ์ฃผ์ž…(<slot>)์„ ์‚ฌ์šฉํ•˜๋Š” ์ •์„ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

// src/user-card.ts
import { LitElement, html, css } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { map } from 'lit/directives/map.js';
import { when } from 'lit/directives/when.js';
import { classMap } from 'lit/directives/class-map.js';

interface UserSkill {
  name: string;
  level: 'Beginner' | 'Intermediate' | 'Expert';
}

@customElement('user-card')
export class UserCard extends LitElement {
  @property({ type: String })
  username = 'ํ™๊ธธ๋™';

  @property({ type: String })
  role = 'Fullstack Engineer';

  @property({ type: Boolean })
  isOnline = false;

  @property({ type: Array })
  skills: UserSkill[] = [
    { name: 'TypeScript', level: 'Expert' },
    { name: 'Lit 3', level: 'Expert' },
    { name: 'Kubernetes', level: 'Intermediate' }
  ];

  static styles = css`
    :host {
      display: block;
      border: 1px solid #cbd5e1;
      border-radius: 8px;
      padding: 1.25rem;
      background: white;
    }

    .badge-status {
      display: inline-block;
      width: 10px;
      height: 10px;
      border-radius: 50%;
      background-color: #94a3b8;
      margin-right: 6px;
    }

    .badge-status.online {
      background-color: #10b981;
      box-shadow: 0 0 6px rgba(16, 185, 129, 0.6);
    }

    .badge-status.offline {
      background-color: #ef4444;
    }

    ul.skill-list {
      list-style: none;
      padding: 0;
      margin: 0.75rem 0 0;
      display: flex;
      flex-wrap: wrap;
      gap: 0.4rem;
    }

    .skill-tag {
      background: #f1f5f9;
      color: #334155;
      padding: 0.2rem 0.5rem;
      border-radius: 4px;
      font-size: 0.8rem;
      font-weight: 500;
    }

    /* Named Slot ๋ฐ Default Slot ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ */
    ::slotted([slot="bio"]) {
      color: #64748b;
      font-size: 0.9rem;
      line-height: 1.4;
      margin-top: 0.5rem;
    }
  `;

  render() {
    return html`
      <div class="card-container">
        <header>
          <span
            class=${classMap({
              'badge-status': true,
              online: this.isOnline,
              offline: !this.isOnline,
            })}
          ></span>
          <strong>${this.username}</strong>
          <span>(${this.role})</span>
        </header>

        <!-- Named Slot: ์™ธ๋ถ€ ๋งˆํฌ์—… ์‚ฝ์ž… ํ—ˆ์šฉ -->
        <slot name="bio"></slot>

        <!-- ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง (when directive) -->
        ${when(
          this.isOnline,
          () => html`<p style="color: #10b981; font-size: 0.8rem; margin: 0.5rem 0 0;">ํ˜„์žฌ ํ™œ๋™ ์ค‘์ž…๋‹ˆ๋‹ค.</p>`,
          () => html`<p style="color: #64748b; font-size: 0.8rem; margin: 0.5rem 0 0;">ํ˜„์žฌ ์˜คํ”„๋ผ์ธ์ž…๋‹ˆ๋‹ค.</p>`
        )}

        <!-- ๋ชฉ๋ก ๋ Œ๋”๋ง (map directive) -->
        <ul class="skill-list">
          ${map(
            this.skills,
            (skill) => html`
              <li class="skill-tag">
                ${skill.name} (${skill.level})
              </li>
            `
          )}
        </ul>

        <!-- Default Slot: ์ž์‹ ์š”์†Œ ์ž๋™ ์ฃผ์ž… -->
        <slot></slot>
      </div>
    `;
  }
}

[3๋‹จ๊ณ„] ์‹ค๋ฌด ์‘์šฉ ํŒจํ„ด: ๋ฐ˜์‘ํ˜• ์ปจํŠธ๋กค๋Ÿฌ(Reactive Controller)๋กœ API ๋ฐ์ดํ„ฐ ๋น„๋™๊ธฐ ์กฐํšŒ

React์˜ Custom Hook์— ํ•ด๋‹นํ•˜๋Š” Lit์˜ ํ•ต์‹ฌ ์ƒํƒœ ์žฌ์‚ฌ์šฉ ๋„๊ตฌ๋Š” Reactive Controller์ž…๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ์™„๋ฒฝํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด HTTP ํ†ต์‹ , ํƒ€์ด๋จธ, ์„ผ์„œ ์ด๋ฒคํŠธ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ๋ชจ๋“ˆํ™”ํ•ฉ๋‹ˆ๋‹ค.

// src/controllers/fetch-controller.ts
import { ReactiveController, ReactiveControllerHost } from 'lit';

export interface FetchResult<T> {
  data: T | null;
  error: Error | null;
  loading: boolean;
}

export class FetchController<T> implements ReactiveController {
  private host: ReactiveControllerHost;
  private url: string;

  data: T | null = null;
  error: Error | null = null;
  loading = false;

  constructor(host: ReactiveControllerHost, url: string) {
    this.host = host;
    this.url = url;
    // ํ˜ธ์ŠคํŠธ ์ปดํฌ๋„ŒํŠธ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด ํ›…์— ์ž๋™ ๋“ฑ๋ก
    this.host.addController(this);
  }

  // ์ปดํฌ๋„ŒํŠธ๊ฐ€ DOM์— ๋งˆ์šดํŠธ๋  ๋•Œ ์ž๋™ ํ˜ธ์ถœ
  hostConnected() {
    this.fetchData();
  }

  async fetchData() {
    this.loading = true;
    this.error = null;
    this.host.requestUpdate();

    try {
      const res = await fetch(this.url);
      if (!res.ok) throw new Error(`HTTP ์š”์ฒญ ์˜ค๋ฅ˜: ${res.status}`);
      this.data = await res.json();
    } catch (err) {
      this.error = err instanceof Error ? err : new Error(String(err));
    } finally {
      this.loading = false;
      this.host.requestUpdate();
    }
  }
}

๋ฐ˜์‘ํ˜• ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์†Œ๋น„ํ•˜๋Š” Lit ์ปดํฌ๋„ŒํŠธ:

// src/post-viewer.ts
import { LitElement, html, css } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { FetchController } from './controllers/fetch-controller';

interface Post {
  id: number;
  title: string;
  body: string;
}

@customElement('post-viewer')
export class PostViewer extends LitElement {
  @property({ type: Number })
  postId = 1;

  // ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์ปจํŠธ๋กค๋Ÿฌ ์ธ์Šคํ„ด์Šคํ™”
  private postApi = new FetchController<Post>(
    this,
    'https://jsonplaceholder.typicode.com/posts/1'
  );

  static styles = css`
    :host {
      display: block;
      font-family: var(--font-sans, system-ui);
      padding: 1rem;
      border: 1px solid #e2e8f0;
      border-radius: 8px;
    }
    .loading {
      color: #3b82f6;
      font-weight: 600;
    }
    .error {
      color: #ef4444;
      background: #fef2f2;
      padding: 0.5rem;
      border-radius: 4px;
    }
    .post-title {
      font-size: 1.1rem;
      margin: 0 0 0.5rem;
      color: #0f172a;
    }
  `;

  render() {
    if (this.postApi.loading) {
      return html`<div class="loading">โณ ๊ฒŒ์‹œ๊ธ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘...</div>`;
    }

    if (this.postApi.error) {
      return html`<div class="error">โŒ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: ${this.postApi.error.message}</div>`;
    }

    const post = this.postApi.data;
    if (!post) {
      return html`<div>๊ฒŒ์‹œ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.</div>`;
    }

    return html`
      <article>
        <h4 class="post-title">#${post.id} ${post.title}</h4>
        <p style="color: #475569; line-height: 1.5;">${post.body}</p>
        <button @click=${() => this.postApi.fetchData()} style="margin-top: 0.5rem; padding: 4px 8px;">
          ์ƒˆ๋กœ๊ณ ์นจ
        </button>
      </article>
    `;
  }
}

4. ํ”„๋ ˆ์ž„์›Œํฌ ์—ฐ๋™ ๊ฐ€์ด๋“œ (React & Vue 3)

Lit 3 ์ปดํฌ๋„ŒํŠธ๋Š” ํ‘œ์ค€ Custom Element์ด๋ฏ€๋กœ ๋ชจ๋“  ํ”„๋ ˆ์ž„์›Œํฌ์™€ ์ฆ‰์‹œ ์—ฐ๋™๋ฉ๋‹ˆ๋‹ค.

โš›๏ธ React 19 / Next.js ์—ฐ๋™ (@lit/react)

React 19๋ถ€ํ„ฐ๋Š” ์ปค์Šคํ…€ ์—˜๋ฆฌ๋จผํŠธ ์†์„ฑ๊ณผ ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ์„ ๋„ค์ดํ‹ฐ๋ธŒ ์ง€์›ํ•˜์ง€๋งŒ, ํ•˜์œ„ ํ˜ธํ™˜ ๋ฐ ์™„์ „ํ•œ TypeScript ์ž๋™ ์™„์„ฑ์„ ์œ„ํ•ด @lit/react ๋ž˜ํผ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

pnpm add @lit/react
// ReactComponent.tsx
import React from 'react';
import { createComponent } from '@lit/react';
import { MyCounter as MyCounterEl } from './my-counter';

// React์šฉ TSX ๋ž˜ํผ ์ปดํฌ๋„ŒํŠธ ์„ ์–ธ
export const MyCounter = createComponent({
  tagName: 'my-counter',
  elementClass: MyCounterEl,
  react: React,
  events: {
    onCountChanged: 'count-changed', // Lit CustomEvent๋ฅผ React on* ํ•ธ๋“ค๋Ÿฌ๋กœ ๋งคํ•‘
  },
});

// React ๋‚ด๋ถ€์—์„œ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉ
export function App() {
  return (
    <MyCounter
      heading="React ์•ฑ ๋‚ด๋ถ€์—์„œ ๋™์ž‘ํ•˜๋Š” Lit"
      onCountChanged={(e) => console.log('์ƒˆ ์นด์šดํŠธ:', e.detail.count)}
    />
  );
}

๐Ÿ’š Vue 3 ์—ฐ๋™ (vite.config.ts)

Vue 3 ํ…œํ”Œ๋ฆฟ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ปค์Šคํ…€ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ Vue ์ปดํฌ๋„ŒํŠธ๋กœ ์ฐฉ๊ฐํ•˜์—ฌ ๊ฒฝ๊ณ ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋„๋ก ํƒœ๊ทธ ํ”„๋ฆฌํ”ฝ์Šค๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

// vite.config.ts (Vue 3 ํ”„๋กœ์ ํŠธ)
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          // 'my-'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ํƒœ๊ทธ๋ฅผ ์ปค์Šคํ…€ ์—˜๋ฆฌ๋จผํŠธ๋กœ ์ธ์‹
          isCustomElement: (tag) => tag.startsWith('my-'),
        },
      },
    }),
  ],
});

5. ์‹ค๋ฌด ์ฃผ์˜์‚ฌํ•ญ & ํ”„๋กœ๋•์…˜ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  1. this.requestUpdate()์™€ ๋น„๋™๊ธฐ ๋ Œ๋”๋ง ๋Œ€๊ธฐ:

    • ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•œ ์งํ›„ DOM ๋…ธ๋“œ๋ฅผ ์ง์ ‘ ์ฟผ๋ฆฌ(this.shadowRoot.querySelector(...))ํ•˜๋ฉด ์•„์ง ์ด์ „ ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๊ฐ€ ๋‚จ์•„์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•ญ์ƒ await this.updateComplete;๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ DOM ํŒจ์น˜๊ฐ€ ๋งˆ์ดํฌ๋กœํƒœ์Šคํฌ์—์„œ ์™„๋ฃŒ๋˜์—ˆ์Œ์„ ๋ณด์žฅํ•œ ํ›„ DOM์— ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  2. Shadow DOM๊ณผ ๊ธ€๋กœ๋ฒŒ CSS:

    • styles = css๋กœ ์„ ์–ธ๋œ ์Šคํƒ€์ผ์€ Shadow DOM ๋‚ด๋ถ€๋กœ ๊ฒฉ๋ฆฌ๋˜๋ฏ€๋กœ ๋ถ€๋ชจ ํŽ˜์ด์ง€์˜ ๊ธ€๋กœ๋ฒŒ CSS ํด๋ž˜์Šค๋‚˜ ํ…Œ๋งˆ๊ฐ€ ์ง์ ‘ ์นจํˆฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ์™ธ๋ถ€ ํ…Œ๋งˆ๋ฅผ ์ง€์›ํ•˜๋ ค๋ฉด **CSS ์ปค์Šคํ…€ ํ”„๋กœํผํ‹ฐ(CSS Variables: var(--theme-primary, #3b82f6))**๋ฅผ ํ™œ์šฉํ•˜๊ฑฐ๋‚˜ ::part(name) ์Šˆ๋„ ์š”์†Œ๋ฅผ ๊ฐœ๋ฐฉ(part="button")ํ•˜์‹ญ์‹œ์˜ค.
  3. ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง ๋ฐ ์บก์Аํ™” ๊ฒฝ๊ณ„ ํ†ต๊ณผ:

    • Lit ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ผ๋ฐ˜ DOM ์ด๋ฒคํŠธ(click, input)๋Š” ์ž๋™์œผ๋กœ Shadow DOM ๊ฒฝ๊ณ„๋ฅผ ํ†ต๊ณผํ•˜์ง€๋งŒ, ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” new CustomEvent()๋Š” ๋ฐ˜๋“œ์‹œ bubbles: true, composed: true๋ฅผ ์ง€์ •ํ•ด์•ผ ๋ถ€๋ชจ ๋ฌธ์„œ์—์„œ ์ฒญ์ทจํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. SSR(Server-Side Rendering) ์ตœ์ ํ™” (@lit-labs/ssr):

    • Next.js, Nuxt, Astro ๋“ฑ ์„œ๋ฒ„ ํ™˜๊ฒฝ์—์„œ ๋ Œ๋”๋งํ•  ๋•Œ๋Š” @lit-labs/ssr์„ ํ†ตํ•ด ์„ ์–ธ์  ์„€๋„์šฐ ๋ฃจํŠธ(Declarative Shadow DOM: <template shadowrootmode="open">)๋กœ ์‚ฌ์ „ ๋ Œ๋”๋งํ•˜์—ฌ ์ฒซ ํ™”๋ฉด ๊นœ๋นก์ž„(FOUC)์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500