vident-view_component 0.13.1 → 1.0.0.alpha2

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.
@@ -1,7 +1,126 @@
1
1
  module Vident
2
2
  module ViewComponent
3
- class Base < Core
3
+ class Base < ::ViewComponent::Base
4
4
  include ::Vident::Component
5
+
6
+ class << self
7
+ def cache_component_modified_time
8
+ cache_sidecar_view_modified_time + cache_rb_component_modified_time
9
+ end
10
+
11
+ def cache_sidecar_view_modified_time
12
+ ::File.exist?(template_path) ? ::File.mtime(template_path).to_i.to_s : ""
13
+ end
14
+
15
+ def cache_rb_component_modified_time
16
+ ::File.exist?(component_path) ? ::File.mtime(component_path).to_i.to_s : ""
17
+ end
18
+
19
+ def template_path
20
+ # Check for common ViewComponent template extensions in order of preference
21
+ extensions = [".html.erb", ".erb", ".html.haml", ".haml", ".html.slim", ".slim"]
22
+ base_path = Rails.root.join(components_base_path, virtual_path)
23
+
24
+ extensions.each do |ext|
25
+ potential_path = "#{base_path}#{ext}"
26
+ return potential_path if File.exist?(potential_path)
27
+ end
28
+
29
+ # Return the default .html.erb path if no template is found
30
+ Rails.root.join(components_base_path, "#{virtual_path}.html.erb").to_s
31
+ end
32
+
33
+ def component_path
34
+ Rails.root.join(components_base_path, "#{virtual_path}.rb").to_s
35
+ end
36
+
37
+ def components_base_path
38
+ ::Rails.configuration.view_component.view_component_path || "app/components"
39
+ end
40
+ end
41
+
42
+ SELF_CLOSING_TAGS = Set[:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr].freeze
43
+
44
+ def root_element(&block)
45
+ tag_type = root_element_tag_type
46
+ child_content = view_context.capture(self, &block) if block_given? # Evaluate before generating the outer tag options to ensure DSL methods are executed
47
+ options = root_element_tag_options
48
+ if SELF_CLOSING_TAGS.include?(tag_type)
49
+ view_context.tag(tag_type, options)
50
+ else
51
+ view_context.content_tag(tag_type, child_content, options)
52
+ end
53
+ end
54
+
55
+ def as_stimulus_targets(...)
56
+ to_data_attribute_string(**stimulus_targets(...))
57
+ end
58
+
59
+ def as_stimulus_target(...)
60
+ to_data_attribute_string(**stimulus_target(...))
61
+ end
62
+
63
+ def as_stimulus_actions(...)
64
+ to_data_attribute_string(**stimulus_actions(...))
65
+ end
66
+
67
+ def as_stimulus_action(...)
68
+ to_data_attribute_string(**stimulus_action(...))
69
+ end
70
+
71
+ def as_stimulus_controllers(...)
72
+ to_data_attribute_string(**stimulus_controllers(...))
73
+ end
74
+
75
+ def as_stimulus_controller(...)
76
+ to_data_attribute_string(**stimulus_controller(...))
77
+ end
78
+
79
+ def as_stimulus_outlets(...)
80
+ to_data_attribute_string(**stimulus_outlets(...))
81
+ end
82
+
83
+ def as_stimulus_outlet(...)
84
+ to_data_attribute_string(**stimulus_outlet(...))
85
+ end
86
+
87
+ def as_stimulus_values(...)
88
+ to_data_attribute_string(**stimulus_values(...))
89
+ end
90
+
91
+ def as_stimulus_value(...)
92
+ to_data_attribute_string(**stimulus_value(...))
93
+ end
94
+
95
+ def as_stimulus_classes(...)
96
+ to_data_attribute_string(**stimulus_classes(...))
97
+ end
98
+
99
+ def as_stimulus_class(...)
100
+ to_data_attribute_string(**stimulus_class(...))
101
+ end
102
+
103
+ private
104
+
105
+ def generate_tag(tag_name, stimulus_data_attributes, options, &block)
106
+ options[:data] ||= {}
107
+ options[:data].merge!(stimulus_data_attributes)
108
+ view_context.content_tag(tag_name, options, &block)
109
+ end
110
+
111
+ def escape_attribute_name_for_html(name)
112
+ name.to_s.gsub(/[^a-zA-Z0-9\-_]/, "").tr("_", "-")
113
+ end
114
+
115
+ def escape_attribute_value_for_html(value)
116
+ value.to_s.gsub('"', "&quot;").gsub("'", "&#39;")
117
+ end
118
+
119
+ def to_data_attribute_string(**attributes)
120
+ attributes.map { |key, value| "data-#{escape_attribute_name_for_html(key)}=\"#{escape_attribute_value_for_html(value)}\"" }
121
+ .join(" ")
122
+ .html_safe
123
+ end
5
124
  end
6
125
  end
7
126
  end
@@ -1,7 +1,5 @@
1
1
  require "vident/view_component/version"
2
- require "vident/view_component/core"
3
2
  require "vident/view_component/base"
4
- require "vident/view_component/root_component"
5
3
  require "vident/view_component/engine" if defined?(Rails)
6
4
 
7
5
  module Vident
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vident-view_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.1
4
+ version: 1.0.0.alpha2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-06-26 00:00:00.000000000 Z
10
+ date: 2025-07-08 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: railties
@@ -55,34 +55,28 @@ dependencies:
55
55
  requirements:
56
56
  - - "~>"
57
57
  - !ruby/object:Gem::Version
58
- version: 0.13.1
58
+ version: 1.0.0.alpha2
59
59
  type: :runtime
60
60
  prerelease: false
61
61
  version_requirements: !ruby/object:Gem::Requirement
62
62
  requirements:
63
63
  - - "~>"
64
64
  - !ruby/object:Gem::Version
65
- version: 0.13.1
65
+ version: 1.0.0.alpha2
66
66
  - !ruby/object:Gem::Dependency
67
67
  name: view_component
68
68
  requirement: !ruby/object:Gem::Requirement
69
69
  requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: 2.74.1
73
- - - "<"
70
+ - - '='
74
71
  - !ruby/object:Gem::Version
75
- version: '4'
72
+ version: 4.0.0.rc2
76
73
  type: :runtime
77
74
  prerelease: false
78
75
  version_requirements: !ruby/object:Gem::Requirement
79
76
  requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: 2.74.1
83
- - - "<"
77
+ - - '='
84
78
  - !ruby/object:Gem::Version
85
- version: '4'
79
+ version: 4.0.0.rc2
86
80
  description: Vident with ViewComponent
87
81
  email:
88
82
  - stevegeek@gmail.com
@@ -95,9 +89,7 @@ files:
95
89
  - README.md
96
90
  - lib/vident/view_component.rb
97
91
  - lib/vident/view_component/base.rb
98
- - lib/vident/view_component/core.rb
99
92
  - lib/vident/view_component/engine.rb
100
- - lib/vident/view_component/root_component.rb
101
93
  - lib/vident/view_component/version.rb
102
94
  homepage: https://github.com/stevegeek/vident
103
95
  licenses:
@@ -1,37 +0,0 @@
1
- module Vident
2
- module ViewComponent
3
- class Core < ::ViewComponent::Base
4
- class << self
5
- def current_component_modified_time
6
- sidecar_view_modified_time + rb_component_modified_time
7
- end
8
-
9
- def sidecar_view_modified_time
10
- ::File.exist?(template_path) ? ::File.mtime(template_path).to_i.to_s : ""
11
- end
12
-
13
- def rb_component_modified_time
14
- ::File.exist?(component_path) ? ::File.mtime(component_path).to_i.to_s : ""
15
- end
16
-
17
- def template_path
18
- File.join components_base_path, "#{virtual_path}.html.erb"
19
- end
20
-
21
- def component_path
22
- File.join components_base_path, "#{virtual_path}.rb"
23
- end
24
-
25
- def components_base_path
26
- ::Rails.configuration.view_component.view_component_path || "app/components"
27
- end
28
- end
29
-
30
- # Helper to create the main element
31
- def parent_element
32
- @parent_element ||= ::Vident::ViewComponent::RootComponent.new(**stimulus_options_for_root_component)
33
- end
34
- alias_method :root, :parent_element
35
- end
36
- end
37
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Vident
4
- module ViewComponent
5
- class RootComponent < ::ViewComponent::Base
6
- include ::Vident::RootComponent
7
-
8
- if Gem.loaded_specs.has_key? "better_html"
9
- begin
10
- include ::Vident::BetterHtml::RootComponent
11
- rescue
12
- raise "if `better_html`` is being used you must install `vident-better_html"
13
- end
14
- end
15
-
16
- SELF_CLOSING_TAGS = Set[:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr].freeze
17
-
18
- def target_tag(tag_name, targets, **options, &block)
19
- parsed = parse_targets(Array.wrap(targets))
20
- options[:data] ||= {}
21
- options[:data].merge!(build_target_data_attributes(parsed))
22
- content = view_context.capture(&block) if block
23
- view_context.content_tag(tag_name, content, options)
24
- end
25
-
26
- def call
27
- # Generate outer tag options and render
28
- tag_type = content_tag_type
29
- child_content = content # Evaluate before generating the outer tag options to ensure DSL methods are executed
30
- options = content_tag_options
31
- if SELF_CLOSING_TAGS.include?(tag_type)
32
- view_context.tag(tag_type, options)
33
- else
34
- view_context.content_tag(tag_type, child_content, options)
35
- end
36
- end
37
-
38
- private
39
-
40
- def content_tag_options
41
- options = @html_options&.dup || {}
42
- data_attrs = tag_data_attributes
43
- options[:data] = options[:data].present? ? data_attrs.merge(options[:data]) : data_attrs
44
- return options unless @id
45
- options.merge(id: @id)
46
- end
47
-
48
- def content_tag_type
49
- @element_tag.presence || :div
50
- end
51
- end
52
- end
53
- end