Skip to content
/ h Public

H is a micro-library (800 B) to create DOM Trees using HyperScript template

License

Notifications You must be signed in to change notification settings

gc-victor/h

Repository files navigation

H

H is a micro-library (~1 KB) to create DOM Trees using HyperScript template.

Key Features

  • Real DOM Tree
  • SSR out of the box
  • JSX compatible
  • Zero dependencies
  • Small API, not much to learn

Let's Play

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="module">
            import h from 'https://cdn.jsdelivr.net/gh/gc-victor/h/dist/esm/index.js';

            let input;
            let count = 0;

            const increment = () => {
                count = count + 1;
                input.value = count;
            };
            const decrement = () => {
                count = count - 1;
                input.value = count;
            };
            const add = (ev) => {
                count = Number(ev.target.value);
            };

            const component = () => {
                return h('div', { id: 'app' }, [
                    h('h1', {}, ['Counter']),
                    h('button', { onClick: increment }, ['+']),
                    h('input', { ref: (el) => input = el, onInput: add, name: 'input', type: 'number', value: count }),
                    h('button', { onClick: decrement }, ['-'])
                ]);
            };

            const app = document.getElementById('app');
            app.parentNode.replaceChild(counter(), app);
        </script>
    </head>
    <body>
        <div id="app">
            <h1>Counter</h1>
            <button>+</button>
            <input name="input" type="number" value="0">
            <button>-</button>
        </div>
    </body>
</html>

Install

You can use pnpm, npm or yarn to install it.

npm install git+https://github.com/gc-victor/h.git#main

Import it in your framework.

import h from 'h';

Or import it in a <script> as a module.

<script type="module">
    import h from 'https://cdn.jsdelivr.net/gh/gc-victor/h/dist/esm/index.js';
</script>

How to use it

It doesn't require bundlers or compilers.

h('h1', {}, ['H']);

Uses camel case for events

h('input', { name: 'input', onInput: () => {} });

Uses className instead of class

h('p', { className: 'paragraph' }, ['Paragraph']);

Uses htmlFor instead or for

h('input', { name: 'input', id: '---' });
h('label', { htmlFor: '---' }, ['Input']);

Uses ref to get an element

h('input', { ref: (el) => input = el,  name: 'input', onInput: () => {} });

Multiple templates can be inherited

h('p', {}, [h('span', {}, [])]);

JSX

To transpile JSX you can use esbuild or Babel plugin to convert it to JavaScript.

import h from 'h';

const App = () =>
    <div id="app">
        <h1>Counter</h1>
        <button onClick={increment}>+</button>
        <input ref={(el) => input = el} onInput={add} name="input" type="number" value={count} />
        <button onClick={decrement}>-</button>
    </div>;

SSR

To use it in SSR, you only need to import the h/document module into the main file.

import 'h/document';

// Your code ...

Acknowledgments

Inspiration

Tools

Compatible Versioning

Summary

Given a version number MAJOR.MINOR, increment the:

  • MAJOR version when you make backwards-incompatible updates of any kind
  • MINOR version when you make 100% backwards-compatible updates

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR format.

ComVer

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all, see if your issue or idea has already been reported. If it hasn't, just open a new clear and descriptive issue.

Commit message conventions

A specification for adding human and machine readable meaning to commit messages.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope and do avoid unrelated commits.

  • Fork it!
  • Clone your fork: git clone http://github.com/<your-username>/h
  • Navigate to the newly cloned directory: cd h
  • Create a new branch for the new feature: git checkout -b my-new-feature
  • Install the tools necessary for development: npm install
  • Make your changes.
  • npm run build to verify your change doesn't increase output size.
  • npm test to make sure your change doesn't break anything.
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request with full remarks documenting your changes.

License

MIT License

Copyright (c) 2021 Víctor García

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

H is a micro-library (800 B) to create DOM Trees using HyperScript template

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published