katalyst-html-attributes 1.0.1 → 1.1.1
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/README.md +46 -0
- data/lib/katalyst/html_attributes.rb +44 -8
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22dc61eb82f19345cd8a5b4e2929cb74b41578fe37950629f37219cd85e958ab
|
4
|
+
data.tar.gz: 72168e12c5d010ec54233e5fb89933c77ebef08fc121c6a559feb0e2462f4cad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1db494a3f7b7d503455df70b2dfb3ee593c2954e4b04d2fea91dfd919ebdb8582239b1b0366c4891e64719b44b0a68777083fa3ea4e0899bb8a9b725c91d62e
|
7
|
+
data.tar.gz: 781b25b38f0d78c0ddb7e517663feba486ca6ad3e15eab41eefaef3f252cdf370344309376ddefa9c12cb52bd659dc53950ab89c88c90353c014bd7693e20f85
|
data/README.md
CHANGED
@@ -30,3 +30,49 @@ class MyViewComponent < ViewComponent::Base
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
```
|
33
|
+
|
34
|
+
You can also define your own named attributes:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class MyViewComponent < ViewComponent::Base
|
38
|
+
include Katalyst::HtmlAttributes
|
39
|
+
|
40
|
+
define_html_attributes :link_attributes, :button_attributes
|
41
|
+
|
42
|
+
def initialize(link:, button:, **html_attributes)
|
43
|
+
super(**html_attributes)
|
44
|
+
|
45
|
+
update_link_attributes(**link) if link
|
46
|
+
update_button_attributes(**button) if button
|
47
|
+
end
|
48
|
+
|
49
|
+
def call
|
50
|
+
tag.div(**html_attributes) do
|
51
|
+
tag.a(**link_attributes) do
|
52
|
+
"Link"
|
53
|
+
end +
|
54
|
+
tag.button(**button_attributes) do
|
55
|
+
"Button"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def default_html_attributes
|
61
|
+
{
|
62
|
+
class: "my-class"
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def default_link_attributes
|
67
|
+
{
|
68
|
+
class: "my-link"
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
def default_button_attributes
|
73
|
+
{
|
74
|
+
class: "my-button"
|
75
|
+
}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
@@ -1,24 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "active_support"
|
4
|
+
require "active_support/rails"
|
4
5
|
|
5
6
|
module Katalyst
|
6
7
|
module HtmlAttributes
|
8
|
+
extend ActiveSupport::Autoload
|
7
9
|
extend ActiveSupport::Concern
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
+
autoload :HasHtmlAttributes
|
12
|
+
|
13
|
+
using HasHtmlAttributes
|
11
14
|
|
15
|
+
class_methods do
|
12
16
|
def define_html_attribute_methods(name, default: {})
|
13
|
-
|
14
|
-
|
17
|
+
ivar = :"@#{name}"
|
18
|
+
default_method = :"default_#{name}"
|
19
|
+
define_method(default_method) { default.deep_dup }
|
20
|
+
private(default_method)
|
15
21
|
|
16
22
|
define_method(name) do
|
17
|
-
|
23
|
+
html_attributes_get(ivar, default_method)
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method(:"#{name}=") do |options|
|
27
|
+
html_attributes_set(ivar, options)
|
18
28
|
end
|
19
29
|
|
20
|
-
define_method("#{name}
|
21
|
-
|
30
|
+
define_method(:"update_#{name}") do |**options, &block|
|
31
|
+
html_attributes_update(ivar, options, &block)
|
22
32
|
end
|
23
33
|
end
|
24
34
|
end
|
@@ -32,5 +42,31 @@ module Katalyst
|
|
32
42
|
|
33
43
|
self.html_attributes = options
|
34
44
|
end
|
45
|
+
|
46
|
+
def options_to_html_attributes(options)
|
47
|
+
options.slice(:id, :aria, :class, :data).merge_html(options.fetch(:html, {}))
|
48
|
+
end
|
49
|
+
module_function(:options_to_html_attributes)
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def html_attributes_get(ivar, default)
|
54
|
+
if instance_variable_defined?(ivar)
|
55
|
+
send(default).merge_html(instance_variable_get(ivar))
|
56
|
+
else
|
57
|
+
send(default)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def html_attributes_set(ivar, options)
|
62
|
+
instance_variable_set(ivar, options_to_html_attributes(options))
|
63
|
+
end
|
64
|
+
|
65
|
+
def html_attributes_update(ivar, options, &block)
|
66
|
+
attributes = instance_variable_get(ivar) || {}
|
67
|
+
attributes = attributes.merge_html(options_to_html_attributes(options))
|
68
|
+
attributes = yield(attributes) if block
|
69
|
+
instance_variable_set(ivar, attributes)
|
70
|
+
end
|
35
71
|
end
|
36
72
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: katalyst-html-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Katalyst Interactive
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
|
-
rubygems_version: 3.
|
71
|
+
rubygems_version: 3.5.11
|
72
72
|
signing_key:
|
73
73
|
specification_version: 4
|
74
74
|
summary: HTML Attributes utilities for use with ViewComponents
|