Package Exports
- preact-custom-element
- preact-custom-element/dist/preact-custom-element.esm.js
- preact-custom-element/dist/preact-custom-element.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (preact-custom-element) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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
Any Preact component can be registered as a custom element simply by importing register and calling it 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'], { shadow: true, mode: 'open', adoptedStyleSheets: [] });
// ^ ^ ^ ^ ^ ^
// | HTML tag name | use shadow-dom | use adoptedStyleSheets
// Component definition Observed attributes Encapsulation mode for the shadow DOM tree* 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>Observed Attributes
The Custom Elements v1 specification requires explicitly listing the names of attributes you want to observe in order to respond when their values are changed. These can be specified via the third parameter that's passed to the register() function:
// Listen to changes to the `name` attribute
register(Greeting, 'x-greeting', ['name']);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({ first, last }) {
return <span>{first} {last}</span>
}
FullName.propTypes = {
first: Object, // you can use PropTypes, or this
last: Object // trick to define untyped props.
};
register(FullName, 'full-name');Passing slots as props
The register() function also accepts an optional fourth parameter, an options bag. At present, it allows you to opt-in to using shadow DOM for your custom element by setting the shadow property to true, and if so, you can also specify the encapsulation mode with mode, which can be either 'open' or 'closed'.
When using shadow DOM, you can make use of named <slot> elements in your component to forward the custom element's children into specific places in the shadow tree.
function TextSection({ heading, content }) {
return (
<div>
<h2>{heading}</h2>
<p>{content}</p>
</div>
);
}
register(TextSelection, 'text-selection', [], { shadow: true });<text-section>
<span slot="heading">My Heading</span>
<span slot="content">Some content goes here.</span>
</text-section>Static Properties
We support a number of static properties on your component that map to special behaviors of the custom element. These can be set on components like so:
class MyCustomElement extends Component {
static tagName = 'my-custom-element';
}
function MyOtherCustomElement() { ... }
MyOtherCustomElement.tagName = 'my-other-custom-element';tagName- the custom element's tag name (if not passed as the second argument to
register())
- the custom element's tag name (if not passed as the second argument to
observedAttributes- an array of attribute names to observe (if not passed as the third argument to
register())
- an array of attribute names to observe (if not passed as the third argument to
formAssociated- a boolean indicating whether the custom element should be form-associated
Related
What's Changed
- fix: expose correct types by @VelizarD in https://github.com/preactjs/preact-custom-element/pull/98
- chore: Bump deps and ci action, add editorconfig by @rschristian in https://github.com/preactjs/preact-custom-element/pull/99
- docs: Update ReadMe & mention
shadow&modeoptions by @rschristian in https://github.com/preactjs/preact-custom-element/pull/102 - chore: Limit files published by @rschristian in https://github.com/preactjs/preact-custom-element/pull/101
- feat: formAssociated attribute by @mira-kovar in https://github.com/preactjs/preact-custom-element/pull/89
- add adoptedStyleSheets to the options by @rhildred in https://github.com/preactjs/preact-custom-element/pull/95
- refactor: Switch to
.d.tsfor types by @rschristian in https://github.com/preactjs/preact-custom-element/pull/100 - chore: Add '@types/mocha' for our test suite by @rschristian in https://github.com/preactjs/preact-custom-element/pull/104
- refactor: Do not manually call connectedCallback by @rschristian in https://github.com/preactjs/preact-custom-element/pull/103
- Provide light dom props children by @paull39 in https://github.com/preactjs/preact-custom-element/pull/79
- Return the newly created HtmlElement class, as opposed to just "undef… by @EmmanuelOga in https://github.com/preactjs/preact-custom-element/pull/91
New Contributors
- @VelizarD made their first contribution in https://github.com/preactjs/preact-custom-element/pull/98
- @rschristian made their first contribution in https://github.com/preactjs/preact-custom-element/pull/99
- @mira-kovar made their first contribution in https://github.com/preactjs/preact-custom-element/pull/89
- @rhildred made their first contribution in https://github.com/preactjs/preact-custom-element/pull/95
- @paull39 made their first contribution in https://github.com/preactjs/preact-custom-element/pull/79
- @EmmanuelOga made their first contribution in https://github.com/preactjs/preact-custom-element/pull/91
Full Changelog: https://github.com/preactjs/preact-custom-element/compare/v4.4.0...4.5.0