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

stimulus-use

Package Overview
Dependencies
Maintainers
2
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stimulus-use

A collection of standard controllers and utilities for Stimulus

  • 0.52.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
82K
increased by10.72%
Maintainers
2
Weekly downloads
ย 
Created
Source

A collection of composable behaviors for your Stimulus Controllers

npm version minified + gzip size types included types included Sauce test status


Stimulus Use Example


  • New lifecycle behaviors: adds new standard behaviors to your Stimulus controllers.
  • Composable: compose at will different behaviors in a single controller with mixins.
  • Modular: built as ES6 modules, just import what you need and tree shaking will remove the rest.
  • Typescript: Types available, better autocompletion.
  • Tiny: 3k gzip + tree shaking ๐ŸŒณ๐ŸŒณ๐ŸŒณ

Getting Started

Stimulus 3

If you want to use stimulus-use with Stimulus 3 you can use the version 0.50.0+. This and all future versions are designed to work with the @hotwired/stimulus npm package.

Note: If other packages still depend on the stimulus npm package you can safely keep that in your package.json, this won't break the stimulus-use compability.

Using npm
npm i stimulus-use @hotwired/stimulus
Using yarn
yarn add stimulus-use @hotwired/stimulus

Stimulus 2 and below

If you want to use stimulus-use with Stimulus 2 (or below) you can use version 0.41.0. This version is designed to work with the stimulus npm package.

Using npm
npm i stimulus-use@0.41.0 stimulus@2.0.0
Using yarn
yarn add stimulus-use@0.41.0 stimulus@2.0.0

Documentation

We got you covered ๐Ÿ‘‰ stimulus-use.github.io/stimulus-use

Mixins

Observers

This set of mixins is built around the Observer APIs and custom events to enhance your controllers with new behaviors.

MixinDescriptionNEW Callbacks
useClickOutsideTracks the clicks outside of the element and adds a new lifecycle callback clickOutside.clickOutside
useHotkeysRegisters hotkeys using the hotkeys-js library and binds them to handler methods
useHoverTracks the user's mouse movements over an element and adds mouseEnter and mouseLeave callbacks to your controller.mouseEnter mouseLeave
useIdleTracks if the user is idle on your page and adds away and back callbacks to your controller.away
back
useIntersectionTracks the element's intersection and adds appear, disappear callbacks to your controller.appear
disappear
useMatchMediaTracks if the window matches a media query string.is[Name], not[Name] and [name]Changed
useMutationTracks mutations on an element, its attributes and/or subtree. Adds a mutate callback to your controller.mutate
useResizeTracks the element's size and adds a new lifecycle callback resize.resize
useTargetMutationTracks when targets are added or removed from the controller's scope, or their contents changed. Adds [target]TargetAdded , [target]TargetRemoved and [target]TargetChanged callback to your controller for each specified target.[target]TargetAdded [target]TargetRemoved [target]TargetChanged
useVisibility
Tracks the page visibility and adds visible, invisible callbacks to your controller.visible
invisible
useWindowFocus
Tracks the window focus and adds focus, unfocus callbacks to your controller.focus
unfocus
useWindowResizeTracks the size of the window object and adds a new lifecycle callback windowResize.windowResize

Optimization

A set of mixin to optimize performances.

MixinDescription
useDebounceAdds the ability to specify an array "debounces" of functions to debounce.
useMemoMemoize expensive getters by mixing in useMemo and adding a static memos array.
useThrottleAdds the ability to specify an array "throttles" of functions to throttle.

Animation

A set of mixin and controllers to build animations.

MixinDescription
useTransitionMixin or controller to apply classes to various stages of an element's transition.

Application

MixinDescription
useApplication, ApplicationControllersupercharged controller for your application.
useDispatchAdds a dispatch helper function to emit custom events. Useful to communicate between different controllers.
useMetaAdds getters to easily access meta values.

Extend or compose

Stimulus-use can be used in two ways: composing with mixins or extending built-in controllers

Composing with mixins

This is the prefered approach as it bring the most flexibility. Simply import a mixin and apply it in the connect or initialize to adds new behaviors to you controller. You can combine several mixins within the same controller.

import { Controller } from '@hotwired/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
  }
}

Extending built-in controllers

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
  }
}

Development

  • Fork the project locally
  • yarn install
  • yarn start - to run the local dev server with examples
  • yarn test - to run the unit tests
  • yarn lint - to run the linter with ESLint
  • yarn format - to format changes with Prettier
  • yarn build - to bundle the app into static files for production

Contributors โœจ

Made with :heart: by @adrienpoly, @marcoroth and all these wonderful contributors (emoji key):

Marco Roth
Marco Roth

๐Ÿš‡ ๐Ÿ’ป ๐Ÿ‘€ ๐Ÿ›
Philipp Daun
Philipp Daun

๐Ÿ›
M. E. Patterson
M. E. Patterson

๐Ÿ›
Jonathan Sundqvist
Jonathan Sundqvist

๐Ÿ“–
Rui Freitas
Rui Freitas

๐Ÿ“–
Nicolas Filzi
Nicolas Filzi

๐Ÿ“–
Benjamin Darcet
Benjamin Darcet

๐Ÿ“–
juancarlosasensio
juancarlosasensio

๐Ÿ“–
lidqqq
lidqqq

๐Ÿš‡ ๐Ÿ›
Julian Rubisch
Julian Rubisch

๐Ÿ’ป ๐Ÿ‘€
Takuya Fukuju
Takuya Fukuju

๐Ÿ“–
Justin Coyne
Justin Coyne

๐Ÿ“–
Asger Behncke Jacobsen
Asger Behncke Jacobsen

๐Ÿ“–
Dan Callaghan
Dan Callaghan

๐Ÿ“–
Konnor Rogers
Konnor Rogers

๐Ÿ› ๐Ÿ’ป
Francisco Presencia
Francisco Presencia

๐Ÿ“–
Takayuki Shimada
Takayuki Shimada

๐Ÿ›
Dylan Clarke
Dylan Clarke

๐Ÿ’ป ๐Ÿ“–
Martin Tomov
Martin Tomov

๐Ÿ“–
Ryan Weaver
Ryan Weaver

๐Ÿ“– ๐Ÿ›
Adrien S
Adrien S

๐Ÿ›
Felix Albroscheit
Felix Albroscheit

๐Ÿ›
Guillaume Briday
Guillaume Briday

๐Ÿ’ป
craisp
craisp

๐Ÿ› ๐Ÿ’ป
Gabriel
Gabriel

๐Ÿ› ๐Ÿ’ป
Donnie Flood
Donnie Flood

๐Ÿ’ป
ร“scar Carretero
ร“scar Carretero

๐Ÿ‘€ ๐Ÿ›
Ikko Ashimine
Ikko Ashimine

๐Ÿ“–
Michael Coyne
Michael Coyne

๐Ÿ›
Ollie Harridge
Ollie Harridge

๐Ÿ“–
Leon Vogt
Leon Vogt

๐Ÿš‡ ๐Ÿ’ป
Thomas Kรถnig
Thomas Kรถnig

๐Ÿ“–
Scott
Scott

๐Ÿ›
Daniel Rikowski
Daniel Rikowski

๐Ÿ›
Marc Kรถhlbrugge
Marc Kรถhlbrugge

๐Ÿค”

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

Acknowledgments

Continuous integration and cross browser testing is generously provided Sauce Labs.

Testing Powered By SauceLabs

FAQs

Package last updated on 15 Oct 2023

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