Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

component-js

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

component-js

Create web page by Javascript

  • 1.0.8
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Create web page by Javascript

Introduction

component-js is a light weight library to create a web page by Javascript, it's philosophy component just a function return DOM element let us could flexibility compose component for create a new component.

You may need component-js when ...

  1. Client-render, sometimes, you need client-render data to DOM element. Traditional way is splicing HTML string or using HTML template engine. However splicing HTML string is easy to wrong and using HTML template is too heavy. So component-js is a good way for you.

  2. HTML component, component-js provider a flexible way to compose component. Although many framework do it excellently, but maybe you just need a simple way to reuse HTML rather than a giant.

How to use

npm install component-js

Hello world!


import {elm,txNode,comp} from 'component-js'
document.getElementById('root')
    .appendChild(elm({
                   		tag:'p',
                   		attr:{
                   			textContent:'hello world!'
                   		}
                   }))

Or more exciting

result

import {elm, comp, txNode} from 'component-js'
import j from 'berserk'

const el = comp.div(
	[
		comp.listGroup(
			[
				comp.div(
					[
						comp.btn({
							evtLir: [
								{
									type: 'click',
									handler: () => j.log('click first btn')
								}
							],
							txt: 'add TODO'
						}),
						comp.btn({
							evtLir: [
								{
									type: 'click',
									handler: () => j.log('click second btn')
								}
							],
							txt: 'show active'
						}),
						comp.btn(
							{
								evtLir: [
									{
										type: 'click',
										handler: () => j.log('click second btn')
									}
								],
								txt: 'show active'
							}
						)
					]
				),
				txNode('dynamic')
			]
		)
	]
)
document.getElementById('root').appendChild(el)

Element api

Constants

elmcreateElement

Create a DOM element by pass option

txNodecreateTextNode

A function for create a textNode by pass text content, like document.createTextNode but bind 'document' as it's this

elm ⇒ createElement

Create a DOM element by pass option

Kind: global constant
Returns: createElement - a DOM element

ParamTypeDescription
optionobject
option.tagstringThe html tag
option.attrobjectThe option correspond element's attributes, default: {}
option.childrenarrayA array of elements, these elements will append as children,default: []
option.evtLirarrayA array of event object, it will bind all event in object to return element example: [{type:click,handler:(e)=>console.log(e),opt:{capture:true}}...], default: []

Example

elm({
		tag:'p',
		attr:{
			textContent:'hello world!'
		}
})

txNode ⇒ createTextNode

A function for create a textNode by pass text content, like document.createTextNode but bind 'document' as it's this

Kind: global constant
Returns: createTextNode - A textNode

ParamType
textstring

Example

import {comp,txNode} from 'component-js'
comp.div(
   [
       txNode('hello world!')
   ]
)

Component api

Note: component-js component is depend on bootstrap

Functions

btn(option)createElement

Create a button element with bootstrap className

listGroup(children)createElement

Create a bootstrap listGroup by pass children element

form(formGroups, submitBtn, opt)createElement

Create a div with form by pass option, fromGroups(see bootstrap),submit button

formGroup(label, type)createElement

Create a div with bootstrap 'formGroup' className for input

div(children)createElement

Create a div by pass it's children elements

btn(option) ⇒ createElement

Create a button element with bootstrap className

Kind: global function
Returns: createElement - A button element with bootstrap@4.x className

ParamTypeDescription
optionobject
option.evtLirarrayExample: [{type:click,handler:(e)=>console.log(e),opt:{capture:true}}...]
option.txtstringExample: 'click me'
option.styleTypestringExample: 'btn-error' default: 'btn-secondary'
option.typestringExample: 'submit' default: 'button'

Example

btn({
	evtLir: [
				{
					type: 'click',
					handler: () => j.log('click second btn')
				}
			],
	txt: 'show active'
	})

listGroup(children) ⇒ createElement

Create a bootstrap listGroup by pass children element

Kind: global function

ParamTypeDescription
childrenarrayexample: [domElement...] a array of dom element, it will wrap with 'list-group-item' className

Example

listGroup([buttonElm1,buttonElm2])

form(formGroups, submitBtn, opt) ⇒ createElement

Create a div with form by pass option, fromGroups(see bootstrap),submit button

Kind: global function
Returns: createElement - A form element with bootstrap style

ParamTypeDescription
formGroupsarrayA array of div with bootstrap 'form-groups' class
submitBtnElementA button element with 'submit'type
optobjectA object of attribute

Example

form(
       [
           formGroupDiv1,
           formGroupDiv1,
       ],
       submitBtn
)

formGroup(label, type) ⇒ createElement

Create a div with bootstrap 'formGroup' className for input

Kind: global function
Returns: createElement - A div element which has label and input element children as well as bootstrap 'formGroup' className

ParamTypeDescription
labelstringThe label text
typestringThe input type

Example

formGroup(
       'password',
       'password'
)

div(children) ⇒ createElement

Create a div by pass it's children elements

Kind: global function
Returns: createElement - A div element

ParamTypeDescription
childrenarrayChildren elements

Example

div(
   [childElm1,childElm2]
)

Keywords

FAQs

Package last updated on 17 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc