forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.rb
More file actions
186 lines (162 loc) · 5.17 KB
/
Copy pathstack.rb
File metadata and controls
186 lines (162 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true
module Primer
module Alpha
# Stack is a layout component that creates responsive horizontal and vertical flows.
class Stack < Primer::Component
DEFAULT_TAG = :div
# Stack's justify argument. Used internally.
class JustifyArg < Primer::ResponsiveArg
attr_reader :values
DEFAULT = :start
MAPPING = {
DEFAULT => "start",
:center => "center",
:end => "end",
:space_between => "space-between",
:space_evenly => "space-evenly"
}.freeze
OPTIONS = [nil, *MAPPING.keys.freeze]
def initialize(values)
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT) do |value|
MAPPING[value]
end
end
def self.arg_name
:justify
end
end
# Stack's direction argument. Used internally.
class DirectionArg < Primer::ResponsiveArg
attr_reader :values
DEFAULT = :vertical
OPTIONS = [
nil,
DEFAULT,
:horizontal
].freeze
def initialize(values)
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT)
end
def self.arg_name
:direction
end
end
# Stack's align argument. Used internally.
class AlignArg < Primer::ResponsiveArg
attr_reader :values
DEFAULT = :stretch
OPTIONS = [
nil,
DEFAULT,
:start,
:center,
:end,
:baseline
].freeze
def initialize(values)
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT)
end
def self.arg_name
:align
end
end
# Stack's wrap argument. Used internally.
class WrapArg < Primer::ResponsiveArg
attr_reader :values
DEFAULT = :nowrap
OPTIONS = [
nil,
DEFAULT,
:wrap,
:reverse
].freeze
def initialize(values)
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT)
end
def self.arg_name
:wrap
end
end
# Stack's padding argument. Used internally.
class PaddingArg < Primer::ResponsiveArg
attr_reader :values
DEFAULT = :none
OPTIONS = [
nil,
DEFAULT,
:condensed,
:normal,
:spacious
].freeze
def initialize(values)
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT)
end
def self.arg_name
:padding
end
end
# Stack's gap argument. Used internally.
class GapArg < Primer::ResponsiveArg
attr_reader :values
DEFAULT = nil
OPTIONS = [
DEFAULT,
:condensed,
:normal,
:spacious
].freeze
def initialize(values)
@values = fetch_or_fallback_all(OPTIONS, values, DEFAULT)
end
def self.arg_name
:gap
end
end
ARG_CLASSES = [
JustifyArg,
DirectionArg,
AlignArg,
WrapArg,
PaddingArg,
GapArg
].freeze
# @param tag [Symbol] Customize the element type of the rendered container.
# @param gap [Symbol] Specify the gap between children elements in the stack. <%= one_of(Primer::Alpha::Stack::GapArg::OPTIONS) %>
# @param direction [Symbol] Specify the direction for the stack container. <%= one_of(Primer::Alpha::Stack::DirectionArg::OPTIONS) %>
# @param align [Symbol] Specify the alignment between items in the cross-axis of the direction. <%= one_of(Primer::Alpha::Stack::AlignArg::OPTIONS) %>
# @param wrap [Symbol] Specify whether items are forced onto one line or can wrap onto multiple lines. <%= one_of(Primer::Alpha::Stack::WrapArg::OPTIONS) %>
# @param justify [Symbol] Specify how items will be distributed in the stacking direction. <%= one_of(Primer::Alpha::Stack::JustifyArg::OPTIONS) %>
# @param padding [Symbol] Specify the padding of the stack container. <%= one_of(Primer::Alpha::Stack::PaddingArg::OPTIONS) %>
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(
tag: DEFAULT_TAG,
justify: JustifyArg::DEFAULT,
gap: GapArg::DEFAULT,
direction: DirectionArg::DEFAULT,
align: AlignArg::DEFAULT,
wrap: WrapArg::DEFAULT,
padding: PaddingArg::DEFAULT,
**system_arguments
)
@system_arguments = system_arguments
@system_arguments[:tag] = tag
@system_arguments[:classes] = class_names(
@system_arguments.delete(:classes),
"Stack"
)
@system_arguments[:data] = merge_data(
@system_arguments, {
data: {
**JustifyArg.for(justify).to_data_attributes,
**GapArg.for(gap).to_data_attributes,
**DirectionArg.for(direction).to_data_attributes,
**AlignArg.for(align).to_data_attributes,
**WrapArg.for(wrap).to_data_attributes,
**PaddingArg.for(padding).to_data_attributes,
}
}
)
end
end
end
end