lostcoords-ui 0.14.12 → 0.14.13

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: dc041cd2b9ef1078c34335a00669cc6fdcdc22622e16a656a6ca20d8e1aaada6
4
- data.tar.gz: cd4f1528f765fde97389ce9242e7c47b18b37c916d87156cb682f1205644449a
3
+ metadata.gz: 729b5ab883cb06c1a4b067c3b0581b3743341a414dd18daf08c0af2b0a407eca
4
+ data.tar.gz: 1fbe2ad618bc983a2f3ad4b5d86a5bdf6c2481755ec81161351d762118b38a5c
5
5
  SHA512:
6
- metadata.gz: d00c1bbca1ca15a7cd38aa205efe5c2caae746cfa4b1e56794e0a0f6160cff618fde0fcc96cf181b0c0a0aa3f618c3acc741921a15c8fff318b38ca7050ab6a3
7
- data.tar.gz: a0fc3feba6f2aecccf32f92d7e015f60bba5b01daa5ea5e5ef48a93efab0d48896cbc00f2810ad241daed534e03cfeb6baaeb601baca20dbb7d2179a14297d9b
6
+ metadata.gz: ac70042419af37f4ea4b5a5bc6c5d9e835420ac953d8b8225b31a6fff792952cb9b4f54a58075152e2cab7f795f5a771dbea737417d2691efaa83925aa766173
7
+ data.tar.gz: 2cbf0a9ee9680c60c46bac6b88b82853308de0c36375b1b90b5d90bb4c26b22547c06d4740b1e015d71bdc71d4609770271181246c750496f0f3acfd00a6faa2
data/README.md CHANGED
@@ -170,6 +170,22 @@ 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.14.13
174
+
175
+ Nothing needs an edit. Fixed:
176
+
177
+ - **Overlays animate out in Safari.** WebKit can't transition `overlay` or `display`, so a closing dialog,
178
+ phone sheet, menu, popover or tooltip left the screen in the frame it closed; a sheet released halfway
179
+ down vanished mid-slide. `lc-sheet` now holds a closing dialog in its exit pose (`[data-closing]`) until
180
+ the slide or fade ends, whether `close()`, Escape, the backdrop, a swipe or a `method="dialog"` form
181
+ closed it, and a lifted panel stays in the top layer, shown as it was, until its exit ends. Chrome keeps
182
+ its native exit. Where the exit is held, `close` fires when it ends; `lc-sheet:exit` fires as it starts,
183
+ and the confirm sheet answers then.
184
+ - **A sheet held still before lifting settles back.** It kept the finger's speed from before the pause,
185
+ so a slow drag, a pause and a lift could dismiss it.
186
+ - **A released sheet carries on at the finger's speed**, shortening its slide (to 240ms at least) instead
187
+ of always taking the full 500ms.
188
+
173
189
  ## Changes in 0.14.12
174
190
 
175
191
  Needs an edit only if you import component entries one by one: `emoji-picker` now also needs `dialog`,
@@ -29,6 +29,12 @@
29
29
  }
30
30
  }
31
31
 
32
+ /* Where `overlay` can't transition, lc-sheet holds a closing dialog open in its exit pose. */
33
+ &[open][data-closing] {
34
+ opacity: 0;
35
+ transform: var(--lc-dialog-from);
36
+ }
37
+
32
38
  /* Fades the tint and the blur, not opacity: Chrome applies a backdrop-filter
33
39
  under opacity < 1 to only some of the page's layers. */
34
40
  &::backdrop {
@@ -73,6 +79,12 @@
73
79
  backdrop-filter: none;
74
80
  }
75
81
 
82
+ /* After the context scrim, which it must outrank. */
83
+ &[open][data-closing]::backdrop {
84
+ background: transparent;
85
+ backdrop-filter: blur(0);
86
+ }
87
+
76
88
  /* The dialog still fades; it no longer moves or grows. */
77
89
  @media (prefers-reduced-motion: reduce) {
78
90
  transform: none;
@@ -1,6 +1,7 @@
1
1
  /* Closed is the exit pose, so closing animates too; allow-discrete holds
2
2
  display/overlay until it ends. Open is FloatingPanel's [data-open] or a
3
- native [popover]'s :popover-open. --lc-popover-from sets the closed pose;
3
+ native [popover]'s :popover-open; a panel lifted to the top layer
4
+ ([data-lc-lifted]) follows [data-open] alone. --lc-popover-from sets the closed pose;
4
5
  origin-* utilities set where it grows from. */
5
6
  @utility lc-popover-shell {
6
7
  opacity: 0;
@@ -12,7 +13,7 @@
12
13
  display var(--lc-motion-feedback) allow-discrete,
13
14
  overlay var(--lc-motion-feedback) allow-discrete;
14
15
 
15
- &:is([data-open], :popover-open) {
16
+ &:is([data-open], :popover-open:not([data-lc-lifted])) {
16
17
  opacity: 1;
17
18
  transform: translateY(0) scale(1);
18
19
  pointer-events: auto;
@@ -28,7 +29,7 @@
28
29
  /* stylelint-disable-next-line no-invalid-position-declaration -- an @utility body nests like a rule */
29
30
  transform: none;
30
31
 
31
- &:is([data-open], :popover-open) {
32
+ &:is([data-open], :popover-open:not([data-lc-lifted])) {
32
33
  @starting-style { transform: none; }
33
34
  }
34
35
  }
@@ -65,6 +65,10 @@
65
65
  @starting-style { transform: translateY(var(--lc-sheet-offscreen)); }
66
66
  }
67
67
 
68
+ /* Where `overlay` can't transition, lc-sheet holds a closing dialog open
69
+ in its exit pose until the slide ends. */
70
+ &[open][data-closing] { transform: translateY(var(--lc-sheet-offscreen)); }
71
+
68
72
  &::backdrop {
69
73
  background: var(--lc-scrim-sheet);
70
74
  backdrop-filter: none;
@@ -81,6 +85,8 @@
81
85
  @starting-style { opacity: 0; }
82
86
  }
83
87
 
88
+ &[open][data-closing]::backdrop { opacity: 0; }
89
+
84
90
  /* The sheet appears in place; lc-sheet's own slides already skip under reduced motion. */
85
91
  @media (prefers-reduced-motion: reduce) {
86
92
  transition: none;
@@ -8,6 +8,16 @@ const MEDIUM_FRACTION = 0.5
8
8
  const NO_DRAG = "input[type=range], [data-lc-sheet-no-drag]"
9
9
  const DRAG_ZONE = 28
10
10
  const KEYBOARD_FREE = 0.5
11
+ // A finger held still this long before lifting has no fling left in it.
12
+ const STILL_MS = 80
13
+ // --lc-ease-sheet, cubic-bezier(0.32, 0.72, 0, 1), starts at 0.72 / 0.32 = 2.25×
14
+ // its average speed. Change both together.
15
+ const EASE_START = 0.72 / 0.32
16
+ const FASTEST_MS = 240
17
+ // Safari and Firefox can't transition `overlay`, so a closing <dialog> leaves
18
+ // the top layer at once and vanishes mid-slide. There the controller plays the
19
+ // exit pose ([data-closing]) first and closes when it ends.
20
+ const EXITS_NATIVELY = CSS.supports("overlay", "auto")
11
21
 
12
22
  export default class extends Controller {
13
23
  static values = { detents: { type: String, default: "fit" } }
@@ -25,6 +35,7 @@ export default class extends Controller {
25
35
  this.element.addEventListener("touchend", this.onTouchEnd)
26
36
  this.element.addEventListener("touchcancel", this.onTouchEnd)
27
37
  this.element.addEventListener("pointerdown", this.onPointerDown)
38
+ if (this.isDialog && !EXITS_NATIVELY) this.holdClose()
28
39
 
29
40
  this.sync = this.sync.bind(this)
30
41
  this.shown = false
@@ -52,6 +63,7 @@ export default class extends Controller {
52
63
  this.element.removeEventListener("touchend", this.onTouchEnd)
53
64
  this.element.removeEventListener("touchcancel", this.onTouchEnd)
54
65
  this.element.removeEventListener("pointerdown", this.onPointerDown)
66
+ this.releaseClose()
55
67
  this.stopMouseTracking()
56
68
  clearTimeout(this.settleTimer)
57
69
  clearTimeout(this.transitionTimer)
@@ -113,7 +125,7 @@ export default class extends Controller {
113
125
  }
114
126
 
115
127
  onTouchEnd(event) {
116
- if (this.gesture?.dragging) this.release()
128
+ if (this.gesture?.dragging) this.release(event?.timeStamp ?? performance.now())
117
129
  else if (this.gesture && event?.type === "touchend") this.focusInPlace(event)
118
130
  this.gesture = null
119
131
  }
@@ -156,7 +168,8 @@ export default class extends Controller {
156
168
  onPointerUp(event) {
157
169
  this.stopMouseTracking()
158
170
  if (this.gesture && Math.abs(event.clientY - this.gesture.startY) > 4) swallowNextClick()
159
- this.onTouchEnd()
171
+ if (this.gesture?.dragging) this.release(event.timeStamp)
172
+ this.gesture = null
160
173
  }
161
174
 
162
175
  stopMouseTracking() {
@@ -212,18 +225,37 @@ export default class extends Controller {
212
225
  )
213
226
  }
214
227
 
215
- release() {
216
- const { detents, velocity } = this.gesture
217
- const projected = this.visibleHeight() - velocity * PROJECTION_MS
228
+ release(time) {
229
+ const { detents, lastT } = this.gesture
230
+ const velocity = time - lastT > STILL_MS ? 0 : this.gesture.velocity
231
+ const visible = this.visibleHeight()
232
+ const projected = visible - velocity * PROJECTION_MS
218
233
 
219
- if (velocity >= 0 && projected < detents[0].height * 0.6) return this.dismiss()
234
+ if (velocity >= 0 && projected < detents[0].height * 0.6) {
235
+ this.pace(visible + this.offscreenGap, velocity)
236
+ return this.dismiss()
237
+ }
220
238
 
221
239
  const target = detents.reduce((best, detent) =>
222
240
  Math.abs(detent.height - projected) < Math.abs(best.height - projected) ? detent : best,
223
241
  )
242
+ // A fling away from the target, or a lift in place, gets the full beat.
243
+ const toward = Math.sign(visible - target.height) === Math.sign(velocity)
244
+ this.pace(visible - target.height, toward ? velocity : 0)
224
245
  this.settle(target, detents)
225
246
  }
226
247
 
248
+ // UIKit hands a released sheet on at the finger's speed. The sheet curve opens
249
+ // at EASE_START times its average speed, so a beat of EASE_START × distance ÷
250
+ // speed carries the fling on without a jolt; a slow lift keeps the full beat.
251
+ pace(distance, velocity) {
252
+ const full = sheetDuration()
253
+ const speed = Math.abs(velocity)
254
+ if (!full || !speed) return this.element.style.removeProperty("--lc-sheet-duration")
255
+ const ms = Math.min(full, Math.max(FASTEST_MS, (EASE_START * Math.abs(distance)) / speed))
256
+ this.element.style.setProperty("--lc-sheet-duration", `${Math.round(ms)}ms`)
257
+ }
258
+
227
259
  settle(detent, detents = this.detents()) {
228
260
  this.detent = detent
229
261
  this.transition()
@@ -242,15 +274,69 @@ export default class extends Controller {
242
274
  })
243
275
  }
244
276
 
277
+ // The cancel may close the dialog, or keep it open (a listener asking to
278
+ // discard changes): then the sheet settles back.
245
279
  dismissDialog() {
280
+ this.transition()
246
281
  const cancel = new Event("cancel", { cancelable: true })
247
282
  this.element.dispatchEvent(cancel)
248
283
  if (!cancel.defaultPrevented) this.element.close()
249
284
 
250
- if (this.element.open) return this.settle(this.detent || this.detents()[0])
285
+ if (this.element.open && !this.closing) return this.settle(this.detent || this.detents()[0])
286
+ this.element.style.transform = ""
287
+ }
251
288
 
252
- this.transition()
289
+ // Every close() then goes through the exit, whoever calls it, and so does a
290
+ // method=dialog form, which the browser would close at once. A native Escape
291
+ // on a dialog no dismiss stack handles still closes at once.
292
+ holdClose() {
293
+ const close = HTMLDialogElement.prototype.close
294
+ this.closeNow = (value) => close.call(this.element, value)
295
+ this.element.close = (value) => this.closeAfterExit(value)
296
+ this.onDialogSubmit = (event) => {
297
+ const method = event.submitter?.getAttribute("formmethod") || event.target.method
298
+ if (method !== "dialog" || event.target.closest("dialog") !== this.element) return
299
+ event.preventDefault()
300
+ this.element.close(event.submitter?.value)
301
+ }
302
+ this.element.addEventListener("submit", this.onDialogSubmit)
303
+ }
304
+
305
+ releaseClose() {
306
+ if (!this.closeNow) return
307
+ delete this.element.close
308
+ this.element.removeEventListener("submit", this.onDialogSubmit)
309
+ clearTimeout(this.exitTimer)
310
+ if (this.closing) this.finishClose()
311
+ }
312
+
313
+ closeAfterExit(value) {
314
+ if (!this.element.open || this.closing) return
315
+ this.closing = { value }
316
+ if (value !== undefined) this.element.returnValue = value
317
+ this.element.toggleAttribute("data-closing", true)
318
+ // close fires once the slide ends; lc-sheet:exit tells a caller waiting on the answer now.
319
+ this.dispatch("exit", { bubbles: false })
253
320
  this.element.style.transform = ""
321
+ this.exitTimer = setTimeout(() => this.finishClose(), this.exitDuration())
322
+ }
323
+
324
+ finishClose() {
325
+ const { value } = this.closing
326
+ this.closing = null
327
+ this.closeNow(value)
328
+ this.element.removeAttribute("data-closing")
329
+ }
330
+
331
+ exitDuration() {
332
+ const { transitionDuration, transitionDelay } = getComputedStyle(this.element)
333
+ const ms = (list) =>
334
+ list.split(",").map((time) => parseFloat(time) * (time.trim().endsWith("ms") ? 1 : 1000))
335
+ const delays = ms(transitionDelay)
336
+ return Math.max(
337
+ 0,
338
+ ...ms(transitionDuration).map((duration, i) => duration + (delays[i % delays.length] || 0)),
339
+ )
254
340
  }
255
341
 
256
342
  detents() {
@@ -324,12 +410,17 @@ export default class extends Controller {
324
410
  this.transitionTimer = setTimeout(() => {
325
411
  this.element.style.transition = ""
326
412
  this.element.style.willChange = ""
327
- }, sheetDuration())
413
+ this.element.style.removeProperty("--lc-sheet-duration")
414
+ }, sheetDuration(this.element))
328
415
  }
329
416
 
330
417
  later(callback) {
331
418
  clearTimeout(this.settleTimer)
332
- this.settleTimer = setTimeout(callback, sheetDuration())
419
+ this.settleTimer = setTimeout(callback, sheetDuration(this.element))
420
+ }
421
+
422
+ get offscreenGap() {
423
+ return parseFloat(getComputedStyle(document.documentElement).fontSize) * 2
333
424
  }
334
425
 
335
426
  resetSurface() {
@@ -339,6 +430,7 @@ export default class extends Controller {
339
430
  style.willChange = ""
340
431
  style.blockSize = ""
341
432
  style.removeProperty("--lc-sheet-floor")
433
+ style.removeProperty("--lc-sheet-duration")
342
434
  }
343
435
 
344
436
  scrollerFor(target) {
@@ -44,9 +44,14 @@ export function ask(
44
44
 
45
45
  return new Promise((resolve) => {
46
46
  dialog.returnValue = ""
47
- dialog.addEventListener("close", () => resolve(dialog.returnValue === "confirm"), {
48
- once: true,
49
- })
47
+ // lc-sheet:exit comes first where the sheet holds its close for the exit slide.
48
+ const answer = () => {
49
+ dialog.removeEventListener("close", answer)
50
+ dialog.removeEventListener("lc-sheet:exit", answer)
51
+ resolve(dialog.returnValue === "confirm")
52
+ }
53
+ dialog.addEventListener("close", answer)
54
+ dialog.addEventListener("lc-sheet:exit", answer)
50
55
  dialog.style.removeProperty("--lc-dialog-origin")
51
56
  showContextDialog(dialog, anchor)
52
57
  registerDialog(dialog, { lockScroll: false })
@@ -4,10 +4,9 @@ import { prefersReducedMotion } from "lostcoords/ui/helpers/viewport"
4
4
  // directly; timers need the duration in milliseconds.
5
5
  export const SHEET_TIMING = "var(--lc-sheet-duration) var(--lc-ease-sheet)"
6
6
 
7
- export function sheetDuration() {
7
+ // A released sheet sets its own --lc-sheet-duration; pass it to read that beat.
8
+ export function sheetDuration(element = document.documentElement) {
8
9
  if (prefersReducedMotion()) return 0
9
- const value = getComputedStyle(document.documentElement)
10
- .getPropertyValue("--lc-sheet-duration")
11
- .trim()
10
+ const value = getComputedStyle(element).getPropertyValue("--lc-sheet-duration").trim()
12
11
  return value.endsWith("ms") ? parseFloat(value) : parseFloat(value) * 1000
13
12
  }
@@ -14,6 +14,11 @@
14
14
  // pinned to a trigger, and onPageLeave to close them before Turbo caches a page.
15
15
 
16
16
  const MARGIN = 8
17
+ // Safari and Firefox can't transition `overlay`: a hidden popover leaves the top
18
+ // layer, and the screen, at once. WebKit doesn't hold `display` for an exit
19
+ // either. There a lifted panel stays in the top layer, shown as it was, until
20
+ // its exit transition ends.
21
+ const EXITS_NATIVELY = CSS.supports("overlay", "auto")
17
22
  const PAGE_LEAVES = ["turbo:before-cache", "turbo:before-visit", "turbo:before-render"]
18
23
 
19
24
  export const supportsTopLayer = () => typeof HTMLElement.prototype.showPopover === "function"
@@ -41,7 +46,16 @@ export function onPageLeave(close) {
41
46
  window.removeEventListener("pagehide", close)
42
47
  }
43
48
  }
44
- const PLACEMENT = ["position", "inset", "top", "left", "margin", "translate", "inlineSize"]
49
+ const PLACEMENT = [
50
+ "position",
51
+ "inset",
52
+ "top",
53
+ "left",
54
+ "margin",
55
+ "translate",
56
+ "inlineSize",
57
+ "display",
58
+ ]
45
59
 
46
60
  const lifts = new WeakMap()
47
61
  const pinned = new WeakSet()
@@ -60,8 +74,12 @@ export function liftToTopLayer(panel, trigger) {
60
74
  // other panel flips above when it would leave the bottom.
61
75
  const above = placed.bottom <= anchor.top + 1
62
76
  const gap = above ? anchor.top - placed.bottom : placed.top - anchor.bottom
77
+ const shown = getComputedStyle(panel).display
63
78
 
64
79
  showTopLayer(panel)
80
+ // A lifted panel is open while [data-open], not while :popover-open, so it can
81
+ // play its exit pose before it leaves the top layer.
82
+ panel.toggleAttribute("data-lc-lifted", true)
65
83
  Object.assign(panel.style, {
66
84
  position: "fixed",
67
85
  inset: "auto",
@@ -114,10 +132,14 @@ export function liftToTopLayer(panel, trigger) {
114
132
 
115
133
  // Keep the pinned spot while the closing transition plays out, then hand
116
134
  // the panel back to its stylesheet unless it has been lifted again.
117
- Promise.allSettled(panel.getAnimations().map((animation) => animation.finished)).then(() => {
135
+ // The caller has just hidden the panel and no style has been read since, so
136
+ // holding its display here starts the exit transition from the open pose.
137
+ if (!EXITS_NATIVELY) panel.style.display = shown
138
+ const exits = panel.getAnimations().map((animation) => animation.finished)
139
+ if (EXITS_NATIVELY) hideTopLayer(panel)
140
+ Promise.allSettled(exits).then(() => {
118
141
  if (!lifts.has(panel)) reset(panel)
119
142
  })
120
- hideTopLayer(panel)
121
143
  }
122
144
  lifts.set(panel, release)
123
145
  return release
@@ -127,6 +149,7 @@ function reset(panel) {
127
149
  pinned.delete(panel)
128
150
  hideTopLayer(panel)
129
151
  panel.removeAttribute("popover")
152
+ panel.removeAttribute("data-lc-lifted")
130
153
  for (const property of PLACEMENT) panel.style[property] = ""
131
154
  }
132
155
 
@@ -1,5 +1,5 @@
1
1
  module Lostcoords
2
2
  module Ui
3
- VERSION = "0.14.12"
3
+ VERSION = "0.14.13"
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.14.12
4
+ version: 0.14.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lost Coordinates