ariadne_view_components 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/ariadne-form-with.d.ts +20 -0
  3. data/app/assets/javascripts/ariadne-form.d.ts +22 -0
  4. data/app/assets/javascripts/ariadne.d.ts +1 -1
  5. data/app/assets/javascripts/ariadne_view_components.js +7 -1
  6. data/app/assets/javascripts/ariadne_view_components.js.map +1 -1
  7. data/app/assets/javascripts/comment-component.d.ts +13 -0
  8. data/app/assets/javascripts/rich-text-area-component.d.ts +4 -0
  9. data/app/{components/ariadne/time_ago_component.d.ts → assets/javascripts/time-ago-component.d.ts} +0 -0
  10. data/app/assets/stylesheets/application.ariadne_view_components.css +1 -0
  11. data/app/assets/stylesheets/prosemirror.css +323 -0
  12. data/app/components/ariadne/ariadne-form.ts +96 -0
  13. data/app/components/ariadne/ariadne.ts +8 -1
  14. data/app/components/ariadne/base_button.rb +6 -7
  15. data/app/components/ariadne/base_component.rb +13 -131
  16. data/app/components/ariadne/blankslate_component.html.erb +5 -5
  17. data/app/components/ariadne/blankslate_component.rb +4 -9
  18. data/app/components/ariadne/body_component.rb +1 -1
  19. data/app/components/ariadne/button_component.rb +7 -6
  20. data/app/components/ariadne/clipboard_copy_component.html.erb +3 -2
  21. data/app/components/ariadne/comment-component.ts +55 -0
  22. data/app/components/ariadne/comment_component.html.erb +17 -20
  23. data/app/components/ariadne/comment_component.rb +29 -17
  24. data/app/components/ariadne/component.rb +4 -4
  25. data/app/components/ariadne/container_component.rb +1 -1
  26. data/app/components/ariadne/counter_component.rb +4 -4
  27. data/app/components/ariadne/flash_component.html.erb +12 -12
  28. data/app/components/ariadne/flash_component.rb +16 -16
  29. data/app/components/ariadne/flex_component.rb +5 -7
  30. data/app/components/ariadne/footer_component.html.erb +1 -1
  31. data/app/components/ariadne/footer_component.rb +1 -1
  32. data/app/components/ariadne/grid_component.html.erb +4 -4
  33. data/app/components/ariadne/grid_component.rb +9 -9
  34. data/app/components/ariadne/header_component.html.erb +7 -7
  35. data/app/components/ariadne/header_component.rb +8 -8
  36. data/app/components/ariadne/heading_component.rb +3 -3
  37. data/app/components/ariadne/heroicon_component.rb +4 -4
  38. data/app/components/ariadne/inline_flex_component.rb +7 -7
  39. data/app/components/ariadne/link_component.rb +2 -2
  40. data/app/components/ariadne/list_component.rb +6 -5
  41. data/app/components/ariadne/narrow_container_component.html.erb +3 -0
  42. data/app/components/ariadne/narrow_container_component.rb +30 -0
  43. data/app/components/ariadne/panel_bar_component.html.erb +20 -0
  44. data/app/components/ariadne/panel_bar_component.rb +79 -0
  45. data/app/components/ariadne/pill_component.rb +2 -2
  46. data/app/components/ariadne/rich-text-area-component.ts +32 -0
  47. data/app/components/ariadne/rich_text_area_component.html.erb +6 -0
  48. data/app/components/ariadne/rich_text_area_component.rb +35 -0
  49. data/app/components/ariadne/slideover-component.ts +3 -3
  50. data/app/components/ariadne/slideover_component.html.erb +3 -3
  51. data/app/components/ariadne/slideover_component.rb +1 -1
  52. data/app/components/ariadne/tab_bar_component.html.erb +3 -0
  53. data/app/components/ariadne/tab_bar_component.rb +45 -0
  54. data/app/components/ariadne/tab_component.html.erb +7 -0
  55. data/app/components/ariadne/tab_component.rb +43 -0
  56. data/app/components/ariadne/{time_ago_component.ts → time-ago-component.ts} +0 -0
  57. data/app/components/ariadne/time_ago_component.rb +2 -2
  58. data/app/components/ariadne/timeline_component.html.erb +6 -6
  59. data/app/components/ariadne/tooltip-component.ts +3 -3
  60. data/app/components/ariadne/tooltip_component.html.erb +1 -1
  61. data/app/components/ariadne/tooltip_component.rb +1 -1
  62. data/app/lib/ariadne/action_view_extensions/form_helper.rb +4 -1
  63. data/app/lib/ariadne/fetch_or_fallback_helper.rb +3 -1
  64. data/app/lib/ariadne/form_builder.rb +10 -10
  65. data/app/lib/ariadne/icon_helper.rb +1 -1
  66. data/lib/ariadne/view_components/engine.rb +169 -3
  67. data/lib/ariadne/view_components/version.rb +1 -1
  68. data/lib/ariadne/view_components.rb +0 -1
  69. data/lib/rubocop/config/default.yml +0 -6
  70. data/lib/rubocop/cop/ariadne/no_tag_memoize.rb +1 -0
  71. data/lib/tasks/docs.rake +6 -1
  72. data/static/arguments.yml +99 -4
  73. data/static/audited_at.json +8 -2
  74. data/static/classes.yml +195 -181
  75. data/static/constants.json +112 -72
  76. data/static/statuses.json +8 -2
  77. metadata +42 -32
  78. data/app/assets/builds/ariadne_view_components.css +0 -1874
  79. data/app/components/ariadne/ariadne.d.ts +0 -1
  80. data/app/components/ariadne/ariadne.js +0 -9
  81. data/app/components/ariadne/clipboard-copy-component.d.ts +0 -4
  82. data/app/components/ariadne/clipboard-copy-component.js +0 -18
  83. data/app/components/ariadne/clipboard_copy_component.d.ts +0 -4
  84. data/app/components/ariadne/clipboard_copy_component.js +0 -18
  85. data/app/components/ariadne/slideover-component.d.ts +0 -9
  86. data/app/components/ariadne/slideover-component.js +0 -20
  87. data/app/components/ariadne/slideover_component.d.ts +0 -9
  88. data/app/components/ariadne/slideover_component.js +0 -19
  89. data/app/components/ariadne/time_ago_component.js +0 -1
  90. data/app/components/ariadne/tooltip-component.d.ts +0 -24
  91. data/app/components/ariadne/tooltip-component.js +0 -42
  92. data/lib/ariadne/classify/utilities.rb +0 -199
  93. data/lib/ariadne/classify/utilities.yml +0 -1817
  94. data/lib/ariadne/classify/validation.rb +0 -18
  95. data/lib/ariadne/classify.rb +0 -124
  96. data/lib/rubocop/cop/ariadne/ariadne_heroicon.rb +0 -252
  97. data/lib/rubocop/cop/ariadne/component_name_migration.rb +0 -35
  98. data/lib/rubocop/cop/ariadne/system_argument_instead_of_class.rb +0 -57
  99. data/lib/tasks/utilities.rake +0 -121
@@ -0,0 +1,13 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+ export default class CommentComponent extends Controller {
3
+ static targets: string[];
4
+ readonly commentComponentTarget: HTMLDivElement;
5
+ readonly tabBarComponentTarget: HTMLElement;
6
+ readonly tabTargets: [HTMLButtonElement];
7
+ SELECTED_TAB_CLASSES: string[];
8
+ PUBLIC_BACKGROUND_COLOR: string;
9
+ INTERNAL_BACKGROUND_COLOR: string;
10
+ connect(): void;
11
+ toggleTab(): void;
12
+ toggleBackgrounds(tab: HTMLButtonElement): void;
13
+ }
@@ -0,0 +1,4 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+ export default class RichTextArea extends Controller {
3
+ connect(): void;
4
+ }
@@ -3,3 +3,4 @@
3
3
  @import 'tailwindcss/utilities';
4
4
 
5
5
  @import 'tooltip-component.css';
6
+ @import 'prosemirror.css';
@@ -0,0 +1,323 @@
1
+ .ProseMirror {
2
+ position: relative;
3
+ }
4
+
5
+ .ProseMirror {
6
+ word-wrap: break-word;
7
+ white-space: pre-wrap;
8
+ -webkit-font-variant-ligatures: none;
9
+ font-variant-ligatures: none;
10
+ }
11
+
12
+ .ProseMirror pre {
13
+ white-space: pre-wrap;
14
+ }
15
+
16
+ .ProseMirror li {
17
+ position: relative;
18
+ }
19
+
20
+ .ProseMirror-hideselection *::selection {
21
+ background: transparent;
22
+ }
23
+ .ProseMirror-hideselection *::-moz-selection {
24
+ background: transparent;
25
+ }
26
+ .ProseMirror-hideselection {
27
+ caret-color: transparent;
28
+ }
29
+
30
+ .ProseMirror-selectednode {
31
+ outline: 2px solid #8cf;
32
+ }
33
+
34
+ /* Make sure li selections wrap around markers */
35
+
36
+ li.ProseMirror-selectednode {
37
+ outline: none;
38
+ }
39
+
40
+ li.ProseMirror-selectednode:after {
41
+ content: '';
42
+ position: absolute;
43
+ left: -32px;
44
+ right: -2px;
45
+ top: -2px;
46
+ bottom: -2px;
47
+ border: 2px solid #8cf;
48
+ pointer-events: none;
49
+ }
50
+ .ProseMirror-textblock-dropdown {
51
+ min-width: 3em;
52
+ }
53
+
54
+ .ProseMirror-menu {
55
+ margin: 0 -4px;
56
+ line-height: 1;
57
+ }
58
+
59
+ .ProseMirror-tooltip .ProseMirror-menu {
60
+ width: -webkit-fit-content;
61
+ width: fit-content;
62
+ white-space: pre;
63
+ }
64
+
65
+ .ProseMirror-menuitem {
66
+ margin-right: 3px;
67
+ display: inline-block;
68
+ }
69
+
70
+ .ProseMirror-menuseparator {
71
+ border-right: 1px solid #ddd;
72
+ margin-right: 3px;
73
+ }
74
+
75
+ .ProseMirror-menu-dropdown,
76
+ .ProseMirror-menu-dropdown-menu {
77
+ font-size: 90%;
78
+ white-space: nowrap;
79
+ }
80
+
81
+ .ProseMirror-menu-dropdown {
82
+ vertical-align: 1px;
83
+ cursor: pointer;
84
+ position: relative;
85
+ padding-right: 15px;
86
+ }
87
+
88
+ .ProseMirror-menu-dropdown-wrap {
89
+ padding: 1px 0 1px 4px;
90
+ display: inline-block;
91
+ position: relative;
92
+ }
93
+
94
+ .ProseMirror-menu-dropdown:after {
95
+ content: '';
96
+ border-left: 4px solid transparent;
97
+ border-right: 4px solid transparent;
98
+ border-top: 4px solid currentColor;
99
+ opacity: 0.6;
100
+ position: absolute;
101
+ right: 4px;
102
+ top: calc(50% - 2px);
103
+ }
104
+
105
+ .ProseMirror-menu-dropdown-menu,
106
+ .ProseMirror-menu-submenu {
107
+ position: absolute;
108
+ background: white;
109
+ color: #666;
110
+ border: 1px solid #aaa;
111
+ padding: 2px;
112
+ }
113
+
114
+ .ProseMirror-menu-dropdown-menu {
115
+ z-index: 15;
116
+ min-width: 6em;
117
+ }
118
+
119
+ .ProseMirror-menu-dropdown-item {
120
+ cursor: pointer;
121
+ padding: 2px 8px 2px 4px;
122
+ }
123
+
124
+ .ProseMirror-menu-dropdown-item:hover {
125
+ background: #f2f2f2;
126
+ }
127
+
128
+ .ProseMirror-menu-submenu-wrap {
129
+ position: relative;
130
+ margin-right: -4px;
131
+ }
132
+
133
+ .ProseMirror-menu-submenu-label:after {
134
+ content: '';
135
+ border-top: 4px solid transparent;
136
+ border-bottom: 4px solid transparent;
137
+ border-left: 4px solid currentColor;
138
+ opacity: 0.6;
139
+ position: absolute;
140
+ right: 4px;
141
+ top: calc(50% - 4px);
142
+ }
143
+
144
+ .ProseMirror-menu-submenu {
145
+ display: none;
146
+ min-width: 4em;
147
+ left: 100%;
148
+ top: -3px;
149
+ }
150
+
151
+ .ProseMirror-menu-active {
152
+ background: #eee;
153
+ border-radius: 4px;
154
+ }
155
+
156
+ .ProseMirror-menu-active {
157
+ background: #eee;
158
+ border-radius: 4px;
159
+ }
160
+
161
+ .ProseMirror-menu-disabled {
162
+ opacity: 0.3;
163
+ }
164
+
165
+ .ProseMirror-menu-submenu-wrap:hover .ProseMirror-menu-submenu,
166
+ .ProseMirror-menu-submenu-wrap-active .ProseMirror-menu-submenu {
167
+ display: block;
168
+ }
169
+
170
+ .ProseMirror-menubar {
171
+ border-top-left-radius: inherit;
172
+ border-top-right-radius: inherit;
173
+ position: relative;
174
+ min-height: 1em;
175
+ color: #666;
176
+ padding: 1px 6px;
177
+ top: 0;
178
+ left: 0;
179
+ right: 0;
180
+ border-bottom: 1px solid silver;
181
+ background: white;
182
+ z-index: 10;
183
+ -moz-box-sizing: border-box;
184
+ box-sizing: border-box;
185
+ overflow: visible;
186
+ }
187
+
188
+ .ProseMirror-icon {
189
+ display: inline-block;
190
+ line-height: 0.8;
191
+ vertical-align: -2px; /* Compensate for padding */
192
+ padding: 2px 8px;
193
+ cursor: pointer;
194
+ }
195
+
196
+ .ProseMirror-menu-disabled.ProseMirror-icon {
197
+ cursor: default;
198
+ }
199
+
200
+ .ProseMirror-icon svg {
201
+ fill: currentColor;
202
+ height: 1em;
203
+ }
204
+
205
+ .ProseMirror-icon span {
206
+ vertical-align: text-top;
207
+ }
208
+ /* Add space around the hr to make clicking it easier */
209
+
210
+ .ProseMirror-example-setup-style hr {
211
+ padding: 2px 10px;
212
+ border: none;
213
+ margin: 1em 0;
214
+ }
215
+
216
+ .ProseMirror-example-setup-style hr:after {
217
+ content: '';
218
+ display: block;
219
+ height: 1px;
220
+ background-color: silver;
221
+ line-height: 2px;
222
+ }
223
+
224
+ .ProseMirror ul,
225
+ .ProseMirror ol {
226
+ padding-left: 30px;
227
+ }
228
+
229
+ .ProseMirror blockquote {
230
+ padding-left: 1em;
231
+ border-left: 3px solid #eee;
232
+ margin-left: 0;
233
+ margin-right: 0;
234
+ }
235
+
236
+ .ProseMirror-example-setup-style img {
237
+ cursor: default;
238
+ }
239
+
240
+ .ProseMirror-prompt {
241
+ background: white;
242
+ padding: 5px 10px 5px 15px;
243
+ border: 1px solid silver;
244
+ position: fixed;
245
+ border-radius: 3px;
246
+ z-index: 11;
247
+ box-shadow: -0.5px 2px 5px rgba(0, 0, 0, 0.2);
248
+ }
249
+
250
+ .ProseMirror-prompt h5 {
251
+ margin: 0;
252
+ font-weight: normal;
253
+ font-size: 100%;
254
+ color: #444;
255
+ }
256
+
257
+ .ProseMirror-prompt input[type='text'],
258
+ .ProseMirror-prompt textarea {
259
+ background: #eee;
260
+ border: none;
261
+ outline: none;
262
+ }
263
+
264
+ .ProseMirror-prompt input[type='text'] {
265
+ padding: 0 4px;
266
+ }
267
+
268
+ .ProseMirror-prompt-close {
269
+ position: absolute;
270
+ left: 2px;
271
+ top: 1px;
272
+ color: #666;
273
+ border: none;
274
+ background: transparent;
275
+ padding: 0;
276
+ }
277
+
278
+ .ProseMirror-prompt-close:after {
279
+ content: '✕';
280
+ font-size: 12px;
281
+ }
282
+
283
+ .ProseMirror-invalid {
284
+ background: #ffc;
285
+ border: 1px solid #cc7;
286
+ border-radius: 4px;
287
+ padding: 5px 10px;
288
+ position: absolute;
289
+ min-width: 10em;
290
+ }
291
+
292
+ .ProseMirror-prompt-buttons {
293
+ margin-top: 5px;
294
+ display: none;
295
+ }
296
+ :root {
297
+ --validation-color: #3dcde6;
298
+ --validation-color-hover: #3dcde624;
299
+ --validation-gray: #a0a0a0;
300
+ --validation-debug-color-dirty: #f7a85e66;
301
+ --validation-debug-color-inflight: #f7a85ea1;
302
+ }
303
+
304
+ .Button {
305
+ padding: 1px 4px;
306
+ border: 1px solid lightgrey;
307
+ border-radius: 3px;
308
+ font-size: 16px;
309
+ cursor: pointer;
310
+ }
311
+
312
+ .Button:hover {
313
+ background-color: #eee;
314
+ }
315
+
316
+ .Input[type='checkbox'] {
317
+ margin-left: calc(var(--gutter-width));
318
+ }
319
+
320
+ .Button:active,
321
+ .Button:focus {
322
+ outline: none;
323
+ }
@@ -0,0 +1,96 @@
1
+ import {Controller} from '@hotwired/stimulus'
2
+ import {html} from 'lit-html'
3
+ import type {TemplateResult} from 'lit-html'
4
+
5
+ type HTMLFormField = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement
6
+
7
+ export default class AriadneFormWith extends Controller {
8
+ static targets = ['formField']
9
+ declare readonly formFieldTargets: [HTMLFormField]
10
+
11
+ connect() {
12
+ this.element.setAttribute('novalidate', 'true')
13
+ this.element.addEventListener('blur', this.onBlur, true)
14
+ this.element.addEventListener('submit', this.onSubmit)
15
+ this.element.addEventListener('ajax:beforeSend', this.onSubmit)
16
+ }
17
+
18
+ disconnect() {
19
+ this.element.removeEventListener('blur', this.onBlur)
20
+ this.element.removeEventListener('submit', this.onSubmit)
21
+ this.element.removeEventListener('ajax:beforeSend', this.onSubmit)
22
+ }
23
+
24
+ onBlur = (event: Event) => {
25
+ this.validateField(event.target as HTMLFormField)
26
+ }
27
+
28
+ onSubmit = (event: Event) => {
29
+ if (!this.validateForm()) {
30
+ event.preventDefault()
31
+ this.firstInvalidField?.focus()
32
+ }
33
+ }
34
+
35
+ validateForm() {
36
+ let isValid = true
37
+ // Not using `find` because we want to validate all the fields
38
+ for (const field of this.formFields) {
39
+ if (this.shouldValidateField(field) && !this.validateField(field)) isValid = false
40
+ }
41
+ return isValid
42
+ }
43
+
44
+ validateField(field: HTMLFormField) {
45
+ if (!this.shouldValidateField(field)) return true
46
+ const isValid = field.checkValidity()
47
+ field.classList.toggle('invalid', !isValid)
48
+ this.refreshErrorForInvalidField(field, isValid)
49
+ return isValid
50
+ }
51
+
52
+ shouldValidateField(field: HTMLFormField) {
53
+ return (
54
+ !field.disabled &&
55
+ !field.classList.contains('ProseMirror') &&
56
+ !['file', 'reset', 'submit', 'button'].includes(field.type)
57
+ )
58
+ }
59
+
60
+ refreshErrorForInvalidField(field: HTMLFormField, isValid: boolean) {
61
+ this.removeExistingErrorMessage(field)
62
+ if (!isValid) this.showErrorForInvalidField(field)
63
+ }
64
+
65
+ removeExistingErrorMessage(field: HTMLFormField) {
66
+ const fieldContainer = field.closest('.field')
67
+ if (!fieldContainer) return
68
+ const existingErrorMessageElement = fieldContainer.querySelector('.error')
69
+ if (existingErrorMessageElement) {
70
+ existingErrorMessageElement?.parentNode?.removeChild(existingErrorMessageElement)
71
+ }
72
+ }
73
+
74
+ showErrorForInvalidField(field: HTMLFormField) {
75
+ field.insertAdjacentHTML('afterend', this.buildFieldErrorHtml(field))
76
+ }
77
+
78
+ buildFieldErrorHtml(field: HTMLFormField) {
79
+ const data = html`<p class="error">${field.validationMessage}</p>`
80
+ return this.getRenderString(data)
81
+ }
82
+
83
+ get formFields(): HTMLFormField[] {
84
+ return Array.from(this.formFieldTargets)
85
+ }
86
+
87
+ get firstInvalidField() {
88
+ return this.formFields.find(field => !field.checkValidity())
89
+ }
90
+
91
+ getRenderString = (data: TemplateResult): string => {
92
+ const {strings, values} = data
93
+ const v = [...values, ''].map(e => (typeof e === 'object' ? this.getRenderString(e as TemplateResult) : e))
94
+ return strings.reduce((acc, s, i) => acc + s + v[i], '')
95
+ }
96
+ }
@@ -1,13 +1,20 @@
1
1
  import {Application} from '@hotwired/stimulus'
2
2
 
3
+ import AriadneForm from './ariadne-form'
4
+
3
5
  import ClipboardCopyComponent from './clipboard-copy-component'
6
+ import CommentComponent from './comment-component'
7
+ import RichTextAreaComponent from './rich-text-area-component'
4
8
  import SlideoverComponent from './slideover-component'
5
9
  import TooltipComponent from './tooltip-component'
6
10
 
7
- import './time_ago_component'
11
+ import './time-ago-component'
8
12
 
9
13
  const application = Application.start()
10
14
 
11
15
  application.register('clipboard-copy-component', ClipboardCopyComponent)
16
+ application.register('ariadne-form', AriadneForm)
17
+ application.register('comment-component', CommentComponent)
18
+ application.register('rich-text-area-component', RichTextAreaComponent)
12
19
  application.register('slideover-component', SlideoverComponent)
13
20
  application.register('tooltip-component', TooltipComponent)
@@ -10,17 +10,16 @@ module Ariadne
10
10
  TYPE_SUBMIT = :submit
11
11
  TYPE_OPTIONS = [DEFAULT_TYPE, :reset, TYPE_SUBMIT].freeze
12
12
 
13
- # TODO: dedupe the classes
14
13
  SIZE_CLASS_MAPPINGS = {
15
- xs: "inline-flex items-center px-2.5 py-1.5 text-xs font-medium rounded",
16
- sm: "inline-flex items-center px-3 py-2 text-sm leading-4 font-medium rounded-m",
17
- md: "inline-flex items-center px-4 py-2 text-sm font-medium rounded-md",
18
- lg: "inline-flex items-center px-4 py-2 text-base font-medium rounded-md",
19
- xl: "inline-flex items-center px-6 py-3 text-base font-medium rounded-md",
14
+ xs: "ariadne-px-2.5 ariadne-py-1.5 ariadne-text-xs ariadne-font-medium ariadne-rounded",
15
+ sm: "ariadne-px-3 ariadne-py-2 ariadne-text-sm ariadne-leading-4 ariadne-font-medium ariadne-rounded-m",
16
+ md: "ariadne-px-4 ariadne-py-2 ariadne-text-sm ariadne-font-medium ariadne-rounded-md",
17
+ lg: "ariadne-px-4 ariadne-py-2 ariadne-text-base ariadne-font-medium ariadne-rounded-md",
18
+ xl: "ariadne-px-6 ariadne-py-3 ariadne-text-base ariadne-font-medium ariadne-rounded-md",
20
19
  }.freeze
21
20
  VALID_SIZES = SIZE_CLASS_MAPPINGS.keys.freeze
22
21
 
23
- DEFAULT_CLASSES = "inline-flex items-center px-4 py-2 text-sm border border-gray-300 shadow-sm text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
22
+ DEFAULT_CLASSES = "ariadne-inline-flex ariadne-items-center ariadne-border ariadne-shadow-sm focus:ariadne-outline-none focus:ariadne-ring-2 focus:ariadne-ring-offset-2"
24
23
 
25
24
  DEFAULT_SIZE = :md
26
25
 
@@ -1,155 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "ariadne/classify"
4
-
5
3
  module Ariadne
6
4
  # All Ariadne ViewComponents accept a standard set of options: classes, which match Tailwind CSS classes, and attributes, which match HTML attributes..
7
5
  #
8
- # Under the hood, classes are [mapped](https://github.com/ariadne/view_components/blob/main/lib/ariadne/classify.rb) to Tailwind CSS classes, with any unconsumed attributes passed to Rails' [`content_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag).
6
+ # Under the hood, any-non class attributes is passed to Rails' [`tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag)
7
+ # (for self-closing tags) or [`content_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag).
9
8
  class BaseComponent < Ariadne::Component
10
9
  SELF_CLOSING_TAGS = [:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr].freeze
11
10
 
12
- # ## HTML attributes
13
- #
14
- # System arguments include most HTML attributes. For example:
15
- #
16
- # | Name | Type | Description |
17
- # | :- | :- | :- |
18
- # | `aria` | `Hash` | Aria attributes: `aria: { label: "foo" }` renders `aria-label='foo'`. |
19
- # | `data` | `Hash` | Data attributes: `data: { foo: :bar }` renders `data-foo='bar'`. |
20
- # | `height` | `Integer` | The height. |
21
- # | `hidden` | `Boolean` | Whether to assign the `hidden` attribute. |
22
- # | `style` | `String` | Inline styles. |
23
- # | `title` | `String` | The `title` attribute. |
24
- # | `width` | `Integer` | The width. |
25
- #
26
- # ## Animation
27
- #
28
- # | Name | Type | Description |
29
- # | :- | :- | :- |
30
- # | `animation` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:animation)) %> |
31
- #
32
- # ## Border
33
- #
34
- # | Name | Type | Description |
35
- # | :- | :- | :- |
36
- # | `border_bottom` | Integer | Set to `0` to remove the bottom border. |
37
- # | `border_left` | Integer | Set to `0` to remove the left border. |
38
- # | `border_radius` | Integer | <%= one_of([0, 1, 2, 3]) %> |
39
- # | `border_right` | Integer | Set to `0` to remove the right border. |
40
- # | `border_top` | Integer | Set to `0` to remove the top border. |
41
- # | `border` | Symbol | <%= one_of([:left, :top, :bottom, :right, :y, :x, true]) %> |
42
- # | `box_shadow` | Boolean, Symbol | Box shadow. <%= one_of([true, :medium, :large, :extra_large, :none]) %> |
43
- #
44
- # ## Color
45
- #
46
- # | Name | Type | Description |
47
- # | :- | :- | :- |
48
- # | `bg` | Symbol | Background color. <%= one_of(Ariadne::Classify::Utilities.mappings(:bg)) %> |
49
- # | `border_color` | Symbol | Border color. <%= one_of(Ariadne::Classify::Utilities.mappings(:border_color)) %> |
50
- # | `color` | Symbol | Text color. <%= one_of(Ariadne::Classify::Utilities.mappings(:color)) %> |
51
- #
52
- # ## Flex
53
- #
54
- # | Name | Type | Description |
55
- # | :- | :- | :- |
56
- # | `align_items` | Symbol | <%= one_of(Ariadne::Classify::FLEX_ALIGN_ITEMS_VALUES) %> |
57
- # | `align_self` | Symbol | <%= one_of(Ariadne::Classify::FLEX_ALIGN_SELF_VALUES) %> |
58
- # | `direction` | Symbol | <%= one_of(Ariadne::Classify::FLEX_DIRECTION_VALUES) %> |
59
- # | `flex` | Integer, Symbol | <%= one_of(Ariadne::Classify::FLEX_VALUES) %> |
60
- # | `flex_grow` | Integer | To enable, set to `0`. |
61
- # | `flex_shrink` | Integer | To enable, set to `0`. |
62
- # | `flex_wrap` | Symbol | <%= one_of(Ariadne::Classify::FLEX_WRAP_MAPPINGS.keys) %> |
63
- # | `justify_content` | Symbol | <%= one_of(Ariadne::Classify::FLEX_JUSTIFY_CONTENT_VALUES) %> |
64
- #
65
- # ## Grid
66
- #
67
- # | Name | Type | Description |
68
- # | :- | :- | :- |
69
- # | `clearfix` | Boolean | Whether to assign the `clearfix` class. |
70
- # | `col` | Integer | Number of columns. <%= one_of(Ariadne::Classify::Utilities.mappings(:col)) %> |
71
- # | `container` | Symbol | Size of the container. <%= one_of(Ariadne::Classify::Utilities.mappings(:container)) %> |
72
- #
73
- # ## Layout
74
- #
75
- # | Name | Type | Description |
76
- # | :- | :- | :- |
77
- # | `display` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:display)) %> |
78
- # | `w` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:w)) %> |
79
- # | `h` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:h)) %> |
80
- # | `hide` | Symbol | Hide the element at a specific breakpoint. <%= one_of(Ariadne::Classify::Utilities.mappings(:hide)) %> |
81
- # | `visibility` | Symbol | Visibility. <%= one_of(Ariadne::Classify::Utilities.mappings(:visibility)) %> |
82
- # | `vertical_align` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:vertical_align)) %> |
83
- #
84
- # ## Position
85
- #
86
- # | Name | Type | Description |
87
- # | :- | :- | :- |
88
- # | `bottom` | Boolean | If `false`, sets `bottom: 0`. |
89
- # | `float` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:float)) %> |
90
- # | `left` | Boolean | If `false`, sets `left: 0`. |
91
- # | `position` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:position)) %> |
92
- # | `right` | Boolean | If `false`, sets `right: 0`. |
93
- # | `top` | Boolean | If `false`, sets `top: 0`. |
94
- #
95
- # ## Spacing
96
- #
97
- # | Name | Type | Description |
98
- # | :- | :- | :- |
99
- # | `m` | Integer | Margin. <%= one_of(Ariadne::Classify::Utilities.mappings(:m)) %> |
100
- # | `mb` | Integer | Margin bottom. <%= one_of(Ariadne::Classify::Utilities.mappings(:mb)) %> |
101
- # | `ml` | Integer | Margin left. <%= one_of(Ariadne::Classify::Utilities.mappings(:ml)) %> |
102
- # | `mr` | Integer | Margin right. <%= one_of(Ariadne::Classify::Utilities.mappings(:mr)) %> |
103
- # | `mt` | Integer | Margin top. <%= one_of(Ariadne::Classify::Utilities.mappings(:mt)) %> |
104
- # | `mx` | Integer | Horizontal margins. <%= one_of(Ariadne::Classify::Utilities.mappings(:mx)) %> |
105
- # | `my` | Integer | Vertical margins. <%= one_of(Ariadne::Classify::Utilities.mappings(:my)) %> |
106
- # | `p` | Integer | Padding. <%= one_of(Ariadne::Classify::Utilities.mappings(:p)) %> |
107
- # | `pb` | Integer | Padding bottom. <%= one_of(Ariadne::Classify::Utilities.mappings(:pb)) %> |
108
- # | `pl` | Integer | Padding left. <%= one_of(Ariadne::Classify::Utilities.mappings(:pl)) %> |
109
- # | `pr` | Integer | Padding right. <%= one_of(Ariadne::Classify::Utilities.mappings(:pr)) %> |
110
- # | `pt` | Integer | Padding left. <%= one_of(Ariadne::Classify::Utilities.mappings(:pt)) %> |
111
- # | `px` | Integer | Horizontal padding. <%= one_of(Ariadne::Classify::Utilities.mappings(:px)) %> |
112
- # | `py` | Integer | Vertical padding. <%= one_of(Ariadne::Classify::Utilities.mappings(:py)) %> |
113
- #
114
- # ## Typography
115
- #
116
- # | Name | Type | Description |
117
- # | :- | :- | :- |
118
- # | `font_family` | Symbol | Font weight. <%= one_of([:mono]) %> |
119
- # | `font_size` | String, Integer, Symbol | <%= one_of(["00", 0, 1, 2, 3, 4, 5, 6, :small, :normal]) %> |
120
- # | `font_style` | Symbol | Font weight. <%= one_of([:italic]) %> |
121
- # | `font_weight` | Symbol | Font weight. <%= one_of([:light, :normal, :bold, :emphasized]) %> |
122
- # | `text_align` | Symbol | Text alignment. <%= one_of([:left, :right, :center]) %> |
123
- # | `text_transform` | Symbol | Text alignment. <%= one_of([:uppercase]) %> |
124
- # | `underline` | Boolean | Whether text should be underlined. |
125
- # | `word_break` | Symbol | Whether to break words on line breaks. <%= one_of(Ariadne::Classify::Utilities.mappings(:word_break)) %> |
126
- #
127
- # ## Other
128
- #
129
- # | Name | Type | Description |
130
- # | :- | :- | :- |
131
- # | classes | String | CSS class name value to be concatenated with generated Tailwind CSS classes. |
132
- # | test_selector | String | Adds `data-test-selector='given value'` in non-Production environments for testing purposes. |
133
11
  def initialize(tag:, classes: "", attributes: {})
134
12
  @tag = tag
135
13
 
136
- @attributes = validate_attributes(tag: tag, attributes: attributes)
14
+ @attributes = validate_attributes(tag: tag, attributes: attributes) || {}
137
15
  @attributes[:"data-ariadne-view-component"] = true
138
16
 
139
- classes = classes.present? ? @attributes.merge(classes: classes) : @attributes.fetch(:classes, "")
140
- @classes = Ariadne::Classify.call(classes)
141
-
142
- # Filter out Ariadne keys so they don't get assigned as HTML attributes
143
- @content_tag_args = add_test_selector(@attributes).except(*Ariadne::Classify::Utilities::UTILITIES.keys)
17
+ classes = @attributes.fetch(:classes, "") if classes.blank?
18
+ @classes = validate_class_names(classes) || {}
19
+ @content_tag_args = {}
144
20
  end
145
21
 
146
22
  def call
147
- options = @content_tag_args.merge(@classes)
23
+ options = @attributes.merge(@classes)
148
24
  if SELF_CLOSING_TAGS.include?(@tag)
149
25
  tag(@tag, options)
150
26
  else
151
27
  content_tag(@tag, content, options)
152
28
  end
153
29
  end
30
+
31
+ private def validate_class_names(classes)
32
+ return if classes.blank?
33
+
34
+ { class: Ariadne::ViewComponents.tailwind_merger.merge(classes) }
35
+ end
154
36
  end
155
37
  end
@@ -1,4 +1,4 @@
1
- <div class="bg-white">
1
+ <%= render Ariadne::NarrowContainerComponent.new do %>
2
2
  <%= render Ariadne::BaseComponent.new(tag: @tag, classes: @classes, attributes: @attributes) do %>
3
3
  <% if icon.present? %>
4
4
  <%= icon %>
@@ -10,17 +10,17 @@
10
10
  <%= description %>
11
11
  <% end %>
12
12
  <%= content %>
13
- <div class="mt-8 flex justify-center">
13
+ <div class="ariadne-mt-8 ariadne-flex ariadne-justify-center">
14
14
  <% if primary_action.present? %>
15
- <div class="inline-flex rounded-md shadow">
15
+ <div class="ariadne-inline-flex ariadne-rounded-md shadow">
16
16
  <%= primary_action %>
17
17
  </div>
18
18
  <% end %>
19
19
  <% if secondary_action.present? %>
20
- <div class="ml-3 inline-flex">
20
+ <div class="ariadne-ml-3 ariadne-inline-flex">
21
21
  <%= secondary_action %>
22
22
  </div>
23
23
  <% end %>
24
24
  </div>
25
25
  <% end %>
26
- </div>
26
+ <% end %>