{"id":14990305,"url":"https://github.com/the-road-to-learn-react/use-custom-element","last_synced_at":"2025-09-09T19:20:50.133Z","repository":{"id":34918207,"uuid":"190191073","full_name":"the-road-to-learn-react/use-custom-element","owner":"the-road-to-learn-react","description":"Custom hook to bridge Custom Elements (Web Components) to React.","archived":false,"fork":false,"pushed_at":"2022-05-11T12:03:37.000Z","size":2061,"stargazers_count":90,"open_issues_count":10,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T02:06:32.665Z","etag":null,"topics":["custom-element","custom-elements","react","react-hook","react-hooks","reactjs","web-component","web-components"],"latest_commit_sha":null,"homepage":"https://www.robinwieruch.de","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/the-road-to-learn-react.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"rwieruch","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-06-04T11:50:15.000Z","updated_at":"2024-11-02T19:33:58.000Z","dependencies_parsed_at":"2022-08-08T02:16:24.656Z","dependency_job_id":null,"html_url":"https://github.com/the-road-to-learn-react/use-custom-element","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/the-road-to-learn-react%2Fuse-custom-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/the-road-to-learn-react%2Fuse-custom-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/the-road-to-learn-react%2Fuse-custom-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/the-road-to-learn-react%2Fuse-custom-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/the-road-to-learn-react","download_url":"https://codeload.github.com/the-road-to-learn-react/use-custom-element/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505863,"owners_count":21115354,"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-element","custom-elements","react","react-hook","react-hooks","reactjs","web-component","web-components"],"created_at":"2024-09-24T14:19:51.759Z","updated_at":"2025-04-12T02:06:38.549Z","avatar_url":"https://github.com/the-road-to-learn-react.png","language":"JavaScript","funding_links":["https://github.com/sponsors/rwieruch"],"categories":[],"sub_categories":[],"readme":"# useCustomElement React Hook\n\n[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-custom-element.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-custom-element) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://slack-the-road-to-learn-react.wieruch.com/) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/use-custom-element.svg)](https://greenkeeper.io/) ![NPM](https://img.shields.io/npm/l/use-custom-element.svg)\n\nCustom hook to bridge Custom Elements (Web Components) to React.\n\n* [Learn how to build Web Components.](https://www.robinwieruch.de/web-components-tutorial)\n* [Learn how to use Web Components in React.](https://www.robinwieruch.de/react-web-components)\n\n## Installation\n\n`npm install use-custom-element`\n\n## Usage\n\nIn this scenario, we are using a specific [Dropdown Web Component](https://github.com/rwieruch/web-components-dropdown) as a React Dropdown Component. By using the custom React hook, we can provide all props in the right format to the custom element and register all event listeners (e.g. onChange from `props`) behind the scenes.\n\n```\nimport React from 'react';\n\n// Web Component Use Case\n// install via npm install road-dropdown\nimport 'road-dropdown';\n\nimport useCustomElement from 'use-custom-element';\n\nconst Dropdown = props =\u003e {\n  const [customElementProps, ref] = useCustomElement(props);\n\n  return \u003croad-dropdown {...customElementProps} ref={ref} /\u003e;\n};\n```\n\nAfterward, the Dropdown component can be used:\n\n```\nconst props = {\n  label: 'Label',\n  option: 'option1',\n  options: {\n    option1: { label: 'Option 1' },\n    option2: { label: 'Option 2' },\n  },\n  onChange: (value) =\u003e console.log(value),\n};\n\nreturn \u003cDropdown {...props} /\u003e;\n```\n\n### Custom Mapping\n\nYou can also define a custom mapping:\n\n```\nimport React from 'react';\n\n// Web Component Use Case\n// install via npm install road-dropdown\nimport 'road-dropdown';\n\nimport useCustomElement from 'use-custom-element';\n\nconst Dropdown = props =\u003e {\n  const [customElementProps, ref] = useCustomElement(props, {\n    option: 'selected',\n    onChange: 'click',\n  });\n\n  console.log(customElementProps);\n\n  // label: \"Label\"\n  // options: \"{\"option1\":{\"label\":\"Option 1\"},\"option2\":{\"label\":\"Option 2\"}}\"\n  // selected: \"option1\"\n\n  // and \"onChange\" mapped to \"click\" event for the event listener\n\n  return \u003cmy-dropdown {...customElementProps} ref={ref} /\u003e;\n};\n```\n\n## Contribute\n\n* `git clone git@github.com:the-road-to-learn-react/use-custom-element.git`\n* `cd use-custom-element`\n* `npm install`\n* `npm run test`\n\n### More\n\n* [Publishing a Node Package to NPM](https://www.robinwieruch.de/publish-npm-package-node/)\n* [Node.js Testing Setup](https://www.robinwieruch.de/node-js-testing-mocha-chai/)\n* [React Testing Setup](https://www.robinwieruch.de/react-testing-tutorial/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthe-road-to-learn-react%2Fuse-custom-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthe-road-to-learn-react%2Fuse-custom-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthe-road-to-learn-react%2Fuse-custom-element/lists"}