katalyst-html-attributes 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c34f229d9784bc7a7a2e3294863b74545363475502ebd8fd596a92065f5ffa59
4
+ data.tar.gz: 0c3fbe888484371b8311364d9975f0d0665ed794b47c37507231a3a9b7f771b6
5
+ SHA512:
6
+ metadata.gz: 1c14e664a778f6b53489f8ffd6b2dd5fbe3246acff220d72dc1c77ea4a9c8227e4f21d952ee361dc6682d028d2346662f28e48f4c9addf9a83ecfdcb2c0ca00e
7
+ data.tar.gz: bd3db38303941162806e219224c880f0656590e001c9f95d4d3a91a4f76c85d0997ae6e282b27e0a28d2571ae15ef4b9636c60e683df0f063a7337abbbeed659
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # HTML Attributes Utilities
2
+
3
+ This is a small library intended to make it easier to deal with HTML
4
+ attributes in View Components. It is based on
5
+ https://github.com/x-govuk/html-attributes-utils
6
+
7
+ It uses refinements for `Hash` that allow:
8
+
9
+ * deep merging while protecting default values from being overwritten
10
+ * tidying hashes by removing key/value pairs that have empty or nil values
11
+
12
+ ## Example use
13
+
14
+ ```ruby
15
+ class MyViewComponent < ViewComponent::Base
16
+ include Katalyst::HtmlAttributes
17
+
18
+ def initialize(**html_attributes)
19
+ super
20
+ end
21
+
22
+ def call
23
+ tag.div(**html_attributes)
24
+ end
25
+
26
+ def default_html_attributes
27
+ {
28
+ class: "my-class"
29
+ }
30
+ end
31
+ end
32
+ ```
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "html_attributes_utils"
4
+
5
+ module Katalyst
6
+ module HtmlAttributes
7
+ # Adds HTML attributes to a component.
8
+ # Accepts HTML attributes from the constructor or via `html_attributes=`.
9
+ # These are merged with the default attributes defined in the component.
10
+ # Adds support for custom html attributes for other tags, e.g.:
11
+ # define_html_attribute_methods :table_attributes, default: {}
12
+ # tag.table(**table_attributes)
13
+ module HasHtmlAttributes
14
+ extend ActiveSupport::Concern
15
+
16
+ using HTMLAttributesUtils
17
+
18
+ MERGEABLE_ATTRIBUTES = [
19
+ *HTMLAttributesUtils::DEFAULT_MERGEABLE_ATTRIBUTES,
20
+ %i[data controller],
21
+ %i[data action],
22
+ ].freeze
23
+
24
+ FLATTENABLE_ATTRIBUTES = [
25
+ %i[data controller],
26
+ %i[data action],
27
+ ].freeze
28
+
29
+ refine NilClass do
30
+ def flatten_html(*)
31
+ self
32
+ end
33
+ end
34
+
35
+ refine Hash do
36
+ def merge_html(attributes)
37
+ result = deep_merge_html_attributes(attributes, mergeable_attributes: MERGEABLE_ATTRIBUTES)
38
+ FLATTENABLE_ATTRIBUTES.each_with_object(result) do |path, flattened|
39
+ flattened.flatten_html(*path)
40
+ end
41
+ end
42
+
43
+ def flatten_html(key, *path)
44
+ if path.empty?
45
+ self[key] = self[key].join(" ") if self[key].is_a?(Array)
46
+ else
47
+ self[key].flatten_html(*path)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "html_attributes/has_html_attributes"
4
+
5
+ module Katalyst
6
+ module HtmlAttributes
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ using HasHtmlAttributes
11
+
12
+ def define_html_attribute_methods(name, default: {})
13
+ define_method("default_#{name}") { default }
14
+ private("default_#{name}")
15
+
16
+ define_method(name) do
17
+ send("default_#{name}").merge_html(instance_variable_get("@#{name}") || {})
18
+ end
19
+
20
+ define_method("#{name}=") do |options|
21
+ instance_variable_set("@#{name}", options.slice(:id, :aria, :class, :data).merge(options.fetch(:html, {})))
22
+ end
23
+ end
24
+ end
25
+
26
+ included do
27
+ define_html_attribute_methods :html_attributes, default: {}
28
+ end
29
+
30
+ def initialize(**options)
31
+ super(**options.except(:id, :aria, :class, :data, :html))
32
+
33
+ self.html_attributes = options
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: katalyst-html-attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Katalyst Interactive
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: html-attributes-utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - developers@katalyst.com.au
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/katalyst/html_attributes.rb
50
+ - lib/katalyst/html_attributes/has_html_attributes.rb
51
+ homepage: https://github.com/katalyst/html-attributes
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ rubygems_mfa_required: 'true'
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '3.2'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.4.20
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: HTML Attributes utilities for use with ViewComponents
75
+ test_files: []