lostcoords-ui 0.12.0 → 0.12.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d40b25262f26a57dea7b82cada5df30d683de3fb28f2c9db0046130cfb26a26f
4
- data.tar.gz: 3c3b01b38a8d674bd8a59f068b6b5e7db92563cecd5d56bdeb43b08257373526
3
+ metadata.gz: 8826e4a226e995babe0ca3b5c0b8acc0814e018e992e685d2792f73d499df817
4
+ data.tar.gz: 9b567d198b078a9dc9e07c4fcddc5ab8271febae59bc088c556e9bb2616f46b7
5
5
  SHA512:
6
- metadata.gz: d05b7df8c044fed6d94d2d8ff97caab7d9e589bdb2d218bf3d5a49bba27c5b7dbb7df2b7c4354e55947ef48025803167fc0d542892ce640bb50c0461efc175fb
7
- data.tar.gz: 9ccbd01d8a5171657cd56e71dd6537628efed50f75fb00445226abc96999559b58fd4ba415664c17ac4820e919908cc74a8dabf2ad8c53efef6b559e6736ef86
6
+ metadata.gz: 87f135447441e6fa94670fe213ae19c3e9e04d377c174aa9ce27df733b130dfbd8e1c92fd461d663fbbb8d3dfe957ce89d9b3752b9cacc17e9b1a7a501e8fb60
7
+ data.tar.gz: 720a4d496cb092ee008ec9d7580606804feb1f72d1b1086e5e7cbad0f972755b32df1d13d882a2ac3c470b7de855f9d2f36771314d58f1ee008538967675dfbf
data/README.md CHANGED
@@ -170,6 +170,30 @@ Omit `only` to register every library controller. Unknown identifiers raise befo
170
170
  registered, and repeated calls on the same application keep later overrides, so register defaults
171
171
  before application overrides.
172
172
 
173
+ ## Changes in 0.12.2
174
+
175
+ Nothing needs an edit. Fixed:
176
+
177
+ - **Back no longer brings a dialog back half open.** Turbo snapshots a page with its open dialogs, and
178
+ restoring one kept the `open` attribute without the top layer: no backdrop, focus trap or Escape, and the
179
+ page stayed pinned by the scroll lock. A dialog joined through `registerDialog` now comes back closed and
180
+ the page unpinned; a controller that reopens its dialog on connect shows it as a real modal again.
181
+
182
+ ## Changes in 0.12.1
183
+
184
+ Nothing needs an edit. Changed:
185
+
186
+ - **A dialog opens with focus on itself.** `lc-dialog` and the new **`showDialog(dialog)`** helper in
187
+ `lostcoords/ui/helpers/dialog` focus the dialog rather than its first control, so the close button no longer
188
+ shows the focus ring after a tap and a first field no longer raises the keyboard. A control marked
189
+ `autofocus` still takes focus.
190
+ - **The focus halo skips dialogs**, including ones with a `tabindex`.
191
+
192
+ New option:
193
+
194
+ - **`lc_section(header: :from_sm)` hides the heading row on phones**, for a section whose content explains
195
+ itself on a small screen, such as a balance card. Screen readers still get the heading; its actions are hidden.
196
+
173
197
  ## Changes in 0.12.0
174
198
 
175
199
  One edit, and some components look different:
@@ -914,10 +938,12 @@ the scroller's edges and clears the home indicator.
914
938
  <% end %>
915
939
  ```
916
940
 
917
- A controller that opens a `<dialog>` itself joins it to the dismiss stack with `registerDialog` from
918
- `lostcoords/ui/helpers/dialog`, so Escape, a backdrop click and a swipe close it, and the page stops scrolling
919
- behind it. `registerDialog(dialog, { close, priority, restoreFocus, lockScroll })`: `close` may return `false`
920
- to keep the dialog open, such as while it asks to discard changes; `priority` ranks it against panels open in
941
+ A controller that opens a `<dialog>` itself shows it with `showDialog(dialog)` and joins it to the dismiss
942
+ stack with `registerDialog`, both from `lostcoords/ui/helpers/dialog`. `showDialog` opens it modal with focus
943
+ on the dialog itself, so no focus ring lands on its close button and no keyboard rises for a first field; a
944
+ control marked `autofocus` still takes focus. With `registerDialog`, Escape, a backdrop click and a swipe close
945
+ it, and the page stops scrolling behind it.
946
+ `registerDialog(dialog, { close, priority, restoreFocus, lockScroll })`: `close` may return `false` to keep the dialog open, such as while it asks to discard changes; `priority` ranks it against panels open in
921
947
  it (a dialog is 10, a panel 20); `restoreFocus` runs after a close; `lockScroll: false` leaves the page
922
948
  scrollable. It returns the unregister function and ends when the dialog closes; a click on the dialog's own
923
949
  padding does not close it.
@@ -24,7 +24,7 @@
24
24
 
25
25
  :where([popover]) { color: inherit; }
26
26
 
27
- :is(a, button, input, textarea, select, summary, [tabindex]):focus-visible,
27
+ :is(a, button, input, textarea, select, summary, [tabindex]:not(dialog)):focus-visible,
28
28
  label:has(input.sr-only:focus-visible, + input.sr-only:focus-visible) {
29
29
  outline: calc(var(--spacing) * 0.5) solid var(--ring, color-mix(in oklab, var(--primary) 50%, transparent));
30
30
  outline-offset: 0;
@@ -83,15 +83,19 @@ module Lostcoords::Ui
83
83
  end
84
84
 
85
85
  # anchor: true links the heading to the section's id:, for pages readers link into.
86
- def lc_section(title:, level: 2, actions: nil, anchor: false, **attributes, &block)
86
+ # header: :from_sm shows the heading row from sm up, for content that explains itself on a phone. Below
87
+ # sm the heading stays for screen readers (sr-only also takes it out of the stack gap) and the actions go.
88
+ def lc_section(title:, level: 2, actions: nil, anchor: false, header: :visible, **attributes, &block)
87
89
  raise ArgumentError, "title must not be blank" if title.blank?
88
90
  raise ArgumentError, "anchor: true needs an id" if anchor && attributes[:id].blank?
89
91
 
90
92
  tag.section(**lc_attributes({ class: lc_stack_class(gap: :heading, extra: ("lc-anchor-target" if anchor)) }, attributes)) do
91
93
  heading = lc_heading(title, level: level, size: :heading, anchor: (attributes[:id] if anchor))
92
94
  action_content = actions.respond_to?(:call) ? capture(&actions) : actions
93
- header = lc_section_header { safe_join([ heading, action_content ].compact) }
94
- safe_join([ header, lc_stack(&block) ])
95
+ from_sm = lc_option!(:header, header, %i[visible from_sm]) == :from_sm
96
+ action_content = tag.div(action_content, class: "contents max-sm:hidden") if from_sm && action_content.present?
97
+ header_row = lc_section_header(class: ("max-sm:sr-only" if from_sm)) { safe_join([ heading, action_content ].compact) }
98
+ safe_join([ header_row, lc_stack(&block) ])
95
99
  end
96
100
  end
97
101
 
@@ -1,5 +1,5 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
- import { registerDialog } from "lostcoords/ui/helpers/dialog"
2
+ import { registerDialog, showDialog } from "lostcoords/ui/helpers/dialog"
3
3
 
4
4
  // Opens a dialog whose body is a Turbo frame fetched on open. It reloads on every
5
5
  // open, so what it shows is never staler than the page that opened it.
@@ -16,7 +16,7 @@ export default class extends Controller {
16
16
  if (this.frameTarget.src) this.frameTarget.reload()
17
17
  else this.frameTarget.src = this.frameTarget.dataset.src
18
18
  }
19
- if (!this.dialogTarget.open) this.dialogTarget.showModal()
19
+ showDialog(this.dialogTarget)
20
20
 
21
21
  this.unregisterDialog = registerDialog(this.dialogTarget)
22
22
  }
@@ -2,6 +2,33 @@ import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
2
2
 
3
3
  const registrations = new WeakMap()
4
4
 
5
+ // Turbo snapshots a page for Back with its open dialogs, and restoring one brings back the open
6
+ // attribute but not the top layer: the dialog shows inline, with no backdrop, focus trap or
7
+ // Escape. A dialog registered when the snapshot was taken is marked, so it comes back closed.
8
+ const MARK = "lcDialog"
9
+ document.addEventListener("turbo:before-render", ({ detail: { newBody } }) => {
10
+ for (const dialog of newBody.querySelectorAll("dialog[data-lc-dialog]")) {
11
+ dialog.removeAttribute("open")
12
+ delete dialog.dataset[MARK]
13
+ }
14
+ })
15
+ // A same-URL refresh morphs the page against server HTML, which never carries the mark.
16
+ document.addEventListener("turbo:before-morph-attribute", (event) => {
17
+ if (event.detail.attributeName === "data-lc-dialog" && registrations.has(event.target))
18
+ event.preventDefault()
19
+ })
20
+
21
+ // Shows a modal <dialog> with focus on the dialog itself rather than its first control: a close
22
+ // button would draw the focus ring after a tap that focused nothing, and a first field would raise
23
+ // a phone's keyboard. A control marked autofocus still takes focus, and Tab reaches every control.
24
+ export function showDialog(dialog) {
25
+ if (dialog.open) return
26
+ dialog.showModal()
27
+ if (dialog.querySelector("[autofocus]")) return
28
+ if (!dialog.hasAttribute("tabindex")) dialog.tabIndex = -1
29
+ dialog.focus({ preventScroll: true })
30
+ }
31
+
5
32
  // Joins a <dialog> to the dismiss stack: Escape, the backdrop and a swipe call close, which may
6
33
  // return false to keep it open, such as while it asks whether to discard changes. priority ranks
7
34
  // it against panels open in it (a dialog is 10, a panel 20); restoreFocus runs after a close;
@@ -42,12 +69,14 @@ export function registerDialog(
42
69
  const unregister = () => {
43
70
  if (registrations.get(dialog) !== unregister) return
44
71
  registrations.delete(dialog)
72
+ delete dialog.dataset[MARK]
45
73
  dialog.removeEventListener("close", unregisterOnClose)
46
74
  dialog.removeEventListener("click", closeOnBackdrop)
47
75
  unregisterDismissable()
48
76
  }
49
77
 
50
78
  registrations.set(dialog, unregister)
79
+ dialog.dataset[MARK] = ""
51
80
  dialog.addEventListener("close", unregisterOnClose)
52
81
  dialog.addEventListener("click", closeOnBackdrop)
53
82
  return unregister
@@ -167,12 +167,22 @@ function unlockDocumentScroll() {
167
167
  const body = document.body
168
168
  if (body?.dataset.modalScrollLock !== "true") return
169
169
 
170
+ releaseScrollLock(body)
171
+ window.scrollTo({ top: lockedScrollY, behavior: "instant" })
172
+ lockedScrollY = 0
173
+ }
174
+
175
+ function releaseScrollLock(body) {
170
176
  body.style.position = ""
171
177
  body.style.inset = ""
172
178
  body.style.top = ""
173
179
  body.style.removeProperty("--lc-scroll-lock-y")
174
180
  body.style.inlineSize = ""
175
181
  delete body.dataset.modalScrollLock
176
- window.scrollTo({ top: lockedScrollY, behavior: "instant" })
177
- lockedScrollY = 0
178
182
  }
183
+
184
+ // A page snapshotted for Back while a dialog held the lock comes back pinned; the dialog
185
+ // that held it comes back closed (helpers/dialog), so the restored page is free to scroll.
186
+ document.addEventListener("turbo:before-render", ({ detail: { newBody } }) => {
187
+ if (newBody.dataset.modalScrollLock === "true") releaseScrollLock(newBody)
188
+ })
@@ -1,5 +1,5 @@
1
1
  module Lostcoords
2
2
  module Ui
3
- VERSION = "0.12.0"
3
+ VERSION = "0.12.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lostcoords-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lost Coordinates