stimulus-rails 0.3.9 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -31
- data/app/assets/javascripts/stimulus-autoloader.js +2 -0
- data/app/assets/javascripts/stimulus-importmap-autoloader.js +18 -12
- data/app/assets/javascripts/stimulus-loading.js +81 -0
- data/app/assets/javascripts/stimulus.js +1948 -1
- data/app/assets/javascripts/stimulus.min.js +5 -0
- data/app/assets/javascripts/stimulus.min.js.map +1 -0
- data/lib/generators/stimulus/USAGE +15 -0
- data/lib/generators/stimulus/stimulus_generator.rb +20 -0
- data/lib/generators/stimulus/templates/controller.js.tt +7 -0
- data/lib/install/app/javascript/controllers/application.js +9 -0
- data/lib/install/app/javascript/controllers/index_for_importmap.js +11 -0
- data/lib/install/app/javascript/controllers/index_for_node.js +8 -0
- data/lib/install/stimulus_with_importmap.rb +19 -0
- data/lib/install/stimulus_with_node.rb +18 -0
- data/lib/stimulus/engine.rb +1 -1
- data/lib/stimulus/manifest.rb +28 -0
- data/lib/stimulus/version.rb +1 -1
- data/lib/tasks/stimulus_tasks.rake +33 -11
- metadata +16 -8
- data/app/assets/javascripts/stimulus@2.js +0 -1699
- data/lib/install/app/javascript/controllers/index.js +0 -9
- data/lib/install/stimulus_with_asset_pipeline.rb +0 -27
- data/lib/install/stimulus_with_webpacker.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23f5108a2b5f7377a6148df9c993a7a637b82a34e3a13fa5e2aacca4412306eb
|
4
|
+
data.tar.gz: 5a7076d26e257566a2c17f60f0c19ad72623f648cf8d6390ca24cbec498f2b64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f45d9def4599c3b2b872b65125f2d0164d7ab3065f5fc0d179fe90d2370840bffd4056c890d2f6f6c5099ccccc72ae6b75b0c09705e32f4656366e37ff4e0c8c
|
7
|
+
data.tar.gz: 2d463a499ede333ea3a7ceae20d6049f2ee703f35d2c3fe8acab90fa56f37f41bf190c55ec5a169c0393afd34344716aa7a436713aa0a887f1376b7a3165a2a5
|
data/README.md
CHANGED
@@ -2,40 +2,46 @@
|
|
2
2
|
|
3
3
|
[Stimulus](https://stimulus.hotwired.dev) is a JavaScript framework with modest ambitions. It doesn’t seek to take over your entire front-end in fact, it’s not concerned with rendering HTML at all. Instead, it’s designed to augment your HTML with just enough behavior to make it shine. Stimulus pairs beautifully with Turbo to provide a complete solution for fast, compelling applications with a minimal amount of effort. Together they form the core of [Hotwire](https://hotwired.dev).
|
4
4
|
|
5
|
-
Stimulus for Rails makes it easy to use this modest framework with
|
5
|
+
Stimulus for Rails makes it easy to use this modest framework with both import-mapped and JavaScript-bundled apps. It relies on either `importmap-rails` to make Stimulus available via ESM or a Node-capable Rails (like via `jsbundling-rails`) to include Stimulus in the bundle. Make sure to install one of these first!
|
6
6
|
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
10
|
+
This gem is automatically configured for applications made with Rails 7+ (unless `--skip-hotwire` is passed to the generator). But if you're on Rails 6, you can install it manually with:
|
11
|
+
|
10
12
|
1. Add the `stimulus-rails` gem to your Gemfile: `gem 'stimulus-rails'`
|
11
13
|
2. Run `./bin/bundle install`.
|
12
14
|
3. Run `./bin/rails stimulus:install`
|
13
15
|
|
14
|
-
|
16
|
+
The installer will automatically detect whether you're using an [import map](https://github.com/rails/importmap-rails) or [JavaScript bundler](https://github.com/rails/jsbundling-rails) to manage your application's JavaScript. If you're using an import map, the Stimulus dependencies will be pinned to the versions of the library included with this gem. If you're using Node, yarn will add the dependencies to your `package.json` file.
|
17
|
+
|
18
|
+
The installer amends your JavaScript entry point at `app/javascript/application.js` to import the `app/javascript/controllers/index.js` file, which is responsible for setting up your Stimulus application and registering your controllers.
|
19
|
+
|
15
20
|
|
16
|
-
|
17
|
-
2. Append `import "@hotwired/stimulus-autoloader"` to your `app/assets/javascripts/application.js` entrypoint.
|
21
|
+
## Usage with import map
|
18
22
|
|
19
|
-
|
23
|
+
With an import-mapped application, controllers are automatically pinned and registered based on the file structure. The installer will amend your `config/importmap.rb` to configure this such that all controllers in `app/javascript/controllers` are pinned.
|
20
24
|
|
21
|
-
|
25
|
+
By default, your application will be setup to eager load all the controllers mentioned in your import map under "controllers". This works well together with preloading in the import map when you have a modest number of controllers.
|
22
26
|
|
23
|
-
|
24
|
-
2. Create a controllers directory at `app/javascripts/controllers`.
|
25
|
-
3. Create an example controller in `app/javascripts/controllers/hello_controller.js`.
|
26
|
-
4. Install the Stimulus NPM package.
|
27
|
+
If you have a lot of controllers, you may well want to lazy load them instead. This can be done by changing from `eagerLoadControllersFrom` to `lazyLoadControllersFrom` in your `app/javascript/controllers/index.js` file.
|
27
28
|
|
29
|
+
When lazy loading, controllers are not loaded until their data-controller identifier is encountered in the DOM.
|
28
30
|
|
29
|
-
## Usage
|
30
31
|
|
31
|
-
|
32
|
+
## Usage with JavaScript bundler
|
32
33
|
|
33
|
-
|
34
|
+
With an application using a JavaScript bundler, controllers need to be imported and registered directly in the index.js file. But this can be done automatically using either the Stimulus generator (`./bin/rails generate stimulus [controller]`) or the dedicated `stimulus:manifest:update` task. Either will overwrite the `controllers/index.js` file.
|
35
|
+
|
36
|
+
You're encouraged to use the generator to add new controllers like so:
|
34
37
|
|
35
38
|
```javascript
|
39
|
+
// Run "./bin/rails g stimulus hello" to create the file and update the index, then amend:
|
40
|
+
|
36
41
|
// app/assets/javascripts/controllers/hello_controller.js
|
37
|
-
import { Controller } from "stimulus"
|
42
|
+
import { Controller } from "@hotwired/stimulus"
|
38
43
|
|
44
|
+
// Connects with data-controller="hello"
|
39
45
|
export default class extends Controller {
|
40
46
|
static targets = [ "name", "output" ]
|
41
47
|
|
@@ -45,23 +51,6 @@ export default class extends Controller {
|
|
45
51
|
}
|
46
52
|
```
|
47
53
|
|
48
|
-
And it'll be activated and registered automatically when encountering the data-controller attribute in your DOM:
|
49
|
-
|
50
|
-
```html
|
51
|
-
<div data-controller="hello">
|
52
|
-
<input data-hello-target="name" type="text">
|
53
|
-
|
54
|
-
<button data-action="click->hello#greet">
|
55
|
-
Greet
|
56
|
-
</button>
|
57
|
-
|
58
|
-
<span data-hello-target="output">
|
59
|
-
</span>
|
60
|
-
</div>
|
61
|
-
```
|
62
|
-
|
63
|
-
That's it!
|
64
|
-
|
65
54
|
|
66
55
|
## License
|
67
56
|
|
@@ -50,3 +50,5 @@ new MutationObserver((mutationsList) => {
|
|
50
50
|
}).observe(document, { attributeFilter: [controllerAttribute], subtree: true, childList: true })
|
51
51
|
|
52
52
|
autoloadControllersWithin(document)
|
53
|
+
|
54
|
+
console.warn("stimulus-autoload.js has been deprecated in favor of stimulus-loading.js")
|
@@ -1,21 +1,27 @@
|
|
1
|
-
import
|
1
|
+
// FIXME: es-module-shim won't shim the dynamic import without this explicit import
|
2
|
+
import "@hotwired/stimulus"
|
2
3
|
|
3
|
-
function registerControllersFrom(under) {
|
4
|
-
const paths = Object.keys(
|
5
|
-
|
4
|
+
export function registerControllersFrom(under, application) {
|
5
|
+
const paths = Object.keys(parseImportmapJson())
|
6
|
+
.filter(path => path.match(new RegExp(`^${under}/.*_controller$`)))
|
7
|
+
|
8
|
+
paths.forEach(path => registerControllerFromPath(path, under, application))
|
9
|
+
}
|
10
|
+
|
11
|
+
export function parseImportmapJson() {
|
12
|
+
return JSON.parse(document.querySelector("script[type=importmap]").text).imports
|
6
13
|
}
|
7
14
|
|
8
|
-
function registerControllerFromPath(path, under) {
|
9
|
-
const name = path
|
15
|
+
function registerControllerFromPath(path, under, application) {
|
16
|
+
const name = path
|
17
|
+
.replace(new RegExp(`^${under}/`), "")
|
18
|
+
.replace("_controller", "")
|
19
|
+
.replace(/\//g, "--")
|
20
|
+
.replace(/_/g, "-")
|
10
21
|
|
11
22
|
import(path)
|
12
23
|
.then(module => application.register(name, module.default))
|
13
24
|
.catch(error => console.log(`Failed to register controller: ${name} (${path})`, error))
|
14
25
|
}
|
15
26
|
|
16
|
-
|
17
|
-
const importmap = JSON.parse(document.querySelector("script[type=importmap]").text).imports
|
18
|
-
|
19
|
-
registerControllersFrom("controllers")
|
20
|
-
|
21
|
-
export { application, registerControllersFrom }
|
27
|
+
console.warn("stimulus-importmap-autoload.js has been deprecated in favor of stimulus-loading.js")
|
@@ -0,0 +1,81 @@
|
|
1
|
+
// FIXME: es-module-shim won't shim the dynamic import without this explicit import
|
2
|
+
import "@hotwired/stimulus"
|
3
|
+
|
4
|
+
const controllerAttribute = "data-controller"
|
5
|
+
const registeredControllers = {}
|
6
|
+
|
7
|
+
// Eager load all controllers registered beneath the `under` path in the import map to the passed application instance.
|
8
|
+
export function eagerLoadControllersFrom(under, application) {
|
9
|
+
const paths = Object.keys(parseImportmapJson()).filter(path => path.match(new RegExp(`^${under}/.*_controller$`)))
|
10
|
+
paths.forEach(path => registerControllerFromPath(path, under, application))
|
11
|
+
}
|
12
|
+
|
13
|
+
function parseImportmapJson() {
|
14
|
+
return JSON.parse(document.querySelector("script[type=importmap]").text).imports
|
15
|
+
}
|
16
|
+
|
17
|
+
function registerControllerFromPath(path, under, application) {
|
18
|
+
const name = path
|
19
|
+
.replace(new RegExp(`^${under}/`), "")
|
20
|
+
.replace("_controller", "")
|
21
|
+
.replace(/\//g, "--")
|
22
|
+
.replace(/_/g, "-")
|
23
|
+
|
24
|
+
import(path)
|
25
|
+
.then(module => registerController(name, module, application))
|
26
|
+
.catch(error => console.debug(`Failed to register controller: ${name} (${path})`, error))
|
27
|
+
}
|
28
|
+
|
29
|
+
|
30
|
+
// Lazy load controllers registered beneath the `under` path in the import map to the passed application instance.
|
31
|
+
export function lazyLoadControllersFrom(under, application, element = document) {
|
32
|
+
lazyLoadExistingControllers(under, application, element)
|
33
|
+
lazyLoadNewControllers(under, application, element)
|
34
|
+
}
|
35
|
+
|
36
|
+
function lazyLoadExistingControllers(under, application, element) {
|
37
|
+
queryControllerNamesWithin(element).forEach(controllerName => loadController(controllerName, under, application))
|
38
|
+
}
|
39
|
+
|
40
|
+
function lazyLoadNewControllers(under, application, element) {
|
41
|
+
new MutationObserver((mutationsList) => {
|
42
|
+
for (const { attributeName, target, type } of mutationsList) {
|
43
|
+
switch (type) {
|
44
|
+
case "attributes": {
|
45
|
+
if (attributeName == controllerAttribute && target.getAttribute(controllerAttribute)) {
|
46
|
+
extractControllerNamesFrom(target).forEach(controllerName => loadController(controllerName, under, application))
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
case "childList": {
|
51
|
+
lazyLoadExistingControllers(under, application, target)
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}).observe(element, { attributeFilter: [controllerAttribute], subtree: true, childList: true })
|
56
|
+
}
|
57
|
+
|
58
|
+
function queryControllerNamesWithin(element) {
|
59
|
+
return Array.from(element.querySelectorAll(`[${controllerAttribute}]`)).map(extractControllerNamesFrom).flat()
|
60
|
+
}
|
61
|
+
|
62
|
+
function extractControllerNamesFrom(element) {
|
63
|
+
return element.getAttribute(controllerAttribute).split(/\s+/).filter(content => content.length)
|
64
|
+
}
|
65
|
+
|
66
|
+
function loadController(name, under, application) {
|
67
|
+
import(controllerFilename(name, under))
|
68
|
+
.then(module => registerController(name, module, application))
|
69
|
+
.catch(error => console.debug(`Failed to autoload controller: ${name}`, error))
|
70
|
+
}
|
71
|
+
|
72
|
+
function controllerFilename(name, under) {
|
73
|
+
return `${under}/${name.replace(/--/g, "/").replace(/-/g, "_")}_controller`
|
74
|
+
}
|
75
|
+
|
76
|
+
function registerController(name, module, application) {
|
77
|
+
if (name in registeredControllers) return
|
78
|
+
|
79
|
+
application.register(name, module.default)
|
80
|
+
registeredControllers[name] = true
|
81
|
+
}
|