5.0.4 β€’ Published 1 year ago

shiva v5.0.4

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
1 year ago

shiva

GitHub license npm version Bitbucket Pipeline Status PRs Welcome javascript front-end declarative ui library

shiva is a minimal JavaScript library for building user interfaces.

Aim

The aim of shiva is to use JavaScript to create HTML elements with a more declarative DSL (yes I know, another DSL 😁).

  • No need for a HTML templating language like jsx
  • Have access to the HTML element for further manipulation after creation

So instead of:

<div>Hi there πŸ‘‹</div>

shiva provides a function called div():

import { div } from "shiva"

const HTMLDivElement = div("Hi there πŸ‘‹")

Nested

Nested elements are easy to construct:

import { div } from "shiva"

const nestedHTMLDivElement = div(
    div("Hi there πŸ‘‹"),
    div("Hi there πŸ‘‹"),
)

This will produce:

<div>
    <div>Hi there πŸ‘‹</div>
    <div>Hi there πŸ‘‹</div>
</div>

shiva will append HTML elements if they are passed as an argument. It will create and append a textNode element if a string is passed.

Attributes

To pass attributes to an element:

import { div } from "shiva"

const handler = () => {
    console.log("handled it πŸ‘Š")
}

const superDiv = div("Hi there πŸ‘‹", { 
    onclick: handler,
    class: style,
    id: "Super Div πŸ¦Έβ€β™‚οΈ",
    style: {
        fontSize: "3rem"
    }
})

shiva will apply attributes if an object is passed as one of the arguments.

Elements

shiva provides:

  • div()
  • img()
  • p()
  • h1()
  • h2()
  • h3()
  • h4()
  • h5()
  • h6()
  • b()
  • strong()
  • i()
  • em()
  • a()
  • ul()
  • ol()
  • li()
  • span()
  • br()
  • code()
  • pre()
  • button()

To create other HTML elements not included in the API, shiva provides a more generic element() function.

import { element } from "shiva"

const HTMLButtonElement = element("button", "Click me ⬇️")

Any HTML element, including custom elements, can be created.

Components

Element functions like div() or element() take a HTML element as an argument.

import { div } from "shiva"

const component = (title) => {
    return div(title) 
    // returns a HTMLDivElement
}

const divAndComponent = div(
    component("The TITLE")
)

This pattern can be used to build an application out of pieces, each component just needs to return a HTML element (or array of HTML elements).

Of course, component() can be imported from another file.

Installation

npm install shiva --save

shiva is distributed as an ES module. Please use a module aware build tool to import.

See ./consumer for an example of how to include shiva in a project.

Getting started

Create root element

To append a root element that all other elements will attach to:

import { div } from 'shiva'

const app = () => {
    div({ root: true }, 
        componentA(),
        componentB()
    )
}

app()

This will append a HTMLElement <div> to the body of the page with componentA and componentB nested inside.

πŸ‘‰ componentA and componentB must return a HTML element or array of HTML elements to be appended to the parent.

State

shiva provides a very simple publish / subscribe utility.

pubsub() returns an array where the first element is the subscribe function and the second element is the publish function.

import { pubsub } from "shiva"

const [subscribe, publish] = pubsub([1, 2, 4, 5])

subscribe(data => {
    console.log(data)
    // logs the value passed to publish()
})

// some time later
publish([6,7,9,12]) // logs [6,7,9,12]

API

pubsub(
    initial?: any,
    reducer?: (current: any, next: any) => void
)

Initialise state with initial.

Run a function on new state with a reducer function. Can be used for more complex logic or when the next state depends on the previous one.

const reducer = (state, newState) => return state + newState

const [subscribe, publish] = pubsub("DOGE", reducer)

subscribe(state => {
    console.log(state)
})

publish(" to the moon πŸš€") // logs "DOGE to the moon πŸš€" 

Global store

Extending the publish / subscribe pattern across components creates a global store, useful if you don't want to pass subscribers and publishers through nested components.

shiva provides a createStore() function to create this global store.

// global-store.js
import { createStore } from "shiva"

const globalStore = createStore("🌍")

export default globalStore

Now subscribe or publish to this store.

// another file
import globalStore from "./global-store"

const [subscriberGlobal, publishGlobal] = globalStore

// Here we can name the subscribe / publish functions, note we don't call the globalStore, it is already a subscribe / publish tuple.

License

MIT license

5.0.4

1 year ago

5.0.3

1 year ago

4.1.8

3 years ago

4.1.7

3 years ago

4.1.6

3 years ago

4.1.5

4 years ago

4.1.4

5 years ago

4.1.3

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.0.4

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.1.0

5 years ago

3.4.0

6 years ago

3.3.0

7 years ago

3.2.3

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.5.8

7 years ago

2.5.7

7 years ago

2.5.6

7 years ago

2.5.5

7 years ago

2.5.4

7 years ago

2.5.3

8 years ago

2.5.2

8 years ago

2.5.1

8 years ago

2.5.0

8 years ago

2.4.7

9 years ago

2.4.6

9 years ago

2.4.5

9 years ago

2.4.4

9 years ago

2.4.3

9 years ago

2.4.2

9 years ago

2.4.1

9 years ago

2.4.0

9 years ago

2.3.7

9 years ago

2.3.6

9 years ago

2.3.5

9 years ago

2.3.4

9 years ago

2.3.3

9 years ago

2.3.2

9 years ago

2.3.1

9 years ago

2.3.0

9 years ago

2.2.0

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.7

9 years ago

2.0.6

9 years ago

2.0.5

9 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.0

9 years ago

0.6.2

9 years ago

0.6.1

9 years ago

0.6.0

9 years ago

0.5.14

9 years ago

0.5.12

9 years ago

0.5.11

10 years ago

0.5.10

10 years ago

0.5.9

10 years ago

0.5.8

10 years ago

0.5.7

10 years ago

0.5.6

10 years ago

0.5.5

10 years ago

0.5.4

10 years ago

0.5.3

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago