optics_view_components 0.1.1 → 0.1.2
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/.rubocop.yml +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +11 -1
- data/Rakefile +3 -1
- data/app/components/optics/application_view_component.rb +84 -80
- data/app/components/optics/application_view_component_preview.rb +4 -2
- data/app/components/optics/button/component.rb +4 -2
- data/app/components/optics/button/preview.rb +9 -9
- data/app/components/optics/icon/preview.rb +27 -25
- data/demo/Gemfile +0 -1
- data/demo/Gemfile.lock +1 -5
- data/demo/app/controllers/home_controller.rb +3 -2
- data/lib/optics/view_components/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a96cbd214ce4d3eb339e032c1c04ccc3435751f2618e5b41f9175114fd8e7de
|
4
|
+
data.tar.gz: 5d7edae41972ec1d189827dcdb71bdf0859cd26c7164f2de992ba282fc37df95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dc9e7910cdd85f698f9080704e2b16f5b8bc38e78509ddca578d7c8a4b35f9df810b06a04d6f19fe51471a81baaad88f9518a45d2091956c074117b042eaf04
|
7
|
+
data.tar.gz: fa393926ee1c68a2e14a6795f8b01c8a815aa0f324227e55df5df030e03afda1414c0cced29f4f632e56327ab5478e3925ff3315fecb3de8bc273f706a3e6ecb
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -14,7 +14,17 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
17
|
-
|
17
|
+
Add to `config/application.rb`:
|
18
|
+
|
19
|
+
```
|
20
|
+
require 'optics/view_components'
|
21
|
+
require 'optics/view_components/engine'
|
22
|
+
```
|
23
|
+
|
24
|
+
Use in a view:
|
25
|
+
```
|
26
|
+
<%= render(Optics::Button::Component.new(label: 'Hello')) %>
|
27
|
+
```
|
18
28
|
|
19
29
|
## Development
|
20
30
|
|
data/Rakefile
CHANGED
@@ -1,112 +1,116 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module Optics
|
4
|
+
class ApplicationViewComponent < ViewComponent::Base
|
5
|
+
class_attribute :optional_attributes, default: {}
|
6
|
+
class_attribute :required_attributes, default: {}
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
class << self
|
9
|
+
# To allow DB queries, put this in the class definition:
|
10
|
+
# self.allow_db_queries = true
|
11
|
+
attr_accessor :allow_db_queries
|
12
|
+
alias allow_db_queries? allow_db_queries
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
def requires(parameter, default: nil)
|
15
|
+
required_attributes[parameter] = default
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
attr_reader parameter
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
def accepts(parameter, default: nil)
|
21
|
+
optional_attributes[parameter] = default
|
21
22
|
|
22
|
-
|
23
|
+
attr_reader parameter
|
24
|
+
end
|
25
|
+
|
26
|
+
def inherited(subclass)
|
27
|
+
subclass.optional_attributes = optional_attributes.dup
|
28
|
+
subclass.required_attributes = required_attributes.dup
|
29
|
+
super
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
|
-
def
|
26
|
-
subclass.optional_attributes = optional_attributes.dup
|
27
|
-
subclass.required_attributes = required_attributes.dup
|
33
|
+
def initialize(**attributes)
|
28
34
|
super
|
29
|
-
end
|
30
|
-
end
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
+
initialize_instance_variables(required_attributes, attributes) do |key, _default|
|
37
|
+
raise ArgumentError, "Missing keyword: :#{key}"
|
38
|
+
end
|
36
39
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
initialize_instance_variables(optional_attributes, attributes) do |key, default|
|
41
|
+
instance_variable_set("@#{key}", default)
|
42
|
+
end
|
40
43
|
|
41
|
-
|
42
|
-
|
44
|
+
@attributes = attributes.except(*optional_attributes.keys, *required_attributes.keys)
|
45
|
+
end
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
+
def class_for(name)
|
48
|
+
"c--#{identifier}--#{name}"
|
49
|
+
end
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
+
def container_class
|
52
|
+
"c--#{identifier}"
|
53
|
+
end
|
51
54
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
+
def class_names_for(*args)
|
56
|
+
class_names(*args).map { |name| class_for(name) }.join(' ')
|
57
|
+
end
|
55
58
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
# Inspired by https://github.com/primer/view_components/blob/v0.1.9/app/lib/primer/class_name_helper.rb#L9
|
60
|
+
def class_names(*args) # rubocop:disable Metrics/MethodLength
|
61
|
+
[].tap do |classes|
|
62
|
+
args.each do |class_name|
|
63
|
+
case class_name
|
64
|
+
when String
|
65
|
+
classes << class_name
|
66
|
+
when Hash
|
67
|
+
class_name.each do |key, val|
|
68
|
+
classes << key if val
|
69
|
+
end
|
66
70
|
end
|
67
71
|
end
|
68
|
-
end
|
69
72
|
|
70
|
-
|
71
|
-
|
73
|
+
classes.compact!
|
74
|
+
classes.uniq!
|
75
|
+
end
|
72
76
|
end
|
73
|
-
end
|
74
77
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
78
|
+
# Used by Stimulus. E.g. data-controller=identifier => data-controller='dynamic-form--field'
|
79
|
+
def identifier
|
80
|
+
@identifier ||= self.class.name.sub('::Component', '').underscore.gsub('_', '-').gsub('/', '--')
|
81
|
+
end
|
79
82
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
83
|
+
# Helper for Stimulus data attributes
|
84
|
+
def data_for(method:, target:)
|
85
|
+
data_method(method).merge(data_target(target))
|
86
|
+
end
|
84
87
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
88
|
+
# Helper for Stimulus controller method
|
89
|
+
def data_method(method, event: 'click')
|
90
|
+
{ action: "#{event}->#{identifier}##{method}" }
|
91
|
+
end
|
89
92
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
93
|
+
# Helper for Stimulus controller target
|
94
|
+
def data_target(target)
|
95
|
+
{ "#{identifier}": { target: } }
|
96
|
+
end
|
94
97
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
98
|
+
# Helper for Stimulus controller value
|
99
|
+
def data_values(values)
|
100
|
+
values.transform_keys do |key|
|
101
|
+
"#{identifier}-#{key}-value"
|
102
|
+
end
|
99
103
|
end
|
100
|
-
end
|
101
104
|
|
102
|
-
|
105
|
+
private
|
103
106
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
107
|
+
def initialize_instance_variables(configured_attributes, attributes)
|
108
|
+
configured_attributes.each do |key, default|
|
109
|
+
if attributes.key?(key)
|
110
|
+
instance_variable_set("@#{key}", attributes[key])
|
111
|
+
else
|
112
|
+
yield(key, default)
|
113
|
+
end
|
110
114
|
end
|
111
115
|
end
|
112
116
|
end
|
@@ -11,14 +11,14 @@ module Optics
|
|
11
11
|
# @param variant select {{ ::Button::Component::STYLES }}
|
12
12
|
# @param url text
|
13
13
|
def default( # rubocop:disable Metrics/ParameterLists
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
border: true,
|
15
|
+
icon: false,
|
16
|
+
id: nil,
|
17
|
+
label: 'Default',
|
18
|
+
size: 'normal',
|
19
|
+
variant: 'default',
|
20
|
+
url: nil
|
21
|
+
)
|
22
22
|
render(Button::Component.new(
|
23
23
|
border:,
|
24
24
|
icon:,
|
@@ -26,7 +26,7 @@ module Optics
|
|
26
26
|
label:,
|
27
27
|
size:,
|
28
28
|
variant:,
|
29
|
-
url
|
29
|
+
url:
|
30
30
|
))
|
31
31
|
end
|
32
32
|
end
|
@@ -1,31 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
render(
|
20
|
-
Icon::Component.new(
|
21
|
-
emphasis:,
|
22
|
-
filled:,
|
23
|
-
name:,
|
24
|
-
size:,
|
25
|
-
title:,
|
26
|
-
weight:
|
27
|
-
)
|
3
|
+
module Optics
|
4
|
+
module Icon
|
5
|
+
class Preview < ViewComponent::Preview
|
6
|
+
# @param emphasis select {{ ::Icon::Component::EMPHASES }}
|
7
|
+
# @param filled toggle
|
8
|
+
# @param name
|
9
|
+
# @param size select {{ ::Icon::Component::SIZES }}
|
10
|
+
# @param title
|
11
|
+
# @param weight select {{ ::Icon::Component::WEIGHTS }}
|
12
|
+
def default( # rubocop:disable Metrics/ParameterLists
|
13
|
+
emphasis: 'normal',
|
14
|
+
filled: false,
|
15
|
+
name: 'settings',
|
16
|
+
size: 'normal',
|
17
|
+
title: nil,
|
18
|
+
weight: 'normal'
|
28
19
|
)
|
20
|
+
render(
|
21
|
+
Icon::Component.new(
|
22
|
+
emphasis:,
|
23
|
+
filled:,
|
24
|
+
name:,
|
25
|
+
size:,
|
26
|
+
title:,
|
27
|
+
weight:
|
28
|
+
)
|
29
|
+
)
|
30
|
+
end
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
data/demo/Gemfile
CHANGED
data/demo/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
optics_view_components (0.1.
|
4
|
+
optics_view_components (0.1.1)
|
5
5
|
view_component (> 2.0, < 4.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -89,9 +89,6 @@ GEM
|
|
89
89
|
activesupport (>= 5.0)
|
90
90
|
i18n (1.13.0)
|
91
91
|
concurrent-ruby (~> 1.0)
|
92
|
-
importmap-rails (1.1.6)
|
93
|
-
actionpack (>= 6.0.0)
|
94
|
-
railties (>= 6.0.0)
|
95
92
|
io-console (0.6.0)
|
96
93
|
irb (1.6.4)
|
97
94
|
reline (>= 0.3.0)
|
@@ -187,7 +184,6 @@ DEPENDENCIES
|
|
187
184
|
bootsnap
|
188
185
|
cssbundling-rails (~> 1.1)
|
189
186
|
debug
|
190
|
-
importmap-rails
|
191
187
|
optics_view_components!
|
192
188
|
puma (~> 6.0)
|
193
189
|
rails (~> 7.0.4, >= 7.0.4.3)
|