polaris_view_components 0.5.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -2
  3. data/app/assets/javascripts/polaris_view_components/autocomplete_controller.js +136 -0
  4. data/app/assets/javascripts/polaris_view_components/button_controller.js +47 -0
  5. data/app/assets/javascripts/polaris_view_components/dropzone_controller.js +495 -0
  6. data/app/assets/javascripts/polaris_view_components/frame_controller.js +41 -0
  7. data/app/assets/javascripts/polaris_view_components/index.js +21 -1
  8. data/app/assets/javascripts/polaris_view_components/modal_controller.js +25 -0
  9. data/app/assets/javascripts/polaris_view_components/option_list_controller.js +41 -0
  10. data/app/assets/javascripts/polaris_view_components/polaris_controller.js +28 -0
  11. data/app/assets/javascripts/polaris_view_components/popover_controller.js +56 -0
  12. data/app/assets/javascripts/polaris_view_components/scrollable_controller.js +60 -0
  13. data/app/assets/javascripts/polaris_view_components/text_field_controller.js +4 -0
  14. data/app/assets/javascripts/polaris_view_components/toast_controller.js +68 -0
  15. data/app/assets/javascripts/polaris_view_components/utils.js +23 -0
  16. data/app/assets/javascripts/polaris_view_components.js +2400 -1
  17. data/app/assets/stylesheets/polaris_view_components/custom.css +104 -6
  18. data/app/assets/stylesheets/polaris_view_components.css +2147 -2021
  19. data/app/assets/stylesheets/polaris_view_components.postcss.css +1 -1
  20. data/app/components/polaris/action.rb +3 -3
  21. data/app/components/polaris/action_list/item_component.html.erb +35 -0
  22. data/app/components/polaris/action_list/item_component.rb +41 -0
  23. data/app/components/polaris/action_list/section_component.html.erb +16 -0
  24. data/app/components/polaris/action_list/section_component.rb +26 -0
  25. data/app/components/polaris/action_list_component.html.erb +13 -0
  26. data/app/components/polaris/action_list_component.rb +25 -0
  27. data/app/components/polaris/autocomplete/action_component.rb +7 -0
  28. data/app/components/polaris/autocomplete/option_component.rb +35 -0
  29. data/app/components/polaris/autocomplete/section_component.html.erb +9 -0
  30. data/app/components/polaris/autocomplete/section_component.rb +12 -0
  31. data/app/components/polaris/autocomplete_component.html.erb +30 -0
  32. data/app/components/polaris/autocomplete_component.rb +65 -0
  33. data/app/components/polaris/avatar_component.rb +2 -2
  34. data/app/components/polaris/badge_component.rb +2 -2
  35. data/app/components/polaris/banner_component.rb +8 -8
  36. data/app/components/polaris/base_button.rb +2 -2
  37. data/app/components/polaris/base_checkbox.rb +48 -0
  38. data/app/components/polaris/base_component.rb +1 -1
  39. data/app/components/polaris/base_radio_button.rb +38 -0
  40. data/app/components/polaris/button_component.html.erb +13 -0
  41. data/app/components/polaris/button_group_component.rb +8 -8
  42. data/app/components/polaris/callout_card_component.rb +6 -6
  43. data/app/components/polaris/caption_component.rb +3 -3
  44. data/app/components/polaris/card/header_component.rb +2 -2
  45. data/app/components/polaris/card/section_component.rb +4 -4
  46. data/app/components/polaris/card_component.html.erb +4 -0
  47. data/app/components/polaris/card_component.rb +6 -5
  48. data/app/components/polaris/character_count.rb +10 -10
  49. data/app/components/polaris/checkbox_component.html.erb +1 -5
  50. data/app/components/polaris/checkbox_component.rb +20 -13
  51. data/app/components/polaris/choice_component.rb +2 -2
  52. data/app/components/polaris/choice_list_component.rb +6 -6
  53. data/app/components/polaris/component.rb +6 -1
  54. data/app/components/polaris/data_table/cell_component.rb +4 -4
  55. data/app/components/polaris/data_table/column_component.rb +1 -1
  56. data/app/components/polaris/data_table_component.rb +4 -4
  57. data/app/components/polaris/description_list_component.rb +5 -5
  58. data/app/components/polaris/display_text_component.rb +3 -3
  59. data/app/components/polaris/dropzone_component.html.erb +156 -0
  60. data/app/components/polaris/dropzone_component.rb +150 -0
  61. data/app/components/polaris/empty_state_component.rb +4 -4
  62. data/app/components/polaris/exception_list/item_component.rb +3 -3
  63. data/app/components/polaris/exception_list_component.rb +2 -2
  64. data/app/components/polaris/filters_component.html.erb +22 -0
  65. data/app/components/polaris/filters_component.rb +61 -8
  66. data/app/components/polaris/footer_help_component.rb +2 -2
  67. data/app/components/polaris/form_layout/group_component.rb +5 -5
  68. data/app/components/polaris/form_layout/item_component.rb +2 -2
  69. data/app/components/polaris/form_layout_component.rb +4 -4
  70. data/app/components/polaris/frame/save_bar_component.html.erb +23 -0
  71. data/app/components/polaris/frame/save_bar_component.rb +31 -0
  72. data/app/components/polaris/frame/top_bar_component.html.erb +30 -0
  73. data/app/components/polaris/frame/top_bar_component.rb +18 -0
  74. data/app/components/polaris/frame_component.html.erb +44 -0
  75. data/app/components/polaris/frame_component.rb +33 -0
  76. data/app/components/polaris/heading_component.rb +2 -2
  77. data/app/components/polaris/headless_button.html.erb +13 -0
  78. data/app/components/polaris/headless_button.rb +18 -6
  79. data/app/components/polaris/icon_component.rb +3 -3
  80. data/app/components/polaris/index_table/cell_component.rb +3 -3
  81. data/app/components/polaris/index_table/column_component.rb +1 -1
  82. data/app/components/polaris/index_table_component.rb +4 -4
  83. data/app/components/polaris/inline_error_component.rb +2 -2
  84. data/app/components/polaris/label_component.rb +3 -3
  85. data/app/components/polaris/labelled_component.rb +3 -3
  86. data/app/components/polaris/layout/annotated_section.rb +2 -2
  87. data/app/components/polaris/layout/section.rb +2 -2
  88. data/app/components/polaris/layout_component.rb +4 -4
  89. data/app/components/polaris/link_component.rb +4 -4
  90. data/app/components/polaris/list_component.rb +5 -5
  91. data/app/components/polaris/logo.rb +13 -0
  92. data/app/components/polaris/modal/section_component.rb +19 -0
  93. data/app/components/polaris/modal_component.html.erb +79 -0
  94. data/app/components/polaris/modal_component.rb +98 -0
  95. data/app/components/polaris/navigation/item_component.html.erb +31 -0
  96. data/app/components/polaris/navigation/item_component.rb +85 -0
  97. data/app/components/polaris/navigation/section_component.html.erb +17 -0
  98. data/app/components/polaris/navigation/section_component.rb +64 -0
  99. data/app/components/polaris/navigation_component.html.erb +29 -0
  100. data/app/components/polaris/navigation_component.rb +15 -0
  101. data/app/components/polaris/option_list/checkbox_component.html.erb +14 -0
  102. data/app/components/polaris/option_list/checkbox_component.rb +37 -0
  103. data/app/components/polaris/option_list/option_component.rb +24 -0
  104. data/app/components/polaris/option_list/radio_button_component.rb +54 -0
  105. data/app/components/polaris/option_list/section_component.html.erb +14 -0
  106. data/app/components/polaris/option_list/section_component.rb +53 -0
  107. data/app/components/polaris/option_list_component.html.erb +15 -0
  108. data/app/components/polaris/option_list_component.rb +67 -0
  109. data/app/components/polaris/page_actions_component.rb +9 -8
  110. data/app/components/polaris/page_component.rb +5 -5
  111. data/app/components/polaris/pagination_component.rb +2 -6
  112. data/app/components/polaris/popover/pane_component.html.erb +25 -0
  113. data/app/components/polaris/popover/pane_component.rb +20 -0
  114. data/app/components/polaris/popover/section_component.rb +19 -0
  115. data/app/components/polaris/popover_component.html.erb +31 -0
  116. data/app/components/polaris/popover_component.rb +113 -0
  117. data/app/components/polaris/progress_bar_component.rb +6 -6
  118. data/app/components/polaris/radio_button_component.html.erb +1 -6
  119. data/app/components/polaris/radio_button_component.rb +18 -8
  120. data/app/components/polaris/resource_item_component.html.erb +16 -6
  121. data/app/components/polaris/resource_item_component.rb +43 -10
  122. data/app/components/polaris/resource_list_component.rb +3 -3
  123. data/app/components/polaris/scrollable_component.html.erb +5 -0
  124. data/app/components/polaris/scrollable_component.rb +48 -0
  125. data/app/components/polaris/select_component.rb +4 -4
  126. data/app/components/polaris/setting_toggle_component.html.erb +10 -0
  127. data/app/components/polaris/setting_toggle_component.rb +24 -0
  128. data/app/components/polaris/shopify_navigation_component.rb +8 -8
  129. data/app/components/polaris/skeleton_body_text_component.rb +2 -2
  130. data/app/components/polaris/spacer_component.rb +4 -4
  131. data/app/components/polaris/spinner_component.rb +3 -3
  132. data/app/components/polaris/stack/item_component.rb +15 -0
  133. data/app/components/polaris/stack_component.rb +6 -22
  134. data/app/components/polaris/subheading_component.rb +2 -2
  135. data/app/components/polaris/tabs/tab_component.html.erb +10 -0
  136. data/app/components/polaris/tabs/tab_component.rb +34 -0
  137. data/app/components/polaris/tabs_component.html.erb +7 -0
  138. data/app/components/polaris/tabs_component.rb +37 -0
  139. data/app/components/polaris/tag_component.rb +3 -2
  140. data/app/components/polaris/text_container_component.rb +3 -3
  141. data/app/components/polaris/text_field_component.rb +25 -11
  142. data/app/components/polaris/text_style_component.rb +5 -5
  143. data/app/components/polaris/thumbnail_component.rb +3 -3
  144. data/app/components/polaris/toast_component.html.erb +21 -0
  145. data/app/components/polaris/toast_component.rb +40 -0
  146. data/app/components/polaris/top_bar/user_menu_component.html.erb +19 -0
  147. data/app/components/polaris/top_bar/user_menu_component.rb +9 -0
  148. data/app/components/polaris/visually_hidden_component.rb +1 -1
  149. data/app/helpers/polaris/form_builder.rb +19 -8
  150. data/app/helpers/polaris/url_helper.rb +3 -3
  151. data/app/helpers/polaris/view_helper.rb +24 -4
  152. data/app/validators/type_validator.rb +2 -2
  153. data/config/locales/en.yml +6 -0
  154. data/lib/generators/polaris_view_components/install_generator.rb +5 -5
  155. data/lib/polaris/view_components/engine.rb +16 -2
  156. data/lib/polaris/view_components/version.rb +1 -1
  157. data/lib/polaris_view_components.rb +1 -1
  158. metadata +79 -16
  159. data/app/components/polaris/application_component.rb +0 -35
  160. data/app/components/polaris/dropzone/component.html.erb +0 -72
  161. data/app/components/polaris/dropzone/component.rb +0 -128
  162. data/app/components/polaris/dropzone/controller.js +0 -226
  163. data/app/components/polaris/dropzone/utils.js +0 -57
  164. data/app/components/polaris/new_component.rb +0 -10
  165. data/app/helpers/polaris/conditional_helper.rb +0 -11
@@ -0,0 +1,495 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+ import { debounce, formatBytes } from './utils'
3
+
4
+ const dragEvents = ['dragover', 'dragenter', 'drop']
5
+ const SIZES = {
6
+ SMALL: 'small',
7
+ MEDIUM: 'medium',
8
+ LARGE: 'large',
9
+ EXTRA_LARGE: 'extraLarge',
10
+ }
11
+
12
+ // eslint-disable-next-line import/no-default-export
13
+ export default class extends Controller {
14
+ static targets = [
15
+ 'container',
16
+ 'fileUpload',
17
+ 'loader',
18
+ 'input',
19
+ 'preview',
20
+ 'previewTemplate',
21
+ 'itemTemplate',
22
+ 'overlay',
23
+ 'errorOverlay'
24
+ ]
25
+ static classes = ['disabled']
26
+ static values = {
27
+ accept: String,
28
+ allowMultiple: Boolean,
29
+ disabled: Boolean,
30
+ dropOnPage: Boolean,
31
+ focused: Boolean,
32
+ renderPreview: Boolean,
33
+ size: String
34
+ }
35
+
36
+ files = []
37
+ acceptedFiles = []
38
+ rejectedFiles = []
39
+ _dragging = false
40
+ dragTargets = []
41
+ previewRendered = false
42
+ _size = 'large'
43
+
44
+ connect () {
45
+ document.body.addEventListener('click', this.onExternalTriggerClick)
46
+ addEventListener('resize', this.onWindowResize)
47
+ addEventListener('direct-uploads:start', this.onDirectUploadsStart)
48
+ addEventListener('direct-uploads:end', this.onDirectUploadsEnd)
49
+ addEventListener('direct-upload:initialize', this.onDirectUploadInitialize)
50
+ addEventListener('direct-upload:start', this.onDirectUploadStart)
51
+ addEventListener('direct-upload:progress', this.onDirectUploadProgress)
52
+ addEventListener('direct-upload:error', this.onDirectUploadError)
53
+ addEventListener('direct-upload:end', this.onDirectUploadEnd)
54
+
55
+ this.onWindowResize()
56
+ }
57
+
58
+ disconnect () {
59
+ document.body.removeEventListener('click', this.onExternalTriggerClick)
60
+ removeEventListener('resize', this.onWindowResize)
61
+ removeEventListener('direct-uploads:start', this.onDirectUploadsStart)
62
+ removeEventListener('direct-uploads:end', this.onDirectUploadsEnd)
63
+ removeEventListener('direct-upload:initialize', this.onDirectUploadInitialize)
64
+ removeEventListener('direct-upload:start', this.onDirectUploadStart)
65
+ removeEventListener('direct-upload:progress', this.onDirectUploadProgress)
66
+ removeEventListener('direct-upload:error', this.onDirectUploadError)
67
+ removeEventListener('direct-upload:end', this.onDirectUploadEnd)
68
+ }
69
+
70
+ onExternalTriggerClick = (event) => {
71
+ const trigger = event.target.closest(`[data-${this.identifier}-external-target="trigger"]`)
72
+ if (!trigger) return
73
+
74
+ event.preventDefault()
75
+
76
+ this.onClick()
77
+ }
78
+
79
+ onWindowResize = debounce(() => {
80
+ const size = this.calculateSize()
81
+
82
+ if (size !== this.size) {
83
+ this.size = size
84
+ }
85
+ }, 50)
86
+
87
+ onBlur () {
88
+ this.focusedValue = false
89
+ }
90
+
91
+ onChange (e) {
92
+ this.stopEvent(e)
93
+ if (this.disabled) return
94
+
95
+ this.clearFiles()
96
+
97
+ const fileList = getDataTransferFiles(e)
98
+ const { files, acceptedFiles, rejectedFiles } = this.getValidatedFiles(fileList)
99
+ this.dragTargets = []
100
+
101
+ this.files = files
102
+ this.acceptedFiles = acceptedFiles
103
+ this.rejectedFiles = rejectedFiles
104
+
105
+ this.render()
106
+ }
107
+
108
+ onDragOver (e) {
109
+ this.stopEvent(e)
110
+ if (this.disabled) return
111
+ }
112
+
113
+ onDragEnter (e) {
114
+ this.stopEvent(e)
115
+ if (this.disabled) return
116
+
117
+ if (e.target && !this.dragTargets.includes(e.target)) {
118
+ this.dragTargets.push(e.target)
119
+ }
120
+
121
+ if (this.dragging) return
122
+
123
+ this.dragging = true
124
+ }
125
+
126
+ onDragLeave (e) {
127
+ this.stopEvent(e)
128
+ if (this.disabled) return
129
+
130
+ this.dragTargets = this.dragTargets.filter(el => {
131
+ const compareNode = this.element
132
+
133
+ return el !== e.target && compareNode && compareNode.contains(el)
134
+ })
135
+
136
+ if (this.dragTargets.length > 0) return
137
+
138
+ this.dragging = false
139
+ }
140
+
141
+ onDrop (e) {
142
+ this.stopEvent(e)
143
+ if (this.disabled) return
144
+
145
+ this.dragging = false
146
+
147
+ this.onChange(e)
148
+ }
149
+
150
+ onFocus () {
151
+ this.focusedValue = true
152
+ }
153
+
154
+ onClick () {
155
+ if (this.disabledValue) return
156
+
157
+ this.open()
158
+ }
159
+
160
+ onDirectUploadsStart = () => {
161
+ this.disable()
162
+ }
163
+
164
+ onDirectUploadsEnd = () => {
165
+ this.enable()
166
+ this.clearFiles()
167
+ this.loaderTarget.classList.remove("Polaris--hidden")
168
+ }
169
+
170
+ onDirectUploadInitialize = (event) => {
171
+ const { target, detail } = event
172
+ const { id, file } = detail
173
+ const dropzone = target.closest('.Polaris-DropZone')
174
+ if (!dropzone) return
175
+
176
+ const content = dropzone.querySelector(`[data-file-name="${file.name}"]`)
177
+ const progressBar = content.parentElement.querySelector('[data-target="progress-bar"]')
178
+ progressBar.id = `direct-upload-${id}`
179
+ }
180
+
181
+ onDirectUploadStart = (event) => {
182
+ const { id } = event.detail
183
+ const progressBar = document.getElementById(`direct-upload-${id}`)
184
+ if (!progressBar) return
185
+
186
+ progressBar.classList.remove("Polaris--hidden")
187
+ }
188
+
189
+ onDirectUploadProgress = (event) => {
190
+ const { id, progress } = event.detail
191
+ const progressBar = document.getElementById(`direct-upload-${id}`)
192
+ if (!progressBar) return
193
+
194
+ const progressElement = progressBar.querySelector('.Polaris-ProgressBar__Indicator')
195
+ progressElement.style.width = `${progress}%`
196
+ }
197
+
198
+ onDirectUploadError = (event) => {
199
+ const { id, error } = event.detail
200
+ const progressBar = document.getElementById(`direct-upload-${id}`)
201
+ if (!progressBar) return
202
+
203
+ event.preventDefault()
204
+ progressBar.classList.add("Polaris--hidden")
205
+ const uploadError = progressBar.parentElement.querySelector('[data-target="upload-error"]')
206
+ const errorIcon = uploadError.querySelector('.Polaris-InlineError__Icon')
207
+ if (errorIcon) errorIcon.remove()
208
+ uploadError.classList.remove("Polaris--hidden")
209
+ }
210
+
211
+ onDirectUploadEnd = (event) => {
212
+ const { id } = event.detail
213
+ const progressBar = document.getElementById(`direct-upload-${id}`)
214
+ if (!progressBar) return
215
+
216
+ progressBar.classList.add("Polaris-ProgressBar--colorSuccess")
217
+ }
218
+
219
+ open () {
220
+ this.inputTarget.click()
221
+ }
222
+
223
+ focusedValueChanged () {
224
+ this.element.classList.toggle('Polaris-DropZone--focused', this.focusedValue)
225
+ }
226
+
227
+ stopEvent (e) {
228
+ e.preventDefault()
229
+ e.stopPropagation()
230
+ }
231
+
232
+ getValidatedFiles (files) {
233
+ const acceptedFiles = []
234
+ const rejectedFiles = []
235
+
236
+ Array.from(files).forEach(file => {
237
+ if (fileAccepted(file, this.acceptValue)) {
238
+ acceptedFiles.push(file)
239
+ } else {
240
+ rejectedFiles.push(file)
241
+ }
242
+ })
243
+
244
+ if (!this.allowMultipleValue) {
245
+ acceptedFiles.splice(1, acceptedFiles.length)
246
+ rejectedFiles.push(...acceptedFiles.slice(1))
247
+ }
248
+
249
+ return { files, acceptedFiles, rejectedFiles }
250
+ }
251
+
252
+ render () {
253
+ if (this.files.length === 0) {
254
+ this.toggleFileUpload(true)
255
+ this.toggleErrorOverlay(false)
256
+ } else if (this.rejectedFiles.length > 0) {
257
+ this.toggleFileUpload(false)
258
+ this.toggleErrorOverlay(true)
259
+
260
+ const dropRejectedEvent = new CustomEvent('polaris-dropzone:drop-rejected', {
261
+ detail: { rejectedFiles: this.rejectedFiles }
262
+ })
263
+ this.element.dispatchEvent(dropRejectedEvent)
264
+ } else if (this.acceptedFiles.length > 0) {
265
+ if (this.renderPreviewValue) {
266
+ this.renderUploadedFiles()
267
+ this.toggleFileUpload(false)
268
+ }
269
+
270
+ this.toggleErrorOverlay(false)
271
+
272
+ const dropAcceptedEvent = new CustomEvent('polaris-dropzone:drop-accepted', {
273
+ detail: {acceptedFiles: this.acceptedFiles }
274
+ })
275
+ this.element.dispatchEvent(dropAcceptedEvent)
276
+ }
277
+
278
+ const dropEvent = new CustomEvent('polaris-dropzone:drop', {
279
+ detail: {
280
+ files: this.files,
281
+ acceptedFiles: this.acceptedFiles,
282
+ rejectedFiles: this.rejectedFiles
283
+ }
284
+ })
285
+ this.element.dispatchEvent(dropEvent)
286
+ }
287
+
288
+ renderUploadedFiles () {
289
+ if (this.acceptedFiles.length === 0) return
290
+
291
+ const clone = this.previewTemplateTarget.content.cloneNode(true)
292
+ const filesTarget = clone.querySelector('.target')
293
+ let files = this.acceptedFiles
294
+
295
+ if (this.sizeValue == 'small') files = [files[0]]
296
+
297
+ files
298
+ .map(file => this.renderFile(file))
299
+ .forEach(fragment => filesTarget.parentNode.appendChild(fragment))
300
+ filesTarget.remove()
301
+
302
+ this.containerTarget.prepend(clone)
303
+
304
+ this.previewRendered = true
305
+ }
306
+
307
+ toggleFileUpload (show = false) {
308
+ this.fileUploadTarget.classList.toggle('Polaris-VisuallyHidden', !show)
309
+ }
310
+
311
+ toggleErrorOverlay (show = false) {
312
+ this.errorOverlayTarget.classList.toggle('Polaris-VisuallyHidden', !show)
313
+ this.element.classList.toggle('Polaris-DropZone--hasError', show)
314
+ }
315
+
316
+ renderFile (file) {
317
+ const validImageTypes = ['image/gif', 'image/jpeg', 'image/png']
318
+ const clone = this.itemTemplateTarget.content.cloneNode(true)
319
+ const [icon, thumbnail, content, fileSize] = [
320
+ clone.querySelector('[data-target="icon"]'),
321
+ clone.querySelector('[data-target="thumbnail"]'),
322
+ clone.querySelector('[data-target="content"]'),
323
+ clone.querySelector('[data-target="file-size"]')
324
+ ]
325
+
326
+ if (validImageTypes.includes(file.type)) {
327
+ const img = thumbnail.querySelector('img')
328
+ img.alt = file.name
329
+ img.src = window.URL.createObjectURL(file)
330
+ icon.remove()
331
+ } else {
332
+ thumbnail.remove()
333
+ }
334
+
335
+ if (this.sizeValue != 'small') {
336
+ content.insertAdjacentText('afterbegin', file.name)
337
+ content.setAttribute('data-file-name', file.name)
338
+ fileSize.textContent = formatBytes(file.size)
339
+ }
340
+
341
+ return clone
342
+ }
343
+
344
+ clearFiles () {
345
+ if (!this.previewRendered) return
346
+
347
+ this.acceptedFiles = []
348
+ this.files = []
349
+ this.rejectedFiles = []
350
+
351
+ if (!this.hasPreviewTarget) return
352
+
353
+ this.previewTarget.remove()
354
+ this.previewRendered = false
355
+ }
356
+
357
+ calculateSize () {
358
+ const width = this.element.getBoundingClientRect().width
359
+
360
+ let size = SIZES.LARGE
361
+
362
+ if (width < 100) {
363
+ size = SIZES.SMALL
364
+ } else if (width < 160) {
365
+ size = SIZES.MEDIUM
366
+ } else if (width > 300) {
367
+ size = SIZES.EXTRA_LARGE
368
+ }
369
+
370
+ this.size = size
371
+
372
+ return size
373
+ }
374
+
375
+ getSizeClass (size = 'large') {
376
+ return this.sizeClassesSchema[size] || this.sizeClassesSchema.large
377
+ }
378
+
379
+ disable() {
380
+ this.disabled = true
381
+ this.element.classList.add(this.disabledClass)
382
+ this.inputTarget.disabled = true
383
+ }
384
+
385
+ enable() {
386
+ this.disabled = false
387
+ this.element.classList.remove(this.disabledClass)
388
+ this.inputTarget.disabled = false
389
+ }
390
+
391
+ get fileListRendered () {
392
+ return this.element.querySelector('[data-rendered]')
393
+ }
394
+
395
+ get dropNode () {
396
+ return this.dropOnPageValue ? document : this.element
397
+ }
398
+
399
+ get disabled () {
400
+ return this.disabledValue
401
+ }
402
+
403
+ set disabled (val) {
404
+ this.disabledValue = val
405
+ }
406
+
407
+ get dragging () {
408
+ return this._dragging
409
+ }
410
+
411
+ set dragging (val) {
412
+ this._dragging = val
413
+
414
+ this.element.classList.toggle('Polaris-DropZone--isDragging', val)
415
+ this.overlayTarget.classList.toggle('Polaris-VisuallyHidden', !val)
416
+ }
417
+
418
+ get sizeClassesSchema () {
419
+ return {
420
+ [SIZES.SMALL]: 'Polaris-DropZone--sizeSmall',
421
+ [SIZES.MEDIUM]: 'Polaris-DropZone--sizeMedium',
422
+ [SIZES.LARGE]: 'Polaris-DropZone--sizeLarge',
423
+ [SIZES.EXTRA_LARGE]: 'Polaris-DropZone--sizeExtraLarge',
424
+ }
425
+ }
426
+
427
+ get size () {
428
+ return this._size
429
+ }
430
+
431
+ set size (val) {
432
+ this._size = val
433
+
434
+ const sizeClassesToRemove = Object.values(this.sizeClassesSchema)
435
+ sizeClassesToRemove.forEach(className => this.element.classList.remove(className))
436
+
437
+ this.element.classList.add(this.getSizeClass(val))
438
+ }
439
+ }
440
+
441
+ export function fileAccepted (file, accept) {
442
+ return file.type === 'application/x-moz-file' || accepts(file, accept)
443
+ }
444
+
445
+ export function getDataTransferFiles (event) {
446
+ if (isDragEvent(event) && event.dataTransfer) {
447
+ const dt = event.dataTransfer
448
+
449
+ if (dt.files && dt.files.length) {
450
+ return Array.from(dt.files)
451
+ } else if (dt.items && dt.items.length) {
452
+ // Chrome is the only browser that allows to read the file list on drag
453
+ // events and uses `items` instead of `files` in this case.
454
+ return Array.from(dt.items)
455
+ }
456
+ } else if (isChangeEvent(event) && event.target.files) {
457
+ // Return files from even when a file was selected from an upload dialog
458
+ return Array.from(event.target.files)
459
+ }
460
+
461
+ return []
462
+ }
463
+
464
+ function accepts (file, acceptedFiles = ['']) {
465
+ if (file && acceptedFiles) {
466
+ const fileName = file.name || ''
467
+ const mimeType = file.type || ''
468
+ const baseMimeType = mimeType.replace(/\/.*$/, '')
469
+ const acceptedFilesArray = Array.isArray(acceptedFiles)
470
+ ? acceptedFiles
471
+ : acceptedFiles.split(',')
472
+
473
+ return acceptedFilesArray.some((type) => {
474
+ const validType = type.trim()
475
+ if (validType.startsWith('.')) {
476
+ return fileName.toLowerCase().endsWith(validType.toLowerCase())
477
+ } else if (validType.endsWith('/*')) {
478
+ // This is something like a image/* mime type
479
+ return baseMimeType === validType.replace(/\/.*$/, '')
480
+ }
481
+
482
+ return mimeType === validType
483
+ })
484
+ }
485
+
486
+ return true
487
+ }
488
+
489
+ function isDragEvent (event) {
490
+ return dragEvents.indexOf(event.type) > 0
491
+ }
492
+
493
+ function isChangeEvent (event) {
494
+ return event.type === 'change'
495
+ }
@@ -0,0 +1,41 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import { useTransition } from "stimulus-use"
3
+
4
+ export default class extends Controller {
5
+ static targets = ["navigationOverlay", "navigation", "saveBar"]
6
+
7
+ connect() {
8
+ if (!this.hasNavigationTarget) { return }
9
+
10
+ useTransition(this, {
11
+ element: this.navigationTarget,
12
+ enterFrom: "Polaris-Frame__Navigation--enter",
13
+ enterTo: "Polaris-Frame__Navigation--visible Polaris-Frame__Navigation--enterActive",
14
+ leaveActive: "Polaris-Frame__Navigation--exitActive",
15
+ leaveFrom: "Polaris-Frame__Navigation--exit",
16
+ leaveTo: "",
17
+ removeToClasses: false,
18
+ hiddenClass: false,
19
+ })
20
+ }
21
+
22
+ // Actions
23
+
24
+ openMenu() {
25
+ this.enter()
26
+ this.navigationOverlayTarget.classList.add("Polaris-Backdrop", "Polaris-Backdrop--belowNavigation")
27
+ }
28
+
29
+ closeMenu() {
30
+ this.leave()
31
+ this.navigationOverlayTarget.classList.remove("Polaris-Backdrop", "Polaris-Backdrop--belowNavigation")
32
+ }
33
+
34
+ showSaveBar() {
35
+ this.saveBarTarget.classList.add("Polaris-Frame-CSSAnimation--endFade")
36
+ }
37
+
38
+ hideSaveBar() {
39
+ this.saveBarTarget.classList.remove("Polaris-Frame-CSSAnimation--endFade")
40
+ }
41
+ }
@@ -1,11 +1,31 @@
1
+ import Autocomplete from './autocomplete_controller'
2
+ import Button from './button_controller'
3
+ import Dropzone from './dropzone_controller'
4
+ import Frame from './frame_controller'
5
+ import Modal from './modal_controller'
6
+ import OptionList from './option_list_controller'
7
+ import Polaris from './polaris_controller'
8
+ import Popover from './popover_controller'
1
9
  import ResourceItem from './resource_item_controller'
10
+ import Scrollable from './scrollable_controller'
2
11
  import Select from './select_controller'
3
12
  import TextField from './text_field_controller'
13
+ import Toast from './toast_controller'
4
14
 
5
- export { ResourceItem, Select, TextField }
15
+ export { Frame, Modal, Polaris, Popover, ResourceItem, Scrollable, Select, TextField }
6
16
 
7
17
  export function registerPolarisControllers(application) {
18
+ application.register('polaris-autocomplete', Autocomplete)
19
+ application.register('polaris-button', Button)
20
+ application.register('polaris-dropzone', Dropzone)
21
+ application.register('polaris-frame', Frame)
22
+ application.register('polaris-modal', Modal)
23
+ application.register('polaris-option-list', OptionList)
24
+ application.register('polaris', Polaris)
25
+ application.register('polaris-popover', Popover)
8
26
  application.register('polaris-resource-item', ResourceItem)
27
+ application.register('polaris-scrollable', Scrollable)
9
28
  application.register('polaris-select', Select)
10
29
  application.register('polaris-text-field', TextField)
30
+ application.register('polaris-toast', Toast)
11
31
  }
@@ -0,0 +1,25 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ export default class extends Controller {
4
+ static classes = ["hidden", "backdrop"]
5
+ static values = {
6
+ open: Boolean
7
+ }
8
+
9
+ connect() {
10
+ if (this.openValue) {
11
+ this.open()
12
+ }
13
+ }
14
+
15
+ open() {
16
+ this.element.classList.remove(this.hiddenClass)
17
+ this.element.insertAdjacentHTML('afterend', `<div class="${this.backdropClass}"></div>`)
18
+ this.backdrop = this.element.nextElementSibling
19
+ }
20
+
21
+ close() {
22
+ this.element.classList.add(this.hiddenClass)
23
+ this.backdrop.remove()
24
+ }
25
+ }
@@ -0,0 +1,41 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ export default class extends Controller {
4
+ static targets = ["radioButton"]
5
+ static classes = ["selected"]
6
+
7
+ connect() {
8
+ this.updateSelected()
9
+ }
10
+
11
+ // Actions
12
+
13
+ update(event) {
14
+ const target = event.currentTarget
15
+ target.classList.add(this.selectedClass)
16
+ this.deselectAll(target)
17
+ }
18
+
19
+ // Private
20
+
21
+ updateSelected() {
22
+ this.radioButtonTargets.forEach(element => {
23
+ const input = element.querySelector('input[type=radio]')
24
+ if (input.checked) {
25
+ element.classList.add(this.selectedClass)
26
+ } else {
27
+ element.classList.remove(this.selectedClass)
28
+ }
29
+ })
30
+ }
31
+
32
+ deselectAll(target) {
33
+ this.radioButtonTargets.forEach(element => {
34
+ if (!element.isEqualNode(target)) {
35
+ const input = element.querySelector('input[type=radio]')
36
+ input.checked = false
37
+ element.classList.remove(this.selectedClass)
38
+ }
39
+ })
40
+ }
41
+ }
@@ -0,0 +1,28 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ export default class extends Controller {
4
+ openModal() {
5
+ this.findElement("modal").open()
6
+ }
7
+
8
+ disableButton() {
9
+ this.findElement("button").disable()
10
+ }
11
+
12
+ enableButton() {
13
+ this.findElement("button").enable()
14
+ }
15
+
16
+ showToast() {
17
+ this.findElement("toast").show()
18
+ }
19
+
20
+ // Private
21
+
22
+ findElement(type) {
23
+ const targetId = this.element.dataset.target.replace("#", "")
24
+ const target = document.getElementById(targetId)
25
+ const controllerName = `polaris-${type}`
26
+ return this.application.getControllerForElementAndIdentifier(target, controllerName)
27
+ }
28
+ }