Giter Site home page Giter Site logo

rza's Introduction

RZA

Create simple HTML elements

const RZA = require('rza')

class MyElement extends RZA {
  get defaults () {
    // default render settings
    return { wrap: false }
  }
  async render (settings, innerHTML) {
    if (settings.wrap) {
      return `<span>${innerHTML}</span>`
    } else {
      return innerHTML
    }
  }
}

window.customElements.define('my-element', MyElement)
<!-- Example w/ defaults -->
<my-element>Test</my-element>

<!-- Renders -->

<my-element>
  <render>Test</render>
</my-element>

<!-- Example w/ property set -->

<my-element wrap>Test</my-element>

<!-- Renders -->

<my-element>
  <render>
    <span>Test</span>
  </render>
</my-element>

<!-- Setting properties dynamically cause re-rendering -->

<script>
  let elem = document.querySelector('my-element')
  elem.setAttribute('wrap', false)
  /* or we can just set element properties for the same thing */
  elem.wrap = false
  /* changing the element value ALSO triggers a rerender */
  elem.textContent = 'New Test'
</script>

<!-- Triggers a Render, batched into a single render() call. -->

<my-element>
  <render>New Test</render>
</my-element>

See also: markdown-element.

render() function.

The render function you define will be called whenever relevant state (settings) are altered.

Returning a string will reset the innerHTML of the <render> element.

Returning the previous element value is a noop, RZA assumes that because you are returning the prior element you have manipulated it correctly.

Returning an HTML Element will append that element as a child and put it in the render slot of the shadowDOM so that it can be seen.

default types.

Setting an array for defaults defines those property names as settings but with no real defaults.

This means that element attributes and properties of these names will be treated as settings, and alterations to them will cause re-renders.

class MyElement extends RZA {
  get defaults () {
    return ['propname']
  }
}

Functions (sync and async) can be used as dynamic initializers for default properties.

class MyElement extends RZA {
  get defaults () {
    return { prop: async () => 'example' }
  }
}

rza's People

Contributors

mikeal avatar

Watchers

 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.