proscenium-ui 0.1.0

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.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +28 -0
  4. data/lib/proscenium/ui/badge/index.css +75 -0
  5. data/lib/proscenium/ui/badge.rb +32 -0
  6. data/lib/proscenium/ui/breadcrumbs/computed_element.rb +71 -0
  7. data/lib/proscenium/ui/breadcrumbs/control.rb +103 -0
  8. data/lib/proscenium/ui/breadcrumbs/element.rb +16 -0
  9. data/lib/proscenium/ui/breadcrumbs/index.css +84 -0
  10. data/lib/proscenium/ui/breadcrumbs.rb +136 -0
  11. data/lib/proscenium/ui/combobox/index.css +162 -0
  12. data/lib/proscenium/ui/combobox/index.js +420 -0
  13. data/lib/proscenium/ui/combobox.rb +186 -0
  14. data/lib/proscenium/ui/component.rb +22 -0
  15. data/lib/proscenium/ui/flash/index.css +1 -0
  16. data/lib/proscenium/ui/flash/index.js +77 -0
  17. data/lib/proscenium/ui/flash.rb +15 -0
  18. data/lib/proscenium/ui/form/field_methods.rb +95 -0
  19. data/lib/proscenium/ui/form/fields/base.rb +189 -0
  20. data/lib/proscenium/ui/form/fields/checkbox/index.jsx +48 -0
  21. data/lib/proscenium/ui/form/fields/checkbox/index.module.css +9 -0
  22. data/lib/proscenium/ui/form/fields/checkbox/previews/basic.jsx +8 -0
  23. data/lib/proscenium/ui/form/fields/checkbox.rb +32 -0
  24. data/lib/proscenium/ui/form/fields/combobox.rb +117 -0
  25. data/lib/proscenium/ui/form/fields/date.module.css +27 -0
  26. data/lib/proscenium/ui/form/fields/datetime.rb +15 -0
  27. data/lib/proscenium/ui/form/fields/hidden.rb +9 -0
  28. data/lib/proscenium/ui/form/fields/input/index.jsx +71 -0
  29. data/lib/proscenium/ui/form/fields/input/index.module.css +13 -0
  30. data/lib/proscenium/ui/form/fields/input/previews/basic.jsx +8 -0
  31. data/lib/proscenium/ui/form/fields/input.rb +14 -0
  32. data/lib/proscenium/ui/form/fields/radio_group.rb +175 -0
  33. data/lib/proscenium/ui/form/fields/radio_input/index.jsx +44 -0
  34. data/lib/proscenium/ui/form/fields/radio_input/index.module.css +13 -0
  35. data/lib/proscenium/ui/form/fields/radio_input/previews/basic.jsx +8 -0
  36. data/lib/proscenium/ui/form/fields/radio_input.rb +17 -0
  37. data/lib/proscenium/ui/form/fields/rich_textarea.css +23 -0
  38. data/lib/proscenium/ui/form/fields/rich_textarea.js +6 -0
  39. data/lib/proscenium/ui/form/fields/rich_textarea.rb +18 -0
  40. data/lib/proscenium/ui/form/fields/select.jsx +47 -0
  41. data/lib/proscenium/ui/form/fields/select.module.css +46 -0
  42. data/lib/proscenium/ui/form/fields/select.rb +302 -0
  43. data/lib/proscenium/ui/form/fields/tel.css +297 -0
  44. data/lib/proscenium/ui/form/fields/tel.js +83 -0
  45. data/lib/proscenium/ui/form/fields/tel.rb +54 -0
  46. data/lib/proscenium/ui/form/fields/textarea/index.jsx +50 -0
  47. data/lib/proscenium/ui/form/fields/textarea/index.module.css +13 -0
  48. data/lib/proscenium/ui/form/fields/textarea/previews/basic.jsx +8 -0
  49. data/lib/proscenium/ui/form/fields/textarea.rb +18 -0
  50. data/lib/proscenium/ui/form/index.css +52 -0
  51. data/lib/proscenium/ui/form/translation.rb +71 -0
  52. data/lib/proscenium/ui/form.rb +197 -0
  53. data/lib/proscenium/ui/railtie.rb +23 -0
  54. data/lib/proscenium/ui/ujs/class.js +15 -0
  55. data/lib/proscenium/ui/ujs/data_confirm.js +23 -0
  56. data/lib/proscenium/ui/ujs/data_disable_with.js +68 -0
  57. data/lib/proscenium/ui/ujs/index.js +10 -0
  58. data/lib/proscenium/ui/version.rb +7 -0
  59. data/lib/proscenium/ui.rb +36 -0
  60. metadata +177 -0
@@ -0,0 +1,162 @@
1
+ @layer pui {
2
+ pui-combobox {
3
+ position: relative;
4
+ display: inline-block;
5
+
6
+ [part='input'] {
7
+ width: 100%;
8
+ box-sizing: border-box;
9
+ }
10
+
11
+ &:not([data-multiple]) [part='input'] {
12
+ padding-right: 3em;
13
+ }
14
+
15
+ [part='clear'],
16
+ [part='toggle'] {
17
+ position: absolute;
18
+ top: 0;
19
+ right: 0;
20
+ bottom: 0;
21
+ display: flex;
22
+ align-items: center;
23
+ justify-content: center;
24
+ width: 1.5em;
25
+ background: none;
26
+ border: none;
27
+ cursor: pointer;
28
+ color: inherit;
29
+ font-size: 1em;
30
+ padding: 0;
31
+ line-height: 1;
32
+ transition:
33
+ opacity 0.15s ease,
34
+ transform 0.15s ease;
35
+
36
+ &[hidden] {
37
+ display: none;
38
+ }
39
+
40
+ @media (pointer: coarse) {
41
+ width: 2.5em;
42
+ }
43
+ }
44
+
45
+ [part='clear'] {
46
+ right: 1.5em;
47
+ opacity: 0.4;
48
+
49
+ &::after {
50
+ content: '';
51
+ position: absolute;
52
+ right: 0;
53
+ top: 25%;
54
+ height: 50%;
55
+ border-right: 1px solid color-mix(in srgb, currentColor 25%, transparent);
56
+ }
57
+
58
+ &:hover,
59
+ &:active {
60
+ opacity: 1;
61
+ }
62
+ }
63
+
64
+ [part='input'][aria-expanded='true'] ~ [part='toggle'] {
65
+ transform: rotate(180deg);
66
+ }
67
+
68
+ &[disabled] [part='clear'],
69
+ &[disabled] [part='toggle'] {
70
+ display: none;
71
+ }
72
+
73
+ [part='listbox'] {
74
+ position: absolute;
75
+ top: calc(100% + 2px);
76
+ left: 0;
77
+ right: 0;
78
+ max-height: 15em;
79
+ overflow-y: auto;
80
+ overscroll-behavior: contain;
81
+ scrollbar-gutter: stable;
82
+ margin: 0;
83
+ padding: 0;
84
+ list-style: none;
85
+ background-color: var(--pui-input-background-color, Canvas);
86
+ border: var(--pui-input-border, 1px solid);
87
+ border-radius: var(--pui-input-border-radius);
88
+ z-index: 10;
89
+ }
90
+
91
+ [role='option'] {
92
+ padding: 0.2em 0.4em;
93
+ cursor: pointer;
94
+
95
+ &[data-active] {
96
+ background-color: var(--pui-combobox-active-bg, SelectedItem);
97
+ color: var(--pui-combobox-active-color, SelectedItemText);
98
+ }
99
+
100
+ &[aria-selected='true'] {
101
+ font-weight: 600;
102
+ }
103
+
104
+ &[hidden] {
105
+ display: none;
106
+ }
107
+
108
+ @media (pointer: coarse) {
109
+ padding: 0.6em 0.5em;
110
+ }
111
+ }
112
+
113
+ [part='tags'] {
114
+ display: flex;
115
+ flex-wrap: wrap;
116
+ gap: 0.25em;
117
+ margin-bottom: 0.25em;
118
+ }
119
+
120
+ [part='tag'] {
121
+ display: inline-flex;
122
+ align-items: center;
123
+ gap: 0.25em;
124
+ padding: 0.1em 0.4em;
125
+ border: 1px solid;
126
+ border-radius: 0.2em;
127
+ font-size: 0.875em;
128
+ line-height: 1.25;
129
+ }
130
+
131
+ [part='tag-remove'] {
132
+ background: none;
133
+ border: none;
134
+ cursor: pointer;
135
+ padding: 0.1em;
136
+ min-width: 1.2em;
137
+ min-height: 1.2em;
138
+ line-height: 1;
139
+ font-size: 1em;
140
+ color: inherit;
141
+ opacity: 0.6;
142
+ transition: opacity 0.15s ease;
143
+
144
+ &:hover,
145
+ &:active {
146
+ opacity: 1;
147
+ }
148
+
149
+ @media (pointer: coarse) {
150
+ padding: 0.3em;
151
+ min-width: 2em;
152
+ min-height: 2em;
153
+ }
154
+ }
155
+
156
+ &[disabled] {
157
+ opacity: 0.5;
158
+ cursor: not-allowed;
159
+ pointer-events: none;
160
+ }
161
+ }
162
+ }
@@ -0,0 +1,420 @@
1
+ class PuiCombobox extends HTMLElement {
2
+ connectedCallback() {
3
+ this.$input = this.querySelector('[role="combobox"]')
4
+ this.$listbox = this.querySelector('[role="listbox"]')
5
+ this.$hidden = this.querySelector('input[type="hidden"]')
6
+ this.$toggle = this.querySelector('[part="toggle"]')
7
+ this.$clear = this.querySelector('[part="clear"]')
8
+ this._uid = this.$listbox.id.replace('-listbox', '')
9
+ this._activeIndex = -1
10
+ this._abortController = null
11
+ this._debounceTimer = null
12
+
13
+ this.$input.addEventListener('input', this)
14
+ this.$input.addEventListener('keydown', this)
15
+ this.$input.addEventListener('focus', this)
16
+ document.addEventListener('click', this)
17
+
18
+ if (this.multiple) {
19
+ this.querySelectorAll('[part="tag-remove"]').forEach(btn => {
20
+ btn.addEventListener('click', this)
21
+ })
22
+ }
23
+ }
24
+
25
+ disconnectedCallback() {
26
+ this.$input.removeEventListener('input', this)
27
+ this.$input.removeEventListener('keydown', this)
28
+ this.$input.removeEventListener('focus', this)
29
+ document.removeEventListener('click', this)
30
+
31
+ if (this._abortController) this._abortController.abort()
32
+ if (this._debounceTimer) clearTimeout(this._debounceTimer)
33
+ }
34
+
35
+ handleEvent(event) {
36
+ switch (event.type) {
37
+ case 'input':
38
+ this.#onInput(event)
39
+ break
40
+ case 'keydown':
41
+ this.#onKeydown(event)
42
+ break
43
+ case 'focus':
44
+ this.#onFocus()
45
+ break
46
+ case 'click':
47
+ this.#onClick(event)
48
+ break
49
+ }
50
+ }
51
+
52
+ get multiple() {
53
+ return this.hasAttribute('data-multiple')
54
+ }
55
+
56
+ get src() {
57
+ return this.getAttribute('data-src')
58
+ }
59
+
60
+ get minChars() {
61
+ return parseInt(this.getAttribute('data-min-chars') || '0', 10)
62
+ }
63
+
64
+ get debounce() {
65
+ return parseInt(this.getAttribute('data-debounce') || '300', 10)
66
+ }
67
+
68
+ get options() {
69
+ return Array.from(this.$listbox.querySelectorAll('[role="option"]'))
70
+ }
71
+
72
+ #onFocus() {
73
+ if (!this.src && this.$input.value === '') {
74
+ this.#showAll()
75
+ }
76
+ }
77
+
78
+ #onInput() {
79
+ this.#updateClearButton()
80
+ const term = this.$input.value.trim()
81
+
82
+ if (term.length < this.minChars) {
83
+ if (this.src) this.$listbox.innerHTML = ''
84
+ this.#close()
85
+ return
86
+ }
87
+
88
+ if (this.src) {
89
+ this.#fetchAsync(term)
90
+ } else {
91
+ this.#filterStatic(term)
92
+ }
93
+ }
94
+
95
+ #filterStatic(term) {
96
+ const lower = term.toLowerCase()
97
+ let hasVisible = false
98
+
99
+ this.options.forEach(opt => {
100
+ const match = opt.textContent.toLowerCase().includes(lower)
101
+ opt.hidden = !match
102
+ if (match) hasVisible = true
103
+ })
104
+
105
+ if (hasVisible) {
106
+ this.#open()
107
+ } else {
108
+ this.#close()
109
+ }
110
+
111
+ this._activeIndex = -1
112
+ this.#clearActive()
113
+ if (this.$toggle) this.$toggle.hidden = !hasVisible
114
+ }
115
+
116
+ #fetchAsync(term) {
117
+ if (this._debounceTimer) clearTimeout(this._debounceTimer)
118
+
119
+ this._debounceTimer = setTimeout(async () => {
120
+ if (this._abortController) this._abortController.abort()
121
+ this._abortController = new AbortController()
122
+
123
+ try {
124
+ const url = new URL(this.src, window.location.origin)
125
+ url.searchParams.set('q', term)
126
+
127
+ const response = await fetch(url.toString(), {
128
+ signal: this._abortController.signal,
129
+ headers: { Accept: 'application/json' }
130
+ })
131
+ const data = await response.json()
132
+
133
+ this.$listbox.innerHTML = ''
134
+ data.forEach((item, i) => {
135
+ const li = document.createElement('li')
136
+ li.setAttribute('role', 'option')
137
+ li.id = `${this._uid}-option-${i}`
138
+ li.dataset.value = item.value
139
+ li.textContent = item.label
140
+
141
+ if (this.#isSelected(String(item.value))) {
142
+ li.setAttribute('aria-selected', 'true')
143
+ }
144
+
145
+ this.$listbox.appendChild(li)
146
+ })
147
+
148
+ this._activeIndex = -1
149
+ if (data.length > 0) {
150
+ this.#open()
151
+ } else {
152
+ this.#close()
153
+ }
154
+ } catch (e) {
155
+ if (e.name !== 'AbortError') throw e
156
+ }
157
+ }, this.debounce)
158
+ }
159
+
160
+ #onKeydown(event) {
161
+ const opts = this.options.filter(o => !o.hidden)
162
+
163
+ switch (event.key) {
164
+ case 'ArrowDown':
165
+ event.preventDefault()
166
+ if (!this.#isOpen()) {
167
+ this.#showAll()
168
+ } else {
169
+ this._activeIndex = Math.min(this._activeIndex + 1, opts.length - 1)
170
+ this.#setActive(opts)
171
+ }
172
+ break
173
+
174
+ case 'ArrowUp':
175
+ event.preventDefault()
176
+ this._activeIndex = Math.max(this._activeIndex - 1, 0)
177
+ this.#setActive(opts)
178
+ break
179
+
180
+ case 'Home':
181
+ event.preventDefault()
182
+ this._activeIndex = 0
183
+ this.#setActive(opts)
184
+ break
185
+
186
+ case 'End':
187
+ event.preventDefault()
188
+ this._activeIndex = opts.length - 1
189
+ this.#setActive(opts)
190
+ break
191
+
192
+ case 'Enter':
193
+ event.preventDefault()
194
+ if (this._activeIndex >= 0 && this._activeIndex < opts.length) {
195
+ this.#selectOption(opts[this._activeIndex])
196
+ }
197
+ break
198
+
199
+ case 'Escape':
200
+ this.#close()
201
+ this.$input.blur()
202
+ break
203
+
204
+ case 'Backspace':
205
+ if (this.multiple && this.$input.value === '') {
206
+ this.#removeLastTag()
207
+ }
208
+ break
209
+ }
210
+ }
211
+
212
+ #onClick(event) {
213
+ const option = event.target.closest('[role="option"]')
214
+ if (option && this.$listbox.contains(option)) {
215
+ this.#selectOption(option)
216
+ return
217
+ }
218
+
219
+ const removeBtn = event.target.closest('[part="tag-remove"]')
220
+ if (removeBtn && this.contains(removeBtn)) {
221
+ const tag = removeBtn.closest("[part='tag']")
222
+ if (tag) this.#removeTag(tag.dataset.value)
223
+ return
224
+ }
225
+
226
+ if (this.$clear && event.target.closest('[part="clear"]')) {
227
+ this.$input.value = ''
228
+ this.$hidden.value = ''
229
+ if (this.src) {
230
+ this.$listbox.innerHTML = ''
231
+ } else {
232
+ this.#clearSelected()
233
+ }
234
+ this.#updateClearButton()
235
+ this.#close()
236
+ this.$input.focus()
237
+ this.dispatchEvent(
238
+ new CustomEvent('combobox:change', { detail: { value: '' }, bubbles: true })
239
+ )
240
+ return
241
+ }
242
+
243
+ if (this.$toggle && event.target.closest('[part="toggle"]')) {
244
+ if (this.#isOpen()) {
245
+ this.#close()
246
+ } else {
247
+ this.#showAll()
248
+ this.$input.focus()
249
+ }
250
+ return
251
+ }
252
+
253
+ if (!this.contains(event.target)) {
254
+ this.#close()
255
+ }
256
+ }
257
+
258
+ #selectOption(option) {
259
+ const value = option.dataset.value
260
+ const label = option.textContent
261
+
262
+ if (this.multiple) {
263
+ if (this.#isSelected(value)) {
264
+ this.#removeTag(value)
265
+ } else {
266
+ this.#addTag(label, value)
267
+ option.setAttribute('aria-selected', 'true')
268
+ }
269
+ this.$input.value = ''
270
+ this.$input.focus()
271
+ } else {
272
+ this.$input.value = label
273
+ this.$hidden.value = value
274
+
275
+ this.#clearSelected()
276
+ option.setAttribute('aria-selected', 'true')
277
+
278
+ this.#updateClearButton()
279
+ this.#close()
280
+ }
281
+
282
+ this.dispatchEvent(
283
+ new CustomEvent('combobox:change', {
284
+ detail: { value: this.multiple ? this.#selectedValues() : value },
285
+ bubbles: true
286
+ })
287
+ )
288
+ }
289
+
290
+ #addTag(label, value) {
291
+ // Add hidden input
292
+ const hiddenInput = document.createElement('input')
293
+ hiddenInput.type = 'hidden'
294
+ hiddenInput.name = this.$hidden.name + '[]'
295
+ hiddenInput.value = value
296
+ this.$hidden.after(hiddenInput)
297
+
298
+ // Add tag element
299
+ const tagsContainer = this.querySelector('[part="tags"]')
300
+ const tag = document.createElement('span')
301
+ tag.setAttribute('part', 'tag')
302
+ tag.dataset.value = value
303
+ tag.textContent = label + ' '
304
+
305
+ const removeBtn = document.createElement('button')
306
+ removeBtn.setAttribute('part', 'tag-remove')
307
+ removeBtn.type = 'button'
308
+ removeBtn.setAttribute('aria-label', `Remove ${label}`)
309
+ removeBtn.innerHTML = '&times;'
310
+ removeBtn.addEventListener('click', this)
311
+
312
+ tag.appendChild(removeBtn)
313
+ tagsContainer.appendChild(tag)
314
+ }
315
+
316
+ #removeTag(value) {
317
+ // Remove hidden input
318
+ const input = this.querySelector(
319
+ `input[type="hidden"][value="${CSS.escape(value)}"][name$="[]"]`
320
+ )
321
+ if (input) input.remove()
322
+
323
+ // Remove tag element
324
+ const tag = this.querySelector(`[part="tag"][data-value="${CSS.escape(value)}"]`)
325
+ if (tag) {
326
+ const btn = tag.querySelector('[part="tag-remove"]')
327
+ if (btn) btn.removeEventListener('click', this)
328
+ tag.remove()
329
+ }
330
+
331
+ // Update listbox
332
+ this.options.forEach(opt => {
333
+ if (opt.dataset.value === value) {
334
+ opt.removeAttribute('aria-selected')
335
+ }
336
+ })
337
+
338
+ this.dispatchEvent(
339
+ new CustomEvent('combobox:change', {
340
+ detail: { value: this.#selectedValues() },
341
+ bubbles: true
342
+ })
343
+ )
344
+ }
345
+
346
+ #removeLastTag() {
347
+ const tags = this.querySelectorAll('[part="tag"]')
348
+ if (tags.length > 0) {
349
+ const lastTag = tags[tags.length - 1]
350
+ this.#removeTag(lastTag.dataset.value)
351
+ }
352
+ }
353
+
354
+ #isSelected(value) {
355
+ return !!this.querySelector(`input[type="hidden"][value="${CSS.escape(value)}"][name$="[]"]`)
356
+ }
357
+
358
+ #selectedValues() {
359
+ return Array.from(this.querySelectorAll('input[type="hidden"][name$="[]"]')).map(
360
+ input => input.value
361
+ )
362
+ }
363
+
364
+ #showAll() {
365
+ this.options.forEach(opt => (opt.hidden = false))
366
+ this.#open()
367
+ }
368
+
369
+ #open() {
370
+ this.$listbox.hidden = false
371
+ this.$input.setAttribute('aria-expanded', 'true')
372
+ this.#updateToggleButton()
373
+ }
374
+
375
+ #close() {
376
+ this.$listbox.hidden = true
377
+ this.$input.setAttribute('aria-expanded', 'false')
378
+ this._activeIndex = -1
379
+ this.#clearActive()
380
+ this.#updateToggleButton()
381
+ }
382
+
383
+ #isOpen() {
384
+ return !this.$listbox.hidden
385
+ }
386
+
387
+ #setActive(opts) {
388
+ this.#clearActive()
389
+ if (this._activeIndex >= 0 && this._activeIndex < opts.length) {
390
+ const opt = opts[this._activeIndex]
391
+ opt.setAttribute('data-active', '')
392
+ this.$input.setAttribute('aria-activedescendant', opt.id)
393
+ opt.scrollIntoView({ block: 'nearest' })
394
+ }
395
+ }
396
+
397
+ #updateClearButton() {
398
+ if (this.$clear) {
399
+ this.$clear.hidden = this.$input.value === '' && this.$hidden.value === ''
400
+ }
401
+ }
402
+
403
+ #updateToggleButton() {
404
+ if (this.$toggle) {
405
+ const hasVisibleOptions = this.options.some(o => !o.hidden)
406
+ this.$toggle.hidden = !hasVisibleOptions
407
+ }
408
+ }
409
+
410
+ #clearSelected() {
411
+ this.options.forEach(o => o.removeAttribute('aria-selected'))
412
+ }
413
+
414
+ #clearActive() {
415
+ this.options.forEach(o => o.removeAttribute('data-active'))
416
+ this.$input.removeAttribute('aria-activedescendant')
417
+ }
418
+ }
419
+
420
+ !customElements.get('pui-combobox') && customElements.define('pui-combobox', PuiCombobox)