You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Resolves#1064
Given Rails 8 has been released without transpilation or bundled
JavaScript by default, this documentation update demonstrates how to
use Stimulus and CSS in a ViewComponent without webpacker.
A more terse demonstration would be possible without the view component
arguments and/or less complicated Stimulus behaviour however that might
be less instructive.
One of the trickier aspects of binding Stimulus controllers is often
determining the correct data-controller key to use, especially when
namespaces and/or multi-word component names are involved which this example
makes explicit.
Approach 2 demonstrates the Stimulus behaviour but not the css, that
would likely involve dartsass-rails
While ViewComponent doesn't provide any built-in tooling to do so, it’s possible to include JavaScript and CSS alongside components.
10
10
11
+
## Propshaft / Stimulus
12
+
13
+
To use a [transpiler-less and bundler-less approach to JavaScript](https://world.hey.com/dhh/modern-web-apps-without-javascript-bundling-or-transpiling-a20f2755) (the default for Rails 8), Stimulus and CSS can be used inside ViewComponents one of two ways:
14
+
15
+
### Upgrading a pre-Rails 8 app
16
+
17
+
```ruby
18
+
# Gemfile (then run `bundle install`)
19
+
gem 'importmap-rails'# JavaScript version/digests without transpiling/bundling
20
+
gem 'propshaft'# Load static assets like JavaScript/CSS/images without transpilation/webpacker
### Approach 1 - Default _app/components_ ViewComponent directory using named Stimulus controllers, no autoloading
50
+
51
+
Locate CSS and Stimulus js with a ViewComponent. This example demonstrates a _HelloWorldComponent_ in an _examples_ namespace with a sidecar file naming approach:
52
+
53
+
```console
54
+
app/components
55
+
├── ...
56
+
├── examples
57
+
| ├── hello_world_component
58
+
| | ├── hello_world_component_controller.js
59
+
| | ├── hello_world_component.css
60
+
| | └── hello_world_component.html.erb
61
+
| └── hello_world_component.rb
62
+
├── ...
63
+
```
64
+
65
+
#### 1. Prepare _app/components_ as an asset path for css and ensure hot reloads of Stimulus JavaScript
"<em>This</em> will demonstrate the use of <b>Stimulus</b> and <b>CSS</b> in a ViewComponent".html_safe
157
+
}
158
+
%>
159
+
...
160
+
```
161
+
162
+
### Approach 2 - Autoloaded ViewComponents in a sub-directory
163
+
164
+
Stimulus controllers [won't currently autoload](https://github.com/ViewComponent/view_component/issues/1064#issuecomment-1163314487) if ViewComponents are located at:
165
+
166
+
```console
167
+
app/components
168
+
```
169
+
170
+
a workaround is to put ViewComponents in a subdirectory:
To use the Webpacker gem to compile assets located in `app/components`:
12
205
13
206
1. In `config/webpacker.yml`, add `"app/components"` to the `additional_paths` array (for example `additional_paths: ["app/components"]`).
@@ -115,54 +308,3 @@ class Comment extends HTMLElement {
115
308
}
116
309
customElements.define('my-comment', Comment)
117
310
```
118
-
119
-
## Stimulus
120
-
121
-
In Stimulus, create a 1:1 mapping between a Stimulus controller and a component. To load in Stimulus controllers from the `app/components` tree, amend the Stimulus boot code in `app/javascript/controllers/index.js`:
This enables the creation of files such as `app/components/widget_controller.js`, where the controller identifier matches the `data-controller` attribute in the component's HTML template.
138
-
139
-
After configuring Webpack to load Stimulus controller files from the `components` directory, add the path to `additional_paths` in `config/webpacker.yml`:
140
-
141
-
```yml
142
-
additional_paths: ["app/components"]
143
-
```
144
-
145
-
When placing a Stimulus controller inside a sidecar directory, be aware that when referencing the controller [each forward slash in a namespaced controller file’s path becomes two dashes in its identifier](
See [Generators Options](generators.html#generate-a-stimulus-controller) to generate a Stimulus controller alongside the component using the generator.
0 commit comments