lostcoords-ui 0.7.0 → 0.8.1

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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +338 -28
  3. data/app/assets/tailwind/lostcoords/ui/card.css +14 -6
  4. data/app/assets/tailwind/lostcoords/ui/collapsible.css +6 -0
  5. data/app/assets/tailwind/lostcoords/ui/components/dialog.css +2 -2
  6. data/app/assets/tailwind/lostcoords/ui/components/search.css +5 -0
  7. data/app/assets/tailwind/lostcoords/ui/components.css +1 -0
  8. data/app/assets/tailwind/lostcoords/ui/core.css +2 -2
  9. data/app/assets/tailwind/lostcoords/ui/dialog.css +17 -0
  10. data/app/assets/tailwind/lostcoords/ui/emoji-picker.css +2 -1
  11. data/app/assets/tailwind/lostcoords/ui/popover.css +6 -4
  12. data/app/assets/tailwind/lostcoords/ui/search.css +96 -0
  13. data/app/assets/tailwind/lostcoords/ui/sheet.css +41 -8
  14. data/app/assets/tailwind/lostcoords/ui/theme.css +18 -0
  15. data/app/assets/tailwind/lostcoords/ui/toast.css +34 -4
  16. data/app/assets/tailwind/lostcoords/ui/typography.css +10 -0
  17. data/app/helpers/lostcoords/ui/badge_helper.rb +33 -15
  18. data/app/helpers/lostcoords/ui/button_helper.rb +71 -2
  19. data/app/helpers/lostcoords/ui/choice_helper.rb +9 -16
  20. data/app/helpers/lostcoords/ui/collapsible_helper.rb +26 -4
  21. data/app/helpers/lostcoords/ui/dialog_helper.rb +52 -12
  22. data/app/helpers/lostcoords/ui/form_helper.rb +30 -10
  23. data/app/helpers/lostcoords/ui/glass_helper.rb +6 -0
  24. data/app/helpers/lostcoords/ui/layout_helper.rb +13 -5
  25. data/app/helpers/lostcoords/ui/list_group_helper.rb +95 -0
  26. data/app/helpers/lostcoords/ui/menu_helper.rb +84 -13
  27. data/app/helpers/lostcoords/ui/navigation_helper.rb +57 -12
  28. data/app/helpers/lostcoords/ui/primitives_helper.rb +128 -34
  29. data/app/helpers/lostcoords/ui/render_attributes_helper.rb +3 -1
  30. data/app/helpers/lostcoords/ui/search_helper.rb +55 -0
  31. data/app/helpers/lostcoords/ui/sheet_helper.rb +20 -8
  32. data/app/helpers/lostcoords/ui/structures_helper.rb +114 -20
  33. data/app/helpers/lostcoords/ui/tailwind_helper.rb +1 -1
  34. data/app/helpers/lostcoords/ui/toast_helper.rb +11 -4
  35. data/app/helpers/lostcoords/ui/typography_helper.rb +5 -3
  36. data/app/javascript/lostcoords/ui/controllers/lc_menu_controller.js +5 -6
  37. data/app/javascript/lostcoords/ui/controllers/lc_nav_controller.js +79 -0
  38. data/app/javascript/lostcoords/ui/controllers/lc_search_controller.js +288 -0
  39. data/app/javascript/lostcoords/ui/controllers/lc_selection_controller.js +1 -6
  40. data/app/javascript/lostcoords/ui/controllers/lc_sheet_controller.js +29 -15
  41. data/app/javascript/lostcoords/ui/helpers/confirm.js +57 -0
  42. data/app/javascript/lostcoords/ui/helpers/floating_panel.js +9 -0
  43. data/app/javascript/lostcoords/ui/index.js +4 -2
  44. data/app/views/lostcoords/ui/components/_lc_collapsible.html.erb +3 -3
  45. data/app/views/lostcoords/ui/components/_lc_confirm_sheet.html.erb +14 -0
  46. data/app/views/lostcoords/ui/components/_lc_dialog.html.erb +28 -16
  47. data/app/views/lostcoords/ui/components/_lc_floating_panel.html.erb +11 -6
  48. data/app/views/lostcoords/ui/components/_lc_search.html.erb +26 -0
  49. data/config/locales/en.yml +11 -0
  50. data/config/locales/es.yml +11 -0
  51. data/config/locales/ja.yml +11 -0
  52. data/lib/lostcoords/ui/builders.rb +1 -1
  53. data/lib/lostcoords/ui/form_builder.rb +27 -12
  54. data/lib/lostcoords/ui/parts.rb +72 -8
  55. data/lib/lostcoords/ui/tokens.rb +3 -1
  56. data/lib/lostcoords/ui/version.rb +1 -1
  57. metadata +10 -2
  58. data/app/javascript/lostcoords/ui/controllers/lc_confirm_dialog_controller.js +0 -39
@@ -0,0 +1,79 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Each visit renders a new nav, so the indicator's last spot is kept per nav
4
+ // label and the new one slides from there to its current link.
5
+ const spots = new Map()
6
+
7
+ export default class extends Controller {
8
+ static targets = ["indicator", "link"]
9
+
10
+ connect() {
11
+ this.key = this.element.getAttribute("aria-label")
12
+ this.initialLink = this.currentLink
13
+ this.restore = () => this.select({ currentTarget: this.initialLink }, { animate: false })
14
+ document.addEventListener("turbo:before-cache", this.restore)
15
+
16
+ // The observer reports once on observe, after the first placement below.
17
+ let observed = false
18
+ this.observer = new ResizeObserver(() => {
19
+ if (observed) this.place({ animate: false })
20
+ observed = true
21
+ })
22
+ this.observer.observe(this.element)
23
+
24
+ const previous = spots.get(this.key)
25
+ if (previous) this.move(previous, { animate: false })
26
+ this.frame = requestAnimationFrame(() => this.place({ animate: Boolean(previous) }))
27
+ }
28
+
29
+ disconnect() {
30
+ cancelAnimationFrame(this.frame)
31
+ this.observer.disconnect()
32
+ document.removeEventListener("turbo:before-cache", this.restore)
33
+ }
34
+
35
+ select(event, { animate = true } = {}) {
36
+ if (event.button > 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
37
+
38
+ for (const link of this.linkTargets) {
39
+ if (link === event.currentTarget) link.setAttribute("aria-current", "page")
40
+ else link.removeAttribute("aria-current")
41
+ }
42
+ this.place({ animate })
43
+ }
44
+
45
+ get currentLink() {
46
+ return this.linkTargets.find((link) => link.getAttribute("aria-current") === "page")
47
+ }
48
+
49
+ place({ animate }) {
50
+ const link = this.currentLink
51
+ if (!link?.offsetParent) {
52
+ this.element.removeAttribute("data-indicator")
53
+ return
54
+ }
55
+
56
+ const rtl = getComputedStyle(this.element).direction === "rtl"
57
+ const spot = {
58
+ x: rtl ? link.offsetLeft + link.offsetWidth - this.element.clientWidth : link.offsetLeft,
59
+ y: link.offsetTop,
60
+ width: link.offsetWidth,
61
+ height: link.offsetHeight,
62
+ }
63
+ spots.set(this.key, spot)
64
+ this.move(spot, { animate })
65
+ }
66
+
67
+ move({ x, y, width, height }, { animate }) {
68
+ const style = this.indicatorTarget.style
69
+ style.transition = animate ? "" : "none"
70
+ style.translate = `${x}px ${y}px`
71
+ style.width = `${width}px`
72
+ style.height = `${height}px`
73
+ this.element.toggleAttribute("data-indicator", true)
74
+ if (!animate) {
75
+ this.indicatorTarget.offsetWidth
76
+ style.transition = ""
77
+ }
78
+ }
79
+ }
@@ -0,0 +1,288 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import { registerDialog } from "lostcoords/ui/helpers/dialog"
3
+ import { prefersReducedMotion } from "lostcoords/ui/helpers/viewport"
4
+
5
+ // Full-screen search. Each query dispatches lc-search:query with { query, results }; a listener
6
+ // that calls preventDefault fills results itself. Otherwise the controller asks the server
7
+ // (src) for the results HTML, or filters the rows already rendered by their text.
8
+ export default class extends Controller {
9
+ static targets = [
10
+ "trigger",
11
+ "dialog",
12
+ "stack",
13
+ "bar",
14
+ "glyph",
15
+ "input",
16
+ "clear",
17
+ "cancel",
18
+ "panel",
19
+ "results",
20
+ "result",
21
+ "empty",
22
+ "prompt",
23
+ "status",
24
+ ]
25
+ static values = {
26
+ src: String,
27
+ param: { type: String, default: "q" },
28
+ empty: String,
29
+ one: String,
30
+ other: String,
31
+ }
32
+
33
+ connect() {
34
+ this.animations = []
35
+ this.observer = new MutationObserver(() => this.refresh())
36
+ this.observer.observe(this.resultsTarget, { childList: true, subtree: true })
37
+ }
38
+
39
+ disconnect() {
40
+ this.observer.disconnect()
41
+ clearTimeout(this.timeout)
42
+ this.request?.abort()
43
+ this.returnFocus = null
44
+ this.reset()
45
+ }
46
+
47
+ // ⌘K / Ctrl+K comes from lc-shortcut: it presses the trigger while search is closed
48
+ // and refocuses the field while it is open.
49
+ open() {
50
+ if (this.closing) return
51
+ if (this.dialogTarget.open) return this.inputTarget.focus()
52
+ this.returnFocus =
53
+ document.activeElement === document.body ? this.triggerTarget : document.activeElement
54
+ this.element.dataset.open = ""
55
+ this.dialogTarget.showModal()
56
+ this.unregisterDialog = registerDialog(this.dialogTarget, { close: () => this.close() })
57
+ this.inputTarget.focus({ preventScroll: true })
58
+ this.inputTarget.select()
59
+ if (this.searched !== this.query) this.search()
60
+ this.refresh()
61
+ this.grow()
62
+ }
63
+
64
+ close() {
65
+ if (!this.dialogTarget.open || this.closing) return
66
+ this.closing = true
67
+ this.fade()
68
+ Promise.allSettled(this.animations.map((animation) => animation.finished)).then(() =>
69
+ this.reset(),
70
+ )
71
+ }
72
+
73
+ // Also runs before Turbo caches the page, so a restored page never shows search open.
74
+ reset() {
75
+ this.animations.forEach((animation) => animation.cancel())
76
+ this.animations = []
77
+ this.dialogTarget.close()
78
+ this.unregisterDialog?.()
79
+ this.unregisterDialog = null
80
+ this.closing = false
81
+ delete this.element.dataset.open
82
+ }
83
+
84
+ closed() {
85
+ this.reset()
86
+ this.returnFocus?.focus({ preventScroll: true })
87
+ this.returnFocus = null
88
+ }
89
+
90
+ // A click on the backdrop closes search, and so does choosing a result, which matters for
91
+ // links within the page and Turbo visits that keep it.
92
+ dismiss(event) {
93
+ if (event.target.hasAttribute("data-lc-search-dismiss")) this.close()
94
+ else if (event.target.closest?.("[data-slot=search-result]")) this.close()
95
+ }
96
+
97
+ clear() {
98
+ this.inputTarget.value = ""
99
+ this.inputTarget.focus()
100
+ this.input()
101
+ }
102
+
103
+ input(event) {
104
+ if (event?.isComposing) return
105
+ this.clearTarget.hidden = !this.inputTarget.value
106
+ clearTimeout(this.timeout)
107
+ // The server answers each query, so it waits for a pause in typing.
108
+ if (this.srcValue) this.timeout = setTimeout(() => this.search(), 150)
109
+ else this.search()
110
+ }
111
+
112
+ submit(event) {
113
+ event.preventDefault()
114
+ this.openFirst()
115
+ }
116
+
117
+ navigate(event) {
118
+ if (event.isComposing || event.keyCode === 229) return
119
+ if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return
120
+ event.preventDefault()
121
+ const items = [this.inputTarget, ...this.visibleResults()]
122
+ const index = items.indexOf(document.activeElement)
123
+ const step = event.key === "ArrowDown" ? 1 : -1
124
+ items[Math.min(Math.max(index + step, 0), items.length - 1)].focus()
125
+ }
126
+
127
+ get query() {
128
+ return this.inputTarget.value.normalize("NFKC").trim()
129
+ }
130
+
131
+ search() {
132
+ const query = this.query
133
+ this.searched = query
134
+ const event = this.dispatch("query", {
135
+ detail: { query, results: this.resultsTarget },
136
+ cancelable: true,
137
+ })
138
+ if (event.defaultPrevented) return
139
+ if (this.srcValue) this.load(query)
140
+ else this.filter(query)
141
+ }
142
+
143
+ load(query) {
144
+ this.request?.abort()
145
+ const request = new AbortController()
146
+ this.request = request
147
+ const url = new URL(this.srcValue, location.href)
148
+ url.searchParams.set(this.paramValue, query)
149
+ this.busy(true)
150
+ this.loading = fetch(url, { headers: { Accept: "text/html" }, signal: request.signal })
151
+ .then((response) => {
152
+ if (!response.ok) throw new Error(`${url} answered ${response.status}`)
153
+ return response.text()
154
+ })
155
+ .then((html) => {
156
+ if (this.request !== request) return
157
+ this.loaded = query
158
+ this.busy(false)
159
+ this.resultsTarget.innerHTML = html
160
+ })
161
+ .catch((error) => {
162
+ if (error.name === "AbortError") return
163
+ console.error(error)
164
+ if (this.request === request) this.busy(false)
165
+ })
166
+ return this.loading
167
+ }
168
+
169
+ // While the server answers, the empty and prompt notes wait, so they never flash before results.
170
+ busy(pending) {
171
+ this.pending = pending
172
+ if (pending) this.panelTarget.setAttribute("aria-busy", "true")
173
+ else this.panelTarget.removeAttribute("aria-busy")
174
+ this.refresh()
175
+ }
176
+
177
+ filter(query) {
178
+ const words = query.toLowerCase().split(/\s+/).filter(Boolean)
179
+ for (const result of this.resultTargets) {
180
+ const text = `${result.dataset.lcSearchKeywords ?? ""} ${result.textContent}`
181
+ .normalize("NFKC")
182
+ .toLowerCase()
183
+ result.hidden = !words.every((word) => text.includes(word))
184
+ }
185
+ this.refresh()
186
+ }
187
+
188
+ // Enter in the field opens the top result; while the server is still answering a newer
189
+ // query, it waits for that answer.
190
+ openFirst() {
191
+ clearTimeout(this.timeout)
192
+ if (this.srcValue && this.loaded !== this.query) {
193
+ const query = this.query
194
+ if (this.searched !== query) this.search()
195
+ this.loading?.then(() => this.loaded === query && this.openFirst())
196
+ return
197
+ }
198
+ this.visibleResults()[0]?.click()
199
+ }
200
+
201
+ visibleResults() {
202
+ return this.resultTargets.filter((result) => result.checkVisibility())
203
+ }
204
+
205
+ // Groups without a visible result hide, the empty note shows for a query with no results, the
206
+ // prompt for no query and nothing to show, and the status announces the count.
207
+ refresh() {
208
+ for (const group of this.resultsTarget.querySelectorAll("[data-slot=search-group]")) {
209
+ group.hidden = ![...group.querySelectorAll("[data-slot=search-result]")].some(
210
+ (result) => !result.hidden,
211
+ )
212
+ }
213
+ const count = this.resultTargets.filter(
214
+ (result) => !result.hidden && !result.closest("[hidden]"),
215
+ ).length
216
+ const query = this.query
217
+ const settled = !this.pending && count === 0
218
+ this.emptyTarget.hidden = !(settled && query)
219
+ if (!this.emptyTarget.hidden)
220
+ this.emptyTarget.textContent = this.emptyValue.replace("%{query}", query)
221
+ if (this.hasPromptTarget) this.promptTarget.hidden = !(settled && !query)
222
+ const note = !this.emptyTarget.hidden || (this.hasPromptTarget && !this.promptTarget.hidden)
223
+ this.panelTarget.hidden = count === 0 && !note
224
+ this.statusTarget.textContent = query
225
+ ? (count === 1 ? this.oneValue : this.otherValue).replace("%{count}", count)
226
+ : ""
227
+ }
228
+
229
+ // The bar grows out of the trigger and the glyph slides from the trigger's icon to its
230
+ // place in the field; the rest fades in behind it.
231
+ grow() {
232
+ this.animations.forEach((animation) => animation.cancel())
233
+ this.animations = []
234
+ if (prefersReducedMotion()) return
235
+ const from = this.triggerTarget.getBoundingClientRect()
236
+ const to = this.barTarget.getBoundingClientRect()
237
+ const glyph = this.glyphTarget.getBoundingClientRect()
238
+ const icon = this.triggerTarget.querySelector("svg").getBoundingClientRect()
239
+ const insetY = Math.max((to.height - from.height) / 2, 0)
240
+ const shiftY = from.top + from.height / 2 - (to.top + to.height / 2)
241
+ const timing = { duration: 420, easing: "cubic-bezier(0.16, 1, 0.3, 1)", fill: "both" }
242
+ const fade = [
243
+ { opacity: 0, translate: "0 -0.5rem" },
244
+ { opacity: 1, translate: "0 0" },
245
+ ]
246
+ this.animations = [
247
+ this.barTarget.animate(
248
+ [
249
+ {
250
+ clipPath: `inset(${insetY}px ${Math.max(to.right - from.right, 0)}px ${insetY}px ${Math.max(from.left - to.left, 0)}px round 999px)`,
251
+ translate: `0 ${shiftY}px`,
252
+ },
253
+ { clipPath: "inset(-1.5rem round 999px)", translate: "0 0" },
254
+ ],
255
+ timing,
256
+ ),
257
+ this.glyphTarget.animate(
258
+ [
259
+ { translate: `${icon.left + (icon.width - glyph.width) / 2 - glyph.left}px 0` },
260
+ { translate: "0 0" },
261
+ ],
262
+ timing,
263
+ ),
264
+ this.inputTarget.animate([{ opacity: 0 }, { opacity: 1 }], { ...timing, delay: 120 }),
265
+ this.panelTarget.animate(fade, { ...timing, delay: 60 }),
266
+ this.cancelTarget.animate(fade, timing),
267
+ this.dialogTarget.animate([{ opacity: 0 }, { opacity: 1 }], {
268
+ ...timing,
269
+ pseudoElement: "::backdrop",
270
+ }),
271
+ ]
272
+ }
273
+
274
+ // Closing doesn't play the opening back into the trigger: the overlay just fades.
275
+ fade() {
276
+ this.animations.forEach((animation) => animation.cancel())
277
+ this.animations = []
278
+ if (prefersReducedMotion()) return
279
+ const timing = { duration: 160, easing: "ease-out", fill: "both" }
280
+ this.animations = [
281
+ this.stackTarget.animate([{ opacity: 1 }, { opacity: 0 }], timing),
282
+ this.dialogTarget.animate([{ opacity: 1 }, { opacity: 0 }], {
283
+ ...timing,
284
+ pseudoElement: "::backdrop",
285
+ }),
286
+ ]
287
+ }
288
+ }
@@ -2,7 +2,7 @@ import { Controller } from "@hotwired/stimulus"
2
2
  import { navigate } from "lostcoords/ui/helpers/roving_focus"
3
3
 
4
4
  export default class extends Controller {
5
- static targets = ["option", "input"]
5
+ static targets = ["option"]
6
6
  static values = { value: String }
7
7
 
8
8
  connect() {
@@ -44,14 +44,9 @@ export default class extends Controller {
44
44
  option.setAttribute("aria-checked", String(option === selected))
45
45
  option.tabIndex = option === selected ? 0 : -1
46
46
  }
47
- if (this.hasInputTarget) this.inputTarget.value = selected?.dataset.value || ""
48
47
  }
49
48
 
50
49
  changed(option) {
51
- if (this.hasInputTarget) {
52
- this.inputTarget.dispatchEvent(new Event("input", { bubbles: true }))
53
- this.inputTarget.dispatchEvent(new Event("change", { bubbles: true }))
54
- }
55
50
  this.dispatch("change", { detail: { value: option.dataset.value } })
56
51
  }
57
52
 
@@ -1,6 +1,7 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
2
  import { holdDocumentScroll } from "lostcoords/ui/helpers/dismiss_stack"
3
3
  import { SHEET_TIMING, sheetDuration } from "lostcoords/ui/helpers/sheet_motion"
4
+ import { hideTopLayer, supportsTopLayer } from "lostcoords/ui/helpers/top_layer"
4
5
  import { isSheetViewport, prefersReducedMotion, TEXT_ENTRY } from "lostcoords/ui/helpers/viewport"
5
6
 
6
7
  const PROJECTION_MS = 180
@@ -29,20 +30,27 @@ export default class extends Controller {
29
30
  this.element.addEventListener("touchcancel", this.onTouchEnd)
30
31
  this.element.addEventListener("pointerdown", this.onPointerDown)
31
32
 
32
- if (this.isDialog) {
33
- this.observer = new MutationObserver(() =>
34
- this.element.open ? this.present() : this.closed(),
35
- )
36
- this.observer.observe(this.element, { attributes: true, attributeFilter: ["open"] })
37
- if (this.element.open) this.present()
38
- } else {
39
- this.observer = new MutationObserver(() => this.element.hidden || this.pinFloor())
40
- this.observer.observe(this.element, { attributes: true, attributeFilter: ["hidden"] })
41
- }
33
+ this.sync = this.sync.bind(this)
34
+ this.shown = false
35
+ this.observer = new MutationObserver(this.sync)
36
+ this.observer.observe(this.element, {
37
+ attributes: true,
38
+ attributeFilter: ["open", "data-open"],
39
+ })
40
+ this.element.addEventListener("toggle", this.sync)
41
+ this.sync()
42
+ }
43
+
44
+ sync() {
45
+ const shown = this.isShown
46
+ if (shown === this.shown) return
47
+ this.shown = shown
48
+ shown ? this.present() : this.closed()
42
49
  }
43
50
 
44
51
  disconnect() {
45
52
  this.observer?.disconnect()
53
+ this.element.removeEventListener("toggle", this.sync)
46
54
  this.element.removeEventListener("touchstart", this.onTouchStart)
47
55
  this.element.removeEventListener("touchmove", this.onTouchMove)
48
56
  this.element.removeEventListener("touchend", this.onTouchEnd)
@@ -242,7 +250,8 @@ export default class extends Controller {
242
250
  this.transition()
243
251
  this.element.style.transform = `translateY(${this.element.getBoundingClientRect().height + 48}px)`
244
252
  this.later(() => {
245
- this.dispatch("dismiss", { bubbles: false })
253
+ const event = this.dispatch("dismiss", { bubbles: false, cancelable: true })
254
+ if (!event.defaultPrevented) hideTopLayer(this.element)
246
255
  requestAnimationFrame(() => this.resetSurface())
247
256
  })
248
257
  }
@@ -377,10 +386,7 @@ export default class extends Controller {
377
386
 
378
387
  transition() {
379
388
  const timing = prefersReducedMotion() ? "0s" : SHEET_TIMING
380
- const discrete = this.isDialog
381
- ? `, display ${timing} allow-discrete, overlay ${timing} allow-discrete`
382
- : ""
383
- this.element.style.transition = `transform ${timing}, block-size ${timing}${discrete}`
389
+ this.element.style.transition = `transform ${timing}, block-size ${timing}, display ${timing} allow-discrete, overlay ${timing} allow-discrete`
384
390
  clearTimeout(this.transitionTimer)
385
391
  this.transitionTimer = setTimeout(() => {
386
392
  this.element.style.transition = ""
@@ -419,6 +425,14 @@ export default class extends Controller {
419
425
  return this.element instanceof HTMLDialogElement
420
426
  }
421
427
 
428
+ get isShown() {
429
+ if (this.isDialog) return this.element.open
430
+ return (
431
+ this.element.hasAttribute("data-open") ||
432
+ (supportsTopLayer() && this.element.matches(":popover-open"))
433
+ )
434
+ }
435
+
422
436
  get isSheet() {
423
437
  return isSheetViewport()
424
438
  }
@@ -0,0 +1,57 @@
1
+ import { showContextDialog } from "lostcoords/ui/helpers/context_dialog"
2
+ import { registerDialog } from "lostcoords/ui/helpers/dialog"
3
+
4
+ const SHEET_ID = "lc-confirm-sheet"
5
+ const SHORT_LABEL = 24
6
+ const DESTRUCTIVE =
7
+ "[data-turbo-confirm-tone=destructive], [data-variant=destructive], [data-tone=destructive]"
8
+ const MENU = "[data-lc-menu-target~=popover]"
9
+
10
+ // Turbo.config.forms.confirm = confirm. The submitter, then the form, can set
11
+ // data-turbo-confirm-description, data-turbo-confirm-accept and data-turbo-confirm-tone.
12
+ // Without an accept label, a short submitter label replaces "Confirm".
13
+ export function confirm(message, element, submitter) {
14
+ const nodes = [submitter, element].filter((node) => node instanceof Element)
15
+ const read = (name) => nodes.map((node) => node.dataset[name]).find(Boolean) || null
16
+ const label = (submitter || element)?.textContent?.trim().replace(/\s+/g, " ")
17
+ return ask(message, {
18
+ description: read("turboConfirmDescription"),
19
+ accept: read("turboConfirmAccept") || (label && label.length <= SHORT_LABEL ? label : null),
20
+ destructive: nodes.some((node) => node.matches(DESTRUCTIVE)),
21
+ anchor: nodes[0]?.closest(MENU),
22
+ })
23
+ }
24
+
25
+ // anchor: on desktop the sheet opens beside this element, as a menu's follow-up does.
26
+ export function ask(
27
+ message,
28
+ { description = null, accept = null, destructive = false, anchor = null } = {},
29
+ ) {
30
+ const dialog = document.getElementById(SHEET_ID)
31
+ if (!dialog) return Promise.resolve(window.confirm(message))
32
+ if (dialog.open) return Promise.resolve(false)
33
+
34
+ dialog.querySelector("[data-slot=confirm-sheet-message]").textContent = message
35
+ const details = dialog.querySelector("[data-slot=confirm-sheet-description]")
36
+ details.textContent = description || ""
37
+ details.hidden = !description
38
+ for (const button of dialog.querySelectorAll("[data-slot=confirm-sheet-accept]")) {
39
+ const shown = (button.dataset.variant === "destructive") === destructive
40
+ button.hidden = !shown
41
+ button.dataset.defaultLabel ??= button.textContent.trim()
42
+ button.textContent = (shown && accept) || button.dataset.defaultLabel
43
+ }
44
+
45
+ return new Promise((resolve) => {
46
+ dialog.returnValue = ""
47
+ dialog.addEventListener("close", () => resolve(dialog.returnValue === "confirm"), {
48
+ once: true,
49
+ })
50
+ dialog.style.removeProperty("--lc-dialog-origin")
51
+ showContextDialog(dialog, anchor)
52
+ registerDialog(dialog, { lockScroll: false })
53
+ // showModal focuses Cancel, and a confirm raised while typing would hand it the field's focus ring.
54
+ // The sheet itself takes focus; Tab still reaches both buttons.
55
+ dialog.focus({ preventScroll: true })
56
+ })
57
+ }
@@ -40,6 +40,15 @@ export class FloatingPanel extends Controller {
40
40
  this.isOpen ? this.close() : this.open()
41
41
  }
42
42
 
43
+ // A host that is a whole row opens its panel from a click anywhere on the row except its controls.
44
+ toggleFromRow(event) {
45
+ if (this.popoverTarget.contains(event.target)) return
46
+ if (event.target.closest("a, button, input, select, textarea, [data-lc-menu-ignore]")) return
47
+
48
+ event.preventDefault()
49
+ this.isOpen ? this.close() : this.open()
50
+ }
51
+
43
52
  open(opener) {
44
53
  if (this.isOpen) return
45
54
  this.opener = opener instanceof Element ? opener : this.triggerTarget
@@ -1,12 +1,13 @@
1
1
  import CodeBlock from "lostcoords/ui/controllers/lc_code_block_controller"
2
2
  import Collapsible from "lostcoords/ui/controllers/lc_collapsible_controller"
3
- import ConfirmDialog from "lostcoords/ui/controllers/lc_confirm_dialog_controller"
4
3
  import Copy from "lostcoords/ui/controllers/lc_copy_controller"
5
4
  import Dialog from "lostcoords/ui/controllers/lc_dialog_controller"
6
5
  import EmojiPicker from "lostcoords/ui/controllers/lc_emoji_picker_controller"
7
6
  import Kbd from "lostcoords/ui/controllers/lc_kbd_controller"
8
7
  import Menu from "lostcoords/ui/controllers/lc_menu_controller"
8
+ import Nav from "lostcoords/ui/controllers/lc_nav_controller"
9
9
  import Popover from "lostcoords/ui/controllers/lc_popover_controller"
10
+ import Search from "lostcoords/ui/controllers/lc_search_controller"
10
11
  import Selection from "lostcoords/ui/controllers/lc_selection_controller"
11
12
  import Sheet from "lostcoords/ui/controllers/lc_sheet_controller"
12
13
  import Shortcut from "lostcoords/ui/controllers/lc_shortcut_controller"
@@ -17,7 +18,6 @@ import Toggle from "lostcoords/ui/controllers/lc_toggle_controller"
17
18
  import Tooltip from "lostcoords/ui/controllers/lc_tooltip_controller"
18
19
 
19
20
  const controllers = {
20
- "lc-confirm-dialog": ConfirmDialog,
21
21
  "lc-collapsible": Collapsible,
22
22
  "lc-code-block": CodeBlock,
23
23
  "lc-copy": Copy,
@@ -25,7 +25,9 @@ const controllers = {
25
25
  "lc-emoji-picker": EmojiPicker,
26
26
  "lc-kbd": Kbd,
27
27
  "lc-menu": Menu,
28
+ "lc-nav": Nav,
28
29
  "lc-popover": Popover,
30
+ "lc-search": Search,
29
31
  "lc-selection": Selection,
30
32
  "lc-sheet": Sheet,
31
33
  "lc-shortcut": Shortcut,
@@ -1,5 +1,5 @@
1
- <%# locals: (title:, attributes:, content:) %>
1
+ <%# locals: (summary:, summary_attributes:, attributes:, content:) %>
2
2
  <%= tag.details(**attributes) do %>
3
- <summary class="cursor-pointer text-label font-semibold"><%= title %></summary>
4
- <div class="pt-heading"><%= content %></div>
3
+ <%= tag.summary(summary, **summary_attributes) %>
4
+ <div class="pt-heading" data-slot="collapsible-content"><%= content %></div>
5
5
  <% end %>
@@ -0,0 +1,14 @@
1
+ <%# locals: (id:, attributes:) %>
2
+ <%= lc_sheet(tag: :dialog, **attributes) do %>
3
+ <form method="dialog" class="flex flex-col gap-inset p-inset max-sm:pt-7" data-slot="confirm-sheet-content">
4
+ <div class="flex flex-col gap-tight max-sm:text-center">
5
+ <p id="<%= id %>-message" class="<%= lc_heading_class(size: :subheading) %>" data-slot="confirm-sheet-message"></p>
6
+ <p id="<%= id %>-description" class="<%= lc_text_class(size: :small, tone: :muted) %>" data-slot="confirm-sheet-description" hidden></p>
7
+ </div>
8
+ <div class="grid grid-cols-2 gap-related" data-slot="confirm-sheet-actions">
9
+ <button type="submit" value="cancel" class="<%= lc_button_class(variant: :secondary, block: true) %>" data-slot="confirm-sheet-cancel"><%= lc_t("lostcoords.ui.confirm.cancel") %></button>
10
+ <button type="submit" value="confirm" class="<%= lc_button_class(variant: :primary, block: true) %>" data-slot="confirm-sheet-accept" data-variant="primary"><%= lc_t("lostcoords.ui.confirm.accept") %></button>
11
+ <button type="submit" value="confirm" class="<%= lc_button_class(variant: :destructive, block: true) %>" data-slot="confirm-sheet-accept" data-variant="destructive" hidden><%= lc_t("lostcoords.ui.confirm.accept") %></button>
12
+ </div>
13
+ </form>
14
+ <% end %>
@@ -1,16 +1,28 @@
1
- <%# locals: (id:, title:, description:, trigger:, attributes:, content:) %>
2
- <div data-controller="lc-dialog">
3
- <button type="button" class="<%= lc_button_class(variant: :secondary) %>" aria-haspopup="dialog" aria-controls="<%= id %>" data-action="lc-dialog#open" data-slot="dialog-trigger"><%= trigger %></button>
4
- <%= tag.dialog(**attributes) do %>
5
- <div class="flex flex-col gap-inset p-inset" data-slot="dialog-content">
6
- <header class="flex items-start justify-between gap-content" data-slot="dialog-header">
7
- <div class="flex min-w-0 flex-col gap-tight">
8
- <h2 id="<%= id %>-title" class="<%= lc_heading_class(size: :subheading) %>" data-slot="dialog-title"><%= title %></h2>
9
- <% if description.present? %><p id="<%= id %>-description" class="<%= lc_text_class(size: :small, tone: :muted) %>" data-slot="dialog-description"><%= description %></p><% end %>
10
- </div>
11
- <button type="button" class="<%= lc_icon_button_class(size: :sm, shape: :circle, extra: "-me-related -mt-tight") %>" aria-label="<%= lc_t("lostcoords.ui.dialog.close", title: title) %>" data-action="lc-dialog#close"><%= lc_icon "x-mark", options: { "aria-hidden": true } %></button>
12
- </header>
13
- <%= content %>
14
- </div>
15
- <% end %>
16
- </div>
1
+ <%# locals: (id:, title:, description:, trigger:, rendered_trigger:, detents:, close_action:, close_label:, attributes:, actions:, content:) %>
2
+ <% dialog = lc_sheet(tag: :dialog, detents: detents, **attributes) do %>
3
+ <div class="flex min-h-0 flex-col gap-inset p-inset sm:h-full sm:max-h-[inherit]" data-slot="dialog-content">
4
+ <header class="flex shrink-0 items-start justify-between gap-content" data-slot="dialog-header">
5
+ <div class="flex min-w-0 flex-col gap-tight">
6
+ <%= title %>
7
+ <%= description %>
8
+ </div>
9
+ <div class="flex shrink-0 items-center gap-related -me-related -mt-tight" data-slot="dialog-actions">
10
+ <%= actions %>
11
+ <button type="button" class="<%= lc_icon_button_class(size: :sm, shape: :circle) %>" aria-label="<%= close_label %>" data-action="<%= close_action %>" data-slot="dialog-dismiss"><%= lc_icon "x-mark", options: { "aria-hidden": true } %></button>
12
+ </div>
13
+ </header>
14
+ <div class="-mx-inset -mb-inset flex min-h-0 flex-1 flex-col gap-inset px-inset pb-inset sm:overflow-y-auto sm:overscroll-contain" data-slot="dialog-body"><%= content %></div>
15
+ </div>
16
+ <% end %>
17
+ <% if trigger %>
18
+ <div data-controller="lc-dialog">
19
+ <% if rendered_trigger %>
20
+ <%= rendered_trigger %>
21
+ <% else %>
22
+ <button type="button" class="<%= lc_button_class(variant: :secondary) %>" aria-haspopup="dialog" aria-controls="<%= id %>" data-action="lc-dialog#open" data-slot="dialog-trigger"><%= trigger %></button>
23
+ <% end %>
24
+ <%= dialog %>
25
+ </div>
26
+ <% else %>
27
+ <%= dialog %>
28
+ <% end %>