{"id":14990391,"url":"https://github.com/wildhoney/standalone","last_synced_at":"2025-05-07T13:07:43.868Z","repository":{"id":57345238,"uuid":"55620680","full_name":"Wildhoney/Standalone","owner":"Wildhoney","description":"Create framework agnostic components that are truly reusable and interoperable with all the benefits of the React ecosystem – using the HTML5 custom elements API to extend HTML's vocabulary.","archived":false,"fork":false,"pushed_at":"2016-09-26T14:02:11.000Z","size":6807,"stargazers_count":206,"open_issues_count":4,"forks_count":9,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-05-07T13:07:38.000Z","etag":null,"topics":["components","custom-elements","react","reactjs","webcomponents"],"latest_commit_sha":null,"homepage":"http://react-standalone.herokuapp.com/","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/Wildhoney.png","metadata":{"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}},"created_at":"2016-04-06T16:05:52.000Z","updated_at":"2024-03-18T12:04:10.000Z","dependencies_parsed_at":"2022-08-26T08:40:51.954Z","dependency_job_id":null,"html_url":"https://github.com/Wildhoney/Standalone","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FStandalone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FStandalone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FStandalone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FStandalone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Wildhoney","download_url":"https://codeload.github.com/Wildhoney/Standalone/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252883220,"owners_count":21819161,"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":["components","custom-elements","react","reactjs","webcomponents"],"created_at":"2024-09-24T14:20:02.702Z","updated_at":"2025-05-07T13:07:43.839Z","avatar_url":"https://github.com/Wildhoney.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![React Standalone](media/logo.png)\n\n\u003e Create framework agnostic components that are truly reusable and interoperable with all the benefits of the React ecosystem \u0026ndash; using the HTML5 [custom elements API](https://www.w3.org/TR/custom-elements/) to extend HTML's vocabulary.\n\n![Travis](http://img.shields.io/travis/Wildhoney/Standalone.svg?style=flat-square)\n\u0026nbsp;\n![npm](http://img.shields.io/npm/v/react-standalone.svg?style=flat-square)\n\u0026nbsp;\n![License MIT](http://img.shields.io/badge/license-mit-lightgrey.svg?style=flat-square)\n\n* **npm:** `npm install react-standalone --save`\n\n## Table of Contents\n\n* [Getting Started](#getting-started)\n* [Handling Props](#handling-props)\n    * [Specifying a Schema](#specifying-a-schema)\n* [Component Events](#component-events)\n    * [Passing JSON Structure](#passing-json-structure)\n* [Element Methods](#element-methods)\n* [Extending Elements](#extending-elements)\n* [Browser Support](#browser-support)\n    \n---\n\n## Getting Started\n\nTake a look at the [`mars-weather` component](example/packages/mars-weather) for an idea on how to structure your reusable component \u0026ndash; however essentially a *component* consists of a tag name \u0026mdash; such as `mars-weather`, the React `component` and an [optional schema](#specifying-a-schema) using [`osom`](https://github.com/Kikobeats/osom).\n\n```javascript\nimport { createModule } from 'standalone';\nimport schema from './schema';\nimport component from './component';\n\nexport default createModule('mars-weather', { schema, component });\n\n```\n\nOnce you have created your package, a custom element will be created with the supplied `tagName` which can be embedded into the DOM \u0026ndash; all of the React lifecycle methods will be invoked, such as `componentWillUnmount` when the element has been removed from the DOM.\n\n```html\n\u003cmars-weather /\u003e\n```\n\nAs the `mars-weather` component is an entirely custom element, it can be embedded in **any** JavaScript framework \u0026mdash; Angular, Vue, React, Cycle, Ember, [Vanilla](http://vanilla-js.com/), etc...\n\n**Bonus:** Use [Keo with shadow boundaries](https://github.com/Wildhoney/Keo/blob/master/docs/SHADOW_DOM.md) for a true [Polymer-esque](https://www.polymer-project.org/1.0/) feel.\n\n## Handling Props\n\nBy specifying attributes on the custom element, the values of the attributes are passed into your component as props \u0026ndash; any changes to the `state` will be handled internally to your component, whereas any changes to your element's attributes will cause a re-render with the updated `props`.\n\nIn the `mars-weather` example, we have setup the `getDefaultProps` method to return the default props, however users can override the `unit` prop by passing in a [`data` attribute](http://html5doctor.com/html5-custom-data-attributes/) named `data-unit`.\n\n```html\n\u003cmars-weather data-unit=\"C\" /\u003e\n```\n\nIn the above case, the `data-unit` attribute will be transformed to `unit` \u0026mdash; as `Standalone` strips away any `data-` prefixes \u0026mdash; and then re-renders your component, allowing you to access the attribute as `this.props.unit`.\n\n### Specifying a Schema\n\nAs **all** HTML attributes are `string`s, `Standalone` allows you to specify a schema for your component, which will transform `string` attributes into the data type you expect using [`osom`](https://github.com/Kikobeats/osom).\n\n```javascript\nexport default {\n    unit: {\n        type: String,\n        default: 'F'\n    }\n};\n```\n\nOnce you have configured the schema to use for your component, you can happily setup the usual [React `propTypes`](https://facebook.github.io/react/docs/reusable-components.html) specifying the data type you're expecting to be passed through.\n\n## Component Events\n\nUsing [Custom Events](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events) you can easily set-up a communication channel between your components and the outside world.\n\n```javascript\n// Instantiate `CustomEvent` and then specify the name of the event, followed\n// by the payload which will be passed to your listener function.\nconst event = new CustomEvent('migrate-planets', {\n    bubbles: true,\n    detail: {\n        planet: 'Saturn'\n    }\n});\n\nfindDOMNode(this).dispatchEvent(event);\n```\n\nIt's crucial that you emit the event as `bubbles: true` otherwise the event would simply halt at the `findDOMNode(this)` node rather than bubbling up to the `mars-weather` node \u0026mdash; unless you dispatch the event on the `mars-weather` node by using `findDOMNode(this).parentNode`.\n\nWithin your component you emit the event \u0026mdash; `CustomEvent` \u0026mdash; using `dispatchEvent` and then bind your custom element \u0026mdash; such as `mars-weather` \u0026mdash; using `addEventListener` from the outside.\n\n```javascript\nconst node = document.querySelector('mars-weather');\n\nnode.addEventListener('migrate-planets', event =\u003e {\n\n    // Update the `data-planet` attribute to reflect the newly migrated planet\n    // which will cause the component to re-render with the update prop.\n    node.setAttribute('data-planet', event.detail.planet);\n\n});\n```\n\n### Passing JSON Structure\n\nAs invoking `setAttribute` on your component causes React to re-render your component, it may be useful to supply a JSON payload to your component instead \u0026mdash; especially if you're defining a multitude of attributes; this also helps with performance as you would only need one `setAttribute` to update many props and re-render.\n\nBy defining a schema you can specify an attribute that will be parsed as JSON.\n\n```javascript\nexport default {\n    payload: {\n        type: JSON.parse\n    }\n}\n```\n\nAttaching a JSON string to your element's `data-payload` attribute will cause it to be parsed into an object using `JSON.parse`, and passed to your React component as `this.props.payload` which can be defined in the `propTypes` using `PropTypes.shape`.\n\n## Element Methods\n\nAll `Standalone` components extend `HTMLElement.prototype` and allow for adding custom functions to the element \u0026mdash; which you can invoke once you have a reference to the associated element. Take a look at [`mars-weather`'s methods](https://github.com/Wildhoney/Standalone/blob/master/example/packages/mars-weather/methods.js) for an example.\n\n```javascript\nconst getWeather = function() {\n    const weather = this.component.state.weather.atmoOpacity.toLowerCase();\n    return `The current weather on Mars is ${weather}!`;\n};\n\n// ...\n\ndocument.querySelector('mars-weather').getWeather();\n```\n\nWhen a component has been appended to the DOM it will update its `HTMLElement` prototype to assign the rendered component to `getPrototypeOf(this).component` \u0026mdash; this conveniently allows you to access the `props` and `state`, and invoke functions internal to the React component.\n\nIt's worth noting that `this.component` will **only** be available once the component has been appended to the DOM.\n\n## Extending Elements\n\nWith the Custom Elements API it is possible to extend existing elements \u0026ndash; using the `is` attribute to specialise. `Standalone` allows you to extend elements by passing the element to extend.\n\n```javascript\n// Creates a `mars-weather` element.\nexport default createModule('mars-weather', { schema, methods, component });\n\n// Creates a `input[is=\"mars-weather\"]` element.\nexport default createModule('input/mars-weather', { schema, methods, component });\n```\n\nIt's worth noting that when you extend a known element, your element will extend its prototype \u0026ndash; in the case above the `mars-weather` element will extend `input` and its `HTMLInputElement` prototype.\n\n## Browser Support\n\n* Chrome \u003e= v33\n* Opera \u003e= v20\n* *IE \u003e= 11\n* *Safari \u003e= 7\n* *Firefox\n\n_\\* Requires the excellent [webcomponents-lite.js](https://github.com/WebComponents/webcomponentsjs) polyfill (13K gzipped)_\n\n[![forthebadge](http://forthebadge.com/images/badges/built-with-love.svg)](http://forthebadge.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwildhoney%2Fstandalone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwildhoney%2Fstandalone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwildhoney%2Fstandalone/lists"}