{"id":18358909,"url":"https://github.com/uppercod/base-element","last_synced_at":"2025-04-10T03:09:50.295Z","repository":{"id":113250455,"uuid":"183156963","full_name":"UpperCod/base-element","owner":"UpperCod","description":null,"archived":false,"fork":false,"pushed_at":"2019-11-19T17:15:38.000Z","size":101,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-23T22:26:59.687Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/UpperCod.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":"2019-04-24T05:49:52.000Z","updated_at":"2020-01-20T03:23:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"d3d8db09-0efb-40cc-b226-e00f606bd773","html_url":"https://github.com/UpperCod/base-element","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpperCod%2Fbase-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpperCod%2Fbase-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpperCod%2Fbase-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpperCod%2Fbase-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UpperCod","download_url":"https://codeload.github.com/UpperCod/base-element/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239036208,"owners_count":19571463,"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":[],"created_at":"2024-11-05T22:19:53.400Z","updated_at":"2025-02-15T18:30:22.183Z","avatar_url":"https://github.com/UpperCod.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @atomico/base-element\n\n[![npm](https://badgen.net/npm/v/@atomico/base-element)](http://npmjs.com/@atomico/base-element)\n[![gzip](https://badgen.net/bundlephobia/minzip/@atomico/base-element)](https://bundlephobia.com/result?p=@atomico/base-element)\n\nIt provides a minimum utility interface for the creation of web-components.\n\n```ts\nclass BaseElement extends HTMLElement {\n\t/**\n\t * captures the properties defined in `static attributes`;\n\t */\n\tprops: Properties;\n\t/**\n\t * resolves once the component has been mounted to the document\n\t */\n\tmounted: Promise\u003cvoid\u003e;\n\t/**\n\t * resolves once the component has been unmounted from the document.\n\t */\n\tunmounted: Promise\u003cvoid\u003e;\n\t/**\n\t * defines the observables as properties and attributes of the component\n\t */\n\tstatic observables: Observables;\n\t/**\n\t * validates the `value` which is then delivered to the` update({[name]:value})` method.\n\t */\n\tsetProperty(name: string, value: Values): void;\n\t/**\n\t * dispatches every time `setProperty` is successfully executed\n\t */\n\tupdate(props: Properties): void;\n}\n```\n\nThis class is used by [@atomico/element](https://github.com/atomicojs/core) to use [@atomico/core](https://github.com/atomicojs/element) as web-component.\n\n## Objective\n\n`base-element`, allows to create HTMLElements under other libraries, similar to what it does [Skatejs](https://github.com/skatejs/skatejs), but less code.\n\n## Observables\n\ndefines the observables as property and attribute of the component\n\n```js\nclass CustomElement extends Element {\n\tstatic Observables = {\n\t\tfieldString: String, // [field-string]\n\t\tfieldNumber: Number, // [field-number]\n\t\tfieldBoolean: Boolean, // [field-boolean]\n\t\tfieldObject: Object, // [field-object]\n\t\tfieldArray: Array // [field-array],\n\t\tfieldFunction:Function // [field-function]\n\t};\n}\n```\n\nthe attributes force the type of the variable, so if you define an attribute as `Object`, it will apply\n`JSON.parse` to a string type value, to read its type.\n\n```js\n// set property, remember that to apply this WC, you must already be registered\nmyCustomElement.fieldArray = [];\n// set attribute, more benefit using setAttribute, since it allows a deferred loading of the WC\nmyCustomElement.setAttribute(\"field-array\", []);\n```\n\n## Example with lit-html\n\n```jsx\nimport { render, html } from \"lit-html\";\nimport Element from \"@atomico/base-element\";\n\nexport default class extends Element {\n\tcontructor() {\n\t\tsuper();\n\t\tthis.attachShadow({ mode: \"open\" });\n\t\tthis.props = {};\n\t\tthis.update();\n\t}\n\tasync update(props) {\n\t\tthis.props = { ...this.props, ...props };\n\t\tif (this.prevent) return;\n\t\tthis.prevent = true;\n\t\tawait this.mounted;\n\t\tthis.prevent = false;\n\t\trender(this.render(this.props), this.shadowRoot);\n\t}\n}\n```\n\n## Example with preact\n\n```jsx\nimport { render, h } from \"preact\";\nimport Element from \"@atomico/base-element\";\n\nexport default class extends Element {\n\tcontructor() {\n\t\tsuper();\n\t\tthis.attachShadow({ mode: \"open\" });\n\t\tthis.props = {};\n\t\tthis.render = this.render.bind(this);\n\t\tthis.unmounted.then(() =\u003e render(\"\", this.shadowRoot));\n\t\tthis.update();\n\t}\n\tasync update(props) {\n\t\tthis.props = { ...this.props, ...props };\n\t\tif (this.prevent) return;\n\t\tthis.prevent = true;\n\t\tawait this.mounted;\n\t\tthis.prevent = false;\n\t\trender(h(this.render, this.props), this.shadowRoot);\n\t}\n}\n```\n\n## Example with innerHTML\n\n```js\nimport Element from \"@atomico/base-element\";\n\nexport default class extends Element {\n\tcontructor() {\n\t\tsuper();\n\t\tthis.attachShadow({ mode: \"open\" });\n\t\tthis.props = {};\n\t\tthis.update();\n\t}\n\tasync update(props) {\n\t\tthis.props = { ...this.props, ...props };\n\t\tif (this.prevent) return;\n\t\tthis.prevent = true;\n\t\tawait this.mounted;\n\t\tthis.prevent = false;\n\t\tlet nextHTML = this.render(this.props);\n\t\tif (nextHTML !== this.shadowRoot.innerHTML) {\n\t\t\tthis.shadowRoot.innerHTML = nextHTML;\n\t\t}\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuppercod%2Fbase-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuppercod%2Fbase-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuppercod%2Fbase-element/lists"}