An open API service indexing awesome lists of open source software.

https://github.com/scarletflash/declarative-element

Lightweight, simple and reliable boilerplate wrapper for DOM-elements creation
https://github.com/scarletflash/declarative-element

Last synced: 12 months ago
JSON representation

Lightweight, simple and reliable boilerplate wrapper for DOM-elements creation

Awesome Lists containing this project

README

          

# Declarative Element

![CI](https://github.com/ScarletFlash/declarative-element/actions/workflows/on-push.yaml/badge.svg)
![Code Coverage](https://img.shields.io/badge/code--coverage-100%25-green)
![Type Definitions](https://img.shields.io/npm/types/declarative-element)
![NPM License](https://img.shields.io/npm/l/declarative-element)

Lightweight, simple and reliable boilerplate wrapper for DOM-elements creation

- TypeScript API — nothing is written in pure JS
- Optimized for smaller bundle size — manually + minified by ESBuild
- Covered by tests
- SemVer versioning
- ~~Online demo and query constructor~~ TBA

## Installation

```bash
npm install declarative-element@latest
```

## Usage

```javascript
import { getElement } from 'declarative-element';

/** @type {import('declarative-element').Node.WithChildren} */
const input = {
tagName: 'main',
children: [
{
tagName: 'header',
children: [{ tagName: 'h1', innerText: 'HTML Sample' }],
},
{
tagName: 'section',
children: [{ tagName: 'p', innerText: 'Hello, World!' }],
},
],
};

const output = getElement(input);
document.body.appendChild(output);
```

## Samples

JSON
HTML
JS

```json
{ "tagName": "div", "attributes": { "class": "square" } }
```

```html


```

```javascript
const element = document.createElement('div');
element.classList.add('square');
```

```json
{ "tagName": "p", "innerText": "Hello, World!" }
```

```html

Hello, World!


```

```javascript
const element = document.createElement('p');
element.innerHTML = 'Hello, World!';
```

```json
{
"tagName": "a",
"children": [{ "tagName": "button", "innerText": "Subscribe" }]
}
```

```html

Subscribe

```

```javascript
const buttonElement = document.createElement('button');
element.innerHTML = 'Subscribe';

const anchorElement = document.createElement('a');
anchorElement.appendChild(buttonElement);
```

```json
{
"tagName": "html",
"children": [
{
"tagName": "head",
"children": [{ "tagName": "title", "innerText": "Sample" }]
},
{
"tagName": "body",
"children": [
{ "tagName": "header" },
{ "tagName": "main" },
{ "tagName": "footer" }
]
}
]
}
```

```html


Sample



```

```javascript
const titleElement = document.createElement('title');
titleElement.innerHTML = 'Sample';

const headElement = document.createElement('head');
headElement.appendChild(titleElement);

const headerElement = document.createElement('header');

const mainElement = document.createElement('main');

const footerElement = document.createElement('footer');

const bodyElement = document.createElement('body');
bodyElement.appendChild(headerElement);
bodyElement.appendChild(mainElement);
bodyElement.appendChild(footerElement);

const htmlElement = document.createElement('html');
htmlElement.appendChild(headElement);
htmlElement.appendChild(bodyElement);
```