{"id":13395411,"url":"https://github.com/vadimdemedes/dom-chef","last_synced_at":"2025-05-16T07:04:05.591Z","repository":{"id":22311759,"uuid":"95906764","full_name":"vadimdemedes/dom-chef","owner":"vadimdemedes","description":"🍔 Build DOM elements using JSX automatically","archived":false,"fork":false,"pushed_at":"2024-09-03T21:03:20.000Z","size":397,"stargazers_count":519,"open_issues_count":7,"forks_count":12,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-05-04T17:45:52.235Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/vadimdemedes.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/funding.yml","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},"funding":{"open_collective":"vadimdemedes","custom":"https://www.comebackalive.in.ua"}},"created_at":"2017-06-30T16:39:14.000Z","updated_at":"2025-04-28T01:40:06.000Z","dependencies_parsed_at":"2023-12-19T08:06:54.859Z","dependency_job_id":"ae2858c5-69c4-4fe0-82c0-d4c0ce57775d","html_url":"https://github.com/vadimdemedes/dom-chef","commit_stats":{"total_commits":111,"total_committers":14,"mean_commits":7.928571428571429,"dds":0.5045045045045045,"last_synced_commit":"d6fb65c65be6912b6d4361b83cc369cf1fb684fe"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimdemedes%2Fdom-chef","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimdemedes%2Fdom-chef/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimdemedes%2Fdom-chef/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimdemedes%2Fdom-chef/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vadimdemedes","download_url":"https://codeload.github.com/vadimdemedes/dom-chef/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252702250,"owners_count":21790698,"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-30T17:01:57.308Z","updated_at":"2025-05-16T07:04:05.565Z","avatar_url":"https://github.com/vadimdemedes.png","language":"TypeScript","funding_links":["https://opencollective.com/vadimdemedes","https://www.comebackalive.in.ua"],"categories":["TypeScript","JavaScript","Integrations"],"sub_categories":["Other JSX based libraries"],"readme":"\u003ch1 align=\"center\"\u003e\n  \u003cbr\u003e\n  \u003cimg width=\"300\" src=\"media/logo.png\"\u003e\n  \u003cbr\u003e\n  \u003cbr\u003e\n\t\u003cbr\u003e\n\u003c/h1\u003e\n\n\u003e Build regular DOM elements using JSX\n\nWith `dom-chef`, you can use Babel or TypeScript to transform [JSX](https://reactjs.org/docs/introducing-jsx.html) into plain old DOM elements, without using the unsafe `innerHTML` or clumsy `document.createElement` calls.\n\nIt supports everything you expect from JSX, including:\n\n- [SVG elements](#render-svg)\n- [Event listeners](#inline-event-listeners)\n- [Inline CSS](#inline-styles)\n- [Nested elements](#nested-elements)\n- [Function elements](#use-functions)\n\nIf something isn't supported (or doesn't work as well as it does in React) please open an issue!\n\n## Install\n\n```\n$ npm install dom-chef\n```\n\n## Usage\n\nMake sure to use a JSX transpiler (e.g. [Babel](#babel), [TypeScript compiler](#typescript-compiler), [esbuild](https://esbuild.github.io/content-types/#using-jsx-without-react), you only need one of them).\n\n```jsx\nimport {h} from 'dom-chef';\n\nconst handleClick = e =\u003e {\n\t// \u003cbutton\u003e was clicked\n};\n\nconst el = (\n\t\u003cdiv className=\"header\"\u003e\n\t\t\u003cbutton className=\"btn-link\" onClick={handleClick}\u003e\n\t\t\tDownload\n\t\t\u003c/button\u003e\n\t\u003c/div\u003e\n);\n\ndocument.body.appendChild(el);\n```\n\n### Babel\n\n`pragma` and `pragmaFrag` must be configured this way. More information on [Babel’s documentation](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx.html#pragma).\n  \n```js\n// babel.config.js\n  \nconst plugins = [\n\t[\n\t\t'@babel/plugin-transform-react-jsx',\n\t\t{\n\t\t\tpragma: 'h',\n\t\t\tpragmaFrag: 'DocumentFragment',\n\t\t},\n\t],\n];\n\n// ...\n```\n\n### TypeScript compiler\n\n`jsxFactory` and `jsxFragmentFactory` must be configured this way. More information on [TypeScripts’s documentation](https://www.typescriptlang.org/tsconfig#jsxFactory).\n\n\n```jsonc\n// tsconfig.json\n\n{\n\t\"compilerOptions\": {\n\t\t\"jsxFactory\": \"h\",\n\t\t\"jsxFragmentFactory\": \"DocumentFragment\"\n\t}\n}\n```\n\n### Alternative usage\n\nYou can avoid configuring your JSX compiler by just letting it default to `React` and exporting the `React` object:\n\n```js\nimport React from 'dom-chef';\n```\n\n## Recipes\n\n### Set classes\n\n```jsx\nconst el = \u003cspan class=\"a b c\"\u003eText\u003c/span\u003e;\n\n// or use `className` alias\nconst el = \u003cspan className=\"a b c\"\u003eText\u003c/span\u003e;\n```\n\n### Inline styles\n\n```jsx\nconst el = \u003cdiv style={{padding: 10, background: '#000'}} /\u003e;\n```\n\n### Inline event listeners\n\n```jsx\nconst handleClick = e =\u003e {\n\t// \u003cspan\u003e was clicked\n};\n\nconst el = \u003cspan onClick={handleClick}\u003eText\u003c/span\u003e;\n```\n\nThis is equivalent to: `span.addEventListener('click', handleClick)`\n\n### Nested elements\n\n```jsx\nconst title = \u003ch1\u003eHello World\u003c/h1\u003e;\nconst body = \u003cp\u003ePost body\u003c/p\u003e;\n\nconst post = (\n\t\u003cdiv class=\"post\"\u003e\n\t\t{title}\n\t\t{body}\n\t\u003c/div\u003e\n);\n```\n\n### Set innerHTML\n\n```jsx\nconst dangerousHTML = '\u003cscript\u003ealert();\u003c/script\u003e';\n\nconst wannaCry = \u003cdiv dangerouslySetInnerHTML={{__html: dangerousHTML}} /\u003e;\n```\n\n### Render SVG\n\n**Note**: Due to the way `dom-chef` works, tags `\u003ca\u003e`, `\u003caudio\u003e`, `\u003ccanvas\u003e`, `\u003ciframe\u003e`, `\u003cscript\u003e` and `\u003cvideo\u003e` aren't supported inside `\u003csvg\u003e` tag.\n\n```jsx\nconst el = (\n\t\u003csvg width={400} height={400}\u003e\n\t\t\u003ctext x={100} y={100}\u003e\n\t\t\tWow\n\t\t\u003c/text\u003e\n\t\u003c/svg\u003e\n);\n```\n\n### Use functions\n\nIf element names start with an uppercase letter, `dom-chef` will consider them as element-returning functions:\n\n```jsx\nfunction Title() {\n\tconst title = document.createElement('h1');\n\ttitle.classList.add('Heading');\n\treturn title;\n}\n\nconst el = \u003cTitle className=\"red\"\u003eLa Divina Commedia\u003c/Title\u003e;\n// \u003ch1 class=\"Heading red\"\u003eLa Divina Commedia\u003c/h1\u003e\n```\n\nThis makes JSX also a great way to apply multiple attributes and content at once:\n\n```jsx\nconst BaseIcon = () =\u003e document.querySelector('svg.icon').cloneNode(true);\n\ndocument.body.append(\n\t\u003cBaseIcon width=\"16\" title=\"Starry Day\" className=\"margin-0\" /\u003e\n);\n```\n\nTo improve compatibility with React components, `dom-chef` will pass the function's `defaultProps` property to itself (if present). Note that specifying attributes won't override those defaults, but instead set them on the resulting element:\n\n```jsx\nfunction AlertIcon(props) {\n\treturn \u003csvg width={props.size} className={props.className} /\u003e\n}\n\nAlertIcon.defaultProps = {\n\tclassName: 'icon icon-alert'\n\tsize: 16,\n}\n\nconst el = \u003cAlertIcon className=\"margin-0\" size={32} /\u003e;\n// \u003csvg width=\"16\" class=\"icon icon-alert margin-0\" size=\"32\" /\u003e\n```\n\n## License\n\nMIT © [Vadim Demedes](https://github.com/vadimdemedes)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvadimdemedes%2Fdom-chef","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvadimdemedes%2Fdom-chef","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvadimdemedes%2Fdom-chef/lists"}