Giter Club home page Giter Club logo

hooked-hyper's Introduction

hooked-hyper

hyperscript on hooks

Demo

This is a very simple hyperscript library to render elements directly in the DOM. So don't expect a vDOM.

Hooks are supported as well. Hooks have same syntax as React Hooks.

Re-rendering is done by (brutally) removing the affected childs. Scroll-states may get messed-up.

Size is ~1kB minimized and gzipped.

Will work only on modern browsers.

Credits ๐Ÿ‘ go to zserge/o for inspiration.

h, render

Function h is used to render elements into the DOM.

h(element, props, children)

element can be of type HTML-Node, string or functions (functional components) are accepted.

render(document.body, {}, [
  h('span', { className: 'lorem' }, 'Lorem')
  h('span', {}, 'ipsum')
])

renders as

<body>
  <span class="lorem">Lorem</span>
  <span>ipsum</span>
</body>

Functional components have children passed as props (as with react).

e.g. to render children nodes within a <section>:

// functional component
function Comp ({ children, ...props }) {
  return h('section', props, children)
}

// rendering
render(document.body, {}, [
  h(Comp, { className: 'red' }, [
    h('span', { className: 'lorem' }, 'Lorem')
    h('span', {}, 'ipsum')
  ])
])

renders as

<body>
  <section class="red">
    <span class="lorem">Lorem</span>
    <span>ipsum</span>
  </section>
</body>

usage

import { h, render } from './h.js'

const style = `
body > .red { color: red; } 
`

render(document.body, {}, [
  h('style', {}, style),
  h('h1', { className: 'red' }, [
    'hooked hyperscript'
  ]),
  h('p', {}, '<script>/* escaping works */</script>'),
  h('p', { style: { 
    color: 'blue', 
    textTransform: 'uppercase' 
  } }, 'Lorem ipsum.'),
  h('button', { 
    onClick: () => alert('clicked'), 
    style: { marginBottom: '1em' } 
  }, 'Click me'),
])

usage with hooks

import { render, h, Fragment, useState } from './h.js'

function Counter (props) {
  const style = { padding: '1em' }
  const [count, setCount] = useState(0)

  return h(Fragment, {}, [
    h('button', { style, onClick: () => setCount(count - 1)}, '-'),
    h('span', props, count),
    h('button', { style, onClick: () => setCount(count + 1)}, '+'),
  ])
}

render(document.body, {}, 
  h(Counter, { style: { color: 'cyan' } })
)

Other hooks like useReducer, useEffect, useMemo, useCallback, useRef and useContext are supported as well.

license

The Unlicense

hooked-hyper's People

Contributors

commenthol avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.