vident 0.10.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a56c925065ab67c7c41493cd5ab9a1c19fedde171ebc2fcc60477359318ef258
4
- data.tar.gz: 595d0ae7292a2548aece9749cbd7dc4e8f177faa6ea49aebfe90a1864c0014ce
3
+ metadata.gz: 503019b8aac7501339acff5c350afbca9abb55599a696f348321554da15183c0
4
+ data.tar.gz: 175ffd2794c1b7c81bf879a8bb3c9f65c42fa16cc92748be77df255c0b5fdcff
5
5
  SHA512:
6
- metadata.gz: c0ee94c716fee4aa83d496beac7f2d7053e82742006ca1483dbd6ba38ba5ec32fc6f8c8b9f0ca691b05ff2c42947f52aa7937a35c5aef6cfd1bacc6454a7d90c
7
- data.tar.gz: 399b0db4d117ee191f871eb5bb23d5419065ca083a58ba778e9a871839e9de1950837868b4b4043a8d72b7e97f18138678f41ef12813905543bc4c4a1619428a
6
+ metadata.gz: 0c76ae38a7c66032e99b130e21d67ba52f6c9fc946e3476fc945b682e5916ad406f90c12e0db2ab189ddf961e8c4f1a6e2cd64fc7e79bd6464ea5ccb1387e0de
7
+ data.tar.gz: f25903b4f38418a6752f33190713f0d8c9b3f5c2e251fb83476a35b2894df4dc848a3e104c13cf1d10ad448e774f924194020781f23b2c6d9732b061e0351e34
data/CHANGELOG.md CHANGED
@@ -14,6 +14,25 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
14
14
 
15
15
  ### Fixed
16
16
 
17
+
18
+ ## [0.12.0] - 2024-02-25
19
+
20
+ ### Added
21
+
22
+ - `outlet` DSL methods updated so that the selector is scoped to the component's root element by default. This
23
+ is probably the most common use case, and it's now the default.
24
+ - `with_outlets` DSL method added to generate the data-* attributes for the outlets and return as a fragment
25
+ of HTML
26
+
27
+
28
+ ## [0.11.0] - 2024-02-21
29
+
30
+ ### Added
31
+
32
+ - `outlet_host` DSL method to support components hooking themselves into a host component's outlets
33
+
34
+
35
+
17
36
  ## [0.10.1] - 2024-02-21
18
37
 
19
38
  ### Added
data/lib/vident/base.rb CHANGED
@@ -151,6 +151,7 @@ module Vident
151
151
  actions: attribute(:actions) + Array.wrap(options[:actions]),
152
152
  targets: attribute(:targets) + Array.wrap(options[:targets]),
153
153
  outlets: attribute(:outlets) + Array.wrap(options[:outlets]),
154
+ outlet_host: attribute(:outlet_host),
154
155
  named_classes: merge_stimulus_option(options, :named_classes),
155
156
  data_maps: prepare_stimulus_option(options, :data_maps)
156
157
  }
@@ -17,6 +17,7 @@ module Vident
17
17
  attribute :actions, default: [], delegates: false
18
18
  attribute :targets, default: [], delegates: false
19
19
  attribute :outlets, default: [], delegates: false
20
+ attribute :outlet_host, delegates: false
20
21
  attribute :data_maps, default: [], delegates: false
21
22
  attribute :named_classes, delegates: false
22
23
  end
@@ -7,6 +7,7 @@ module Vident
7
7
  actions: nil,
8
8
  targets: nil,
9
9
  outlets: nil,
10
+ outlet_host: nil,
10
11
  named_classes: nil, # https://stimulus.hotwired.dev/reference/css-classes
11
12
  data_maps: nil,
12
13
  element_tag: nil,
@@ -23,6 +24,13 @@ module Vident
23
24
  @named_classes = named_classes
24
25
  @data_map_kvs = {}
25
26
  @data_maps = data_maps
27
+
28
+ outlet_host.connect_outlet(self) if outlet_host.respond_to?(:connect_outlet)
29
+ end
30
+
31
+ def connect_outlet(outlet)
32
+ @outlets ||= []
33
+ @outlets << outlet
26
34
  end
27
35
 
28
36
  # The view component's helpers for setting stimulus data-* attributes on this component.
@@ -61,6 +69,20 @@ module Vident
61
69
  build_target_data_attributes([target(name)])
62
70
  end
63
71
 
72
+ def build_outlet_selector(outlet_selector)
73
+ prefix = @id ? "##{@id} " : ""
74
+ "#{prefix}[data-controller~=#{outlet_selector}]"
75
+ end
76
+
77
+ def outlet(css_selector: nil)
78
+ controller = implied_controller_name
79
+ if css_selector.nil?
80
+ [controller, build_outlet_selector(controller)]
81
+ else
82
+ [controller, css_selector]
83
+ end
84
+ end
85
+
64
86
  # Getter for a named classes list so can be used in view to set initial state on SSR
65
87
  # Returns a String of classes that can be used in a `class` attribute.
66
88
  def named_classes(*names)
@@ -87,6 +109,13 @@ module Vident
87
109
  end
88
110
  alias_method :with_action, :with_actions
89
111
 
112
+ # Return the HTML `data-` attribute for the given outlets
113
+ def with_outlets(*outlets)
114
+ attrs = build_outlet_data_attributes(outlets)
115
+ attrs.map { |dt, n| "data-#{dt}=\"#{n}\"" }.join(" ").html_safe
116
+ end
117
+ alias_method :with_outlet, :with_outlets
118
+
90
119
  private
91
120
 
92
121
  # An implicit Stimulus controller name is built from the implicit controller path
@@ -133,17 +162,29 @@ module Vident
133
162
 
134
163
  def outlet_list
135
164
  return {} unless @outlets&.size&.positive?
165
+ build_outlet_data_attributes(@outlets)
166
+ end
136
167
 
137
- @outlets.each_with_object({}) do |outlet_config, obj|
138
- identifier, css_selector = if outlet_config.is_a?(String)
139
- [outlet_config, "[data-controller~=#{outlet_config}]"]
140
- elsif outlet_config.is_a?(Array)
141
- outlet_config[..1]
142
- elsif respond_to?(:stimulus_identifier)
143
- [outlet_config.stimulus_identifier, "[data-controller~=#{outlet_config.stimulus_identifier}]"]
144
- else
145
- raise ArgumentError, "Invalid outlet config: #{outlet_config}"
146
- end
168
+ def parse_outlet(outlet_config)
169
+ if outlet_config.is_a?(String)
170
+ [outlet_config, build_outlet_selector(outlet_config)]
171
+ elsif outlet_config.is_a?(Symbol)
172
+ outlet_config = outlet_config.to_s.tr("_", "-")
173
+ [outlet_config, build_outlet_selector(outlet_config)]
174
+ elsif outlet_config.is_a?(Array)
175
+ outlet_config[..1]
176
+ elsif outlet_config.respond_to?(:stimulus_identifier) # Is a Component
177
+ [outlet_config.stimulus_identifier, build_outlet_selector(outlet_config.stimulus_identifier)]
178
+ elsif outlet_config.send(:implied_controller_name) # Is a RootComponent ?
179
+ [outlet_config.send(:implied_controller_name), build_outlet_selector(outlet_config.send(:implied_controller_name))]
180
+ else
181
+ raise ArgumentError, "Invalid outlet config: #{outlet_config}"
182
+ end
183
+ end
184
+
185
+ def build_outlet_data_attributes(outlets)
186
+ outlets.each_with_object({}) do |outlet_config, obj|
187
+ identifier, css_selector = parse_outlet(outlet_config)
147
188
  obj[:"#{implied_controller_name}-#{identifier}-outlet"] = css_selector
148
189
  end
149
190
  end
@@ -1,6 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "random/formatter"
3
+ begin
4
+ # Introduced in Ruby 3.1
5
+ require "random/formatter"
6
+ rescue LoadError
7
+ # to support Ruby 3.0
8
+ require "securerandom"
9
+ end
4
10
 
5
11
  module Vident
6
12
  class StableId
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vident
4
- VERSION = "0.10.1"
4
+ VERSION = "0.12.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vident
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-21 00:00:00.000000000 Z
11
+ date: 2024-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties