Code Monkey home page Code Monkey logo

dscript's Introduction

dscript

NPM version Build Status Coverage Status

Code Climate Dependencies DevDependencies

PRs Welcome Commitizen friendly semantic-release

Framework agnostic hyperscript

Should work with any JSX pragma that works out of the box with Babel's JSX implementation or a function that accepts an HTML tag or component, attributes object, and children list.

Install

npm install --save dscript

Note: Webpack users will need to setup a json-loader as dscript relies on html-tags, which uses a JSON file

General Usage

import dscript from 'dscript'
import {element} from 'deku'

const {div, li, ul} = dscript(element)

const handleClick = () => alert('hi!')

export default ({props}) =>
  div('.list-container', {onClick: handleClick}, [
    ul(
      props.items.map(item =>
        li(item.name)
      )
    )
  ])

Usage with React

It is recommended to use dscript-react to remove dscript boilerplate.

Take the following:

import React from 'react'

export default props =>
  <ul>
    {props.items.map(item =>
      <li>{item.name}</li>
    )}
  </ul>

or:

import {createElement} from 'react'

export default props =>
  createElement('ul', {},
    props.items.map(item =>
      createElement('li', {}, [
        item.name
      ])
    )
  )

and instead write:

import {createElement} from 'react'
import dscript from 'dscript'

const {li, ul} = dscript(createElement)

export default props =>
  ul(
    props.items.map(item =>
      li(item.name)
    )
  )

Usage with Deku

It is recommended to use dscript-deku to remove dscript boilerplate.

Take the following:

import {element} from 'deku'

export default ({props}) =>
  <ul>
    {props.items.map(item =>
      <li>{item.name}</li>
    }
  </ul>

or:

import {element} from 'deku'

export default ({props}) =>
  element('ul', {},
    props.items.map(item =>
      element('li', {}, [
        item.name
      ])
    )
  )

and instead write:

import dscript from 'dscript'
import {element} from 'deku'

const {li, ul} = dscript(element)

export default ({props}) =>
  ul(
    props.items.map(item =>
      li(item.name)
    )
  )

Usage with Custom Components

Custom components example is shown using React, but works with any framework that works with dscript's basic functionality.

import dscript from 'dscript'
import {createElement} from 'react'

import customComponent from './custom-component'

const creator = dscript(createElement)

const {div, li, ul} = creator
const customComponentCreator = creator(customComponent)

const handleClick = () => alert('hi!')

export default props =>
  div('.list-container', {onClick: handleClick}, [
    customComponentCreator({total: props.total}),
    ul(
      props.items.map(item =>
        li(item.name)
      )
    )
  ])

API

dscript(createElement)

Returns an object with properties consisting of HTML tags with values being creator functions.

createElement

type: function

A function to use to create the Virtual DOM. For example, React's createElement or Deku's element.

dscript(createElement)(customComponent)

Returns a creator function to be used in dscript.

For example:

import {createElement} from 'react'
import customComponent from './lib/custom-react-component/'
import dscript from 'dscript'

const creator = dscript(createElement)

const {div} = creator
const custom = creator(customComponent)

export default div([custom()])

createElement

Same as above

customComponent

type: any

Should be a valid component for the createElement function.

Creator Functions

creatorFunction([cssClassesAndOrIdSelector,] [attributes,] [children])

A function that returns a virtual DOM node created with createElement.

cssClassesAndOrIdSelector

type: string

default: null

A convenience to add class names and an id to a virtual DOM node. Note: The provided selector will override attribute's class and id.

attributes

type: object

default: {}

An object that will be passed as the attributes to the virutal DOM node.

children

type: ...Elements

default: []

The list of children passed to the created virtual DOM node.

LICENSE

MIT © Dustin Specker

dscript's People

Contributors

dustinspecker avatar greenkeeperio-bot avatar

Watchers

James Cloos avatar @brodycj - C. Jonathan Brody 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.