{"id":13656614,"url":"https://github.com/preactjs/preact-custom-element","last_synced_at":"2025-05-15T23:03:27.658Z","repository":{"id":37458438,"uuid":"63633396","full_name":"preactjs/preact-custom-element","owner":"preactjs","description":"Wrap your component up as a custom element","archived":false,"fork":false,"pushed_at":"2024-07-24T06:53:51.000Z","size":1003,"stargazers_count":372,"open_issues_count":30,"forks_count":54,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-08T01:25:37.404Z","etag":null,"topics":["custom-elements","preact","web-components"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/preactjs.png","metadata":{"funding":{"github":["preactjs"],"open_collective":"preact"},"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-18T20:05:11.000Z","updated_at":"2025-04-16T13:56:43.000Z","dependencies_parsed_at":"2023-09-24T05:11:33.517Z","dependency_job_id":"adc75fa5-0c4f-46e7-87dc-074f1f08df44","html_url":"https://github.com/preactjs/preact-custom-element","commit_stats":{"total_commits":126,"total_committers":18,"mean_commits":7.0,"dds":0.5952380952380952,"last_synced_commit":"9980548c6a216593cad95a5aa2c34e146c9adf84"},"previous_names":["bspaulding/preact-custom-element"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/preactjs%2Fpreact-custom-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/preactjs%2Fpreact-custom-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/preactjs%2Fpreact-custom-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/preactjs%2Fpreact-custom-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/preactjs","download_url":"https://codeload.github.com/preactjs/preact-custom-element/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253726933,"owners_count":21954102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["custom-elements","preact","web-components"],"created_at":"2024-08-02T05:00:25.706Z","updated_at":"2025-05-15T23:03:27.454Z","avatar_url":"https://github.com/preactjs.png","language":"JavaScript","readme":"# preact-custom-element\n\nGenerate/register a custom element from a preact component. As of 3.0.0, this library implements the Custom Elements v1 spec.\nPrevious versions (\u003c 3.0.0) implemented the v0 proposal, which was only implemented in Chrome and is abandoned.\n\n## Usage\n\nImport `register` and call it with your component, a tag name\u003cstrong\u003e*\u003c/strong\u003e, and a list of attribute names you want to observe:\n\n```javascript\nimport register from 'preact-custom-element';\n\nconst Greeting = ({ name = 'World' }) =\u003e (\n  \u003cp\u003eHello, {name}!\u003c/p\u003e\n);\n\nregister(Greeting, 'x-greeting', ['name']);\n```\n\n\u003e _**\\* Note:** as per the [Custom Elements specification](https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name), the tag name must contain a hyphen._\n\nUse the new tag name in HTML, attribute keys and values will be passed in as props:\n\n```html\n\u003cx-greeting name=\"Billy Jo\"\u003e\u003c/x-greeting\u003e\n```\n\nOutput:\n\n```html\n\u003cp\u003eHello, Billy Jo!\u003c/p\u003e\n```\n\n### Prop Names and Automatic Prop Names\n\nThe 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.\n\nIf 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:\n\n```js\nimport register from 'preact-custom-element';\n\n// \u003cx-greeting name=\"Bo\"\u003e\u003c/x-greeting\u003e\nclass Greeting extends Component {\n  // Register as \u003cx-greeting\u003e:\n  static tagName = 'x-greeting';\n\n  // Track these attributes:\n  static observedAttributes = ['name'];\n\n  render({ name }) {\n    return \u003cp\u003eHello, {name}!\u003c/p\u003e;\n  }\n}\nregister(Greeting);\n```\n\nIf no `observedAttributes` are specified, they will be inferred from the keys of `propTypes` if present on the Component:\n\n```js\n// Other option: use PropTypes:\nfunction FullName(props) {\n  return \u003cspan\u003e{props.first} {props.last}\u003c/span\u003e\n}\nFullName.propTypes = {\n  first: Object,   // you can use PropTypes, or this\n  last: Object     // trick to define untyped props.\n};\nregister(FullName, 'full-name');\n```\n\n\n## Related\n\n[preact-shadow-dom](https://github.com/bspaulding/preact-shadow-dom)\n","funding_links":["https://github.com/sponsors/preactjs","https://opencollective.com/preact"],"categories":["JavaScript","Libraries"],"sub_categories":["Integrations"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpreactjs%2Fpreact-custom-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpreactjs%2Fpreact-custom-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpreactjs%2Fpreact-custom-element/lists"}