{"id":13793382,"url":"https://github.com/kofifus/HyperappWebComponent","last_synced_at":"2025-05-12T20:30:57.849Z","repository":{"id":150467257,"uuid":"623233156","full_name":"kofifus/HyperappWebComponent","owner":"kofifus","description":"Hyperapp support for Web Components","archived":false,"fork":false,"pushed_at":"2023-12-05T05:34:10.000Z","size":73,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-14T15:20:03.016Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kofifus.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-04T00:54:39.000Z","updated_at":"2023-04-14T21:40:20.000Z","dependencies_parsed_at":"2023-04-21T08:34:36.235Z","dependency_job_id":"b4227ddf-220b-4d80-9901-780c00dfcdf5","html_url":"https://github.com/kofifus/HyperappWebComponent","commit_stats":null,"previous_names":["kofifus/hyperappwebcomponent"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kofifus%2FHyperappWebComponent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kofifus%2FHyperappWebComponent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kofifus%2FHyperappWebComponent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kofifus%2FHyperappWebComponent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kofifus","download_url":"https://codeload.github.com/kofifus/HyperappWebComponent/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253816680,"owners_count":21968866,"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-08-03T23:00:20.168Z","updated_at":"2025-05-12T20:30:57.595Z","avatar_url":"https://github.com/kofifus.png","language":"JavaScript","funding_links":[],"categories":["Utilities"],"sub_categories":[],"readme":"# HyperappWebComponent\nHyperapp support for Web Components  \n\u0026nbsp;   \n## Syntax:\n```\ncomponentApp(\n  componentName,     // component name \n  elem =\u003e { \n    init?,           // passed to hyperApp\n    style?,          // component stylesheet \n    view,            // passed to hyperApp\n    subscriptions?,  // passed to hyperApp \n    dispatch?,       // passed to hyperApp \n    externalState?,  // state transform for the `onStateChange` event \n    methods?,        // element methods \n    properties?,     // element properties \n    cloneCSS?        // boolean for cloning host CSS\n  }\n)\n```\n\u0026nbsp;   \n\n## Usage:\n```\nimport componentApp from \"https://cdn.skypack.dev/hyperappwebcomponent\"\n```\nHyperappWebComponent provides four mechanisms for communicating between the host and the component:  \n\u0026nbsp;   \n### State change notification\n\nHere the component notifies the host whenever it's state changes. If `externalState` is provided, the component will invoke it on the state before attaching it to the event `detail` property:\n\n```\ncomponentApp('counter-', elem =\u003e {\n  return {\n    init: { counter: 0},\n    style: `button { color: red !important; }`,\n    view: s =\u003e html`\n      \u003cdiv class=\"counter\"\u003e\n        \u003ch1\u003e${s.counter}\u003c/h1\u003e\n        \u003cbutton onclick=${s =\u003e ({ ...s, counter: s.counter + 1 }}\u003e+\u003c/button\u003e\n      \u003c/div\u003e`,\n    externalState: s =\u003e ({ counterValue: s.counter }),\n  }\n})\n\ncomponentApp('game-', elem =\u003e {\n  const CounterStateChange = (s, ev) =\u003e ({...s, counterValue: ev.detail.counterValue})\n\n  return {\n    init: { counterValue: 0 },\n    view: state =\u003e html`\u003ccounter- onstateChange=${CounterStateChange} /\u003e`, \n  }\n})\n```\n\n`game` recieves a notification on a `counter` state change and here merges that state into it's own.\n\n`externalState` allows the component to only expose a part or a modified version of it's state to the outside. If omitted it defaults to `s=\u003es`. To prevent exposing the state use `s=\u003eundefined`\n\n`ev.srcElement` contains the source component  \n\u0026nbsp;   \n### Trigger a host event\n\nHere the component triggers a host event using an Effect:\n\n```\ncomponentApp('game-', elem =\u003e {\n  const CounterStateChange = (s, ev) =\u003e [ \n    { ...s, counterValue: ev.detail.counterValue },\n    ev.detail.counterValue==10 \u0026\u0026 elem.events.finish.effect\n  ]\n  \n  return {\n    init: { counterValue: 0 },\n    view: s =\u003e html`\u003ccounter- onstateChange=${CounterStateChange} /\u003e`, \n  }\n})\n\n\ncomponentApp('flow-', elem =\u003e {\n  return {  \n    init: { mode: 'start' },\n    view: s =\u003e html`\u003cgame- onfinish=${s =\u003e ({ ...s, mode: 'finish' }} /\u003e`\n  }\n})\n```\n\n`elem.events.finish.effect` triggers the `finish` event on the host (with a payload if provided) when `counterValue` reaches 10  \n\u0026nbsp;   \n### Invoke a compenent's method\n\nHere the host invoke a method on the component:\n```\ncomponentApp('score-', elem =\u003e {\n  return {\n    init: { count: 0 },\n    view: s =\u003e html`${s.count}`, \n    methods: {\n      addPoint: s =\u003e ({ ...s, count: s.count + 1 }), \n    },\n  }\n})\n\ncomponentApp('flow-', elem =\u003e {\n  const getSubComponent = name =\u003e elem.shadowRoot.querySelector(name);\n  \n  const Action = s =\u003e {\n    const score = getSubComponent('score-');\n    return [ s, score \u0026\u0026 (dispatch =\u003e score.addPoint()) ];    \n  }\n  \n  return {  \n    view: s =\u003e html`\n      \u003cscore- /\u003e\n      \u003cbutton onclick=${Action}\u003e\u003c/button\u003e`     \n  }\n})\n```\n\n`Action` invokes score's method `addPoint` dispatching the attached action.  \nNote the check for `score` is not necessary here, but in actions triggered from `init` the element does not exist yet.  \n\u0026nbsp;   \n### Change a component's property\n\n```\ncomponentApp('score-', elem =\u003e {\n  init: { scoreCount: 0 },\n  view: s =\u003e html`${s.scoreCount}`, \n  properties: {\n    counter: (s, v) =\u003e ({...s, scoreCount: Number(v) })\n  }\n})\n\ncomponentApp('flow-', elem =\u003e { \n  init: { flowCount: 0 },\n  view: s =\u003e html`\n    \u003cscore- counter=${s.flowCount}/\u003e\n    \u003cbutton onclick=${s =\u003e ({...s, flowCount: flowCount+1 })}\u003e\u003c/button\u003e`     \n})\n```\n\nEvery time the button is clicked, score's property `counter` changes trigerring the attached action in score.  \n\u0026nbsp;   \n### Full example\n[flems](https://tinyurl.com/ycxaesmm)\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkofifus%2FHyperappWebComponent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkofifus%2FHyperappWebComponent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkofifus%2FHyperappWebComponent/lists"}