{"id":15649631,"url":"https://github.com/brycerussell/astro-json-element","last_synced_at":"2025-07-07T16:35:09.870Z","repository":{"id":39000146,"uuid":"506504159","full_name":"BryceRussell/astro-json-element","owner":"BryceRussell","description":"Astro component that allows you to define HTML elements using JS Objects","archived":false,"fork":false,"pushed_at":"2022-12-07T17:59:33.000Z","size":2368,"stargazers_count":45,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-30T17:13:10.503Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Astro","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/BryceRussell.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":"2022-06-23T05:08:48.000Z","updated_at":"2025-02-06T00:42:58.000Z","dependencies_parsed_at":"2023-01-24T19:34:00.697Z","dependency_job_id":null,"html_url":"https://github.com/BryceRussell/astro-json-element","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BryceRussell%2Fastro-json-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BryceRussell%2Fastro-json-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BryceRussell%2Fastro-json-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BryceRussell%2Fastro-json-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BryceRussell","download_url":"https://codeload.github.com/BryceRussell/astro-json-element/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251748956,"owners_count":21637418,"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-10-03T12:30:33.845Z","updated_at":"2025-04-30T17:13:16.447Z","avatar_url":"https://github.com/BryceRussell.png","language":"Astro","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Astro JSON Element\n\nCreate HTML elements using JS objects\n\nThis component was originaly created to interface with html elements inside of a component using props\n\n## Features\n\n- Create html element using js object\n- Recursive, create elements inside elements\n- Control over render order using `slot` prop\n- Add automatically escaped text using `text` prop\n- Add a html string using the `innerHTML` prop\n- Apply default attributes to all child elements\n- `class` attribute uses `class:list` directive (clsx)\n\n## How to use\n\n**Install package**:\n\n```\nnpm i astro-json-element\n```\n\n**Create Element**:\n\n```tsx\n---\nimport { Element } from 'astro-json-element';\n\nconst my_element = {\n    tag: \"h1\",\n    text: \"Heading\",\n    class: \"heading\",\n    id: \"my-heading\";\n}\n---\n\n\u003cElement {...my_element}/\u003e\n// \u003ch1 id=\"my-heading\" class=\"heading\"\u003eHeading\u003c/h1\u003e\n```\n\n## Examples\n\n**Headless Pattern Example**: [`\u003cPagination\u003e`](https://github.com/BryceRussell/astro-bryceguy/blob/master/packages/pagination/pagination/Pagination.astro) component\n\n**Basic Navbar Example**:\n\n![Navbar](https://raw.githubusercontent.com/BryceRussell/astro-json-element/master/examples/navbar.PNG)\n\n\u003e **Note**: Styling using the `style` attribute or [tailwindcss classes](https://tailwindcss.com) is easier and keeps your html and css together in the same file/object\n\n```tsx\n---\nimport { Element } from 'astro-json-element';\n\nconst header = {\n    tag: \"header\",\n    style: \"font-family:Arial;display:flex;justify-content:space-around;margin:1rem;border-radius:5px;background-color:white;border:3px solid purple\",\n    _heading: {\n        tag: \"h1\",\n        text: \"Purple\",\n        style: \"font-weight:bold;font-size:3rem;color:purple;\"\n    },\n    _ul: {\n        tag: \"ul\",\n        style: \"display:flex;align-items:center;gap:1rem;font-weight:bold;font-size:1.25rem;color:purple;\",\n        _item1: {\n            tag: \"li\",\n            _link: {\n                tag: \"a\",\n                text: \"Home\"\n            }\n        },\n        _item2: {\n            tag: \"li\",\n            _link: {\n                tag: \"a\",\n                text: \"Products\"\n            }\n        },\n        _item3: {\n            tag: \"li\",\n            _link: {\n                tag: \"a\",\n                text: \"About\"\n            }\n        },\n        _item4: {\n            tag: \"li\",\n            style: \"padding:.75rem 1rem;border-radius:5px;background-color:purple;color:white;\",\n            _link: {\n                tag: \"a\",\n                text: \"Contact\"\n            }\n        },\n    }\n}\n---\n\n\u003cElement {...header}/\u003e\n```\n\n## Render Order\n\n1. slot `first`\n2. _[child] slot `first`\n3. `text`\n4. _[child] slot `before`\n5. `slot` (default)\n6. _[child] slot `after`\n7. `innerHTML`\n8. _[child] slot `last`\n9. slot `last`\n\n## Slots\n\n**Default**: Elements are slot in at the center of the render order (5 of 9)\n\n### `first`\n\nFirst element in [Render Order](#render-order)\n\n### `last`\n\nLast elemenet in [Render Order](#render-order)\n\n\n## Props\n\n### `tag`\n\n**Type**: `string`\n\n**Default**: `div`\n\nDefines what HTML tag the element will be\n\n### `slot`\n\n**Type**: `string`\n\n**Options**: `first`, `before`, `after`, `last`\n\n**Default**: `last`\n\nControls where a _child element will be rendered inside of a parent json-element\n\nsee [Render Order](#render-order)\n\n\n### `text`\n\n**Type**: `string`\n\nSet the text of an element, automatically escaped\n\n### `innerHTML`\n\n**Type**: `string`\n\nSet the innerHTML of an element, a string of HTML\n\n### `defaults`\n\n**Type**: `object`\n\nDefine default props for _child elements\n\n### `debug`\n\n**Type**: `boolean`\n\n**Default**: `false`\n\nIf true the element will print its props to the console\n\n### `...attrs`\n\n**Type**: `object`\n\nAny other key/value pairs will be added as attributes to your element\n\n```tsx\n---\nconst my_element = {\n    tag: \"span\",\n    text: \"Text\",\n    id: \"my-element\",\n    key: value,\n}\n---\n\n\u003cElement {...my_element}/\u003e\n// \u003cspan id=\"my-element\" key=\"value\"\u003eText\u003c/span\u003e\n```\n\n## `class`\n\n**Type**: `set | object | array | string`\n\nThe class attribute uses the `class:list` directive (clsx):\n\n```tsx\n---\nconst my_element = {\n    tag: \"div\",\n    class: ['This', 'is', 'a', 'test']\n}\n---\n\n\u003cElement {...my_element}/\u003e\n// \u003cdiv class=\"This is a test\"\u003eText\u003c/div\u003e\n```\n\n### `_[child]`\n\n**Type**: `astro-json-element`\n\nastro-json-elements are recursive, you can create elements inside of elements by prefixing a key with `_`\n\n__NOTE:__ Some tags like h1-6 and p tags do not allow children and will slot the child element after the defined element inside the parent element\n\n**Example**:\n\n![Header](https://raw.githubusercontent.com/BryceRussell/astro-json-element/master/examples/header.PNG)\n\n```tsx\n---\nconst header = {\n    tag: \"header\",\n    style: \"display:flex;justify-content:center;background-color:white;border:3px solid purple\",\n    _heading: {\n        tag: \"h1\",\n        text: \"Purple Heading\",\n        style: \"font-weight:bold;font-size:3rem;color:purple;\"\n    }\n}\n---\n\n\u003cElement {...header}/\u003e\n```\n\n![List](https://raw.githubusercontent.com/BryceRussell/astro-json-element/master/examples/list.PNG)\n\n```tsx\n---\nconst list = {\n    tag: \"ul\",\n    style: \"display:flex;align-items:center;gap:1rem;font-weight:bold;font-size:1.25rem;color:purple;\",\n    _item1: {\n        tag: \"li\",\n        _link: {\n            tag: \"a\",\n            text: \"Home\"\n        }\n    },\n    _item2: {\n        tag: \"li\",\n        _link: {\n            tag: \"a\",\n            text: \"Products\"\n        }\n    },\n    _item3: {\n        tag: \"li\",\n        _link: {\n            tag: \"a\",\n            text: \"About\"\n        }\n    },\n    _item4: {\n        tag: \"li\",\n        style: \"padding:.75rem 1rem;border-radius:5px;background-color:purple;color:white;\",\n        _link: {\n            tag: \"a\",\n            text: \"Contact\"\n        }\n    },\n}\n---\n\n\u003cElement {...list}/\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrycerussell%2Fastro-json-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrycerussell%2Fastro-json-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrycerussell%2Fastro-json-element/lists"}