{"id":13614450,"url":"https://github.com/zouhir/preact-habitat","last_synced_at":"2025-05-16T12:02:37.303Z","repository":{"id":40680405,"uuid":"79223894","full_name":"zouhir/preact-habitat","owner":"zouhir","description":"Zero configuration Preact widgets renderer in any host DOM","archived":false,"fork":false,"pushed_at":"2022-03-10T16:21:04.000Z","size":1467,"stargazers_count":516,"open_issues_count":12,"forks_count":36,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-02T07:02:55.299Z","etag":null,"topics":["embed","iframe","portable","preact","vdom"],"latest_commit_sha":null,"homepage":"","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/zouhir.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":"2017-01-17T12:08:35.000Z","updated_at":"2025-03-24T17:19:23.000Z","dependencies_parsed_at":"2022-07-14T05:10:35.731Z","dependency_job_id":null,"html_url":"https://github.com/zouhir/preact-habitat","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouhir%2Fpreact-habitat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouhir%2Fpreact-habitat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouhir%2Fpreact-habitat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouhir%2Fpreact-habitat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zouhir","download_url":"https://codeload.github.com/zouhir/preact-habitat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248008628,"owners_count":21032556,"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":["embed","iframe","portable","preact","vdom"],"created_at":"2024-08-01T20:01:01.706Z","updated_at":"2025-04-09T09:02:19.027Z","avatar_url":"https://github.com/zouhir.png","language":"JavaScript","funding_links":[],"categories":["Frameworks"],"sub_categories":["Preact"],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/zouhir/preact-habitat/blob/master/docs/artworkv3.gif?raw=true\" height=300px\u003e\n\u003c/div\u003e\n\n[![Build Status][build-badge]][build]\n[![Code Coverage][coverage-badge]][coverage]\n[![downloads][downloads-badge]][npmcharts]\n[![version][version-badge]][package]\n[![gzip size][gzip-badge]][unpkg-dist]\n[![module formats: umd, cjs, and es][module-formats-badge]][unpkg-dist]\n[![Supports Preact and React][preact-badge]][preact]\n[![MIT License][license-badge]][LICENSE]\n\n\n## Preact Habitat \n\nA 900 Bytes module for that will make plugging in Preact components and widgets in any CMS or website as fun as lego!\n\n### Demos\n\nLogin Widget [Source Code 💻](https://github.com/zouhir/preact-habitat/tree/master/examples/login-form)\n\nLogin Widget Integration pen [Codepen Demo 🖋](https://codepen.io/zouhir/pen/brrOPB?editors=1000)\n\n### Installation\n\n```bash\nnpm install --save preact-habitat\n```\n\n### Core Features\n\n- 2 ways to passing props from DOM.\n- Multiple rendering options.\n- Light weight ( \u003c 1KB ).\n- Compatible with React widgets through preact-compat.\n- In use in high traffic web applications.\n\n### Basic Usage Example\n\n```js\nimport habitat from 'preact-habitat';\nimport WidgetAwesome from './components/WidgetAwesome';\n\nconst { render } = habitat(WidgetAwesome);\n\n/**\n** other selecors options:\n**\n** \".classname\"  for querying DOM element by its class name\n**\n** \"#div-id\"  for querying DOM element by its ID value\n**\n** \"[data-attribute-example='widget-here']\"  for querying DOM element by its data attribute name \u0026 val\n**\n**/\n\nrender({\n  selector: '.some-class', // Searches and mounts in \u003cdiv class=\"some-class\"\u003e\u003c/div\u003e\n  defaultProps: undefined, // Default props for all widgets\n  inline: false,\n  clean: false,\n  clientSpecified: false\n});\n```\n\nin `webpack.config.js` or any other build tool bundle output format should be `UMD`:\n\n```js\noutput: {\n  libraryTarget: 'umd'\n}\n```\n\nin the DOM you'd like to mount your widget in:\n\n```html\n\u003cdiv class=\"some-class\"\u003e \u003c!-- as specified in render, habitat will mount the component in this--\u003e\n  \u003cscript type=\"application/json\"\u003e\n    {\n      \"title\": \"Widget Title passed as prop\",\n      \"theme\": \"red\",\n      \"anotherProp\": \"Thanks for trying this widget out!\"\n    }\n  \u003c/script\u003e\n\u003c/div\u003e\n```\n\nNow, build your production ready preact widget and you're all set, TADA! 🎉\n\n## API Docs\n\n### habitat(...)\n\naccepts a single Preact component as its only argument\n\n##### example: \n```js\nimport { h } form 'preact';\nimport habitat from 'preact-habitat';\n\nconst Widget = () =\u003e \u003ch1\u003eHello, World!\u003c/h1\u003e;\n\nconst { render } = habitat(Widget); // NOTE: pass Widget and not \u003cWidget /\u003e\n\nrender({\n  ...\n});\n```\n\n### render(options)\n\nrender function accepts an options Object which supports the following properties:\n\n#### option.selector\n\n\u003eString: `.myclass`, `#myid`, `[data-selector=\"my-data-attr\"]`\n\nDOM Element selector used to retrieve the DOM elements you want to mount the widget in\n\n#### option.defaultProps\n\n\u003e Object: {} || undefined (default)\n\nDefault props to be rendered throughout widgets, you can replace each value [declaring props](#passing-props).\n\n#### option.inline\n\u003e Boolean: true || false (default)\n\nSet to true if you want to use the parent DOM node as a host for your widget without specifing any selectors.\n\nexample:\n\n```html\n\u003cdiv class=\"beautiful-container\"\u003e\n  \u003c!-- inline set to true will make this widget render in it's parent \n      wrapper class=\"beautiful-container\" without using selector option--\u003e\n  \u003cscript async src=\"cdn.preactwidget...\"\u003e\u003c/script\u003e\n\u003c/div\u003e\n```\n\n#### option.clean\n\u003e Boolean: true || false (default)\n\nclean will remove all the innerHTML from the HTMl element the widget will mount in.\n\nexample:\n\nif we set the widget to be mounted inside the selector \".beautiful-container\" with {clean: true} it will remove the Loading div as soon as it renders.\n\n```html\n\u003cdiv class=\"beautiful-container\"\u003e\n  \u003cdiv class=\"loader\"\u003eLOADING...\u003c/div\u003e\n\u003c/div\u003e\n\n\u003cscript async src=\"cdn.preactwidget...\"\u003e\u003c/script\u003e\n```\n\n#### option.clientSpecified\n\u003e Boolean: true || false (default)\n\nThis option allows who ever using the script to specifit the selector which they'd like to mount the widget in\n\n```html\n\u003cdiv class=\"beautiful-container\"\u003e\n  \u003cdiv class=\"loader\"\u003eLOADING...\u003c/div\u003e\n\u003c/div\u003e\n\n\u003cscript async src=\"cdn.preactwidget...\" data-mount-in=\".beautiful-container\"\u003e\u003c/script\u003e\n```\n\n### Passing Props\n\nThere are 2 ways to pass props, either via data-attributes or application/json script tag\n\n#### via props script\n\nSimply add a `\u003cscript\u003e` tag with `type=\"application/json\"` or `type=\"text/props\"` and ensure the content is valid JSON. multiple script tags will be merged together and passed down.\n\n```html\n\u003cdiv class=\"beautiful-container\" data-prop-name=\"preact habitat\" data-prop-version=\"v3.0.0\" data-prop-theme-color=\"green\"\u003e\n  \u003cscript type=\"application/json\"\u003e\n    {\n      \"name\": \"preact habitat\",\n      \"version\":\"v3.0.0\",\n      \"themeColor\": \"green\"\n    }\n  \u003c/script\u003e\n\u003c/div\u003e\n```\n\n#### via data-attribute\n\nthe data attribute has to always start with `data-prop-` examples:\n\n`data-prop-name` will be available in your component as `name`\n\n`data-prop-version` will be available in your component as `version`\n\n`data-prop-theme-color` will be available in your component as `themeColor` *NOTE* the lowerCamelCase when there's a -\n\n```html\n\u003cdiv class=\"beautiful-container\" data-prop-name=\"preact habitat\" data-prop-version=\"v3.0.0\" data-prop-theme-color=\"green\"\u003e\n  \n\u003c/div\u003e\n```\n\n## License\n[MIT](LICENSE) - Copyright (c) [Zouhir Chahoud](https://zouhir.org)\n\n## Credits\nArtwork By: [Oleg Turbaba, Dribble](https://dribbble.com/turbaba)\n\n\n[build-badge]: https://img.shields.io/travis/zouhir/preact-habitat.svg?style=flat-square\n[build]: https://travis-ci.org/zouhir/preact-habitat\n[coverage-badge]: https://img.shields.io/codecov/c/github/zouhir/preact-habitat.svg?style=flat-square\n[coverage]: https://codecov.io/github/zouhir/preact-habitat\n[version-badge]: https://img.shields.io/npm/v/preact-habitat.svg?style=flat-square\n[package]: https://www.npmjs.com/package/preact-habitat\n[downloads-badge]: https://img.shields.io/npm/dm/preact-habitat.svg?style=flat-square\n[npmcharts]: http://npmcharts.com/compare/preact-habitat\n[license-badge]: https://img.shields.io/npm/l/preact-habitat.svg?style=flat-square\n[license]: https://github.com/zouhir/preact-habitat/blob/master/LICENSE\n[preact-badge]: https://img.shields.io/badge/%E2%9A%9B%EF%B8%8F-preact-6F2FBF.svg?style=flat-square\n[preact]: https://preactjs.com\n[gzip-badge]: http://img.badgesize.io/https://unpkg.com/preact-habitat@3.0.2/dist/preact-habitat.umd.js?compression=gzip\u0026label=gzip%20size\u0026style=flat-square\n[unpkg-dist]: https://unpkg.com/preact-habitat/dist/\n[module-formats-badge]: https://img.shields.io/badge/module%20formats-umd%2C%20cjs%2C%20es-green.svg?style=flat-square\n[github-star-badge]: https://img.shields.io/github/stars/zouhir/preact-habitat.svg?style=social\n[github-star]: https://github.com/zouhir/preact-habitat/stargazers\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzouhir%2Fpreact-habitat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzouhir%2Fpreact-habitat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzouhir%2Fpreact-habitat/lists"}