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

@wheatup/pivotjs

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wheatup/pivotjs

Pivot.js is a super lite-weight library for creating DOM structure with javascript.

  • 1.1.6
  • unpublished
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Pivot.js is a super lite-weight library for creating DOM structure with javascript.

Basic usage

Register a view

P.View('TestView', () => ({
  render () {
    return P('div', { class: 'test-view' }, [
      P('h1', 'Hello World!'),
      P('ul', [P('li', 'hello'), P('li', 'world')])
    ])
  }
}));

Insert a view into DOM

document.body.append(P('TestView').dom);

This will create a structure like this and insert it into docment.body:

<div data-pivot="TestView" class="test-view">
  <h1>Hello World!</h1>
  <ul>
    <li>hello</li>
    <li>world</li>
  </ul>
</div>

You can get a normal HTMLElement by calling P('TestView').dom

Nesting another view

P.View('TestView', () => ({
  render () {
    return P('div', { class: 'test-view' }, [
      P('h1', 'Hello World!'),

      // instead of tag name, specify the view name you registered
      P('NestedView', { candies: 42 }),

      // and you can nest a duplicate that has different properties
      P('NestedView', { candies: 1337 })
    ])
  }
}));

P.View('NestedView', () => ({
  render () {
    // get the properties from this.props that passed from above
    return P('span', this.props.candies)
  }
}));

This will create a structure like this:

<div data-pivot="TestView" class="test-view">
  <h1>Hello World!</h1>
  <span data-pivot="NestedView">42</span>
  <span data-pivot="NestedView">1337</span>
</div>

Handling events

P.View('TestView', () => ({

  // you can create a function directly here
  onClickMe() {
    alert('ouch');
  },

  render() {
    return (
      P('div', { class: 'test-view' }, [
        P('h1', { text: 'Hello World!' }),

        // when the span is clicked, it alerts "ouch"
        P('span', 'Click Me!', { '@click': () => this.onClickMe() })
      ])
    );
  }
}));

Modifying states

P.View('TestView', () => ({
  candies: 0,

  addCandy(number) {
    this.candies += number;
  },

  render() {
    return (
      P('div', { class: 'test-view ' + this.candies ? 'enough' : 'not-enough' }, [
        P('span', 'Candies: ' + this.candies),

        P('button', 'Add 1', { '@click': () => this.addCandy(1) }),
        P('button', 'Add 2', { '@click': () => this.addCandy(2) }]
      ])
    );
  }
}));

Using refs and lifecycles

P.View('TestView', () => ({
  loaded () {
    console.log(this.refs.username.value) // Alice
  },

  render () {
    return P('div', [
      P('input', { ref: 'username', type: 'text', value: 'Alice' })
    ])
  }
}))

Keywords

FAQs

Package last updated on 24 Aug 2021

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