All Projects → preactjs → Preact Custom Element

preactjs / Preact Custom Element

Licence: mit
Wrap your component up as a custom element

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Preact Custom Element

Bolt
The Bolt Design System provides robust Twig and Web Component-powered UI components, reusable visual styles, and powerful tooling to help developers, designers, and content authors build, maintain, and scale best of class digital experiences.
Stars: ✭ 186 (-12.26%)
Mutual labels:  web-components, custom-elements, preact
Omi
Front End Cross-Frameworks Framework - 前端跨框架跨平台框架
Stars: ✭ 12,153 (+5632.55%)
Mutual labels:  web-components, custom-elements, preact
Dataformsjs
🌟 DataFormsJS 🌟 A minimal JavaScript Framework and standalone React and Web Components for rapid development of high quality websites and single page applications.
Stars: ✭ 95 (-55.19%)
Mutual labels:  web-components, custom-elements, preact
Time Elements
Web component extensions to the standard <time> element.
Stars: ✭ 2,318 (+993.4%)
Mutual labels:  web-components, custom-elements
Custom Elements
All about HTML Custom Elements
Stars: ✭ 188 (-11.32%)
Mutual labels:  web-components, custom-elements
Clipboard Copy Element
Copy element text content or input values to the clipboard.
Stars: ✭ 198 (-6.6%)
Mutual labels:  web-components, custom-elements
Auto Check Element
An input element that validates its value with a server endpoint.
Stars: ✭ 85 (-59.91%)
Mutual labels:  web-components, custom-elements
Elm Canvas
A canvas drawing library for Elm
Stars: ✭ 124 (-41.51%)
Mutual labels:  web-components, custom-elements
Nutmeg
Build, test, and publish vanilla Web Components with a little spice
Stars: ✭ 111 (-47.64%)
Mutual labels:  web-components, custom-elements
Virtual Dom
关于Vue,React,Preact和Omi等框架源码的解读
Stars: ✭ 139 (-34.43%)
Mutual labels:  web-components, preact
Ion Phaser
A web component to use Phaser Framework with Angular, React, Vue, etc 🎮
Stars: ✭ 152 (-28.3%)
Mutual labels:  web-components, custom-elements
Markdown Toolbar Element
Markdown formatting buttons for text inputs.
Stars: ✭ 160 (-24.53%)
Mutual labels:  web-components, custom-elements
Vanilla Hamburger
Animated hamburger menu icons for modern web apps (1.8 KB) 🍔
Stars: ✭ 110 (-48.11%)
Mutual labels:  web-components, custom-elements
Aybolit
Lightweight web components library built with LitElement.
Stars: ✭ 90 (-57.55%)
Mutual labels:  web-components, custom-elements
Shadow Dom In Depth
Everything you need to know about Shadow DOM
Stars: ✭ 191 (-9.91%)
Mutual labels:  web-components, custom-elements
Image Crop Element
A custom element for cropping a square image. Returns x, y, width, and height.
Stars: ✭ 115 (-45.75%)
Mutual labels:  web-components, custom-elements
Task Lists Element
Drag and drop task list items.
Stars: ✭ 73 (-65.57%)
Mutual labels:  web-components, custom-elements
Use Custom Element
Custom hook to bridge Custom Elements (Web Components) to React.
Stars: ✭ 77 (-63.68%)
Mutual labels:  web-components, custom-elements
Html Include Element
Easily include external HTML into your pages.
Stars: ✭ 143 (-32.55%)
Mutual labels:  web-components, custom-elements
Swiss
🇨🇭Functional custom elements
Stars: ✭ 169 (-20.28%)
Mutual labels:  web-components, custom-elements

preact-custom-element

Generate/register a custom element from a preact component. As of 3.0.0, this library implements the Custom Elements v1 spec. Previous versions (< 3.0.0) implemented the v0 proposal, which was only implemented in Chrome and is abandoned.

Usage

Import CustomElement and call with your component a tag name *, and a list of attribute names you want to observe:

import register from 'preact-custom-element';

const Greeting = ({ name = 'World' }) => (
  <p>Hello, {name}!</p>
);

register(Greeting, 'x-greeting', ['name']);

* Note: as per the Custom Elements specification, the tag name must contain a hyphen.

Use the new tag name in HTML, attribute keys and values will be passed in as props:

<x-greeting name="Billy Jo"></x-greeting>

Output:

<p>Hello, Billy Jo!</p>

Prop Names and Automatic Prop Names

The Custom Elements V1 specification requires explictly stating the names of any attributes you want to observe. From your Preact component perspective, props could be an object with any keys at runtime, so it's not always clear which props should be accepted as attributes.

If you omit the third parameter to register(), the list of attributes to observe can be specified using a static observedAttributes property on your Component. This also works for the Custom Element's name, which can be specified using a tagName static property:

import register from 'preact-custom-element';

// <x-greeting name="Bo"></x-greeting>
class Greeting extends Component {
  // Register as <x-greeting>:
  static tagName = 'x-greeting';

  // Track these attributes:
  static observedAttributes = ['name'];

  render({ name }) {
    return <p>Hello, {name}!</p>;
  }
}
register(Greeting);

If no observedAttributes are specified, they will be inferred from the keys of propTypes if present on the Component:

// Other option: use PropTypes:
function FullName(props) {
  return <span>{props.first} {props.last}</span>
}
FullName.propTypes = {
  first: Object,   // you can use PropTypes, or this
  last: Object     // trick to define untyped props.
};
register(FullName, 'full-name');

Related

preact-shadow-dom

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].