Web Components
Web Components
Reusable custom HTML elements. Framework-independent.
Web Components
Reusable custom HTML elements. Framework-independent.
Web Components is a set of web standard technologies for creating reusable custom HTML elements. It consists of three core technologies: Custom Elements, Shadow DOM, and HTML Templates, and allows you to build encapsulated components without frameworks such as React or Vue.
Custom Elements define new HTML tags. If you define an element's behavior with a class and register it with customElements.define(), you can use it like <my-component>. Shadow DOM encapsulates styles and markup so that external CSS does not affect the inside.
The biggest advantage of Web Components is framework independence. A component built once can be used anywhere: React, Vue, Angular, or plain HTML. They are used as shared components across frameworks in design systems, widgets, and micro-frontends.
In practice, libraries such as Lit, Stencil, and Fast make Web Components development easier. Large services such as YouTube, GitHub, and Salesforce Lightning actively use Web Components. However, if IE11 support is required, a polyfill is needed.
// Custom Element 정의
class MyCounter extends HTMLElement {
constructor() {
super();
// Shadow DOM 생성 (캡슐화)
this.attachShadow({ mode: 'open' });
this.count = 0;
}
connectedCallback() {
// DOM에 추가될 때 호출
this.render();
this.shadowRoot.querySelector('button')
.addEventListener('click', () => this.increment());
}
increment() {
this.count++;
this.render();
// 커스텀 이벤트 발생
this.dispatchEvent(new CustomEvent('count-changed', {
detail: { count: this.count }
}));
}
render() {
this.shadowRoot.innerHTML = `
<style>
/* Shadow DOM 내부에만 적용 */
button { padding: 8px 16px; cursor: pointer; }
span { font-size: 1.5rem; margin: 0 10px; }
</style>
<button>+</button>
<span>${this.count}</span>
`;
}
}
// 커스텀 요소 등록
customElements.define('my-counter', MyCounter);
// HTML에서 사용
// <my-counter></my-counter>
// React에서 사용
function App() {
const handleCount = (e) => console.log(e.detail.count);
return <my-counter onCount-changed={handleCount} />;
}
"If each team uses a different framework in a micro-frontend, it's best to build the shared components as Web Components. Whether it's the React team or the Vue team, they can use the same design system components."
"Web Components consist of Custom Elements, Shadow DOM, and HTML Templates. Shadow DOM encapsulates styles, so there's no need to worry about external CSS conflicts, and it works regardless of the framework. Using the Lit library lets you write them more declaratively."
"With Shadow DOM, external styles don't leak in, but to intentionally receive theme variables, you need to use CSS Custom Properties (--variables). You can also allow styling of only specific parts with the ::part() pseudo-element."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Web Components require client-side JavaScript. In SSR environments, Declarative Shadow DOM or a hydration strategy is needed.
React does not automatically handle custom events. You need to use a ref or create a wrapper component to attach event listeners directly.
Improve the development experience with libraries such as Lit, and expose CSS Custom Properties to support theming. Providing framework wrappers improves usability.