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 SSD | node_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. ์ค๋ฌด ์ฃผ์์ฌํญ & ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ
-
this.requestUpdate()์ ๋น๋๊ธฐ ๋ ๋๋ง ๋๊ธฐ:- ์ํ๋ฅผ ๋ณ๊ฒฝํ ์งํ DOM ๋
ธ๋๋ฅผ ์ง์ ์ฟผ๋ฆฌ(
this.shadowRoot.querySelector(...))ํ๋ฉด ์์ง ์ด์ ๋ ๋๋ง ๊ฒฐ๊ณผ๊ฐ ๋จ์์์ ์ ์์ต๋๋ค. - ํญ์
await this.updateComplete;๋ฅผ ํธ์ถํ์ฌ DOM ํจ์น๊ฐ ๋ง์ดํฌ๋กํ์คํฌ์์ ์๋ฃ๋์์์ ๋ณด์ฅํ ํ DOM์ ์ ๊ทผํด์ผ ํฉ๋๋ค.
- ์ํ๋ฅผ ๋ณ๊ฒฝํ ์งํ DOM ๋
ธ๋๋ฅผ ์ง์ ์ฟผ๋ฆฌ(
-
Shadow DOM๊ณผ ๊ธ๋ก๋ฒ CSS:
styles = css๋ก ์ ์ธ๋ ์คํ์ผ์ Shadow DOM ๋ด๋ถ๋ก ๊ฒฉ๋ฆฌ๋๋ฏ๋ก ๋ถ๋ชจ ํ์ด์ง์ ๊ธ๋ก๋ฒ CSS ํด๋์ค๋ ํ ๋ง๊ฐ ์ง์ ์นจํฌํ์ง ์์ต๋๋ค.- ์ธ๋ถ ํ
๋ง๋ฅผ ์ง์ํ๋ ค๋ฉด **CSS ์ปค์คํ
ํ๋กํผํฐ(CSS Variables:
var(--theme-primary, #3b82f6))**๋ฅผ ํ์ฉํ๊ฑฐ๋::part(name)์๋ ์์๋ฅผ ๊ฐ๋ฐฉ(part="button")ํ์ญ์์ค.
-
์ด๋ฒคํธ ๋ฒ๋ธ๋ง ๋ฐ ์บก์ํ ๊ฒฝ๊ณ ํต๊ณผ:
- Lit ์ปดํฌ๋ํธ ๋ด๋ถ์์ ๋ฐ์ํ๋ ์ผ๋ฐ DOM ์ด๋ฒคํธ(
click,input)๋ ์๋์ผ๋ก Shadow DOM ๊ฒฝ๊ณ๋ฅผ ํต๊ณผํ์ง๋ง, ์ง์ ์์ฑํ๋new CustomEvent()๋ ๋ฐ๋์bubbles: true, composed: true๋ฅผ ์ง์ ํด์ผ ๋ถ๋ชจ ๋ฌธ์์์ ์ฒญ์ทจํ ์ ์์ต๋๋ค.
- Lit ์ปดํฌ๋ํธ ๋ด๋ถ์์ ๋ฐ์ํ๋ ์ผ๋ฐ DOM ์ด๋ฒคํธ(
-
SSR(Server-Side Rendering) ์ต์ ํ (@lit-labs/ssr):
- Next.js, Nuxt, Astro ๋ฑ ์๋ฒ ํ๊ฒฝ์์ ๋ ๋๋งํ ๋๋
@lit-labs/ssr์ ํตํด ์ ์ธ์ ์๋์ฐ ๋ฃจํธ(Declarative Shadow DOM: <template shadowrootmode="open">)๋ก ์ฌ์ ๋ ๋๋งํ์ฌ ์ฒซ ํ๋ฉด ๊น๋นก์(FOUC)์ ๋ฐฉ์งํ ์ ์์ต๋๋ค.
- Next.js, Nuxt, Astro ๋ฑ ์๋ฒ ํ๊ฒฝ์์ ๋ ๋๋งํ ๋๋
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์