opal_stimulus 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +65 -42
- data/lib/opal_stimulus/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ed5dba8c0e0b7d1f75d37a60d417f2e2af188a9adf0b249b9a55af55d67e0933
|
|
4
|
+
data.tar.gz: '0970f685ca56fb9d945615e53f0986bc1688c933c099aa550072fd11c1d15a9f'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 30354047b38f078cd5d809b608011c6e26a17d7f3953d6ba20824f33cf5f5cca2673b2d9a1eed1bb6fce9a9c2a4d9783537bc52695c156b1cef7ad982ed570bb
|
|
7
|
+
data.tar.gz: 715f6366c20a94eb653fa66ba322baced833f604cb49ae1b2202f0f32563cfddbfeb1e0787e0f511dde49659939d1e876eb884777d166a28658063eb9d7fece6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -1,45 +1,46 @@
|
|
|
1
1
|
# Opal Stimulus for Rails
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Write your [Stimulus](https://stimulus.hotwired.dev/) controllers in Ruby instead of JavaScript! 🎉
|
|
4
|
+
|
|
5
|
+
**Opal Stimulus** uses the [Opal Ruby-to-JavaScript compiler](https://opalrb.com/) to bring the elegance of Ruby to your frontend controllers. If you love Ruby and prefer its syntax over JavaScript, this gem is for you.
|
|
6
|
+
|
|
7
|
+
**Compatibility:** Rails >= 7.2
|
|
4
8
|
|
|
5
9
|
[](https://github.com/josephschito/opal_stimulus/actions/workflows/main.yml)
|
|
6
10
|
|
|
7
|
-
##
|
|
11
|
+
## Click here to watch the walkthrough! 👇
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
[<img width="640" height="360" alt="opal stimulus" src="https://github.com/user-attachments/assets/3eba6d3f-c62a-4faa-b918-5181ec9e2efc" />](https://www.youtube.com/watch?v=YyaVUUMq7po)
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
gem 'opal_stimulus'
|
|
13
|
-
```
|
|
15
|
+
## Quick Start
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
Add to your Rails app and get started in seconds:
|
|
16
18
|
|
|
17
19
|
```bash
|
|
18
|
-
bundle
|
|
20
|
+
bundle add opal_stimulus
|
|
19
21
|
rails opal_stimulus:install
|
|
22
|
+
bin/dev
|
|
20
23
|
```
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
Generate your first Ruby controller:
|
|
23
26
|
|
|
24
27
|
```bash
|
|
25
|
-
bin/
|
|
28
|
+
bin/rails generate opal_stimulus hello
|
|
26
29
|
```
|
|
27
30
|
|
|
28
|
-
|
|
31
|
+
This creates `app/opal/controllers/hello_controller.rb`. If you have an existing JavaScript controller, you can remove it:
|
|
29
32
|
|
|
30
33
|
```bash
|
|
31
|
-
bin/rails
|
|
34
|
+
bin/rails destroy stimulus hello # removes app/javascript/controllers/hello_controller.js
|
|
32
35
|
```
|
|
33
|
-
This command will create `app/opal/controllers/hello_controller.rb`
|
|
34
36
|
|
|
35
|
-
##
|
|
37
|
+
## Example: Hello World
|
|
36
38
|
|
|
37
|
-
Here's
|
|
39
|
+
Here's the classic Stimulus example, but in Ruby! Compare with the [JavaScript version](https://stimulus.hotwired.dev/#:~:text=%2F%2F%20hello_controller.js%0Aimport%20%7B%20Controller%20%7D%20from%20%22stimulus%22%0A%0Aexport%20default%20class%20extends%20Controller%20%7B%0A%20%20static%20targets%20%3D%20%5B%20%22name%22%2C%20%22output%22%20%5D%0A%0A%20%20greet()%20%7B%0A%20%20%20%20this.outputTarget.textContent%20%3D%0A%20%20%20%20%20%20%60Hello%2C%20%24%7Bthis.nameTarget.value%7D!%60%0A%20%20%7D%0A%7D).
|
|
38
40
|
|
|
39
|
-
**Ruby Controller:**
|
|
41
|
+
**Ruby Controller (`app/opal/controllers/hello_controller.rb`):**
|
|
40
42
|
|
|
41
43
|
```ruby
|
|
42
|
-
# app/opal/controllers/hello_controller.rb
|
|
43
44
|
class HelloController < StimulusController
|
|
44
45
|
self.targets = ["name", "output"]
|
|
45
46
|
|
|
@@ -49,31 +50,30 @@ class HelloController < StimulusController
|
|
|
49
50
|
end
|
|
50
51
|
```
|
|
51
52
|
|
|
52
|
-
**HTML:**
|
|
53
|
+
**HTML (unchanged from regular Stimulus):**
|
|
53
54
|
|
|
54
55
|
```html
|
|
55
56
|
<div data-controller="hello">
|
|
56
57
|
<input data-hello-target="name" type="text">
|
|
57
|
-
|
|
58
|
-
<
|
|
59
|
-
Greet
|
|
60
|
-
</button>
|
|
61
|
-
|
|
62
|
-
<span data-hello-target="output">
|
|
63
|
-
</span>
|
|
58
|
+
<button data-action="click->hello#greet">Greet</button>
|
|
59
|
+
<span data-hello-target="output"></span>
|
|
64
60
|
</div>
|
|
65
61
|
```
|
|
66
62
|
|
|
67
|
-
|
|
63
|
+
### Key Differences from JavaScript Stimulus
|
|
68
64
|
|
|
69
|
-
-
|
|
70
|
-
-
|
|
71
|
-
-
|
|
65
|
+
- **Snake case**: JavaScript's `containerTarget` becomes `container_target` in Ruby
|
|
66
|
+
- **DOM objects**: All `target`, `element`, `document`, `window`, and `event` objects are `JS::Proxy` instances that provide Ruby-friendly access to JavaScript APIs
|
|
67
|
+
- **HTML stays the same**: Your templates and data attributes work exactly like regular Stimulus
|
|
72
68
|
|
|
69
|
+
## Reference Examples
|
|
73
70
|
|
|
74
|
-
|
|
71
|
+
Based on the [Stimulus Reference](https://stimulus.hotwired.dev/reference/controllers), here's how common patterns work in Ruby:
|
|
75
72
|
|
|
76
73
|
### Lifecycle Callbacks
|
|
74
|
+
|
|
75
|
+
> 📚 [Stimulus Lifecycle Callbacks Reference](https://stimulus.hotwired.dev/reference/lifecycle-callbacks)
|
|
76
|
+
|
|
77
77
|
```ruby
|
|
78
78
|
class AlertController < StimulusController
|
|
79
79
|
def initialize; end
|
|
@@ -82,12 +82,15 @@ class AlertController < StimulusController
|
|
|
82
82
|
end
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
-
### Actions
|
|
85
|
+
### Actions & Events
|
|
86
|
+
|
|
87
|
+
> 📚 [Stimulus Actions Reference](https://stimulus.hotwired.dev/reference/actions)
|
|
88
|
+
|
|
86
89
|
```ruby
|
|
87
90
|
class WindowResizeController < StimulusController
|
|
88
91
|
def resized(event)
|
|
89
92
|
if !@resized && event.target.inner_width >= 1080
|
|
90
|
-
puts "Full HD
|
|
93
|
+
puts "Full HD detected!"
|
|
91
94
|
@resized = true
|
|
92
95
|
end
|
|
93
96
|
end
|
|
@@ -95,6 +98,9 @@ end
|
|
|
95
98
|
```
|
|
96
99
|
|
|
97
100
|
### Targets
|
|
101
|
+
|
|
102
|
+
> 📚 [Stimulus Targets Reference](https://stimulus.hotwired.dev/reference/targets)
|
|
103
|
+
|
|
98
104
|
```ruby
|
|
99
105
|
class ContainerController < StimulusController
|
|
100
106
|
self.targets = ["container"]
|
|
@@ -112,6 +118,9 @@ end
|
|
|
112
118
|
```
|
|
113
119
|
|
|
114
120
|
### Outlets
|
|
121
|
+
|
|
122
|
+
> 📚 [Stimulus Outlets Reference](https://stimulus.hotwired.dev/reference/outlets)
|
|
123
|
+
|
|
115
124
|
```ruby
|
|
116
125
|
class ChatController < StimulusController
|
|
117
126
|
self.outlets = [ "user-status" ]
|
|
@@ -125,6 +134,9 @@ end
|
|
|
125
134
|
```
|
|
126
135
|
|
|
127
136
|
### Values
|
|
137
|
+
|
|
138
|
+
> 📚 [Stimulus Values Reference](https://stimulus.hotwired.dev/reference/values)
|
|
139
|
+
|
|
128
140
|
```ruby
|
|
129
141
|
class LoaderController < StimulusController
|
|
130
142
|
self.values = { url: :string }
|
|
@@ -146,18 +158,24 @@ end
|
|
|
146
158
|
```
|
|
147
159
|
|
|
148
160
|
### CSS Classes
|
|
161
|
+
|
|
162
|
+
> 📚 [Stimulus CSS Classes Reference](https://stimulus.hotwired.dev/reference/css-classes)
|
|
163
|
+
|
|
149
164
|
```ruby
|
|
150
165
|
class SearchController < StimulusController
|
|
151
|
-
self.classes = [
|
|
166
|
+
self.classes = ["loading"]
|
|
152
167
|
|
|
153
|
-
def
|
|
154
|
-
element.class_list.add
|
|
168
|
+
def load_results
|
|
169
|
+
element.class_list.add(loading_class)
|
|
155
170
|
end
|
|
156
171
|
end
|
|
157
172
|
```
|
|
158
173
|
|
|
159
|
-
##
|
|
160
|
-
|
|
174
|
+
## Working with the DOM
|
|
175
|
+
|
|
176
|
+
Opal Stimulus gives you Ruby-friendly access to all the browser APIs you know and love:
|
|
177
|
+
|
|
178
|
+
### `window`
|
|
161
179
|
```ruby
|
|
162
180
|
class WindowController < StimulusController
|
|
163
181
|
def connect
|
|
@@ -169,20 +187,25 @@ class WindowController < StimulusController
|
|
|
169
187
|
end
|
|
170
188
|
```
|
|
171
189
|
|
|
172
|
-
###
|
|
190
|
+
### `document`
|
|
173
191
|
```ruby
|
|
174
192
|
class DocumentController < StimulusController
|
|
175
193
|
def connect
|
|
176
|
-
|
|
177
|
-
headers.each do |h1|
|
|
194
|
+
document.querySelectorAll("h1").each do |h1|
|
|
178
195
|
h1.text_content = "Opal is great!"
|
|
179
196
|
end
|
|
180
197
|
end
|
|
181
198
|
end
|
|
182
199
|
```
|
|
183
200
|
|
|
184
|
-
|
|
185
|
-
|
|
201
|
+
|
|
202
|
+
## Development
|
|
203
|
+
|
|
204
|
+
Run the test suite:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
bundle exec rake
|
|
208
|
+
```
|
|
186
209
|
|
|
187
210
|
## Contributing
|
|
188
211
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: opal_stimulus
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Joseph Schito
|
|
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
137
137
|
- !ruby/object:Gem::Version
|
|
138
138
|
version: '0'
|
|
139
139
|
requirements: []
|
|
140
|
-
rubygems_version: 3.
|
|
140
|
+
rubygems_version: 3.7.2
|
|
141
141
|
specification_version: 4
|
|
142
142
|
summary: 'Opal Stimulus: Write Stimulus controllers in Ruby'
|
|
143
143
|
test_files: []
|