phlex-stimulus 0.1.0
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 +7 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +16 -0
- data/LICENSE.txt +21 -0
- data/README.md +341 -0
- data/Rakefile +12 -0
- data/lib/generators/phlex/stimulus/controller/USAGE +5 -0
- data/lib/generators/phlex/stimulus/controller/controller_generator.rb +49 -0
- data/lib/generators/phlex/stimulus/controller/templates/component.rb.erb +30 -0
- data/lib/generators/phlex/stimulus/controller/templates/controller.ts.erb +23 -0
- data/lib/generators/phlex/stimulus/generator_mixin.rb +21 -0
- data/lib/generators/phlex/stimulus/install/USAGE +5 -0
- data/lib/generators/phlex/stimulus/install/install_generator.rb +111 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/base.rb.erb +29 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/chain_controller.rb.erb +29 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/classlist_controller.rb.erb +30 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/controller.rb.erb +10 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/controller_chain.rb.erb +32 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/controller_template.rb.erb +24 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/log_controller.rb.erb +33 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components/sleep_controller.rb.erb +30 -0
- data/lib/generators/phlex/stimulus/install/templates/app/components.rb.erb +7 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/application.ts.erb +5 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/application.ts.erb +10 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/chain_controller.ts.erb +32 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/chainable_controller.ts.erb +44 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/classlist_controller.ts.erb +30 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/index.ts.erb +4 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/log_controller.ts.erb +25 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/sleep_controller.ts.erb +19 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/controllers/typed_controller.ts.erb +53 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/utils/misc.ts.erb +37 -0
- data/lib/generators/phlex/stimulus/install/templates/app/javascript/utils/template.ts.erb +11 -0
- data/lib/phlex/stimulus/components/base.rb +81 -0
- data/lib/phlex/stimulus/components/controller.rb +170 -0
- data/lib/phlex/stimulus/components.rb +9 -0
- data/lib/phlex/stimulus/version.rb +8 -0
- data/lib/phlex/stimulus.rb +15 -0
- data/sorbet/config +7 -0
- data/sorbet/rbi/annotations/.gitattributes +1 -0
- data/sorbet/rbi/annotations/actionmailer.rbi +15 -0
- data/sorbet/rbi/annotations/actionpack.rbi +430 -0
- data/sorbet/rbi/annotations/actionview.rbi +75 -0
- data/sorbet/rbi/annotations/activejob.rbi +50 -0
- data/sorbet/rbi/annotations/activemodel.rbi +119 -0
- data/sorbet/rbi/annotations/activerecord.rbi +172 -0
- data/sorbet/rbi/annotations/activesupport.rbi +561 -0
- data/sorbet/rbi/annotations/globalid.rbi +30 -0
- data/sorbet/rbi/annotations/minitest.rbi +116 -0
- data/sorbet/rbi/annotations/railties.rbi +104 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/dsl/.gitattributes +1 -0
- data/sorbet/rbi/dsl/active_support/callbacks.rbi +21 -0
- data/sorbet/rbi/gems/.gitattributes +1 -0
- data/sorbet/rbi/gems/action_text-trix@2.1.19.rbi +9 -0
- data/sorbet/rbi/gems/actioncable@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/actionmailbox@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/actionmailer@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/actionpack@8.1.3.rbi +20478 -0
- data/sorbet/rbi/gems/actiontext@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/actionview@8.1.3.rbi +14722 -0
- data/sorbet/rbi/gems/activejob@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/activemodel@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/activerecord@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/activestorage@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/activesupport@8.1.3.rbi +20153 -0
- data/sorbet/rbi/gems/ast@2.4.3.rbi +550 -0
- data/sorbet/rbi/gems/base64@0.3.0.rbi +545 -0
- data/sorbet/rbi/gems/benchmark@0.5.0.rbi +621 -0
- data/sorbet/rbi/gems/booleans@0.1.3.rbi +31 -0
- data/sorbet/rbi/gems/builder@3.3.0.rbi +9 -0
- data/sorbet/rbi/gems/byebug@12.0.0.rbi +49 -0
- data/sorbet/rbi/gems/concurrent-ruby@1.3.8.rbi +10656 -0
- data/sorbet/rbi/gems/connection_pool@3.0.2.rbi +9 -0
- data/sorbet/rbi/gems/crass@1.0.7.rbi +648 -0
- data/sorbet/rbi/gems/date@3.5.1.rbi +391 -0
- data/sorbet/rbi/gems/drb@2.2.3.rbi +9 -0
- data/sorbet/rbi/gems/erb@6.0.6.rbi +1600 -0
- data/sorbet/rbi/gems/erubi@1.13.1.rbi +155 -0
- data/sorbet/rbi/gems/globalid@1.4.0.rbi +9 -0
- data/sorbet/rbi/gems/i18n@1.15.2.rbi +2285 -0
- data/sorbet/rbi/gems/io-console@0.8.2.rbi +9 -0
- data/sorbet/rbi/gems/json@2.21.1.rbi +2289 -0
- data/sorbet/rbi/gems/lint_roller@1.1.0.rbi +189 -0
- data/sorbet/rbi/gems/logger@1.7.0.rbi +896 -0
- data/sorbet/rbi/gems/loofah@2.25.2.rbi +1134 -0
- data/sorbet/rbi/gems/mail@2.9.1.rbi +9 -0
- data/sorbet/rbi/gems/marcel@1.2.1.rbi +9 -0
- data/sorbet/rbi/gems/mini_mime@1.1.5.rbi +9 -0
- data/sorbet/rbi/gems/minitest@6.0.6.rbi +1482 -0
- data/sorbet/rbi/gems/net-imap@0.6.4.1.rbi +9 -0
- data/sorbet/rbi/gems/net-pop@0.1.2.rbi +9 -0
- data/sorbet/rbi/gems/net-protocol@0.2.2.rbi +244 -0
- data/sorbet/rbi/gems/net-smtp@0.5.1.rbi +9 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +147 -0
- data/sorbet/rbi/gems/nio4r@2.7.5.rbi +9 -0
- data/sorbet/rbi/gems/nokogiri@1.19.4.rbi +8168 -0
- data/sorbet/rbi/gems/parallel@1.28.0.rbi +270 -0
- data/sorbet/rbi/gems/phlex-rails@2.4.0.rbi +1945 -0
- data/sorbet/rbi/gems/phlex@2.4.1.rbi +1413 -0
- data/sorbet/rbi/gems/pp@0.6.4.rbi +387 -0
- data/sorbet/rbi/gems/prettyprint@0.2.0.rbi +455 -0
- data/sorbet/rbi/gems/prism@1.9.0.rbi +42226 -0
- data/sorbet/rbi/gems/racc@1.8.1.rbi +165 -0
- data/sorbet/rbi/gems/rack-session@2.1.2.rbi +756 -0
- data/sorbet/rbi/gems/rack-test@2.2.0.rbi +681 -0
- data/sorbet/rbi/gems/rack@3.2.6.rbi +4653 -0
- data/sorbet/rbi/gems/rackup@2.3.1.rbi +9 -0
- data/sorbet/rbi/gems/rails-dom-testing@2.3.0.rbi +517 -0
- data/sorbet/rbi/gems/rails-html-sanitizer@1.7.1.rbi +620 -0
- data/sorbet/rbi/gems/rails@8.1.3.rbi +9 -0
- data/sorbet/rbi/gems/railties@8.1.3.rbi +3507 -0
- data/sorbet/rbi/gems/rainbow@3.1.1.rbi +362 -0
- data/sorbet/rbi/gems/rake@13.4.2.rbi +3130 -0
- data/sorbet/rbi/gems/rbi@0.4.0.rbi +5706 -0
- data/sorbet/rbi/gems/rbs@4.0.3.rbi +6908 -0
- data/sorbet/rbi/gems/refract@1.1.0.rbi +9 -0
- data/sorbet/rbi/gems/reline@0.6.3.rbi +2446 -0
- data/sorbet/rbi/gems/require-hooks@0.4.0.rbi +152 -0
- data/sorbet/rbi/gems/rexml@3.4.4.rbi +4905 -0
- data/sorbet/rbi/gems/rubocop-espago@1.3.0.rbi +9 -0
- data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +988 -0
- data/sorbet/rbi/gems/rubydex@0.2.9.rbi +841 -0
- data/sorbet/rbi/gems/securerandom@0.4.1.rbi +75 -0
- data/sorbet/rbi/gems/shoulda-context@2.0.0.rbi +544 -0
- data/sorbet/rbi/gems/spoom@1.8.4.rbi +6738 -0
- data/sorbet/rbi/gems/tapioca@0.19.2.rbi +3599 -0
- data/sorbet/rbi/gems/thor@1.5.0.rbi +3870 -0
- data/sorbet/rbi/gems/timeout@0.6.1.rbi +200 -0
- data/sorbet/rbi/gems/tsort@0.2.0.rbi +389 -0
- data/sorbet/rbi/gems/tzinfo@2.0.6.rbi +5474 -0
- data/sorbet/rbi/gems/unicode-display_width@3.2.0.rbi +130 -0
- data/sorbet/rbi/gems/unicode-emoji@4.2.0.rbi +332 -0
- data/sorbet/rbi/gems/uri@1.1.1.rbi +2402 -0
- data/sorbet/rbi/gems/useragent@0.16.11.rbi +9 -0
- data/sorbet/rbi/gems/websocket-driver@0.8.2.rbi +9 -0
- data/sorbet/rbi/gems/websocket-extensions@0.1.5.rbi +9 -0
- data/sorbet/rbi/gems/zeitwerk@2.8.2.rbi +9 -0
- data/sorbet/rbi/shims/gems/parser.rbi +12 -0
- data/sorbet/rbi/shims/gems/rails.rbi +55 -0
- data/sorbet/rbi/shims/gems/refract.rbi +13 -0
- data/sorbet/rbi/shims/gems/shoulda-context.rbi +16 -0
- data/sorbet/tapioca/compilers/phlex_controller.rb +80 -0
- data/sorbet/tapioca/config.yml +26 -0
- data/sorbet/tapioca/extensions/load_gem.rb +1 -0
- data/sorbet/tapioca/require.rb +4 -0
- metadata +266 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0f20ab06752e9826fc2e3861ee9878cd26448f2094ccbd5779ffacb64b6fe364
|
|
4
|
+
data.tar.gz: 9b0bb287d885da0b196d9aab1b21d9d9b1048f2d4d19e2c15d969dfba7f0a1e2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4afabcd2606a43cdf8f029e27afa3dc86c119fd68f6e5cda842ba729ac9df2659110b048f1cbb56df7f502a343cf61f6b6da8e163373780cc6d3af151f82de99
|
|
7
|
+
data.tar.gz: 9c25eddafac1a456cb42303eac937eb5f95b898a446daa85ffe981528c210bbe0b92dbce5a79812b26d3ace3e4567489bec9940dbc22f594680ab91138d6fb41
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
4.0.6
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
Add changes in new features here. Do not change the gem's version in pull/merge requests.
|
|
11
|
+
|
|
12
|
+
## [0.1.0] - 22.07.2026
|
|
13
|
+
|
|
14
|
+
[Diff](https://github.com/espago/phlex-stimulus/compare/v0.0.0...v0.1.0)
|
|
15
|
+
|
|
16
|
+
- Initial release
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Espago
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
# Phlex::Stimulus
|
|
2
|
+
|
|
3
|
+
`phlex-stimulus` pairs every [Stimulus](https://stimulus.hotwired.dev/) controller in your app with a
|
|
4
|
+
[Phlex](https://www.phlex.fun/) component, and keeps both sides strictly typed.
|
|
5
|
+
|
|
6
|
+
For each Stimulus controller (the TypeScript side) there is a matching Phlex component
|
|
7
|
+
(the Ruby side). The component owns the markup that mounts the controller — the `data-controller`
|
|
8
|
+
attribute, its targets, actions, and values — and exposes strictly [Sorbet](https://sorbet.org/)-typed
|
|
9
|
+
helpers so the wiring between Ruby and JS can't silently drift.
|
|
10
|
+
|
|
11
|
+
Controllers can also be **chained**: a set of controllers can be rendered so that each one's `connect`
|
|
12
|
+
logic runs to completion before the next one connects.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
bundle add phlex-stimulus
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
gem install phlex-stimulus
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then run the installer.
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
bundle exec rails generate phlex:stimulus:install
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
### Generating a controller + component
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
bundle exec rails generate phlex:stimulus:controller Summary
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
This creates a matched pair:
|
|
43
|
+
|
|
44
|
+
- `app/components/summary_controller.rb` — a `Components::SummaryController`
|
|
45
|
+
- `app/javascript/controllers/summary_controller.ts` — a `SummaryController`
|
|
46
|
+
|
|
47
|
+
### Ruby controller components
|
|
48
|
+
|
|
49
|
+
A controller component declares the Stimulus controller name, its actions, and its targets. Those
|
|
50
|
+
declarations generate strictly-typed helpers so your markup references controllers, actions, and targets
|
|
51
|
+
by method call instead of by hand-written string:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
# typed: strict
|
|
55
|
+
|
|
56
|
+
module Components
|
|
57
|
+
class SummaryController < Controller
|
|
58
|
+
self.controller_name = 'summary'
|
|
59
|
+
|
|
60
|
+
actions :redirect # => defines SummaryController.redirect_action #=> "summary#redirect"
|
|
61
|
+
targets :body # => defines SummaryController.body_target #=> "body"
|
|
62
|
+
|
|
63
|
+
#: (title: String) -> void
|
|
64
|
+
def initialize(title:)
|
|
65
|
+
@title = title
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @override
|
|
69
|
+
#: ?{ -> void } -> void
|
|
70
|
+
def view_template(&block)
|
|
71
|
+
div(data: { controller: self.class.controller_name, title: @title }) do
|
|
72
|
+
block&.call
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
You can render a controller like any other Phlex component:
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
render SummaryController.new(title: 'Report') do
|
|
84
|
+
# define an action
|
|
85
|
+
button(data: { action: event('click', SummaryController.redirect_action) }) { 'Go' }
|
|
86
|
+
|
|
87
|
+
# define a target
|
|
88
|
+
div(data: { SummaryController.target_key => SummaryController.body_target }) do
|
|
89
|
+
plan "Foo"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Controller component helpers
|
|
95
|
+
|
|
96
|
+
#### `_action`
|
|
97
|
+
|
|
98
|
+
Each action you define will result in a corresponding `_action` method being available in Ruby.
|
|
99
|
+
Sorbet is fully aware of these methods thanks to a tapioca compiler.
|
|
100
|
+
|
|
101
|
+
These methods just return the name of the controller together with the name of the action: `"controller#action"`.
|
|
102
|
+
They mostly exist so that sorbet can check if the actions your using when building HTML elements
|
|
103
|
+
actually exist.
|
|
104
|
+
|
|
105
|
+
You can read more about actions [here](https://stimulus.hotwired.dev/reference/actions).
|
|
106
|
+
|
|
107
|
+
```rb
|
|
108
|
+
class SummaryController < Controller
|
|
109
|
+
self.controller_name = 'summary'
|
|
110
|
+
|
|
111
|
+
actions :foo
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
SummaryController.foo_action #=> "summary#foo"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
This can be used to attach an action to an HTML element with full type safety.
|
|
118
|
+
|
|
119
|
+
```rb
|
|
120
|
+
div(data: { action: event('click', SummaryController.foo_action) })
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
This is the same as:
|
|
124
|
+
|
|
125
|
+
```rb
|
|
126
|
+
div(data: { action: 'click->summary#foo' })
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### `_target`
|
|
130
|
+
|
|
131
|
+
Each target you define will result in a corresponding `_target` method being available in Ruby.
|
|
132
|
+
Sorbet is fully aware of these methods thanks to a tapioca compiler.
|
|
133
|
+
|
|
134
|
+
These methods just return the name of the target so they may seem pointless.
|
|
135
|
+
They exist only so that sorbet can check if the actions your using when building HTML elements
|
|
136
|
+
actually exist.
|
|
137
|
+
|
|
138
|
+
You can read more about targets [here](https://stimulus.hotwired.dev/reference/targets).
|
|
139
|
+
|
|
140
|
+
```rb
|
|
141
|
+
class SummaryController < Controller
|
|
142
|
+
self.controller_name = 'summary'
|
|
143
|
+
|
|
144
|
+
targets :foo
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
SummaryController.foo_target #=> "foo"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
You would use it like so to attach a target with full type safety:
|
|
151
|
+
|
|
152
|
+
```rb
|
|
153
|
+
div(data: { SummaryController.target_key => SummaryController.foo_target })
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
This is the same as:
|
|
157
|
+
|
|
158
|
+
```rb
|
|
159
|
+
div(data: { 'summary-target' => 'foo' })
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
#### `target_key`
|
|
163
|
+
|
|
164
|
+
This method returns the key that can be used to attach targets to the controller.
|
|
165
|
+
|
|
166
|
+
```rb
|
|
167
|
+
class SummaryController < Controller
|
|
168
|
+
self.controller_name = 'summary'
|
|
169
|
+
|
|
170
|
+
targets :foo
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
SummaryController.target_key #=> "summary-target"
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
You would use it like so to attach a target with full type safety:
|
|
177
|
+
|
|
178
|
+
```rb
|
|
179
|
+
div(data: { SummaryController.target_key => SummaryController.foo_target })
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
This is the same as:
|
|
183
|
+
|
|
184
|
+
```rb
|
|
185
|
+
div(data: { 'summary-target' => 'foo' })
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### `dispatched`
|
|
189
|
+
|
|
190
|
+
This method helps you get the names of custom events dispatched by a stimulus controller.
|
|
191
|
+
You can read more about event dispatching [here](https://stimulus.hotwired.dev/reference/controllers#cross-controller-coordination-with-events).
|
|
192
|
+
|
|
193
|
+
```rb
|
|
194
|
+
class SummaryController < Controller
|
|
195
|
+
self.controller_name = 'summary'
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
SummaryController.dispatched('redirected') #=> "summary:redirected"
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
You would use it like so to define an action on an dispatched event:
|
|
202
|
+
|
|
203
|
+
```rb
|
|
204
|
+
div(data: { action: event(SummaryController.dispatched('redirected'), OtherController.do_action) })
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
This is the same as:
|
|
208
|
+
|
|
209
|
+
```rb
|
|
210
|
+
div(data: { 'summary:redirected' => 'other#do' })
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Component Helpers
|
|
214
|
+
|
|
215
|
+
There are some useful helper methods you can use in every phlex component.
|
|
216
|
+
|
|
217
|
+
#### `attrbool`
|
|
218
|
+
|
|
219
|
+
`attrbool` converts a Ruby boolean to `'true'` or `nil` so the boolean value
|
|
220
|
+
can be used in attributes of HTML elements
|
|
221
|
+
|
|
222
|
+
```rb
|
|
223
|
+
attrbool(true) #=> "true"
|
|
224
|
+
attrbool(false) #=> nil
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
You would use it when passing a boolean value from Ruby to an HTML attribute.
|
|
228
|
+
|
|
229
|
+
```rb
|
|
230
|
+
button(data: { enabled: attrbool(@enabled) })
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### `event`
|
|
234
|
+
|
|
235
|
+
`event` let's you easily and safely create stimulus action strings.
|
|
236
|
+
|
|
237
|
+
```rb
|
|
238
|
+
event('click', 'summary#redirect') #=> "click->summary#redirect"
|
|
239
|
+
event('click', SummaryController.redirect_action) #=> "click->summary#redirect"
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
You would use it when attaching a controller action to some HTML element.
|
|
243
|
+
|
|
244
|
+
```rb
|
|
245
|
+
button(data: { action: event('click', SummaryController.redirect_action) })
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
#### `class_list`
|
|
249
|
+
|
|
250
|
+
`class_list` let's you easily add classes to html elements conditionally.
|
|
251
|
+
|
|
252
|
+
```rb
|
|
253
|
+
class_list("foo", "bar", "baz") #=> "foo bar baz"
|
|
254
|
+
|
|
255
|
+
error = false
|
|
256
|
+
class_list("foo", "red" if error, "bar") #=> "foo bar"
|
|
257
|
+
|
|
258
|
+
error = true
|
|
259
|
+
class_list("foo", "red" if error, "bar") #=> "foo red bar"
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
You would use it when declaring an HTML element.
|
|
263
|
+
|
|
264
|
+
```rb
|
|
265
|
+
div(class: class_list("foo", "red" if error, "bar")) do
|
|
266
|
+
plain "Hello!"
|
|
267
|
+
end
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
`class_list` is aliased as `strlist` as it may be useful for other things than CSS classes.
|
|
271
|
+
|
|
272
|
+
### Stimulus controllers
|
|
273
|
+
|
|
274
|
+
There is a `TypedController` factory that let's you define strict types for controller targets.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import { application } from "./application"
|
|
278
|
+
import { TypedController } from "./typed_controller"
|
|
279
|
+
|
|
280
|
+
class SummaryController extends TypedController<HTMLElement, { body: HTMLDivElement }>() {
|
|
281
|
+
static targets = ["body"]
|
|
282
|
+
|
|
283
|
+
redirect() {
|
|
284
|
+
this.bodyTarget.innerText = "…" // fully typed
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
application.register("summary", SummaryController)
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Chaining controllers
|
|
292
|
+
|
|
293
|
+
Sometimes controllers must connect in a specific order — each one finishing its setup before the next
|
|
294
|
+
starts. `phlex-stimulus` supports this on both sides.
|
|
295
|
+
|
|
296
|
+
On the TypeScript side, a chainable controller extends `ChainableController` (or
|
|
297
|
+
`TypedChainableController` for typed targets) and puts its `connect` logic in `init()`. When `init()`
|
|
298
|
+
resolves, the controller dispatches a `ready` event that the chain waits on:
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
class LogController extends ChainableController<HTMLElement> {
|
|
302
|
+
async init() {
|
|
303
|
+
console.log("connected")
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
On the Ruby side, render your controller components through `Components::ControllerChain`. It renders
|
|
309
|
+
each controller inside a deferred `<template>` and drives them via the `chain` controller so they attach
|
|
310
|
+
one after another:
|
|
311
|
+
|
|
312
|
+
```ruby
|
|
313
|
+
render Components::ControllerChain.new(
|
|
314
|
+
Components::SleepController.new(timeout: 500),
|
|
315
|
+
Components::LogController.new(message: "ready!"),
|
|
316
|
+
)
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
This would result in a *"ready!"* console message getting displayed after 500 milliseconds.
|
|
320
|
+
|
|
321
|
+
Two of the bundled controllers are especially useful as chain building blocks:
|
|
322
|
+
|
|
323
|
+
- `Components::SleepController.new(timeout:)` — pauses the chain for the given number of milliseconds.
|
|
324
|
+
- `Components::LogController.new(message:, on_event:)` — `console.log`s a message when it connects
|
|
325
|
+
(handy for debugging a chain).
|
|
326
|
+
|
|
327
|
+
You can look those up as examples to see how you can implement your own chainable controllers.
|
|
328
|
+
|
|
329
|
+
## Development
|
|
330
|
+
|
|
331
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
332
|
+
|
|
333
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
334
|
+
|
|
335
|
+
## Contributing
|
|
336
|
+
|
|
337
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/espago/phlex-stimulus. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/espago/phlex-stimulus/blob/main/CODE_OF_CONDUCT.md).
|
|
338
|
+
|
|
339
|
+
## License
|
|
340
|
+
|
|
341
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# typed: false
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative '../generator_mixin'
|
|
5
|
+
|
|
6
|
+
module Phlex::Stimulus::Generators
|
|
7
|
+
class ControllerGenerator < ::Rails::Generators::NamedBase
|
|
8
|
+
include GeneratorMixin
|
|
9
|
+
|
|
10
|
+
source_root File.expand_path('templates', __dir__)
|
|
11
|
+
|
|
12
|
+
#: -> void
|
|
13
|
+
def create_files
|
|
14
|
+
template 'controller.ts.erb',
|
|
15
|
+
File.join(destination_root, 'app', 'javascript', 'controllers', class_path, "#{file_name}_controller.ts")
|
|
16
|
+
|
|
17
|
+
template 'component.rb.erb',
|
|
18
|
+
File.join(destination_root, 'app', 'components', class_path, "#{file_name}_controller.rb")
|
|
19
|
+
|
|
20
|
+
append_to_file File.join(destination_root, 'app', 'javascript', 'controllers', 'index.ts'),
|
|
21
|
+
"import \"./#{class_path}/#{file_name}_controller\"\n"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
#: -> String
|
|
27
|
+
def path_to_root_controller_dir
|
|
28
|
+
@path_to_root_controller_dir ||= begin
|
|
29
|
+
result = String.new
|
|
30
|
+
result << './'
|
|
31
|
+
class_path.length.times do
|
|
32
|
+
result << '../'
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
result
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
#: -> String
|
|
40
|
+
def stimulus_controller_name
|
|
41
|
+
@stimulus_controller_name ||= (class_path + [file_name]).join('--')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
#: -> String
|
|
45
|
+
def class_name_without_namespace
|
|
46
|
+
file_name.camelize
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module Components
|
|
5
|
+
# Does something ;)
|
|
6
|
+
class <%= namespace if namespaced? %><%= class_name %>Controller < Controller
|
|
7
|
+
self.controller_name = '<%= stimulus_controller_name %>'
|
|
8
|
+
|
|
9
|
+
actions :act
|
|
10
|
+
targets :foo
|
|
11
|
+
|
|
12
|
+
#: (name: String) -> void
|
|
13
|
+
def initialize(name)
|
|
14
|
+
@name = name
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @override
|
|
18
|
+
#: ?{ -> void } -> void
|
|
19
|
+
def view_template(&block)
|
|
20
|
+
div(
|
|
21
|
+
data: {
|
|
22
|
+
controller: self.class.controller_name,
|
|
23
|
+
name: @name,
|
|
24
|
+
},
|
|
25
|
+
) do
|
|
26
|
+
block&.call
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { application } from "<%= path_to_root_controller_dir %>application"
|
|
2
|
+
import { TypedController } from "<%= path_to_root_controller_dir %>typed_controller"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Does something ;)
|
|
6
|
+
**/
|
|
7
|
+
class <%= class_name_without_namespace %>Controller extends TypedController<HTMLElement, { foo: HTMLDivElement }>() {
|
|
8
|
+
static targets = [ "foo" ]
|
|
9
|
+
|
|
10
|
+
name!: string
|
|
11
|
+
|
|
12
|
+
connect() {
|
|
13
|
+
let dataset = this.element.dataset
|
|
14
|
+
this.name = dataset.name!
|
|
15
|
+
console.log(`Hello from <%= class_name_without_namespace %>Controller with name of ${this.name}!`)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
act() {
|
|
19
|
+
console.log(`I am <%= class_name_without_namespace %>Controller with name of ${this.name}, and I act now!`)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
application.register("<%= stimulus_controller_name %>", <%= class_name_without_namespace %>Controller)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# typed: false
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module Phlex::Stimulus::Generators
|
|
5
|
+
module GeneratorMixin
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
#: (String) -> void
|
|
9
|
+
def replace_with_template(path)
|
|
10
|
+
app_path = File.join(destination_root, path)
|
|
11
|
+
remove_file app_path
|
|
12
|
+
template "#{path}.erb", app_path
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
#: (String) -> void
|
|
16
|
+
def copy_template(path)
|
|
17
|
+
template "#{path}.erb",
|
|
18
|
+
File.join(destination_root, path)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# typed: false
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative '../generator_mixin'
|
|
5
|
+
|
|
6
|
+
module Phlex::Stimulus::Generators
|
|
7
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
8
|
+
include GeneratorMixin
|
|
9
|
+
|
|
10
|
+
source_root File.expand_path('templates', __dir__)
|
|
11
|
+
|
|
12
|
+
def remove_importmap_if_present
|
|
13
|
+
return unless importmap_installed?
|
|
14
|
+
|
|
15
|
+
say 'Importmap detected — removing it', :yellow
|
|
16
|
+
remove_importmap
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def install_jsbundling_unless_present
|
|
20
|
+
return if jsbundling_installed?
|
|
21
|
+
|
|
22
|
+
say 'Installing jsbundling-rails (esbuild)', :yellow
|
|
23
|
+
install_jsbundling_with_esbuild
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def install_stimulus
|
|
27
|
+
say 'Installing stimulus', :yellow
|
|
28
|
+
gem 'stimulus-rails'
|
|
29
|
+
Bundler.with_original_env { run 'bundle install' }
|
|
30
|
+
rails_command 'stimulus:install'
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def create_files
|
|
34
|
+
copy_template 'app/components.rb'
|
|
35
|
+
copy_template 'app/components/base.rb'
|
|
36
|
+
copy_template 'app/components/controller.rb'
|
|
37
|
+
copy_template 'app/components/chain_controller.rb'
|
|
38
|
+
copy_template 'app/components/controller_template.rb'
|
|
39
|
+
copy_template 'app/components/controller_chain.rb'
|
|
40
|
+
copy_template 'app/components/classlist_controller.rb'
|
|
41
|
+
copy_template 'app/components/log_controller.rb'
|
|
42
|
+
copy_template 'app/components/sleep_controller.rb'
|
|
43
|
+
|
|
44
|
+
remove_file File.join(destination_root, 'app/javascript/application.js')
|
|
45
|
+
copy_template 'app/javascript/application.ts'
|
|
46
|
+
|
|
47
|
+
remove_file File.join(destination_root, 'app/javascript/controllers/application.js')
|
|
48
|
+
copy_template 'app/javascript/controllers/application.ts'
|
|
49
|
+
|
|
50
|
+
remove_file File.join(destination_root, 'app/javascript/controllers/index.js')
|
|
51
|
+
copy_template 'app/javascript/controllers/index.ts'
|
|
52
|
+
|
|
53
|
+
copy_template 'app/javascript/controllers/typed_controller.ts'
|
|
54
|
+
copy_template 'app/javascript/controllers/chain_controller.ts'
|
|
55
|
+
copy_template 'app/javascript/controllers/chainable_controller.ts'
|
|
56
|
+
copy_template 'app/javascript/controllers/log_controller.ts'
|
|
57
|
+
copy_template 'app/javascript/controllers/sleep_controller.ts'
|
|
58
|
+
copy_template 'app/javascript/controllers/classlist_controller.ts'
|
|
59
|
+
|
|
60
|
+
copy_template 'app/javascript/utils/misc.ts'
|
|
61
|
+
copy_template 'app/javascript/utils/template.ts'
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
#: (String) -> void
|
|
67
|
+
def replace_with_template(path)
|
|
68
|
+
app_path = File.join(destination_root, path)
|
|
69
|
+
remove_file app_path
|
|
70
|
+
template "#{path}.erb", app_path
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
#: (String) -> void
|
|
74
|
+
def copy_template(path)
|
|
75
|
+
template "#{path}.erb",
|
|
76
|
+
File.join(destination_root, path)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def importmap_installed?
|
|
80
|
+
File.exist?(File.join(destination_root, 'config/importmap.rb')) ||
|
|
81
|
+
gemfile_includes?('importmap-rails')
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def jsbundling_installed?
|
|
85
|
+
gemfile_includes?('jsbundling-rails')
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def remove_importmap
|
|
89
|
+
remove_file File.join(destination_root, 'config/importmap.rb')
|
|
90
|
+
gsub_file 'Gemfile', /^\s*gem ["']importmap-rails["'].*\n/, ''
|
|
91
|
+
|
|
92
|
+
layout = File.join('app/views/layouts', 'application.html.erb')
|
|
93
|
+
return unless File.exist?(File.join(destination_root, layout))
|
|
94
|
+
|
|
95
|
+
gsub_file layout, /^\s*<%=\s*javascript_importmap_tags.*%>\n/, ''
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def install_jsbundling_with_esbuild
|
|
99
|
+
gem 'jsbundling-rails'
|
|
100
|
+
Bundler.with_original_env { run 'bundle install' }
|
|
101
|
+
rails_command 'javascript:install:esbuild'
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def gemfile_includes?(gem_name)
|
|
105
|
+
gemfile = File.join(destination_root, 'Gemfile')
|
|
106
|
+
return false unless File.exist?(gemfile)
|
|
107
|
+
|
|
108
|
+
File.read(gemfile).match?(/^\s*gem ["']#{Regexp.escape(gem_name)}["']/)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Abstract class for all Phlex components that render HTML in this app.
|
|
5
|
+
#
|
|
6
|
+
# @abstract
|
|
7
|
+
class Components::Base < Phlex::Stimulus::Components::Base
|
|
8
|
+
# Include any helpers you want to be available across all components
|
|
9
|
+
include Phlex::Rails::Helpers::Routes
|
|
10
|
+
|
|
11
|
+
if Rails.env.development?
|
|
12
|
+
#: -> void
|
|
13
|
+
def before_template
|
|
14
|
+
newline
|
|
15
|
+
comment { "Before #{self.class.name}" }
|
|
16
|
+
newline
|
|
17
|
+
super
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
#: -> void
|
|
21
|
+
def after_template
|
|
22
|
+
newline
|
|
23
|
+
comment { "After #{self.class.name}" }
|
|
24
|
+
newline
|
|
25
|
+
super
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|