### Basic useIdle example
Source: https://stimulus-use.dev/use-idle.html
Basic example of using useIdle with a custom idle timeout of 3000 milliseconds.
```js
connect() {
useIdle(this, { ms: 3000 });
}
```
--------------------------------
### Install with Yarn
Source: https://stimulus-use.dev/getting_started.html
Install Stimulus Use with Yarn in a project that uses a build system.
```bash
yarn add stimulus-use
```
--------------------------------
### Install with npm
Source: https://stimulus-use.dev/getting_started.html
Install Stimulus Use with npm in a project that uses a build system.
```bash
npm i stimulus-use
```
--------------------------------
### Install dependencies with yarn
Source: https://stimulus-use.dev/playground.html
Install project dependencies after cloning the repository.
```bash
yarn install
```
--------------------------------
### Install stimulus-use with yarn for Stimulus 3
Source: https://stimulus-use.dev/
Installs stimulus-use and @hotwired/stimulus via yarn for use with Stimulus 3.
```bash
yarn add stimulus-use @hotwired/stimulus
```
--------------------------------
### Install preview build with npm
Source: https://stimulus-use.dev/getting_started.html
Install an unreleased preview build from a commit or pull request using npm. Replace {commit} with a commit SHA (e.g. 209b96d) or a pull request number (e.g. 606).
```bash
npm i https://pkg.pr.new/stimulus-use@{commit}
```
--------------------------------
### Install preview build with Yarn
Source: https://stimulus-use.dev/getting_started.html
Install an unreleased preview build from a commit or pull request using Yarn. Replace {commit} with a commit SHA (e.g. 209b96d) or a pull request number (e.g. 606).
```bash
yarn add https://pkg.pr.new/stimulus-use@{commit}
```
--------------------------------
### Install stimulus-use with npm for Stimulus 3
Source: https://stimulus-use.dev/
Installs stimulus-use and @hotwired/stimulus via npm for use with Stimulus 3.
```bash
npm i stimulus-use @hotwired/stimulus
```
--------------------------------
### Install hotkeys-js dependency
Source: https://stimulus-use.dev/use-hotkeys.html
Install the required peer dependency using yarn.
```bash
yarn add hotkeys-js
```
--------------------------------
### Start local playground with yarn
Source: https://stimulus-use.dev/playground.html
Launch the local playground, available at http://localhost:8080/ by default.
```bash
yarn start
```
--------------------------------
### Install stimulus-use 0.41.0 with npm for Stimulus 2
Source: https://stimulus-use.dev/
Installs stimulus-use version 0.41.0 and stimulus version 2.0.0 via npm for use with Stimulus 2 or below.
```bash
npm i stimulus-use@0.41.0 stimulus@2.0.0
```
--------------------------------
### Controlling observation with observe/unobserve
Source: https://stimulus-use.dev/use-visibility.html
Shows how to get the observe and unobserve functions from useVisibility to manually control observation. Observation starts automatically and is cleaned up on disconnect.
```javascript
import { Controller } from '@hotwired/stimulus'
import { useVisibility } from 'stimulus-use'
export default class extends Controller {
connect() {
const [observe, unobserve] = useVisibility(this)
this.observe = observe
this.unobserve = unobserve
}
}
```
--------------------------------
### useIdle with all options
Source: https://stimulus-use.dev/use-idle.html
Example with all options: sets idle timeout to 3000ms, initial state to idle, listens only to 'click' events, disables event dispatching, and removes the event prefix.
```js
connect() {
useIdle(this, { ms: 3000, initialState: true, events: ['click'], dispatchEvent: false, eventPrefix: false });
}
```
--------------------------------
### useThrottle basic implementation
Source: https://stimulus-use.dev/use-throttle.html
Example showing how to throttle a function by adding it to the static throttles array and calling useThrottle in the connect method.
```js
import { ApplicationController, useThrottle } from 'stimulus-use'
export default class extends ApplicationController {
static throttles = ['add']
connect() {
useThrottle(this, { wait: 100 })
}
add() {
// this function is throttled.
}
instant() {
//this function is not throttled.
}
}
```
--------------------------------
### Composing useApplication
Source: https://stimulus-use.dev/use-application.html
Example of initializing useApplication inside a standard Stimulus controller's connect method.
```javascript
import { Controller } from '@hotwired/stimulus'
import { useApplication } from 'stimulus-use'
export default class extends Controller {
connect() {
useApplication(this)
}
}
```
--------------------------------
### Handling Resize Events in HTML
Source: https://stimulus-use.dev/use-resize.html
Example of listening to the dispatched resize event in the HTML template.
```html
...
```
--------------------------------
### HTML Controller Setup
Source: https://stimulus-use.dev/use-lazy-load.html
The required HTML structure for the controller element, using a data attribute to specify the image source.
```html
```
--------------------------------
### useThrottle per-function wait configuration
Source: https://stimulus-use.dev/use-throttle.html
Example demonstrating how to specify different wait times for individual functions within the throttles array.
```js
export default class extends ApplicationController {
static throttles = [
'click',
{
name: 'fetch',
wait: 500
}
]
connect() {
useThrottle(this, { wait: 100 })
}
click() {
// this function is throttled with a wait time of 100ms.
}
fetch() {
// this function is throttled with a wait time of 500ms.
}
instant() {
// this function is not throttled.
}
}
```
--------------------------------
### Alpine JS directive style configuration
Source: https://stimulus-use.dev/use-transition.html
Example of data attributes used for transition states.
```html
data-transition-enter-class="transform opacity-0 scale-95"
data-transition-enter-start-class="transition ease-out duration-300"
data-transition-enter-end-class="transform opacity-100 scale-100"
data-transition-leave-class="transform opacity-100 scale-100"
data-transition-leave-start-class="transition ease-in duration-300"
data-transition-leave-end-class="transform opacity-0 scale-95"
```
--------------------------------
### Custom CSS transition implementation
Source: https://stimulus-use.dev/use-transition.html
A complete example using custom CSS classes with the useTransition mixin in a Stimulus controller.
```html
This element will fade out and in!
```
```js
import { Controller } from '@hotwired/stimulus';
import { useTransition } from 'stimulus-use';
export default class extends Controller {
static targets = ['boxToClose']
connect() {
useTransition(this, {
element: this.boxToCloseTarget,
enterActive: 'fade-enter-active',
enterFrom: 'fade-enter-from',
enterTo: 'fade-enter-to',
leaveActive: 'fade-leave-active',
leaveFrom: 'fade-leave-from',
leaveTo: 'fade-leave-to',
hiddenClass: 'd-none',
// set this, because the item *starts* in an open state
transitioned: true,
});
}
close() {
this.leave();
}
open() {
this.enter();
}
toggle() {
this.toggleTransition();
}
}
```
```css
.fade-enter-active, .fade-leave-active {
transition: opacity 500ms;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
.fade-enter-to, .fade-leave-from {
opacity: 1;
}
.d-none {
display: none;
}
```
--------------------------------
### useHover Composing
Source: https://stimulus-use.dev/use-hover.html
Example of using useHover by composing it within a standard Stimulus controller.
```javascript
import { Controller } from '@hotwired/stimulus'
import { useHover } from 'stimulus-use'
export default class extends Controller {
connect() {
useHover(this, { element: this.element });
}
mouseEnter() {
// ...
}
mouseLeave() {
// ...
}
}
```
--------------------------------
### useResize Composition
Source: https://stimulus-use.dev/use-resize.html
Example of using useResize by composing it within a Stimulus controller.
```javascript
import { Controller } from '@hotwired/stimulus'
import { useResize } from 'stimulus-use'
export default class extends Controller {
static targets = ['width']
connect() {
useResize(this)
}
resize({ width }) {
this.widthTarget.textContent = width
}
}
```
--------------------------------
### Basic useDispatch Usage
Source: https://stimulus-use.dev/use-dispatch.html
Example of applying the mixin and using the dispatch function to emit a custom event from a controller.
```javascript
// item_controller.js
import { Controller } from '@hotwired/stimulus'
import { useDispatch } from 'stimulus-use'
export default class extends Controller {
connect() {
useDispatch(this)
}
add() {
// dispatch a custom event item:add
this.dispatch("add")
}
}
```
--------------------------------
### Controlling observation with observe/unobserve
Source: https://stimulus-use.dev/use-window-focus.html
useWindowFocus returns an [observe, unobserve] tuple. Store them on the controller to start and stop observing manually. Observation starts automatically and is cleaned up on disconnect.
```javascript
import { Controller } from '@hotwired/stimulus'
import { useWindowFocus } from 'stimulus-use'
export default class extends Controller {
connect() {
const [observe, unobserve] = useWindowFocus(this)
this.observe = observe
this.unobserve = unobserve
}
}
```
--------------------------------
### useResize Extending Controller
Source: https://stimulus-use.dev/use-resize.html
Example of using useResize by extending the ResizeController class.
```javascript
import { ResizeController } from 'stimulus-use'
export default class extends ResizeController {
static targets = ['width']
resize({ width }) {
this.widthTarget.textContent = width
}
}
```
--------------------------------
### Simple Hotkey Definition
Source: https://stimulus-use.dev/use-hotkeys.html
Example of defining multiple hotkeys using the simple format within a Stimulus controller.
```js
import { Controller } from '@hotwired/stimulus'
import { useHotkeys } from 'stimulus-use/hotkeys'
export default class extends Controller {
connect() {
useHotkeys(this, {
'/': [this.singleKeyHandler],
'cmd+a': [this.metaKeyHandler],
b: [this.inputHandler],
e: [this.inputHandler, this.inputTarget]
})
}
}
```
--------------------------------
### useMatchMedia Event Handling
Source: https://stimulus-use.dev/use-match-media.html
Example of listening to dispatched media query events in HTML using Stimulus data attributes.
```html
...
```
--------------------------------
### Load StimulusUse via UMD script tag
Source: https://stimulus-use.dev/getting_started.html
Load StimulusUse via a