- Notifications
You must be signed in to change notification settings - Fork53
Wrap your component up as a custom element
License
preactjs/preact-custom-element
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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.
ImportCustomElement and call with your component a tag name*, and a list of attribute names you want to observe:
importregisterCustomElementfrom"preact-custom-element";constGreeting=({ name="World"})=>(<p>Hello,{name}!</p>);registerCustomElement(Greeting,"x-greeting",["name"]);
* Note: as per theCustom 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-greetingname="Billy Jo"></x-greeting>
Output:
<p>Hello, Billy Jo!</p>
The Custom Elements V1 spec requires you to explictly state the attribute names you want to observe. From your Preact component perspective,props could be an object with any keys at runtime. This unfortunate combination of factors leaves us needing to explicitly state them.
It's possible that a compile step could introspect your usages of props and generate the glue code here. Please send me a link if you do this!
About
Wrap your component up as a custom element
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.