Top Related Projects
Web Components specifications
Our original Web Component library.
A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
Cybernetically enhanced web apps
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Quick Overview
The WICG/webcomponents repository is a community-driven effort to develop and standardize Web Components specifications. It serves as a collaborative space for discussing, proposing, and refining new features and APIs related to Web Components, aiming to enhance the web platform's capabilities for creating reusable and encapsulated custom elements.
Pros
- Encourages community involvement in shaping web standards
- Promotes the development of interoperable and reusable UI components
- Facilitates the creation of more modular and maintainable web applications
- Provides a platform for discussing and addressing real-world use cases and challenges
Cons
- Standardization process can be slow, potentially delaying the adoption of new features
- Compatibility issues may arise across different browsers during the implementation phase
- Learning curve for developers new to Web Components concepts and best practices
- Limited browser support for some proposed features, requiring polyfills or fallbacks
Code Examples
- Creating a custom element:
class MyElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
:host { display: block; }
</style>
<h1>Hello, Web Components!</h1>
`;
}
}
customElements.define('my-element', MyElement);
- Using Shadow DOM for encapsulation:
class EncapsulatedElement extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'closed' });
const style = document.createElement('style');
style.textContent = `
.internal { color: red; }
`;
const div = document.createElement('div');
div.classList.add('internal');
div.textContent = 'This style is encapsulated';
shadow.appendChild(style);
shadow.appendChild(div);
}
}
customElements.define('encapsulated-element', EncapsulatedElement);
- Using HTML templates:
const template = document.createElement('template');
template.innerHTML = `
<style>
p { font-weight: bold; }
</style>
<p>This content is from a template</p>
`;
class TemplateElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
customElements.define('template-element', TemplateElement);
Getting Started
To start using Web Components in your project:
- Ensure you're using a modern browser that supports Web Components.
- Create a new custom element by extending
HTMLElement
:
class MyComponent extends HTMLElement {
constructor() {
super();
this.innerHTML = '<p>Hello from MyComponent!</p>';
}
}
customElements.define('my-component', MyComponent);
- Use your custom element in HTML:
<my-component></my-component>
For more advanced usage, explore Shadow DOM, HTML Templates, and Custom Element lifecycle callbacks.
Competitor Comparisons
Web Components specifications
Pros of webcomponents
- More active community and development
- Better documentation and examples
- Wider browser support and compatibility
Cons of webcomponents
- Larger codebase and potentially more complex
- May include unnecessary features for some projects
- Slightly steeper learning curve for beginners
Code Comparison
webcomponents:
<template id="my-paragraph">
<p>My paragraph content</p>
</template>
<script>
class MyParagraph extends HTMLElement {
constructor() {
super();
const template = document.getElementById('my-paragraph');
const templateContent = template.content;
this.attachShadow({mode: 'open'}).appendChild(
templateContent.cloneNode(true)
);
}
}
customElements.define('my-paragraph', MyParagraph);
</script>
webcomponents>:
<my-paragraph>My paragraph content</my-paragraph>
<script>
class MyParagraph extends HTMLElement {
connectedCallback() {
this.innerHTML = ``;
}
}
customElements.define('my-paragraph', MyParagraph);
</script>
Note: The code comparison is hypothetical, as both repositories are actually the same project. The comparison is provided to illustrate potential differences in implementation approaches for web components.
Our original Web Component library.
Pros of polymer
- Provides a complete library for building web components with additional features
- Offers a set of pre-built elements and tools for rapid development
- Includes data binding and property observation systems
Cons of polymer
- Larger learning curve due to additional abstractions and Polymer-specific concepts
- Potential performance overhead compared to vanilla web components
- Dependency on the Polymer library, which may not be necessary for simpler projects
Code comparison
webcomponents (vanilla):
<template id="my-element">
<style>/* styles */</style>
<div>Content</div>
</template>
<script>
class MyElement extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'}).appendChild(
document.getElementById('my-element').content.cloneNode(true)
);
}
}
customElements.define('my-element', MyElement);
</script>
polymer:
<dom-module id="my-element">
<template>
<style>/* styles */</style>
<div>Content</div>
</template>
<script>
Polymer({
is: 'my-element',
// Additional Polymer-specific properties and methods
});
</script>
</dom-module>
The webcomponents repository focuses on the core Web Components specifications, while polymer provides a higher-level abstraction and additional features for building web components. The choice between them depends on project requirements and developer preferences.
A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
Pros of Stencil
- Provides a complete toolchain for building web components
- Offers automatic optimizations and performance enhancements
- Includes a virtual DOM for efficient rendering
Cons of Stencil
- Adds an additional layer of abstraction over native web components
- Requires learning Stencil-specific syntax and conventions
- May have a steeper learning curve for developers new to web components
Code Comparison
Stencil component:
@Component({
tag: 'my-component',
styleUrl: 'my-component.css',
shadow: true
})
export class MyComponent {
@Prop() name: string;
render() {
return <div>Hello, {this.name}</div>;
}
}
Native Web Component:
class MyComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.shadowRoot.innerHTML = `<div>Hello, ${this.getAttribute('name')}</div>`;
}
}
customElements.define('my-component', MyComponent);
While Webcomponents focuses on defining and standardizing native web component specifications, Stencil provides a more opinionated and feature-rich approach to building web components. Stencil offers additional tooling and optimizations but requires learning its specific ecosystem, whereas Webcomponents allows for more flexibility in implementation but may require more manual setup and optimization.
Cybernetically enhanced web apps
Pros of Svelte
- Offers a complete framework with built-in state management and reactivity
- Compiles to highly efficient vanilla JavaScript, resulting in smaller bundle sizes
- Provides a simpler, more intuitive syntax for building components
Cons of Svelte
- Smaller ecosystem and community compared to Web Components
- Less flexibility for integration with other frameworks or libraries
- Requires a build step, which may not be suitable for all projects
Code Comparison
Svelte component:
<script>
export let name = 'World';
</script>
<h1>Hello {name}!</h1>
Web Components:
class HelloWorld extends HTMLElement {
connectedCallback() {
this.innerHTML = `<h1>Hello ${this.getAttribute('name') || 'World'}!</h1>`;
}
}
customElements.define('hello-world', HelloWorld);
Key Differences
- Svelte uses a compiler-based approach, while Web Components are native browser features
- Svelte components are typically more concise and easier to read
- Web Components offer better cross-framework compatibility and reusability
- Svelte provides more built-in features for application development, while Web Components focus on encapsulation and reusability
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
Pros of Vue
- Comprehensive framework with built-in state management and routing
- Gentle learning curve and excellent documentation
- Large ecosystem with extensive tooling and community support
Cons of Vue
- Opinionated structure may limit flexibility for some projects
- Potential performance overhead compared to native Web Components
- Requires build step for optimal usage
Code Comparison
Vue component:
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
data() {
return { message: 'Hello, Vue!' }
}
}
</script>
Web Component:
class HelloComponent extends HTMLElement {
connectedCallback() {
this.innerHTML = '<div>Hello, Web Components!</div>';
}
}
customElements.define('hello-component', HelloComponent);
Key Differences
- Vue provides a more structured approach with a full framework
- Web Components offer native browser support without additional libraries
- Vue has a more extensive ecosystem and tooling
- Web Components provide better encapsulation and reusability across different frameworks
Use Cases
- Vue: Large-scale applications, rapid prototyping, projects requiring a full framework
- Web Components: Cross-framework components, lightweight custom elements, projects prioritizing native browser features
The library for web and native user interfaces.
Pros of React
- Robust ecosystem with extensive libraries and tools
- Virtual DOM for efficient rendering and updates
- Strong community support and frequent updates
Cons of React
- Steeper learning curve for beginners
- Requires additional tools for full functionality (e.g., state management)
- Potential performance overhead for simple applications
Code Comparison
React component:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
Web Component:
class Greeting extends HTMLElement {
connectedCallback() {
this.innerHTML = `<h1>Hello, ${this.getAttribute('name')}!</h1>`;
}
}
customElements.define('my-greeting', Greeting);
Key Differences
- React uses JSX syntax, while Web Components use native HTML and JavaScript
- React components are typically function-based, Web Components are class-based
- React requires a build step, Web Components can run directly in the browser
- React offers a more opinionated structure, Web Components provide lower-level APIs
Use Cases
- React: Complex, interactive web applications with frequent updates
- Web Components: Reusable, framework-agnostic UI elements for cross-project use
Both technologies have their strengths, and the choice depends on project requirements, team expertise, and desired level of abstraction.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Web Components
Web Components are a new browser feature that provides a standard component model for the Web, consisting of several pieces maintained in different places:
- Shadow DOM
- Most of the parts are now maintained in DOM Standard, called shadow trees. Some of the parts are still remained in this repository. See Issue #661 for the progress of upstreaming those parts from the partially out-of-date Shadow DOM document.
- Issues against the DOM and HTML Standard.
- Issues raised in this repository.
- The old issue tracker on W3C bugzilla, which is no longer used.
- Custom Elements
- Custom elements were upstreamed into the HTML Standard (and bits in the DOM Standard) and are maintained there.
- Issues against the DOM and HTML Standard.
- Issues raised in this repository.
- The old issue tracker on W3C bugzilla, which is no longer used.
- HTML Templates
- HTML Templates were upstreamed into the HTML Standard and are fully maintained there.
- CSS changes
- The CSS WG works on CSS Scoping and CSS Shadow Parts, which help dealing with shadow trees with various selectors. Various other parts of CSS and its object model are also impacted by shadow trees and directly worked on in the various CSS specificaions.
- Issues against the CSS WG repository.
- JSON, CSS, HTML Modules
- Successor to the abandoned HTML Imports, allows JSON, CSS, and HTML markup to be requested by a component. HTML Modules Spec work is being incubated upstream in whatwg/html (see PR). For new issues, please file against whatwg/html or here in w3c/webcomponents. See also the HTML Modules explainer, initial proposal, and earlier design ideas.
- Issues raised in HTML Standard
- Issues raised in this repository. Includes related issues for other potential module-types, such as CSS and JSON.
- Old issue tracker includes a few issues filed when the feature was first proposed. Please avoid filing new issues there.
Issues
Please file issues in the most specific repository possible, per the above issue pointers. (It's often not this repository.)
Abandoned features:
- HTML Imports
- The current issue tracker.
- The old issue tracker on W3C bugzilla, which is no longer used.
- Note: HTML Modules are intended to replace HTML Imports (see above).
Top Related Projects
Web Components specifications
Our original Web Component library.
A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
Cybernetically enhanced web apps
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot