Code Monkey home page Code Monkey logo

embroidery's Introduction

Embroidery

npm bundle size npm version

Note! This library is just an experiment and not intended for production use (yet).

Install

yarn add embroidery
or
npm i embroidery

Usage

Add controller, target and action attributes to your HTML

<!-- HTML somewhere in your web app -->
<div data-controller="myController">
  <input data-target="name" type="text" />

  <button data-action="click->greet">Greet</button>

  <span data-target="output"></span>
</div>

Create a controller in javascript.

// my-controller.js
const myController = {
  greet({ name, output }) {
    output.textContent = name.value
  },
}

Register your controller in your main javascript file.

import { Embroidery } from 'embroidery'
import { myController } from 'my-controller'
let app = Embroidery.start()
app.register({ myController })

Partials

Add a partial (async external html). This is good for inserting remote fragments into your HTML to keep page loads fast.

<div data-partial="/endpoint-that-returns-html"></div>

Actions

Default actions

Some elements have default actions, which means you don't have to specify click, submit etc. For example:

// Before
  <button data-action="click->hello">Hello</button>
// After
  <button data-action="hello">Hello</button>
Element Event
a 'click'
button 'click'
form 'submit'
input 'input'
select 'change'
textarea 'input'ย 

Multiple actions

If you want to have multiple actions you can separate them with a blank space.

<div data-controller="manyActions">
  <div data-action="mouseover->doThis mouseout->doThat">Do this or that</div>
</div>

Targets

Multiple targets

If you have multiple targets you need to add [] to access them as an array in your controller. The array will be appended with <name>Targets.

<div>
  <div data-target="hello[]">Hello to you</div>
  <div data-target="hello[]">Hello to me</div>
  <div data-target="hello[]">Hello to everyone</div>
</div>
// hello-controller.js
const helloController = {
  init({ helloTargets }) {
    helloTargets.forEach((target) => {
      //...
    })
  },
}

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.