Web Components
Веб-компоненты
A set of browser features for building reusable HTML elements with their own behavior and, optionally, their own markup and styles. They work without requiring a particular JavaScript framework.
What Web Components are
Imagine a date badge used on both a Vue dashboard and a plain HTML help page. Instead of writing two implementations, you can ship a JavaScript module that defines <date-badge>. Each page uses the same tag and interacts with it through attributes, JavaScript properties, and events. Unlike a component that only a particular framework knows how to render, the custom tag is recognized and initialized by the browser once its definition is registered.
The browser features behind them
- Custom Elements give a tag its behavior. You define a class, usually extending
HTMLElement, and register it withcustomElements.define(). The tag name must contain a hyphen. Markup can appear before the module loads: when the definition becomes available, the browser can upgrade the existing element in place. - Shadow DOM gives a host element a separate tree for its implementation. A page selector such as
.labelwon't match an internal node just because it has that class. This is style scoping, not total isolation: inherited values, including CSS custom properties, can still reach the contents. A shadow root is optional. - Templates and slots handle markup reuse and composition. A
<template>holds markup that isn't rendered until you insert it or a copy into the DOM. A<slot>marks where content supplied by the caller will be displayed. Slotted nodes remain children in the regular DOM even though they are displayed inside the component.
Example: a badge with caller-supplied text
<status-badge>In review</status-badge>
<script type="module">
class StatusBadge extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({ mode: 'open' });
root.innerHTML = `
<style>span { border: 1px solid; padding: 2px 6px; }</style>
<span><slot></slot></span>`;
}
}
customElements.define('status-badge', StatusBadge);
</script>
The caller supplies “In review” as ordinary HTML text. The component displays it through the slot without reading it or rebuilding its markup. The span rule styles the badge wrapper, not every span on the page. The HTML assigned to innerHTML here is a fixed string; use textContent when inserting plain text from an untrusted source.
Where they fit and what interviewers check
Design systems and embeddable widgets often need to work across several applications. Web Components give them a shared browser-level interface, but don't supply routing or application state management. A custom tag also doesn't gain the semantics or keyboard behavior of a native button by looking like one. Use native controls inside the component when they already provide the required behavior.
Frontend interviews often test lifecycle and data flow. For example, connectedCallback() isn't a one-time initializer: moving an element with common DOM methods can disconnect and reconnect it. If you subscribe to a page-wide event on connection, remove that listener in disconnectedCallback(). For configuration, use attributes for values that can be expressed as strings and JavaScript properties for objects or arrays. Keeping a property and an attribute in sync takes explicit code.