Giter Club home page Giter Club logo

stimulus-use's Introduction

A collection of composable behaviors for your Stimulus Controllers

npm version minified + gzip size types included types included


Stimulus Use Example


  • New lifecycle behaviors: adds new standard behaviors to your Stimulus controllers.
  • Composable: compose at will different behaviors in a single controller.
  • Modular: built as ES6 modules, just import what you need and tree shaking will remove the rest.
  • Typescript: Types available, better autocompletion.
  • Tiny: 1k gzip
  • MIT Licensed: free for personal and commercial use.

Getting Started

npm

npm i stimulus-use

yarn

yarn add stimulus-use

Modules and Controllers

  • Observers

    This set of controllers is built around the Observer APIs

    useFunction/Controller Description NEW Callbacks
    useIntersection
    IntersectionController
    Tracks the element's intersection and adds appear, disappear callbacks to your controller. appear
    disappear
    useResize
    ResizeController
    Tracks the element's size and adds a new lifecyle callback resize. resize
    useClickOutside
    ClickOutsideController
    Tracks the clicks outside of the element and adds a new lifecyle callback clickOutside. clickOutside
  • Application

  • Idle

    • useIdle, IdleController — Tracks if the user is idle on your page and adds away and back callbacks to your controller.

Extend or compose

Stimulus-use can be used in two ways: extending or composing

Extending

You can create your Stimulus controller from a pre-built Stimulus-use controller which offers the new behavior you're looking for. This method works perfectly when you only need a single behavior for your controller.

import { IntersectionController } from 'stimulus-use'

export default class extends IntersectionController {
  appear(entry) {
    // triggered when the element appears within the viewport
  }
}

Composing

When you need multiple behaviors or you are already extending your controller from another one, you can easily add new behavior with the built-in use functions.

import { Controller } from 'stimulus'
import { useIntersection, useResize } from 'stimulus-use'

export default class extends Controller {
  connect() {
    useIntersection(this)
    useResize(this)
  }

  appear(entry) {
    // triggered when the element appears within the viewport
  }

  resize({ height, width }) {
    // trigered when the element is resized
  }
}

Launch a local playground

Play with Stimulus-use controllers locally before adding them to one of your projects.

Fork and clone the repo (SSH):

git clone [email protected]:stimulus-use/stimulus-use.git

Once in your local stimulus-use directory, run:

yarn install

Then, build the library locally

yarn build

Launch the playground locally (available at http://localhost:8080/ by default):

yarn start

Contributors ✨

Made with ❤️ by @adrienpoly and all these wonderful contributors (emoji key):


Jonathan Sundqvist

📖

Rui Freitas

📖

Nicolas Filzi

📖

Benjamin Darcet

📖

juancarlosasensio

📖

Marco Roth

🚇 💻

Philipp Daun

🐛

M. E. Patterson

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

stimulus-use's People

Contributors

adrienpoly avatar allcontributors[bot] avatar marcoroth avatar jonathan-s avatar dependabot[bot] avatar bdarcet avatar nfilzi avatar rodloboz 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.