{"id":17490863,"url":"https://github.com/nathanjhood/web-components","last_synced_at":"2026-05-11T02:16:12.104Z","repository":{"id":258381030,"uuid":"871688972","full_name":"nathanjhood/web-components","owner":"nathanjhood","description":"How to OOP in the browser.","archived":false,"fork":false,"pushed_at":"2024-10-18T00:38:29.000Z","size":252,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-20T23:40:05.940Z","etag":null,"topics":["browser","javascript","no-config","oop","web-component","web-components","webcomponent","webcomponents"],"latest_commit_sha":null,"homepage":"https://nathanjhood.github.io/web-components/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nathanjhood.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-10-12T17:00:08.000Z","updated_at":"2024-10-18T00:38:32.000Z","dependencies_parsed_at":"2024-10-19T03:19:22.297Z","dependency_job_id":null,"html_url":"https://github.com/nathanjhood/web-components","commit_stats":null,"previous_names":["nathanjhood/web-components"],"tags_count":0,"template":true,"template_full_name":null,"purl":"pkg:github/nathanjhood/web-components","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanjhood%2Fweb-components","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanjhood%2Fweb-components/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanjhood%2Fweb-components/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanjhood%2Fweb-components/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nathanjhood","download_url":"https://codeload.github.com/nathanjhood/web-components/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanjhood%2Fweb-components/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32878374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-10T13:40:02.631Z","status":"online","status_checked_at":"2026-05-11T02:00:05.975Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["browser","javascript","no-config","oop","web-component","web-components","webcomponent","webcomponents"],"created_at":"2024-10-19T07:04:39.574Z","updated_at":"2026-05-11T02:16:12.085Z","avatar_url":"https://github.com/nathanjhood.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Web Components\n\nOOP in the browser!\n\n```html\n\u003c!doctype html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e(...)\u003c/head\u003e\n  \u003cbody\u003e\n    \u003cdiv id=\"root\"\u003e\n      \u003capp-component\u003e\n        \u003cslot\u003e\"Your App Goes Here!\"\u003c/slot\u003e\n      \u003c/app-component\u003e\n    \u003c/div\u003e\n    \u003cscript type=\"module\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n- No config\n- No framework\n- No abstractions\n\nJust raw [Web API](https://developer.mozilla.org/en-US/docs/Web/API).\n\nPowered by TailwindCSS, ESBuild, and fast-refreshing development server!\n\n## How to...\n\nStep by step!\n\n- [`start`](#start)\n- [`class AppComponent {}`](#class-appcomponent-)\n- [`AppComponent.constructor()`](#appcomponentconstructor)\n- [`AppComponent extends HTMLElement`](#appcomponent-extends-htmlelement)\n- [`HTMLElement.super()`](#htmlelementsuper)\n- [`this`](#this)\n- [`AppComponent.innerHtml`](#appcomponentinnerhtml)\n- [`CustomElementRegistry`](#customelementregistry)\n- [`'app-component': AppComponent`](#app-component-appcomponent)\n- [`AppComponent.render()`](#appcomponentrender)\n- [`AppComponent.render(innerHTML)`](#appcomponentrenderinnerhtml)\n- [`AppComponent.setup()`](#appcomponentsetup)\n- [`createElement('app-component')`](#createelementapp-component)\n- [`App()`](#app)\n- [`render(App)`](#renderapp)\n- [`\u003capp-component\u003e`](#app-component)\n- [Tips](#tips)\n- [Further Reading](#further-reading)\n\n### `start`\n\n```sh\n$ git clone git@github.com:nathanjhood/web-components.git\n```\n\n```sh\n$ cd web-components\n```\n\n```sh\n$ npm install\n```\n\n```sh\n# For Windows...\n$env:NODE_ENV=\"development\"\n\n# For Linux/Mac...\nexport NODE_ENV=\"development\"\n```\n\n```sh\n$ npm run start\n```\n\n```ts\n# ...\n\nRebuilding...\n\nDone in 1623ms.\n\nServer running at http://127.0.0.1:3000/\nTo exit: Ctrl + c\n\n```\n\n[Open in your browser](http://localhost:3000) and edit `src/App.js` - the page will automatically refresh itself after every save.\n\n---\n\n### `class AppComponent {}`\n\n```js\n// src/App.js\n\nclass AppComponent {}\n```\n\n---\n\n### `AppComponent.constructor()`\n\n```js\nclass AppComponent {\n  constructor() {/** setup goes here... */}\n}\n```\n\n---\n\n### `AppComponent extends HTMLElement`\n\n```js\n// \"I am a HTMLElement\"\n\nclass AppComponent extends HTMLElement {\n  constructor() {}\n}\n\n// \"...plus more ;) \"\n```\n\n---\n\n### `HTMLElement.super()`\n\n```js\nclass AppComponent extends HTMLElement {\n  constructor() {\n    super(); // MUST do this first...\n  }\n}\n```\n\n---\n\n### `this`\n\n```js\nclass AppComponent extends HTMLElement {\n  constructor() {\n    // inside here, \"this\" means \"this 'AppComponent'\"...\n\n    super();\n    this. // \u003c-- '.' should produce a long list of props and methods...\n  }\n}\n```\n\n---\n\n### `AppComponent.innerHtml`\n\n```js\n// 'innerHTML' === \u003capp-component\u003einnerHTML\u003c/app-component\u003e\n\nclass AppComponent extends HTMLElement {\n  constructor() {\n    super();\n    this.innerHTML = `\u003cslot\u003eYour app goes here\u003c/slot\u003e`;\n  }\n}\n```\n\n### `CustomElementRegistry`\n\n```js\n// IMPORTANT\nwindow.customElements.define('app-component', AppComponent);\n```\n\n---\n\n### `'app-component': AppComponent`\n\n```js\nwindow.customElements.define('app-component', // \u003c-- wrap the class!\n  class AppComponent extends HTMLElement {\n    constructor() {\n      super();\n      this.innerHTML = `\u003cslot\u003eYour app goes here\u003c/slot\u003e`;\n    }\n  }\n); // \u003c-- '.define()' ends here!\n```\n\n---\n\n### `AppComponent.render()`\n\n```js\nwindow.customElements.define('app-component',\nclass AppComponent extends HTMLElement {\n    constructor() {\n      super();\n      this.innerHTML = this.render();\n    }\n    render() {\n      return `\u003cslot\u003eYour app goes here\u003c/slot\u003e`;\n    }\n  }\n);\n```\n\n---\n\n### `AppComponent.render(innerHTML)`\n\n```js\nwindow.customElements.define('app-component',\n  class AppComponent extends HTMLElement {\n    constructor() {\n      super();\n      this.innerHTML = this.render('Your app goes here');\n    }\n    render(innerHTML) {\n      return `\u003cslot\u003e${innerHTML}\u003c/slot\u003e`;\n    }\n  }\n);\n```\n\n---\n\n### `AppComponent.setup()`\n\n```js\nwindow.customElements.define('app-component',\n  class AppComponent extends HTMLElement {\n    constructor() {\n      super();\n      this.setup();\n    }\n    setup() {\n      this.innerHTML = this.render('Your app goes here');\n    }\n    render(innerHTML) {\n      return `\u003cslot\u003e${innerHTML}\u003c/slot\u003e`;\n    }\n  }\n);\n```\n\n---\n\n### `createElement('app-component')`\n\n```js\nwindow.customElements.define('app-component',\n  class AppComponent extends HTMLElement {\n    constructor() {\n      super();\n      this.setup();\n    }\n    setup() {\n      this.innerHTML = this.render('Your app goes here');\n    }\n    render(innerHTML) {\n      return `\u003cslot\u003e${innerHTML}\u003c/slot\u003e`;\n    }\n  }\n);\n\nconst app = document.createElement('app-component');\n```\n\n---\n\n### `App()`\n\n```js\nconst App = () =\u003e {\n  // define the component\n  window.customElements.define('app-component',\n    class AppComponent extends HTMLElement {\n      constructor() {\n        super();\n        this.setup();\n      }\n      setup(): void {\n        this.innerHTML = this.render('Your app goes here');\n      }\n      render(innerHTML) {\n        return `\u003cslot\u003e${innerHTML}\u003c/slot\u003e`;\n      }\n    }\n  );\n  // then return it\n  return document.createElement('app-component');\n}\n\n// Now we can assign it :)\nconst app = App();\n```\n\n---\n\n### `render(App)`\n\n```js\n// src/index.js\n\nimport App = require('./App');\n\nconst render = (element) = {\n  // ...attaches passed-in element to document\n}\n\n// so, pass it our App :)\nrender(App)\n\n```\n\n---\n\n### `\u003capp-component\u003e`\n\n```html\n\u003c!-- This is what you see in your IDE... --\u003e\n\u003c!doctype html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\u003c/head\u003e\n  \u003cbody\u003e\n    \u003cdiv id=\"root\"\u003e\u003c/div\u003e\n    \u003cscript type=\"module\" src=\"./static/js/index.js\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n```html\n\u003c!-- ...this is what you see in your web browser! --\u003e\n\u003c!doctype html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\u003c/head\u003e\n  \u003cbody\u003e\n    \u003cdiv id=\"root\"\u003e\n      \u003capp-component\u003e\n        #shadowRoot (open)\n        \u003cslot\u003e\"Your App Goes Here!\"\u003c/slot\u003e\n      \u003c/app-component\u003e\n    \u003c/div\u003e\n    \u003cscript type=\"module\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n---\n\n\n## Tips\n\n---\n\n### functional approach\n\n```js\n// example:\n\nconst Button = () =\u003e {\n  return document.createElement('button')\n}\n\n// HTMLButtonElement\nconst button = Button();\n\n```\n\n---\n\n### factory method\n\n```js\n// example\n\nconst CustomButton = () =\u003e {\n  class CustomButtonElement extends HTMLButtonElement {\n    constructor() {\n      super();\n    }\n  }\n  customElements.define('custom-button', CustomButtonElement)\n  return document.createElement('custom-button');\n};\n\n// CustomButtom\nconst customButton = CustomButton();\n```\n\n---\n\n### passing props\n\n```js\n/**\n * @typedef CustomButtonProps\n * @property {'submit' | 'reset' | 'button'} type\n */\n\n/**\n * @param {CustomButtonProps} props\n * @returns {HTMLButtonElement}\n */\nconst CustomButton = (props) =\u003e {\n  class CustomButtonElement extends HTMLButtonElement {\n    constructor() {\n      super();\n      this.type = props.type;\n    }\n  }\n  customElements.define('custom-button', CustomButtonElement);\n  return document.createElement('custom-button');\n};\n\nconst customButton = CustomButton({ type: 'submit' });\n\n```\n\n---\n\n### adding children\n\n```js\n/**\n * @typedef CustomButtonProps\n * @property {'submit' | 'reset' | 'button'} type\n * @property {Node | undefined} children\n */\n\n/**\n * @param {CustomButtonProps} props\n * @returns {HTMLButtonElement}\n */\nconst CustomButton = (props) =\u003e {\n  class CustomButtonElement extends HTMLButtonElement {\n    constructor() {\n      super();\n      this.type = props.type;\n      if (props.children) this.appendChild(props.children);\n    }\n  }\n  customElements.define('custom-button', CustomButtonElement);\n  return document.createElement('custom-button');\n};\n\nconst customButtonA = CustomButton({ type: 'submit' });\nconst customButtonB = CustomButton({ type: 'submit', children: customButtonA });\n\n```\n\n---\n\n### adding styles\n\n```js\n\n/**\n * @typedef CustomButtonProps\n * @property {'submit' | 'reset' | 'button'} type\n * @property {Node | undefined} children\n * @property {string | undefined} className\n */\n\n/**\n * @param {CustomButtonProps} props\n * @returns {HTMLButtonElement}\n */\nconst CustomButton = (props) =\u003e {\n  class CustomButtonElement extends HTMLButtonElement {\n    constructor() {\n      super();\n      this.type = props.type;\n      if (props.children) this.appendChild(props.children);\n      if (props.className) this.className = props.className;\n    }\n  }\n  customElements.define('custom-button', CustomButtonElement);\n  return document.createElement('custom-button');\n};\n\nconst tailwindButton = CustomButton({\n  type: 'submit',\n  className: 'flex align-left text-white bg-red-500',\n});\n\n```\n\n---\n\n## Further Reading\n\n- [webcomponents.org](https://www.webcomponents.org/)\n- [eisenebergeffect @ Medium: Hello Web Components](https://eisenbergeffect.medium.com/hello-web-components-795ed1bd108e)\n- [MDN's Web Component examples](https://github.com/mdn/web-components-examples)\n- [MDN's Web API glossary](https://developer.mozilla.org/en-US/docs/Web/API)\n- [MDN's Web API - HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement)\n- [Building Interoperable Web Components (including React)](https://css-tricks.com/building-interoperable-web-components-react/)\n\n[Read me on github.com](https://github.com/nathanjhood/web-components)\n\n[Read me on nathanjhood.github.io](https://nathanjhood.github.io/web-components)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathanjhood%2Fweb-components","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnathanjhood%2Fweb-components","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathanjhood%2Fweb-components/lists"}