{"id":13447568,"url":"https://github.com/yelouafi/petit-dom","last_synced_at":"2025-04-13T05:07:53.853Z","repository":{"id":22566260,"uuid":"96702105","full_name":"yelouafi/petit-dom","owner":"yelouafi","description":"minimalist virtual dom library","archived":false,"fork":false,"pushed_at":"2022-12-12T05:15:50.000Z","size":511,"stargazers_count":509,"open_issues_count":10,"forks_count":36,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-13T05:07:48.848Z","etag":null,"topics":[],"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/yelouafi.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}},"created_at":"2017-07-09T18:07:43.000Z","updated_at":"2025-04-08T11:23:57.000Z","dependencies_parsed_at":"2023-01-13T22:04:19.133Z","dependency_job_id":null,"html_url":"https://github.com/yelouafi/petit-dom","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yelouafi%2Fpetit-dom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yelouafi%2Fpetit-dom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yelouafi%2Fpetit-dom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yelouafi%2Fpetit-dom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yelouafi","download_url":"https://codeload.github.com/yelouafi/petit-dom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665747,"owners_count":21142123,"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-07-31T05:01:21.220Z","updated_at":"2025-04-13T05:07:53.827Z","avatar_url":"https://github.com/yelouafi.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Frameworks"],"sub_categories":["Rest of the Pack"],"readme":"# petit-dom\n\nA minimalist virtual DOM library.\n\n- Supports HTML \u0026 SVG elements.\n- Supports Render functions and Fragments.\n- Custom components allows to build your own abstraction around DOM elements.\n- Directives allows you to attach custom behavior to DOM elements.\n\n## Installation\n\nThe library is provided as a set of ES modules. You can install using `npm` or `yarn` and then import\nfrom `petit-dom` (see example below).\n\n```sh\n$ npm install --save petit-dom\n```\n\nor\n\n```sh\n$ yarn add petit-dom\n```\n\n\u003e Note however no transpiled build is provided. The library will work with all recent versions of `Node` and major browsers. If you're targeting older platforms, make sure to transpile to the desired ES version.\n\nTo run the examples, you can run a local web server (like npm `http-server` module) from the root folder of the project. Since all example use ES6 modules, you can simply navigate to the example you want and load the desired HTML file.\n\n## Usage\n\nIf you're using Babel you can use JSX syntax by configuring the jsx runtime\n\n```json\n{\n  \"presets\": [\n    [\n      \"@babel/preset-react\",\n      { \"runtime\": \"automatic\", \"importSource\": \"petit-dom\" }\n    ]\n  ]\n}\n```\n\n```js\nimport { render } from \"petit-dom\";\n\n//  assuming your HTML contains a node with \"root\" id\nconst parentNode = document.getElementById(\"root\");\n\n// mount\nrender(\u003ch1\u003eHello world!\u003c/h1\u003e, parentNode);\n\n// patch\nrender(\u003ch1\u003eHello again\u003c/h1\u003e, parentNode);\n```\n\nAlternatively you can use the classic Babel transform via `/* @jsx h */` on the top. You can also use the raw `h` function calls if you want, see examples folder for usage.\n\npetit-dom also supports render functions\n\n```js\nimport { render } from \"petit-dom\";\n\nfunction Box(props) {\n  return (\n    \u003cdiv\u003e\n      \u003ch1\u003e{props.title}\u003c/h1\u003e\n      \u003cp\u003e{props.children}\u003c/p\u003e\n    \u003c/div\u003e\n  );\n}\n\nrender(\u003cBox title=\"Fancy box\"\u003ePut your content here\u003c/Box\u003e, parentNode);\n```\n\nrender functions behave like React pure components. Patching with the same\narguments will not cause any re-rendering. You can also attach a `shouldUpdate`\nfunction to the render function to customize the re-rendering behavior (By default\nprops are tested for shallow equality).\n\n## Custom components\n\nBesides HTML/SVG tag names, fragments and render fucntions, the `h` function also accepts any object\nwith the following signature\n\n```js\n{\n  mount(self);\n  patch(self);\n  unmount(self);\n}\n```\n\nEach of the 3 functions will be called by the library at the moment suggested by its name.\n\nThe `self` argument which is an aboject holding the following properties:\n\n- `render(...)`: To create/update DOM content for the component\n- `props`: the current props passed to the JSX element (or `h` function)\n- `oldProps`: the previous props, it's value is `undefined` inside `mount`\n\nYou can also attach arbitrary properties to the object, they will persist between different\ninvocations.\n\nSee examples folder for how to define some custom components.\n\n## Directives\n\nYou can also attach custom behaviors to DOM nodes. Directives allows you to obtain references\nto DOM nodes and manage their lifecycle.\n\nA directive is an object with the current interface\n\n```js\n{\n  mount(domElement, value);\n  patch(domElement, newValue, oldValue);\n  unmount(element, lastValue);\n}\n```\n\nThere's an example of a simple log directive in the examples folder.\n\n## API\n\n### `h(type, props, ...children)`\n\nCreates a virtual node.\n\n- `type`: a string (HTML or SVG tag name), or a custom component (see above)\n\n- `props`: in the case of HTML/SVG tags, this corresponds to the attributes/properties\n  to be set in the real DOM node. In the case of components, `{ ...props, children }` is\n  passed to the appropriate component function (`mount` or `patch`).\n\n### `render(vnode, parentDom, options = {})`\n\nrenders a virtual node into the DOM. The function will initially create a DOM node\nas specified the virtual node `vnode` and append it to the children of`parentDOM`.\nSubsequent calls will update the previous DOM node (or replace it if it's a different tag).\n\nOptionally, you can use `options` to pass custom directives, for example:\n\n```js\n  let log = { ... }, // defines a log directive\n  render(\u003cSomething /\u003e, parent, { directives: { log } });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyelouafi%2Fpetit-dom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyelouafi%2Fpetit-dom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyelouafi%2Fpetit-dom/lists"}