Catalog / Hotwire Cheatsheet
Hotwire Cheatsheet
A comprehensive cheat sheet for Hotwire, covering Turbo and Stimulus, with examples and usage guidelines to quickly implement modern web features.
Turbo Basics
Turbo Drive
Turbo Drive automatically accelerates links and form submissions. Key Features:
|
Enabling Turbo Drive: |
Disabling Turbo Drive: |
|
Turbo Visit Options: |
Turbo Frames
Turbo Frames allow you to isolate sections of a page into independent components that can be updated individually. Key Features:
|
Defining a Turbo Frame:
|
Updating a Turbo Frame:
|
Turbo Streams
Turbo Streams deliver page changes as fragments of HTML to update parts of the page. Key Features:
|
Example Turbo Stream Response:
|
Rails Example:
|
Stimulus Basics
Stimulus Controllers
Stimulus organizes JavaScript in your application by connecting DOM elements to JavaScript objects called controllers. Key Concepts:
|
Defining a Controller:
|
Connecting a Controller to HTML:
|
Actions
Actions connect DOM events to controller methods. Syntax: |
Example:
|
Controller Method:
|
Targets
Targets provide a way to reference specific DOM elements within a controller. Syntax: |
Example:
|
Controller Access:
|
Advanced Turbo
Turbo Streams from Server
Sending Turbo Streams from the server enables real-time updates to specific parts of your page. Use Cases:
|
Rails Example:
|
Broadcasting Turbo Streams:
|
Turbo Frames and Forms
Using Turbo Frames with forms allows you to update only the form or related sections of the page after submission. Benefits:
|
Example:
|
Controller Response:
|
Advanced Stimulus
Values API
The Values API in Stimulus allows you to define typed values that are automatically synchronized with data attributes. Supported Types:
|
Example:
|
HTML:
|
Classes API
The Classes API in Stimulus lets you automatically toggle CSS classes on elements based on controller state. Benefits:
|
Example:
|
HTML:
|