Git Product home page Git Product logo

jumpstart-js's Introduction

jumpstart-js

A quick way to wire up your DOM elements with interactivity using data- attributes

<div data-mounts="dropdown">
  <button data-ref="button">Open</button>

  <ul data-ref="list">
    <li>Foo</li>
    <li>Bar</li>
    <li>Baz</li>
  </ul>
</div>

<script type="module">
  import { mount } from 'https://brandoncbang.github.io/jumpstart-js/jumpstart.js';

  mount('dropdown', (root, props, refs) => {
    let show = false;

    function updateList() {
      refs.list.style.display = show ? '' : 'none';
    }

    refs.button.onclick = (e) => {
      show = !show;
      updateList();
    }

    updateList();
  });
</script>

If you're enhancing your markup with Vanilla JS, using document.querySelector() for all the elements you need to interact with can get tedious. Jumpstart uses the attributes data-mounts, data-props, and data-refs to let you easily hook up your JavaScript to elements that use them.

Attributes

data-mounts

Set an identifier used to determine which callback to run on the element. The element is then passed as the root element of that callback.

<div data-mounts="counter">
  ...
</div>

<script>
  mount('counter', (root, props, refs) => {
    root // <div data-mounts="counter">
  });
</script>

data-props

Lets you add JSON data to your root element that gets passed to your mount callback function. Useful for when you need to configure interactivity differently in different parts of your markup that use the same interactivity.

<form>
  <h2>Guests (limit: 8)</h2>
  <div class="guests-repeater" data-mounts="field-repeater" data-props='{ "maxRows": 8 }'>
    ...
  </div>
  
  <h2>Pets (limit: 2)</h2>
  <div class="pets-repeater" data-mounts="field-repeater" data-props='{ "maxRows": 2 }'>
    ...
  </div>
</form>

<script>
  mount('field-repeater', (root, props, refs) => {
    props.maxRows; // Guests field repeater: 8, Pets field repeater: 2
  });
</script>

data-ref

Any child elements with this attribute get added to a refs object, with the attribute value as the key. Multiple elements with the same ref key get added to an array under the key.

<div data-mounts="color-changer">
  <div class="w-16 h-16" data-ref="colorSquare"></div>
  
  <div class="flex space-x-2">
    <button data-ref="buttons" data-color="red">Make it red!</button>
    <button data-ref="buttons" data-color="blue">Make it blue!</button>
    <button data-ref="buttons" data-color="orange">Make it orange!</button>
    <button data-ref="buttons" data-color="green">Make it green!</button>
  </div>
</div>

<script>
  mount('color-changer', (root, props, refs) => {
    refs.buttons.forEach(button => {
      button.onclick = (e) => {
        refs.colorSquare.style.background = button.dataset.color;
      }
    });
  });
</script>

jumpstart-js's People

Contributors

brandoncbang 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.