velvetkit 0.4.1 → 0.5.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 (148) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +143 -2
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/accordion/trigger.rb +1 -1
  5. data/registry/app/components/ui/badge.rb +1 -1
  6. data/registry/app/components/ui/base.rb +2 -2
  7. data/registry/app/components/ui/button.rb +1 -1
  8. data/registry/app/components/ui/card/action.html.erb +1 -0
  9. data/registry/app/components/ui/card/action.rb +14 -0
  10. data/registry/app/components/ui/card/content.html.erb +1 -1
  11. data/registry/app/components/ui/card/content.rb +4 -2
  12. data/registry/app/components/ui/card/description.html.erb +1 -1
  13. data/registry/app/components/ui/card/description.rb +4 -2
  14. data/registry/app/components/ui/card/footer.html.erb +1 -1
  15. data/registry/app/components/ui/card/footer.rb +4 -2
  16. data/registry/app/components/ui/card/header.html.erb +1 -1
  17. data/registry/app/components/ui/card/header.rb +9 -2
  18. data/registry/app/components/ui/card/title.html.erb +1 -1
  19. data/registry/app/components/ui/card/title.rb +4 -2
  20. data/registry/app/components/ui/card.html.erb +1 -1
  21. data/registry/app/components/ui/card.rb +49 -3
  22. data/registry/app/components/ui/carousel.html.erb +7 -0
  23. data/registry/app/components/ui/carousel.rb +60 -0
  24. data/registry/app/components/ui/chart.html.erb +3 -3
  25. data/registry/app/components/ui/chart.rb +18 -10
  26. data/registry/app/components/ui/checkbox.html.erb +6 -0
  27. data/registry/app/components/ui/checkbox.rb +11 -4
  28. data/registry/app/components/ui/fieldset.html.erb +6 -0
  29. data/registry/app/components/ui/fieldset.rb +76 -0
  30. data/registry/app/components/ui/gallery/image.rb +14 -1
  31. data/registry/app/components/ui/gallery.html.erb +32 -3
  32. data/registry/app/components/ui/gallery.rb +46 -7
  33. data/registry/app/components/ui/link.rb +1 -1
  34. data/registry/app/components/ui/menu/item.rb +1 -1
  35. data/registry/app/components/ui/radio_group/item.html.erb +1 -1
  36. data/registry/app/components/ui/radio_group/item.rb +5 -1
  37. data/registry/app/components/ui/radio_group.html.erb +1 -1
  38. data/registry/app/components/ui/radio_group.rb +42 -2
  39. data/registry/app/components/ui/scrubber.html.erb +18 -0
  40. data/registry/app/components/ui/scrubber.rb +62 -0
  41. data/registry/app/components/ui/select.html.erb +1 -1
  42. data/registry/app/components/ui/select.rb +16 -1
  43. data/registry/app/components/ui/selection_bar.html.erb +7 -0
  44. data/registry/app/components/ui/selection_bar.rb +75 -0
  45. data/registry/app/components/ui/sidebar/group_label.rb +1 -1
  46. data/registry/app/components/ui/sidebar/inset.rb +5 -2
  47. data/registry/app/components/ui/sidebar/menu_button.rb +24 -9
  48. data/registry/app/components/ui/sidebar/menu_item.html.erb +1 -1
  49. data/registry/app/components/ui/sidebar/menu_item.rb +31 -5
  50. data/registry/app/components/ui/sidebar/menu_sub.rb +59 -0
  51. data/registry/app/components/ui/sidebar.html.erb +3 -3
  52. data/registry/app/components/ui/sidebar.rb +91 -13
  53. data/registry/app/components/ui/switch.rb +2 -2
  54. data/registry/app/components/ui/table/column.html.erb +1 -1
  55. data/registry/app/components/ui/table/column.rb +26 -2
  56. data/registry/app/components/ui/table/head.html.erb +1 -1
  57. data/registry/app/components/ui/table/head.rb +15 -2
  58. data/registry/app/components/ui/table/row.html.erb +1 -1
  59. data/registry/app/components/ui/table/row.rb +19 -5
  60. data/registry/app/components/ui/table.html.erb +2 -2
  61. data/registry/app/components/ui/table.rb +29 -4
  62. data/registry/app/components/ui/tabs/trigger.rb +1 -1
  63. data/registry/app/components/ui/toggle_group.html.erb +1 -1
  64. data/registry/app/components/ui/toggle_group.rb +26 -3
  65. data/registry/app/javascript/controllers/ui/carousel_controller.ts +63 -0
  66. data/registry/app/javascript/controllers/ui/file_field_controller.ts +1 -1
  67. data/registry/app/javascript/controllers/ui/gallery_controller.ts +52 -14
  68. data/registry/app/javascript/controllers/ui/scrubber_controller.ts +188 -0
  69. data/registry/app/javascript/controllers/ui/selection_bar_controller.ts +122 -0
  70. data/registry/app/javascript/controllers/ui/sidebar_controller.ts +102 -32
  71. data/registry/components.yml +12 -4
  72. data/registry/config/locales/velvetkit.en.yml +16 -0
  73. data/registry/icons/lucide/arrow-down.svg +16 -0
  74. data/registry/icons/lucide/arrow-up.svg +16 -0
  75. data/registry/icons/lucide/image.svg +17 -0
  76. data/registry/icons/lucide/star.svg +15 -0
  77. data/registry/icons/phosphor/arrow-down.svg +2 -0
  78. data/registry/icons/phosphor/arrow-up.svg +2 -0
  79. data/registry/icons/phosphor/image.svg +2 -0
  80. data/registry/icons/phosphor/star.svg +2 -0
  81. data/registry/icons/tabler/arrow-down.svg +18 -0
  82. data/registry/icons/tabler/arrow-up.svg +18 -0
  83. data/registry/icons/tabler/photo.svg +19 -0
  84. data/registry/icons/tabler/star.svg +16 -0
  85. data/registry/icons.yml +4 -0
  86. data/registry/test/components/previews/ui/card_preview/edge_to_edge.html.erb +19 -0
  87. data/registry/test/components/previews/ui/card_preview/image.html.erb +12 -0
  88. data/registry/test/components/previews/ui/card_preview/just_content.html.erb +10 -0
  89. data/registry/test/components/previews/ui/card_preview/open_bill.html.erb +18 -0
  90. data/registry/test/components/previews/ui/card_preview/playground.html.erb +17 -0
  91. data/registry/test/components/previews/ui/card_preview/settings_form.html.erb +15 -24
  92. data/registry/test/components/previews/ui/card_preview/sign_in.html.erb +23 -0
  93. data/registry/test/components/previews/ui/card_preview/small.html.erb +17 -0
  94. data/registry/test/components/previews/ui/card_preview/spacing.html.erb +13 -0
  95. data/registry/test/components/previews/ui/card_preview/stats.html.erb +11 -0
  96. data/registry/test/components/previews/ui/card_preview/team.html.erb +21 -0
  97. data/registry/test/components/previews/ui/card_preview.rb +42 -28
  98. data/registry/test/components/previews/ui/carousel_preview/highlights.html.erb +17 -0
  99. data/registry/test/components/previews/ui/carousel_preview.rb +22 -0
  100. data/registry/test/components/previews/ui/chart_preview/whole_dollars.html.erb +19 -0
  101. data/registry/test/components/previews/ui/chart_preview.rb +7 -1
  102. data/registry/test/components/previews/ui/checkbox_preview/in_form.html.erb +2 -3
  103. data/registry/test/components/previews/ui/fieldset_preview/block_of_fields.html.erb +18 -0
  104. data/registry/test/components/previews/ui/fieldset_preview/groups.html.erb +14 -0
  105. data/registry/test/components/previews/ui/fieldset_preview/model_errors.html.erb +22 -0
  106. data/registry/test/components/previews/ui/fieldset_preview/playground.html.erb +5 -0
  107. data/registry/test/components/previews/ui/fieldset_preview.rb +24 -0
  108. data/registry/test/components/previews/ui/gallery_preview/grid.html.erb +25 -0
  109. data/registry/test/components/previews/ui/gallery_preview.rb +3 -0
  110. data/registry/test/components/previews/ui/scrubber_preview/default.html.erb +24 -0
  111. data/registry/test/components/previews/ui/scrubber_preview.rb +6 -0
  112. data/registry/test/components/previews/ui/selection_bar_preview/floating.html.erb +16 -0
  113. data/registry/test/components/previews/ui/selection_bar_preview/table.html.erb +26 -0
  114. data/registry/test/components/previews/ui/selection_bar_preview.rb +9 -0
  115. data/registry/test/components/previews/ui/sidebar_preview/_shell.html.erb +5 -4
  116. data/registry/test/components/previews/ui/sidebar_preview/pill_links.html.erb +52 -0
  117. data/registry/test/components/previews/ui/sidebar_preview.rb +3 -0
  118. data/registry/test/components/previews/ui/table_preview/selectable.html.erb +26 -0
  119. data/registry/test/components/previews/ui/table_preview/sortable.html.erb +20 -0
  120. data/registry/test/components/previews/ui/table_preview/sticky_header.html.erb +13 -0
  121. data/registry/test/components/previews/ui/table_preview.rb +9 -0
  122. data/registry/test/components/ui/button_test.rb +2 -2
  123. data/registry/test/components/ui/card/action_test.rb +19 -0
  124. data/registry/test/components/ui/card/content_test.rb +1 -11
  125. data/registry/test/components/ui/card/footer_test.rb +3 -2
  126. data/registry/test/components/ui/card/header_test.rb +3 -4
  127. data/registry/test/components/ui/card/title_test.rb +3 -4
  128. data/registry/test/components/ui/card_test.rb +63 -2
  129. data/registry/test/components/ui/carousel_test.rb +38 -0
  130. data/registry/test/components/ui/chart_test.rb +20 -0
  131. data/registry/test/components/ui/checkbox_test.rb +11 -2
  132. data/registry/test/components/ui/fieldset_test.rb +119 -0
  133. data/registry/test/components/ui/gallery_test.rb +40 -5
  134. data/registry/test/components/ui/link_test.rb +1 -1
  135. data/registry/test/components/ui/number_input_test.rb +1 -1
  136. data/registry/test/components/ui/radio_group_test.rb +106 -0
  137. data/registry/test/components/ui/scrubber_test.rb +43 -0
  138. data/registry/test/components/ui/select_test.rb +21 -0
  139. data/registry/test/components/ui/selection_bar_test.rb +46 -0
  140. data/registry/test/components/ui/sidebar/menu_button_test.rb +9 -0
  141. data/registry/test/components/ui/sidebar_test.rb +79 -1
  142. data/registry/test/components/ui/switch_test.rb +4 -4
  143. data/registry/test/components/ui/toggle_group_test.rb +45 -0
  144. metadata +60 -5
  145. data/registry/test/components/previews/ui/card_preview/complete.html.erb +0 -31
  146. data/registry/test/components/previews/ui/card_preview/default.html.erb +0 -7
  147. data/registry/test/components/previews/ui/card_preview/with_footer.html.erb +0 -13
  148. data/registry/test/components/previews/ui/card_preview/with_header.html.erb +0 -11
@@ -13,6 +13,11 @@
13
13
  # joins bordered buttons. Give the group a `label:` (its accessible name), and
14
14
  # give an icon-only item a `label:` too, which becomes its hidden text and
15
15
  # tooltip. Listen for `change` on the group to act as soon as the choice changes.
16
+ #
17
+ # For a visible name, give it a `legend:` (and a `hint:` or `error:`): it then
18
+ # renders through UI::Fieldset, which takes the group's role and name, around
19
+ # the buttons. Bound to a form, the model's errors on the field show on their
20
+ # own, and `legend: true` uses the attribute's name.
16
21
  class UI::ToggleGroup < UI::Base
17
22
  variants(
18
23
  base: "inline-flex w-fit items-center",
@@ -31,7 +36,8 @@ class UI::ToggleGroup < UI::Base
31
36
  }
32
37
 
33
38
  def initialize(name: nil, value: nil, multiple: false, variant: :default, size: :default,
34
- label: nil, form: nil, field: nil, required: false, disabled: false,
39
+ label: nil, legend: nil, hint: nil, error: nil,
40
+ form: nil, field: nil, required: false, disabled: false,
35
41
  class: nil, **html_attrs)
36
42
  @name = name
37
43
  @value = value
@@ -39,6 +45,9 @@ class UI::ToggleGroup < UI::Base
39
45
  @variant = variant
40
46
  @size = size
41
47
  @label = label
48
+ @legend = legend
49
+ @hint = hint
50
+ @error = error
42
51
  @form = form
43
52
  @field = field
44
53
  @required = required
@@ -68,12 +77,26 @@ class UI::ToggleGroup < UI::Base
68
77
  Array(value).map(&:to_s)
69
78
  end
70
79
 
80
+ def role = multiple? ? "group" : "radiogroup"
81
+
82
+ # The legend shows only when given: a bound group that's never been named
83
+ # doesn't grow one when it has an error.
84
+ def fieldset
85
+ @fieldset ||= UI::Fieldset.new(legend: @legend.presence || false, hint: @hint, error: @error,
86
+ form: @form, field: @field,
87
+ required: required?, disabled: @disabled,
88
+ role: role, aria: { label: @label.presence }.compact.merge(@html_attrs[:aria] || {}))
89
+ end
90
+
91
+ # Inside a fieldset, the fieldset has the role and the aria attributes that
92
+ # name and describe the group; the buttons' track keeps the rest.
71
93
  def root_attrs
94
+ wrapped = fieldset.wrap?
72
95
  attrs = @html_attrs.except(:data, :aria)
73
- attrs[:role] = multiple? ? "group" : "radiogroup"
96
+ attrs[:role] = role unless wrapped
74
97
  attrs[:class] = cn(variant_classes(variant: @variant), @user_class)
75
98
  attrs[:data] = { slot: "toggle-group", variant: @variant }.merge(@html_attrs[:data] || {})
76
- attrs[:aria] = { label: @label.presence }.compact.merge(@html_attrs[:aria] || {})
99
+ attrs[:aria] = { label: @label.presence }.compact.merge(@html_attrs[:aria] || {}) unless wrapped
77
100
  attrs
78
101
  end
79
102
  end
@@ -0,0 +1,63 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ // Connects to data-controller="ui--carousel"
4
+ //
5
+ // The strip scrolls by itself (scroll-snap); this only moves it a page at a
6
+ // time from the buttons, and shows each button while there's more that way.
7
+ const EDGE = 2;
8
+
9
+ export default class extends Controller<HTMLElement> {
10
+ static targets = ['track', 'previous', 'next'];
11
+
12
+ declare readonly trackTarget: HTMLElement;
13
+ declare readonly previousTarget: HTMLButtonElement;
14
+ declare readonly nextTarget: HTMLButtonElement;
15
+
16
+ private frame?: number;
17
+ private observer?: ResizeObserver;
18
+
19
+ connect() {
20
+ this.observer = new ResizeObserver(() => this.update());
21
+ this.observer.observe(this.trackTarget);
22
+ this.update();
23
+ }
24
+
25
+ disconnect() {
26
+ this.observer?.disconnect();
27
+ if (this.frame) cancelAnimationFrame(this.frame);
28
+ }
29
+
30
+ previous() {
31
+ this.page(-1);
32
+ }
33
+
34
+ next() {
35
+ this.page(1);
36
+ }
37
+
38
+ update() {
39
+ if (this.frame) return;
40
+
41
+ this.frame = requestAnimationFrame(() => {
42
+ this.frame = undefined;
43
+ const track = this.trackTarget;
44
+ // scrollLeft runs negative in a right-to-left page.
45
+ const scrolled = Math.abs(track.scrollLeft);
46
+ this.show(this.previousTarget, this.nextTarget, scrolled > EDGE);
47
+ this.show(this.nextTarget, this.previousTarget, scrolled + track.clientWidth < track.scrollWidth - EDGE);
48
+ });
49
+ }
50
+
51
+ // Most of a strip's width, so the last card in view is the first one after.
52
+ private page(direction: number) {
53
+ const track = this.trackTarget;
54
+ const forward = getComputedStyle(track).direction === 'rtl' ? -1 : 1;
55
+ track.scrollBy({ left: direction * forward * track.clientWidth * 0.85 });
56
+ }
57
+
58
+ // A button that hides under the keyboard would drop focus to the page.
59
+ private show(button: HTMLButtonElement, other: HTMLButtonElement, shown: boolean) {
60
+ if (!shown && document.activeElement === button) other.focus();
61
+ button.hidden = !shown;
62
+ }
63
+ }
@@ -222,7 +222,7 @@ export default class extends Controller<HTMLElement> {
222
222
  new DirectUpload(file, this.urlValue, {
223
223
  directUploadWillCreateBlobWithXHR: (xhr: XMLHttpRequest) => { state.xhr = xhr; },
224
224
  directUploadWillStoreFileWithXHR: watch,
225
- }).create((error: Error | null, blob?: { signed_id: string }) => {
225
+ }).create((error: string | null, blob?: { signed_id: string }) => {
226
226
  state.done = true;
227
227
  if (!item.isConnected) return;
228
228
  if (error || !blob) {
@@ -33,10 +33,17 @@ type Rect = { left: number; top: number; width: number; height: number };
33
33
  type IndexEvent<E extends Event = Event> = E & { params: { index: number } };
34
34
 
35
35
  export default class extends Controller<HTMLElement> {
36
- static targets = ['track', 'slide', 'thumbnail', 'status', 'dialog', 'viewport', 'zoomImage', 'zoomStatus', 'zoomIn', 'zoomOut', 'lens', 'pane'];
36
+ static targets = ['track', 'slide', 'trigger', 'thumbnail', 'status', 'dialog', 'viewport', 'zoomImage', 'zoomStatus', 'zoomIn', 'zoomOut', 'lens', 'pane',
37
+ 'perImage', 'infoPanel', 'infoToggle'];
37
38
  static values = { position: String, zoomScale: Number };
38
39
 
40
+ declare readonly hasTrackTarget: boolean;
39
41
  declare readonly trackTarget: HTMLElement;
42
+ declare readonly triggerTargets: HTMLElement[];
43
+ declare readonly perImageTargets: HTMLElement[];
44
+ declare readonly hasInfoPanelTarget: boolean;
45
+ declare readonly infoPanelTarget: HTMLElement;
46
+ declare readonly infoToggleTarget: HTMLElement;
40
47
  declare readonly slideTargets: HTMLElement[];
41
48
  declare readonly thumbnailTargets: HTMLElement[];
42
49
  declare readonly statusTarget: HTMLElement;
@@ -90,6 +97,7 @@ export default class extends Controller<HTMLElement> {
90
97
  private goTo(index: number, behavior: ScrollBehavior = this.scrollBehavior) {
91
98
  this.heading = index;
92
99
  this.setIndex(index);
100
+ if (!this.hasTrackTarget) return;
93
101
  this.trackTarget.scrollTo({ left: this.scrollPosition(this.trackTarget, this.slideTargets[index]), behavior });
94
102
  }
95
103
 
@@ -133,7 +141,8 @@ export default class extends Controller<HTMLElement> {
133
141
  this.index = index;
134
142
  this.statusTarget.dataset.ready = 'true';
135
143
  this.statusTarget.textContent = this.position(index);
136
- this.slideTargets.forEach((_, i) => this.slideButton(i)?.setAttribute('tabindex', i === index ? '0' : '-1'));
144
+ if (this.hasTrackTarget) this.slideTargets.forEach((_, i) => this.slideButton(i)?.setAttribute('tabindex', i === index ? '0' : '-1'));
145
+ this.perImageTargets.forEach((element) => (element.hidden = Number(element.dataset.index) !== index));
137
146
  this.thumbnailTargets.forEach((thumbnail, i) => {
138
147
  if (i === index) thumbnail.setAttribute('aria-current', 'true');
139
148
  else thumbnail.removeAttribute('aria-current');
@@ -142,10 +151,34 @@ export default class extends Controller<HTMLElement> {
142
151
  if (strip) strip.scrollTo({ left: this.scrollPosition(strip, this.thumbnailTargets[index]), behavior: this.scrollBehavior });
143
152
  }
144
153
 
145
- private slideButton(index: number) {
154
+ private slideButton(index: number): HTMLElement | null {
155
+ if (!this.hasTrackTarget) return this.triggerFor(index);
146
156
  return this.slideTargets[index].querySelector('button');
147
157
  }
148
158
 
159
+ // With `strip: false` the page's own buttons open the overlay, and the image
160
+ // in each is where it grows from.
161
+ private triggerFor(index: number): HTMLElement | null {
162
+ return this.triggerTargets.find((trigger) => Number(trigger.dataset.uiGalleryIndexParam) === index) ?? null;
163
+ }
164
+
165
+ // Where image `index` is on the page, and the picture drawn there.
166
+ private origin(index: number): { frame: HTMLElement; image: HTMLImageElement | null } | null {
167
+ const frame = this.hasTrackTarget ? this.slideTargets[index] : this.triggerFor(index);
168
+ if (!frame) return null;
169
+ return { frame, image: frame.querySelector('img') };
170
+ }
171
+
172
+ toggleInfo() {
173
+ if (!this.hasInfoPanelTarget) return;
174
+
175
+ const open = this.infoPanelTarget.hidden;
176
+ this.infoPanelTarget.hidden = !open;
177
+ this.infoToggleTarget.setAttribute('aria-expanded', String(open));
178
+ this.dialogTarget.toggleAttribute('data-info', open);
179
+ this.layout();
180
+ }
181
+
149
182
  // Where to scroll `box` so `item` sits in its middle. Measured from what's on
150
183
  // screen, so it holds in a right-to-left page, where scrollLeft runs negative.
151
184
  private scrollPosition(box: HTMLElement, item: HTMLElement): number {
@@ -254,11 +287,10 @@ export default class extends Controller<HTMLElement> {
254
287
  this.animate(this.dialogTarget, { opacity: [0, 1] }, { ...fade, pseudoElement: '::backdrop' });
255
288
  this.overlayButtons.forEach((button) => this.animate(button, { opacity: [0, 1] }, fade));
256
289
 
257
- const slide = this.slideTargets[this.index];
290
+ const from = this.origin(this.index);
258
291
  const image = this.zoomImageTarget;
259
- const origin = slide.querySelector('img');
260
- if (origin?.naturalWidth && image.naturalWidth) {
261
- this.fly(this.drawnImage(slide, origin), image.getBoundingClientRect(), OPEN_MS);
292
+ if (from?.image?.naturalWidth && image.naturalWidth) {
293
+ this.fly(this.drawnImage(from.frame, from.image), image.getBoundingClientRect(), OPEN_MS);
262
294
  } else {
263
295
  this.animate(image, { opacity: [0, 1] }, fade);
264
296
  }
@@ -271,15 +303,14 @@ export default class extends Controller<HTMLElement> {
271
303
  if (this.closing) return;
272
304
 
273
305
  this.goTo(this.index, 'auto');
274
- const slide = this.slideTargets[this.index];
306
+ const to = this.origin(this.index);
275
307
  const image = this.zoomImageTarget;
276
- const origin = slide.querySelector('img');
277
308
  const fade = { duration: CLOSE_MS, easing: 'ease-in', fill: 'forwards' as const };
278
309
  const animations = [
279
310
  this.animate(this.dialogTarget, { opacity: [1, 0] }, { ...fade, pseudoElement: '::backdrop' }),
280
311
  ...this.overlayButtons.map((button) => this.animate(button, { opacity: [1, 0] }, fade)),
281
- origin?.naturalWidth && image.naturalWidth
282
- ? this.fly(image.getBoundingClientRect(), this.drawnImage(slide, origin), CLOSE_MS, true)
312
+ to?.image?.naturalWidth && image.naturalWidth && this.onScreen(to.frame)
313
+ ? this.fly(image.getBoundingClientRect(), this.drawnImage(to.frame, to.image), CLOSE_MS, true)
283
314
  : this.animate(image, { opacity: [1, 0] }, fade),
284
315
  ].filter((animation): animation is Animation => animation !== null);
285
316
 
@@ -306,7 +337,8 @@ export default class extends Controller<HTMLElement> {
306
337
  closed() {
307
338
  this.cancelClosing();
308
339
  this.goTo(this.index, 'auto');
309
- this.slideButton(this.index)?.focus({ preventScroll: true });
340
+ const button = this.slideButton(this.index);
341
+ button?.focus({ preventScroll: this.hasTrackTarget });
310
342
  this.zoomImageTarget.removeAttribute('style');
311
343
  this.setScale(MIN_ZOOM);
312
344
  }
@@ -324,7 +356,7 @@ export default class extends Controller<HTMLElement> {
324
356
  const slide = this.slideTargets[index];
325
357
  const image = this.zoomImageTarget;
326
358
  const large = slide.dataset.zoomSrc ?? '';
327
- const shown = slide.querySelector('img');
359
+ const shown = this.origin(index)?.image;
328
360
  const ready = shown?.complete && shown.naturalWidth > 0 ? shown.currentSrc : '';
329
361
 
330
362
  if (direction) this.slideOut(direction);
@@ -406,6 +438,12 @@ export default class extends Controller<HTMLElement> {
406
438
  return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
407
439
  }
408
440
 
441
+ // A grid scrolled on behind the overlay may have left the image out of sight.
442
+ private onScreen(element: HTMLElement): boolean {
443
+ const rect = element.getBoundingClientRect();
444
+ return rect.bottom > 0 && rect.top < window.innerHeight;
445
+ }
446
+
409
447
  private get overlayButtons(): HTMLElement[] {
410
448
  return Array.from(this.dialogTarget.querySelectorAll('button'));
411
449
  }
@@ -530,7 +568,7 @@ export default class extends Controller<HTMLElement> {
530
568
  if (event.ctrlKey || event.metaKey || event.altKey) return;
531
569
 
532
570
  if (this.dialogTarget.open) this.dialogKeydown(event);
533
- else if (this.element.contains(event.target as Node)) this.galleryKeydown(event);
571
+ else if (this.hasTrackTarget && this.element.contains(event.target as Node)) this.galleryKeydown(event);
534
572
  }
535
573
 
536
574
  private galleryKeydown(event: KeyboardEvent) {
@@ -0,0 +1,188 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ // Connects to data-controller="ui--scrubber"
4
+ //
5
+ // Places each mark as far down the rail as its heading is down the list, moves
6
+ // the line with the scroll, and scrolls the list while the rail is dragged.
7
+ // The rail stands for the whole scroll, top to bottom, like a scrollbar.
8
+ const LABEL_GAP = 18;
9
+
10
+ export default class extends Controller<HTMLElement> {
11
+ static targets = ['rail', 'mark', 'thumb', 'tip'];
12
+ static values = { for: String };
13
+
14
+ declare readonly railTarget: HTMLElement;
15
+ declare readonly markTargets: HTMLAnchorElement[];
16
+ declare readonly thumbTarget: HTMLElement;
17
+ declare readonly tipTarget: HTMLElement;
18
+ declare readonly forValue: string;
19
+
20
+ private scroller: HTMLElement | null = null;
21
+ private positions: number[] = [];
22
+ private dragging: number | null = null;
23
+ private frame?: number;
24
+ private observer?: ResizeObserver;
25
+
26
+ connect() {
27
+ this.scroller = this.forValue ? document.getElementById(this.forValue) : null;
28
+ this.scrollEvents.addEventListener('scroll', this.scrolled, { passive: true });
29
+ this.observer = new ResizeObserver(() => this.layout());
30
+ this.observer.observe(this.content);
31
+ this.observer.observe(this.railTarget);
32
+ this.layout();
33
+ }
34
+
35
+ disconnect() {
36
+ this.scrollEvents.removeEventListener('scroll', this.scrolled);
37
+ this.observer?.disconnect();
38
+ if (this.frame) cancelAnimationFrame(this.frame);
39
+ }
40
+
41
+ // Where each heading falls in the scroll, as a share of it. Labels too close
42
+ // to the one above are left as dots, so they don't overlap.
43
+ layout() {
44
+ const height = this.railTarget.clientHeight;
45
+ const max = this.maxScroll;
46
+ let lastLabel = -Infinity;
47
+ this.positions = this.markTargets.map((mark) => {
48
+ const heading = this.headingFor(mark);
49
+ const share = heading && max > 0 ? Math.min(1, Math.max(0, this.offsetOf(heading) / max)) : 0;
50
+ const y = share * height;
51
+ mark.style.transform = `translateY(${y}px) translateY(-50%)`;
52
+ if (mark.dataset.major === 'true') {
53
+ const crowded = y - lastLabel < LABEL_GAP;
54
+ mark.classList.toggle('text-transparent', crowded);
55
+ if (!crowded) lastLabel = y;
56
+ }
57
+ return share;
58
+ });
59
+ this.element.toggleAttribute('data-ready', true);
60
+ this.place();
61
+ }
62
+
63
+ grab(event: PointerEvent) {
64
+ if (event.button !== 0) return;
65
+
66
+ event.preventDefault();
67
+ this.dragging = event.pointerId;
68
+ this.railTarget.setPointerCapture(event.pointerId);
69
+ this.scrubTo(event);
70
+ }
71
+
72
+ point(event: PointerEvent) {
73
+ if (this.dragging === event.pointerId) this.scrubTo(event);
74
+ else if (event.pointerType === 'mouse') this.showTip(this.shareAt(event), true);
75
+ }
76
+
77
+ release(event: PointerEvent) {
78
+ if (this.dragging !== event.pointerId) return;
79
+
80
+ this.dragging = null;
81
+ if (this.railTarget.hasPointerCapture(event.pointerId)) this.railTarget.releasePointerCapture(event.pointerId);
82
+ this.hideTip();
83
+ }
84
+
85
+ leave() {
86
+ if (this.dragging === null) this.hideTip();
87
+ }
88
+
89
+ // One stop on the keyboard: the arrows move between the marks.
90
+ keydown(event: KeyboardEvent) {
91
+ const marks = this.markTargets;
92
+ const current = marks.indexOf(document.activeElement as HTMLAnchorElement);
93
+ if (current < 0) return;
94
+
95
+ const next = { ArrowDown: current + 1, ArrowUp: current - 1, Home: 0, End: marks.length - 1 }[event.key];
96
+ if (next === undefined) return;
97
+
98
+ event.preventDefault();
99
+ const index = Math.min(marks.length - 1, Math.max(0, next));
100
+ marks.forEach((mark, i) => (mark.tabIndex = i === index ? 0 : -1));
101
+ marks[index].focus();
102
+ this.showTip(this.positions[index] ?? 0, false, marks[index].dataset.label);
103
+ }
104
+
105
+ private scrolled = () => {
106
+ if (this.frame) return;
107
+ this.frame = requestAnimationFrame(() => {
108
+ this.frame = undefined;
109
+ this.place();
110
+ });
111
+ };
112
+
113
+ // The line, and the mark for where the list is now as the one Tab lands on.
114
+ private place() {
115
+ const max = this.maxScroll;
116
+ const share = max > 0 ? this.scrollTop / max : 0;
117
+ this.thumbTarget.style.transform = `translateY(${share * this.railTarget.clientHeight}px) translateY(-50%)`;
118
+ if (this.markTargets.some((mark) => mark === document.activeElement)) return;
119
+
120
+ const index = this.indexAt(share);
121
+ this.markTargets.forEach((mark, i) => (mark.tabIndex = i === index ? 0 : -1));
122
+ }
123
+
124
+ private scrubTo(event: PointerEvent) {
125
+ const share = this.shareAt(event);
126
+ const top = share * this.maxScroll;
127
+ if (this.scroller) this.scroller.scrollTo({ top });
128
+ else window.scrollTo({ top });
129
+ this.showTip(share, true);
130
+ }
131
+
132
+ private showTip(share: number, atPointer: boolean, text?: string) {
133
+ const label = text ?? this.markTargets[this.indexAt(share)]?.dataset.label ?? '';
134
+ if (!label) return;
135
+
136
+ this.tipTarget.textContent = label;
137
+ this.tipTarget.style.transform = `translateY(${share * this.railTarget.clientHeight}px) translateY(-50%)`;
138
+ this.tipTarget.toggleAttribute('data-shown', true);
139
+ this.tipTarget.dataset.pointer = String(atPointer);
140
+ }
141
+
142
+ private hideTip() {
143
+ this.tipTarget.removeAttribute('data-shown');
144
+ }
145
+
146
+ // The last mark at or above this point of the scroll.
147
+ private indexAt(share: number): number {
148
+ let index = 0;
149
+ this.positions.forEach((position, i) => {
150
+ if (position <= share + 0.001) index = i;
151
+ });
152
+ return index;
153
+ }
154
+
155
+ private shareAt(event: PointerEvent): number {
156
+ const rect = this.railTarget.getBoundingClientRect();
157
+ return Math.min(1, Math.max(0, (event.clientY - rect.top) / rect.height));
158
+ }
159
+
160
+ private headingFor(mark: HTMLAnchorElement): HTMLElement | null {
161
+ const id = decodeURIComponent(mark.hash.slice(1));
162
+ return id ? document.getElementById(id) : null;
163
+ }
164
+
165
+ // How far down the scroll a heading is, measured from what's on screen.
166
+ private offsetOf(element: HTMLElement): number {
167
+ const top = element.getBoundingClientRect().top;
168
+ if (this.scroller) return top - this.scroller.getBoundingClientRect().top + this.scroller.scrollTop;
169
+ return top + window.scrollY;
170
+ }
171
+
172
+ private get maxScroll(): number {
173
+ if (this.scroller) return this.scroller.scrollHeight - this.scroller.clientHeight;
174
+ return document.documentElement.scrollHeight - window.innerHeight;
175
+ }
176
+
177
+ private get scrollTop(): number {
178
+ return this.scroller ? this.scroller.scrollTop : window.scrollY;
179
+ }
180
+
181
+ private get scrollEvents(): HTMLElement | Window {
182
+ return this.scroller ?? window;
183
+ }
184
+
185
+ private get content(): Element {
186
+ return this.scroller?.firstElementChild ?? document.body;
187
+ }
188
+ }
@@ -0,0 +1,122 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ // Connects to data-controller="ui--selection-bar"
4
+ //
5
+ // Watches the checkboxes in the element named by `for`: counts the checked
6
+ // ones, shows the bar while there are any, keeps "select all" in step, and
7
+ // checks a run of boxes on Shift-click. The boxes are found afresh each time,
8
+ // so rows added or replaced by Turbo count too.
9
+ const ITEM = 'input[type="checkbox"][data-selection="item"]:not(:disabled)';
10
+ const ALL = 'input[type="checkbox"][data-selection="all"]';
11
+
12
+ export default class extends Controller<HTMLElement> {
13
+ static targets = ['count'];
14
+ static values = { for: String, one: String, other: String };
15
+
16
+ declare readonly countTarget: HTMLElement;
17
+ declare readonly forValue: string;
18
+ declare readonly oneValue: string;
19
+ declare readonly otherValue: string;
20
+
21
+ // Not `scope`: Stimulus has its own.
22
+ private list: HTMLElement | null = null;
23
+ private observer?: MutationObserver;
24
+ private last: HTMLInputElement | null = null;
25
+ private lastCount = -1;
26
+
27
+ connect() {
28
+ this.list = document.getElementById(this.forValue);
29
+ if (!this.list) return;
30
+
31
+ this.list.addEventListener('click', this.clicked);
32
+ this.list.addEventListener('change', this.changed);
33
+ document.addEventListener('keydown', this.keydown);
34
+ this.observer = new MutationObserver(() => this.update());
35
+ this.observer.observe(this.list, { childList: true, subtree: true });
36
+ this.update();
37
+ }
38
+
39
+ disconnect() {
40
+ this.list?.removeEventListener('click', this.clicked);
41
+ this.list?.removeEventListener('change', this.changed);
42
+ document.removeEventListener('keydown', this.keydown);
43
+ this.observer?.disconnect();
44
+ }
45
+
46
+ clear() {
47
+ const focused = this.element.contains(document.activeElement);
48
+ this.items.forEach((box) => (box.checked = false));
49
+ this.update();
50
+ // The clear button hides with the bar, so focus goes back to the list.
51
+ if (focused) (this.last ?? this.items[0])?.focus();
52
+ }
53
+
54
+ // Shift-click checks or unchecks everything between the last box clicked and
55
+ // this one, as this one now is.
56
+ private clicked = (event: MouseEvent) => {
57
+ const box = event.target as HTMLElement;
58
+ if (!(box instanceof HTMLInputElement) || !box.matches(ITEM)) return;
59
+
60
+ const items = this.items;
61
+ if (event.shiftKey && this.last && this.last !== box) {
62
+ const [from, to] = [items.indexOf(this.last), items.indexOf(box)].sort((a, b) => a - b);
63
+ if (from >= 0) items.slice(from, to + 1).forEach((item) => (item.checked = box.checked));
64
+ }
65
+ this.last = box;
66
+ };
67
+
68
+ // An "all" box inside a [data-selection-group] (a day of photos) chooses
69
+ // that group's items; one outside any group chooses every item.
70
+ private changed = (event: Event) => {
71
+ const box = event.target as HTMLElement;
72
+ if (box instanceof HTMLInputElement && box.matches(ALL)) {
73
+ this.itemsFor(box).forEach((item) => (item.checked = box.checked));
74
+ }
75
+ this.update();
76
+ };
77
+
78
+ private keydown = (event: KeyboardEvent) => {
79
+ if (event.key !== 'Escape' || this.checked.length === 0 || event.defaultPrevented) return;
80
+ const target = event.target as Node;
81
+ if (!this.element.contains(target) && !this.list?.contains(target)) return;
82
+
83
+ event.preventDefault();
84
+ this.clear();
85
+ };
86
+
87
+ private update() {
88
+ const items = this.items;
89
+ const checked = this.checked;
90
+ const count = checked.length;
91
+ this.list?.querySelectorAll<HTMLInputElement>(ALL).forEach((all) => {
92
+ const group = this.itemsFor(all);
93
+ const chosen = group.filter((box) => box.checked).length;
94
+ all.checked = chosen > 0 && chosen === group.length;
95
+ all.indeterminate = chosen > 0 && chosen < group.length;
96
+ });
97
+ this.list?.toggleAttribute('data-selecting', count > 0);
98
+ this.element.hidden = count === 0;
99
+ const text = (count === 1 ? this.oneValue : this.otherValue).replace('%{count}', String(count));
100
+ // Only when it changes: a bar inside the list it watches would otherwise
101
+ // set off its own observer.
102
+ if (this.countTarget.textContent !== text) this.countTarget.textContent = text;
103
+
104
+ if (count === this.lastCount) return;
105
+ this.lastCount = count;
106
+ this.dispatch('change', { prefix: 'selection-bar', detail: { count, values: checked.map((box) => box.value) } });
107
+ }
108
+
109
+ private itemsFor(all: HTMLInputElement): HTMLInputElement[] {
110
+ const group = all.closest<HTMLElement>('[data-selection-group]');
111
+ if (!group || !this.list?.contains(group)) return this.items;
112
+ return Array.from(group.querySelectorAll<HTMLInputElement>(ITEM));
113
+ }
114
+
115
+ private get items(): HTMLInputElement[] {
116
+ return Array.from(this.list?.querySelectorAll<HTMLInputElement>(ITEM) ?? []);
117
+ }
118
+
119
+ private get checked(): HTMLInputElement[] {
120
+ return this.items.filter((box) => box.checked);
121
+ }
122
+ }