Add swipe-to-resize mobile bottom drawer
Mobile sheets (search results, place details, lists, menu, contributions, activity, new place) become draggable bottom sheets with three detents (collapsed / medium / expanded), fling-snapping, and swipe-down-to-dismiss. The sheet is transform-based and content never reflows while dragging. The map, crosshair, and pin-centering react to the live covered height through mapUi.sheetInsetPx, replacing the old hardcoded "bottom 50%" assumptions. Also fixes a backtracking crash that made the drawer disappear when opening a place from search results: the modifier no longer writes tracked service state during render. Writes are coalesced into an animation-frame flush and gated by an ownership token, so a stale sheet can't corrupt the inset.
This commit is contained in:
13 files changed
+1182
-41
No files matched your search
@@ -13,6 +13,7 @@ import eq from 'ember-truth-helpers/helpers/eq';
|
||||
import and from 'ember-truth-helpers/helpers/and';
|
||||
import restoreScroll from '../modifiers/restore-scroll';
|
||||
import observeIntersection from '../modifiers/observe-intersection';
|
||||
import bottomSheet from '../modifiers/bottom-sheet';
|
||||
|
||||
export default class ActivityTimelineComponent extends Component {
|
||||
@tracked isNostrConnectModalOpen = false;
|
||||
@@ -40,7 +41,7 @@ export default class ActivityTimelineComponent extends Component {
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="sidebar">
|
||||
<div class="sidebar" {{bottomSheet detent="medium" onDismiss=@onClose}}>
|
||||
<div class="sidebar-header has-back-btn">
|
||||
<button type="button" class="back-btn" {{on "click" @onBack}}>
|
||||
<Icon @name="arrow-left" @size={{20}} @color="#333" />
|
||||
|
||||
@@ -4,6 +4,7 @@ import { tracked } from '@glimmer/tracking';
|
||||
import { fn } from '@ember/helper';
|
||||
import { service } from '@ember/service';
|
||||
import eq from 'ember-truth-helpers/helpers/eq';
|
||||
import bottomSheet from '../../modifiers/bottom-sheet';
|
||||
|
||||
import AppMenuHome from './home';
|
||||
import AppMenuSettings from './settings';
|
||||
@@ -34,7 +35,10 @@ export default class AppMenu extends Component {
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="sidebar app-menu-pane">
|
||||
<div
|
||||
class="sidebar app-menu-pane"
|
||||
{{bottomSheet detent="expanded" onDismiss=@onClose}}
|
||||
>
|
||||
{{#if (eq this.currentView "menu")}}
|
||||
<AppMenuHome
|
||||
@onNavigate={{this.setView}}
|
||||
|
||||
@@ -9,6 +9,7 @@ import NostrConnect from './nostr-connect';
|
||||
import eq from 'ember-truth-helpers/helpers/eq';
|
||||
import not from 'ember-truth-helpers/helpers/not';
|
||||
import restoreScroll from '../modifiers/restore-scroll';
|
||||
import bottomSheet from '../modifiers/bottom-sheet';
|
||||
|
||||
export default class ContributionsTimelineComponent extends Component {
|
||||
@tracked isNostrConnectModalOpen = false;
|
||||
@@ -31,7 +32,7 @@ export default class ContributionsTimelineComponent extends Component {
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="sidebar">
|
||||
<div class="sidebar" {{bottomSheet detent="medium" onDismiss=@onClose}}>
|
||||
<div class="sidebar-header has-back-btn">
|
||||
<button type="button" class="back-btn" {{on "click" @onBack}}>
|
||||
<Icon @name="arrow-left" @size={{20}} @color="#333" />
|
||||
|
||||
+34
-16
@@ -42,6 +42,7 @@ export default class MapComponent extends Component {
|
||||
crosshairElement;
|
||||
crosshairOverlay;
|
||||
ignoreNextMapClick = false;
|
||||
lastSheetSnapSeq;
|
||||
|
||||
setupMap = modifier((element) => {
|
||||
if (this.mapInstance) return;
|
||||
@@ -718,13 +719,12 @@ export default class MapComponent extends Component {
|
||||
size[0] * 0.15, // Left
|
||||
];
|
||||
|
||||
// Mobile: Bottom sheet covers 50% of the screen height
|
||||
// Mobile: the bottom sheet covers `sheetInsetPx` pixels from the bottom.
|
||||
if (size[0] <= 768) {
|
||||
// We want the geometry to be centered in the top 50% of the screen.
|
||||
// Top padding: 15% of the VISIBLE height (size[1] * 0.5)
|
||||
const visibleHeight = size[1] * 0.5;
|
||||
const inset = this.mapUi.sheetInsetPx;
|
||||
const visibleHeight = Math.max(size[1] - inset, 1);
|
||||
const topPadding = visibleHeight * 0.15;
|
||||
const bottomPadding = size[1] * 0.5 + visibleHeight * 0.15; // Sheet + padding
|
||||
const bottomPadding = inset + visibleHeight * 0.15;
|
||||
|
||||
padding[0] = topPadding;
|
||||
padding[2] = bottomPadding;
|
||||
@@ -802,16 +802,15 @@ export default class MapComponent extends Component {
|
||||
let targetCenter = coords;
|
||||
|
||||
// Check if mobile (width <= 768px matches CSS)
|
||||
if (size[0] <= 768) {
|
||||
// On mobile, the bottom 50% is covered by the sheet.
|
||||
// We want the pin to be in the center of the TOP 50% (visible area), minus the header.
|
||||
|
||||
if (size[0] <= 768 && this.mapUi.sheetInsetPx > 0) {
|
||||
const height = size[1];
|
||||
const inset = this.mapUi.sheetInsetPx;
|
||||
const headerEl = document.querySelector('.app-header');
|
||||
const headerHeight = headerEl ? headerEl.offsetHeight : 60;
|
||||
|
||||
// Visible area is from headerHeight to height / 2 (bottom sheet covers bottom 50%)
|
||||
const visibleCenterY = headerHeight + (height / 2 - headerHeight) / 2;
|
||||
// Visible area runs from the header down to the top of the sheet.
|
||||
const visibleHeight = height - inset;
|
||||
const visibleCenterY = headerHeight + (visibleHeight - headerHeight) / 2;
|
||||
|
||||
// The map center is at y = height * 0.50.
|
||||
// So the pin is "above" the center by (height/2 - visibleCenterY) pixels.
|
||||
@@ -880,13 +879,14 @@ export default class MapComponent extends Component {
|
||||
const headerHeight = headerEl ? headerEl.offsetHeight : 60;
|
||||
|
||||
// 1. Mobile Bottom Sheet Logic (Screen <= 768px)
|
||||
if (size[0] <= 768) {
|
||||
if (size[0] <= 768 && this.mapUi.sheetInsetPx > 0) {
|
||||
const height = size[1];
|
||||
const splitPoint = height / 2;
|
||||
const inset = this.mapUi.sheetInsetPx;
|
||||
const visibleHeight = height - inset;
|
||||
|
||||
// If in bottom half
|
||||
if (pixel[1] > splitPoint) {
|
||||
targetPixelY = headerHeight + (height / 2 - headerHeight) / 2; // Target: Center of visible area above bottom sheet
|
||||
// If obscured by the bottom sheet
|
||||
if (pixel[1] > visibleHeight) {
|
||||
targetPixelY = headerHeight + (visibleHeight - headerHeight) / 2; // Target: Center of visible area above bottom sheet
|
||||
needsPan = true;
|
||||
}
|
||||
}
|
||||
@@ -1006,6 +1006,23 @@ export default class MapComponent extends Component {
|
||||
}
|
||||
});
|
||||
|
||||
// Keep the CSS `--sheet-inset` custom property on the map container in sync
|
||||
// with the mobile bottom sheet. The crosshair (a child of the map container)
|
||||
// reads it via CSS. When a drag settles (snapSeq changes) we pan the selected
|
||||
// pin back into the visible area, but never per drag frame.
|
||||
syncSheetInset = modifier((element, [inset, snapSeq]) => {
|
||||
element.style.setProperty('--sheet-inset', `${inset || 0}px`);
|
||||
|
||||
const isInitialRun = this.lastSheetSnapSeq === undefined;
|
||||
if (!isInitialRun && snapSeq !== this.lastSheetSnapSeq) {
|
||||
const place = this.mapUi.selectedPlace;
|
||||
if (place && place.lat && place.lon) {
|
||||
this.panIfObscured(fromLonLat([place.lon, place.lat]));
|
||||
}
|
||||
}
|
||||
this.lastSheetSnapSeq = snapSeq;
|
||||
});
|
||||
|
||||
decodeVectorTileOsmFeature(feature) {
|
||||
if (!feature?.getId || !feature?.get) {
|
||||
return { decoded: null, reason: 'feature does not expose get()/getId()' };
|
||||
@@ -1387,6 +1404,7 @@ export default class MapComponent extends Component {
|
||||
{{this.updateSelectedPin}}
|
||||
{{this.syncPulse}}
|
||||
{{this.syncCreationMode}}
|
||||
{{this.syncSheetInset this.mapUi.sheetInsetPx this.mapUi.sheetSnapSeq}}
|
||||
></div>
|
||||
</template>
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import humanizeOsmTag from '../helpers/humanize-osm-tag';
|
||||
import { getLocalizedName, getPlaceType } from '../utils/osm';
|
||||
import { NEARBY_CATEGORY } from '../utils/poi-categories';
|
||||
import restoreScroll from '../modifiers/restore-scroll';
|
||||
import bottomSheet from '../modifiers/bottom-sheet';
|
||||
|
||||
export default class PlacesSidebar extends Component {
|
||||
@service storage;
|
||||
@@ -167,7 +168,7 @@ export default class PlacesSidebar extends Component {
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="sidebar">
|
||||
<div class="sidebar" {{bottomSheet detent="medium" onDismiss=@onClose}}>
|
||||
<div
|
||||
class="sidebar-header
|
||||
{{if this.hasHeaderPhoto 'no-border'}}
|
||||
|
||||
@@ -0,0 +1,496 @@
|
||||
import Modifier from 'ember-modifier';
|
||||
import { service } from '@ember/service';
|
||||
import { registerDestructor } from '@ember/destroyable';
|
||||
import {
|
||||
clamp,
|
||||
computeDetentLayout,
|
||||
computeSheetHeight,
|
||||
findDetent,
|
||||
nearestDetentIndex,
|
||||
resolveSnap,
|
||||
} from '../utils/sheet-detents';
|
||||
|
||||
// The mobile breakpoint must match the CSS `width <= 768px` query and the
|
||||
// `size[0] <= 768` checks in components/map.gjs. matchMedia needs the
|
||||
// max-width form.
|
||||
const MOBILE_QUERY = '(max-width: 768px)';
|
||||
const REDUCED_MOTION_QUERY = '(prefers-reduced-motion: reduce)';
|
||||
|
||||
// How far the pointer must travel before a press becomes a drag. Keeps taps on
|
||||
// the header's back/close buttons working.
|
||||
const DRAG_THRESHOLD_PX = 6;
|
||||
|
||||
// Velocity is sampled over this trailing window.
|
||||
const VELOCITY_WINDOW_MS = 100;
|
||||
|
||||
// Must be >= the CSS transform transition duration.
|
||||
const TRANSITION_MS = 300;
|
||||
|
||||
const HANDLE_SELECTOR = '.sheet-handle, .sidebar-header';
|
||||
const INTERACTIVE_SELECTOR = 'button, a, input, select, textarea';
|
||||
|
||||
export default class BottomSheetModifier extends Modifier {
|
||||
@service mapUi;
|
||||
|
||||
element = null;
|
||||
named = {};
|
||||
mediaQuery = null;
|
||||
reducedMotionQuery = null;
|
||||
active = false;
|
||||
destroyed = false;
|
||||
handle = null;
|
||||
previousDetentArg = null;
|
||||
|
||||
// Gesture state
|
||||
pointerId = null;
|
||||
pending = false;
|
||||
dragging = false;
|
||||
startY = 0;
|
||||
startOffset = 0;
|
||||
currentOffset = 0;
|
||||
samples = [];
|
||||
dismissTimer = null;
|
||||
|
||||
// Deferred service writes. The Map reads sheetInsetPx during render, so the
|
||||
// modifier must not write it mid-render; it coalesces writes into a
|
||||
// requestAnimationFrame flush and releases ownership the same way.
|
||||
pendingInset = null;
|
||||
pendingSnap = false;
|
||||
flushRaf = null;
|
||||
releaseRaf = null;
|
||||
|
||||
// Layout, recomputed on viewport changes
|
||||
viewportHeight = 0;
|
||||
sheetHeight = 0;
|
||||
layout = [];
|
||||
|
||||
modify(element, _positional, named) {
|
||||
const firstRun = this.element === null;
|
||||
this.element = element;
|
||||
this.named = named ?? {};
|
||||
|
||||
if (firstRun) {
|
||||
this.mediaQuery = window.matchMedia(MOBILE_QUERY);
|
||||
this.reducedMotionQuery = window.matchMedia(REDUCED_MOTION_QUERY);
|
||||
this.onMediaChange = () => this.applyActiveState();
|
||||
this.mediaQuery.addEventListener('change', this.onMediaChange);
|
||||
this.onResize = () => this.handleViewportChange();
|
||||
window.addEventListener('resize', this.onResize);
|
||||
if (window.visualViewport) {
|
||||
this.onVisualViewport = () => this.handleViewportChange();
|
||||
window.visualViewport.addEventListener('resize', this.onVisualViewport);
|
||||
}
|
||||
registerDestructor(this, () => this.teardown());
|
||||
}
|
||||
|
||||
this.applyActiveState();
|
||||
}
|
||||
|
||||
// --- activation -----------------------------------------------------------
|
||||
|
||||
applyActiveState() {
|
||||
const shouldBeActive = this.mediaQuery.matches && !this.named.disabled;
|
||||
|
||||
if (shouldBeActive && !this.active) {
|
||||
this.activate();
|
||||
} else if (!shouldBeActive && this.active) {
|
||||
this.deactivate();
|
||||
} else if (shouldBeActive) {
|
||||
this.handleDetentArgChange();
|
||||
}
|
||||
}
|
||||
|
||||
activate() {
|
||||
this.active = true;
|
||||
// A pending release from an earlier deactivate must not fire after we have
|
||||
// re-claimed the slot (breakpoint flip / disabled arg flip).
|
||||
this.cancelRelease();
|
||||
this.mapUi.claimSheetInset(this);
|
||||
this.injectHandle();
|
||||
this.element.addEventListener('pointerdown', this.onPointerDown);
|
||||
this.measure();
|
||||
|
||||
const restored = this.mapUi.sheetDetent;
|
||||
const initialName =
|
||||
restored ?? this.named.detent ?? this.layout[this.layout.length - 1].name;
|
||||
this.previousDetentArg = this.named.detent ?? null;
|
||||
this.enterAt(initialName);
|
||||
}
|
||||
|
||||
deactivate() {
|
||||
this.active = false;
|
||||
this.cancelDrag();
|
||||
this.element.removeEventListener('pointerdown', this.onPointerDown);
|
||||
this.removeHandle();
|
||||
this.element.classList.remove('sheet-dragging');
|
||||
this.element.style.removeProperty('--sheet-height');
|
||||
this.element.style.removeProperty('--sheet-offset');
|
||||
this.cancelFlush();
|
||||
this.scheduleRelease();
|
||||
}
|
||||
|
||||
teardown() {
|
||||
this.destroyed = true;
|
||||
if (this.dismissTimer) {
|
||||
clearTimeout(this.dismissTimer);
|
||||
this.dismissTimer = null;
|
||||
}
|
||||
this.cancelDrag();
|
||||
|
||||
if (this.mediaQuery && this.onMediaChange) {
|
||||
this.mediaQuery.removeEventListener('change', this.onMediaChange);
|
||||
}
|
||||
window.removeEventListener('resize', this.onResize);
|
||||
if (window.visualViewport && this.onVisualViewport) {
|
||||
window.visualViewport.removeEventListener(
|
||||
'resize',
|
||||
this.onVisualViewport
|
||||
);
|
||||
}
|
||||
|
||||
if (this.element) {
|
||||
this.element.removeEventListener('pointerdown', this.onPointerDown);
|
||||
this.removeHandle();
|
||||
}
|
||||
// Only release if we actually owned the slot; a modifier that never
|
||||
// activated (desktop) must not touch the service.
|
||||
if (this.active) {
|
||||
this.cancelFlush();
|
||||
this.scheduleRelease();
|
||||
}
|
||||
}
|
||||
|
||||
// --- deferred service writes ---------------------------------------------
|
||||
|
||||
queueInset(px) {
|
||||
this.pendingInset = Math.max(0, Math.round(px));
|
||||
this.scheduleFlush();
|
||||
}
|
||||
|
||||
queueSnap() {
|
||||
this.pendingSnap = true;
|
||||
this.scheduleFlush();
|
||||
}
|
||||
|
||||
// The modifier must not write tracked service state during render (the Map
|
||||
// reads sheetInsetPx while rendering), so writes are coalesced into a
|
||||
// requestAnimationFrame callback, which runs outside the render transaction.
|
||||
scheduleFlush() {
|
||||
if (this.flushRaf !== null) return;
|
||||
this.flushRaf = requestAnimationFrame(() => {
|
||||
this.flushRaf = null;
|
||||
this.flush();
|
||||
});
|
||||
}
|
||||
|
||||
cancelFlush() {
|
||||
if (this.flushRaf !== null) {
|
||||
cancelAnimationFrame(this.flushRaf);
|
||||
this.flushRaf = null;
|
||||
}
|
||||
}
|
||||
|
||||
flush() {
|
||||
if (this.pendingInset !== null) {
|
||||
this.mapUi.setSheetInsetFor(this, this.pendingInset);
|
||||
this.pendingInset = null;
|
||||
}
|
||||
if (this.pendingSnap) {
|
||||
this.pendingSnap = false;
|
||||
this.mapUi.snapSheetFor(this);
|
||||
}
|
||||
}
|
||||
|
||||
// The release is scheduled on its own frame (not tied to the modifier's
|
||||
// lifetime) so it still runs while the modifier is being torn down.
|
||||
// Ownership makes a stale release a no-op once a newer sheet has claimed.
|
||||
scheduleRelease() {
|
||||
this.cancelRelease();
|
||||
this.releaseRaf = requestAnimationFrame(() => {
|
||||
this.releaseRaf = null;
|
||||
this.mapUi.releaseSheetInset(this);
|
||||
});
|
||||
}
|
||||
|
||||
cancelRelease() {
|
||||
if (this.releaseRaf !== null) {
|
||||
cancelAnimationFrame(this.releaseRaf);
|
||||
this.releaseRaf = null;
|
||||
}
|
||||
}
|
||||
|
||||
// --- layout ---------------------------------------------------------------
|
||||
|
||||
measure() {
|
||||
// innerHeight is the numeric source of truth for height; a visualViewport
|
||||
// listener also fires when the iOS keyboard opens, and reading innerHeight
|
||||
// there keeps the measurement identical so the sheet does not collapse.
|
||||
this.viewportHeight = window.innerHeight;
|
||||
this.sheetHeight = computeSheetHeight(this.viewportHeight);
|
||||
this.layout = computeDetentLayout({
|
||||
viewportHeight: this.viewportHeight,
|
||||
sheetHeight: this.sheetHeight,
|
||||
});
|
||||
this.element.style.setProperty('--sheet-height', `${this.sheetHeight}px`);
|
||||
}
|
||||
|
||||
handleViewportChange() {
|
||||
if (!this.active || this.destroyed) return;
|
||||
|
||||
this.measure();
|
||||
|
||||
// Mid-drag: keep the finger-following offset, only refresh the snap
|
||||
// targets so the release settles correctly.
|
||||
if (this.dragging || this.pending) return;
|
||||
|
||||
const name =
|
||||
this.mapUi.sheetDetent ??
|
||||
this.named.detent ??
|
||||
this.layout[this.layout.length - 1].name;
|
||||
const detent =
|
||||
findDetent(this.layout, name) ?? this.layout[this.layout.length - 1];
|
||||
|
||||
// Skip the settle (and its map-pan signal) when nothing actually moved,
|
||||
// e.g. an iOS keyboard opening resizes the visual viewport but not
|
||||
// innerHeight.
|
||||
if (detent.offset === this.currentOffset) return;
|
||||
this.settleAt(detent);
|
||||
}
|
||||
|
||||
handleDetentArgChange() {
|
||||
const requested = this.named.detent ?? null;
|
||||
if (requested === this.previousDetentArg) return;
|
||||
this.previousDetentArg = requested;
|
||||
if (requested === null) return;
|
||||
if (this.dragging || this.pending) return;
|
||||
|
||||
const detent = findDetent(this.layout, requested);
|
||||
if (detent) this.settleAt(detent);
|
||||
}
|
||||
|
||||
// --- positioning ----------------------------------------------------------
|
||||
|
||||
applyOffset(offset) {
|
||||
const clamped = clamp(offset, 0, this.sheetHeight);
|
||||
this.currentOffset = clamped;
|
||||
this.element.style.setProperty('--sheet-offset', `${clamped}px`);
|
||||
this.queueInset(this.sheetHeight - clamped);
|
||||
}
|
||||
|
||||
settleAt(detent) {
|
||||
this.element.classList.remove('sheet-dragging');
|
||||
this.currentOffset = detent.offset;
|
||||
this.element.style.setProperty('--sheet-offset', `${detent.offset}px`);
|
||||
this.queueInset(this.sheetHeight - detent.offset);
|
||||
this.mapUi.setSheetDetent(detent.name);
|
||||
this.queueSnap();
|
||||
}
|
||||
|
||||
enterAt(name) {
|
||||
const detent =
|
||||
findDetent(this.layout, name) ?? this.layout[this.layout.length - 1];
|
||||
|
||||
// Start fully hidden, then slide to the detent on the next frame so the
|
||||
// transform transition animates the entrance.
|
||||
this.currentOffset = this.sheetHeight;
|
||||
this.element.style.setProperty('--sheet-offset', `${this.sheetHeight}px`);
|
||||
this.queueInset(0);
|
||||
|
||||
if (this.reducedMotionQuery.matches) {
|
||||
this.settleAt(detent);
|
||||
return;
|
||||
}
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
if (!this.active || this.destroyed) return;
|
||||
this.settleAt(detent);
|
||||
});
|
||||
}
|
||||
|
||||
// --- handle ---------------------------------------------------------------
|
||||
|
||||
injectHandle() {
|
||||
if (this.handle) return;
|
||||
const handle = document.createElement('div');
|
||||
handle.className = 'sheet-handle';
|
||||
handle.setAttribute('aria-hidden', 'true');
|
||||
handle.setAttribute('data-test-sheet-handle', '');
|
||||
this.element.prepend(handle);
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
removeHandle() {
|
||||
if (this.handle?.parentNode) {
|
||||
this.handle.parentNode.removeChild(this.handle);
|
||||
}
|
||||
this.handle = null;
|
||||
}
|
||||
|
||||
// --- gestures -------------------------------------------------------------
|
||||
|
||||
onPointerDown = (event) => {
|
||||
if (this.destroyed || this.pointerId !== null) return;
|
||||
if (event.pointerType === 'mouse' && event.button !== 0) return;
|
||||
// Only the primary pointer may start a drag, so a second finger landing on
|
||||
// the header during a map pinch does not hijack the gesture.
|
||||
if (!event.isPrimary) return;
|
||||
|
||||
const target = event.target;
|
||||
if (!(target instanceof Element)) return;
|
||||
if (!target.closest(HANDLE_SELECTOR)) return;
|
||||
if (target.closest(INTERACTIVE_SELECTOR)) return;
|
||||
|
||||
this.pointerId = event.pointerId;
|
||||
this.pending = true;
|
||||
this.dragging = false;
|
||||
this.startY = event.clientY;
|
||||
this.startOffset = this.currentOffset;
|
||||
this.samples = [{ y: event.clientY, t: performance.now() }];
|
||||
|
||||
window.addEventListener('pointermove', this.onPointerMove, {
|
||||
passive: false,
|
||||
});
|
||||
window.addEventListener('pointerup', this.onPointerUp);
|
||||
window.addEventListener('pointercancel', this.onPointerCancel);
|
||||
};
|
||||
|
||||
onPointerMove = (event) => {
|
||||
if (event.pointerId !== this.pointerId) return;
|
||||
|
||||
if (this.pending) {
|
||||
const thresholdDy = event.clientY - this.startY;
|
||||
if (Math.abs(thresholdDy) < DRAG_THRESHOLD_PX) return;
|
||||
this.pending = false;
|
||||
this.dragging = true;
|
||||
this.element.classList.add('sheet-dragging');
|
||||
|
||||
// Rebase on the live visual position. While a snap or the entrance
|
||||
// transition is still interpolating, currentOffset already points at the
|
||||
// target, so using it here would make the sheet jump. Reading the actual
|
||||
// transform keeps the gesture continuous.
|
||||
this.startY = event.clientY;
|
||||
this.startOffset = this.readLiveOffset();
|
||||
this.samples = [{ y: event.clientY, t: performance.now() }];
|
||||
}
|
||||
|
||||
if (event.cancelable) event.preventDefault();
|
||||
this.recordSample(event.clientY);
|
||||
this.applyOffset(this.startOffset + (event.clientY - this.startY));
|
||||
};
|
||||
|
||||
onPointerUp = (event) => {
|
||||
if (event.pointerId !== this.pointerId) return;
|
||||
|
||||
// A press that never crossed the threshold is a tap: leave it to the
|
||||
// browser so header buttons keep working.
|
||||
if (this.pending) {
|
||||
this.endGesture();
|
||||
return;
|
||||
}
|
||||
|
||||
const velocity = this.computeVelocity();
|
||||
const target = resolveSnap({
|
||||
offset: this.currentOffset,
|
||||
velocity,
|
||||
layout: this.layout,
|
||||
sheetHeight: this.sheetHeight,
|
||||
});
|
||||
|
||||
this.endGesture();
|
||||
|
||||
if (target === 'dismissed') {
|
||||
this.dismiss();
|
||||
} else {
|
||||
const detent = findDetent(this.layout, target);
|
||||
if (detent) this.settleAt(detent);
|
||||
}
|
||||
};
|
||||
|
||||
onPointerCancel = (event) => {
|
||||
if (event.pointerId !== this.pointerId) return;
|
||||
const wasDragging = this.dragging;
|
||||
this.endGesture();
|
||||
if (!wasDragging) return;
|
||||
|
||||
const detent =
|
||||
this.layout[nearestDetentIndex(this.layout, this.currentOffset)];
|
||||
if (detent) this.settleAt(detent);
|
||||
};
|
||||
|
||||
dismiss() {
|
||||
this.element.classList.remove('sheet-dragging');
|
||||
this.currentOffset = this.sheetHeight;
|
||||
this.element.style.setProperty('--sheet-offset', `${this.sheetHeight}px`);
|
||||
this.queueInset(0);
|
||||
// A drag-dismiss ends the session: the next sheet should use its own
|
||||
// default detent, not this sheet's remembered position.
|
||||
this.mapUi.setSheetDetent(null);
|
||||
|
||||
const onDismiss = this.named.onDismiss;
|
||||
const delay = this.reducedMotionQuery.matches ? 0 : TRANSITION_MS;
|
||||
this.dismissTimer = setTimeout(() => {
|
||||
this.dismissTimer = null;
|
||||
if (typeof onDismiss === 'function') onDismiss();
|
||||
}, delay);
|
||||
}
|
||||
|
||||
endGesture() {
|
||||
window.removeEventListener('pointermove', this.onPointerMove);
|
||||
window.removeEventListener('pointerup', this.onPointerUp);
|
||||
window.removeEventListener('pointercancel', this.onPointerCancel);
|
||||
this.pointerId = null;
|
||||
this.pending = false;
|
||||
this.dragging = false;
|
||||
}
|
||||
|
||||
cancelDrag() {
|
||||
if (this.dismissTimer) {
|
||||
clearTimeout(this.dismissTimer);
|
||||
this.dismissTimer = null;
|
||||
}
|
||||
this.endGesture();
|
||||
this.element?.classList.remove('sheet-dragging');
|
||||
}
|
||||
|
||||
// The sheet's current visual offset in px, read from the rendered transform.
|
||||
// Differs from currentOffset while a CSS transition is still interpolating.
|
||||
readLiveOffset() {
|
||||
const transform = getComputedStyle(this.element).transform;
|
||||
if (!transform || transform === 'none') {
|
||||
return this.currentOffset;
|
||||
}
|
||||
try {
|
||||
const matrix = new DOMMatrixReadOnly(transform);
|
||||
return clamp(matrix.m42, 0, this.sheetHeight);
|
||||
} catch {
|
||||
return this.currentOffset;
|
||||
}
|
||||
}
|
||||
|
||||
recordSample(y) {
|
||||
const now = performance.now();
|
||||
this.samples.push({ y, t: now });
|
||||
while (
|
||||
this.samples.length > 2 &&
|
||||
now - this.samples[0].t > VELOCITY_WINDOW_MS * 1.5
|
||||
) {
|
||||
this.samples.shift();
|
||||
}
|
||||
}
|
||||
|
||||
computeVelocity() {
|
||||
const now = performance.now();
|
||||
const recent = this.samples.filter(
|
||||
(sample) => now - sample.t <= VELOCITY_WINDOW_MS
|
||||
);
|
||||
if (recent.length < 2) return 0;
|
||||
|
||||
const first = recent[0];
|
||||
const last = recent[recent.length - 1];
|
||||
const dt = last.t - first.t;
|
||||
if (dt <= 0) return 0;
|
||||
|
||||
// Positive velocity means the pointer moved downward.
|
||||
return (last.y - first.y) / dt;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,21 @@ export default class MapUiService extends Service {
|
||||
@tracked loadingState = null;
|
||||
@tracked isSidebarVisible = false;
|
||||
@tracked isSidebarOpening = false;
|
||||
// Mobile bottom-sheet state. `sheetInsetPx` is how many pixels the sheet
|
||||
// currently covers from the bottom of the viewport, `sheetSnapSeq` bumps
|
||||
// whenever a drag settles so the map can react once (not per frame), and
|
||||
// `sheetDetent` is the active detent name. `sheetDetent` is intentionally
|
||||
// NOT tracked: the modifier reads it while setting up to restore the
|
||||
// previous position, and reading a tracked value there would re-run setup on
|
||||
// every drag frame.
|
||||
@tracked sheetInsetPx = 0;
|
||||
@tracked sheetSnapSeq = 0;
|
||||
sheetDetent = null;
|
||||
// The modifier that currently owns the covered-height signal. A plain field
|
||||
// so it is safe to write during render. Writes to sheetInsetPx/sheetSnapSeq
|
||||
// are gated on this owner so a stale modifier can never corrupt the value
|
||||
// (see the ownership-token note on the setters below).
|
||||
sheetInsetOwner = null;
|
||||
|
||||
scrollPositions = {};
|
||||
|
||||
@@ -48,6 +63,52 @@ export default class MapUiService extends Service {
|
||||
hideSidebar() {
|
||||
this.isSidebarVisible = false;
|
||||
this.isSidebarOpening = false;
|
||||
this.resetSheet();
|
||||
}
|
||||
|
||||
// --- Bottom-sheet covered-height signal ----------------------------------
|
||||
//
|
||||
// The Map reads sheetInsetPx during render, so the modifier must never write
|
||||
// it (or sheetSnapSeq) while a render is in progress. Instead the modifier
|
||||
// owns the slot and writes through an afterRender flush. Ownership makes the
|
||||
// result independent of the order in which a stale modifier's release and a
|
||||
// live modifier's flush land in the afterRender queue.
|
||||
|
||||
claimSheetInset(owner) {
|
||||
this.sheetInsetOwner = owner;
|
||||
}
|
||||
|
||||
setSheetInsetFor(owner, px) {
|
||||
if (owner !== this.sheetInsetOwner) return;
|
||||
const next = Math.max(0, Math.round(px));
|
||||
// Guard against no-op writes: this build's tracked setter invalidates even
|
||||
// when the value is unchanged, and the drag calls this per frame.
|
||||
if (next !== this.sheetInsetPx) {
|
||||
this.sheetInsetPx = next;
|
||||
}
|
||||
}
|
||||
|
||||
snapSheetFor(owner) {
|
||||
if (owner !== this.sheetInsetOwner) return;
|
||||
this.sheetSnapSeq++;
|
||||
}
|
||||
|
||||
releaseSheetInset(owner) {
|
||||
if (owner !== this.sheetInsetOwner) return;
|
||||
this.sheetInsetOwner = null;
|
||||
if (this.sheetInsetPx !== 0) {
|
||||
this.sheetInsetPx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
setSheetDetent(name) {
|
||||
this.sheetDetent = name;
|
||||
}
|
||||
|
||||
resetSheet() {
|
||||
this.sheetInsetOwner = null;
|
||||
this.sheetInsetPx = 0;
|
||||
this.sheetDetent = null;
|
||||
}
|
||||
|
||||
selectPlace(place, options = {}) {
|
||||
|
||||
+52
-19
@@ -23,6 +23,12 @@ html,
|
||||
body {
|
||||
height: 100%;
|
||||
overscroll-behavior: none; /* Prevent pull-to-refresh on mobile */
|
||||
|
||||
/* The mobile bottom sheet is translated below the viewport edge while
|
||||
collapsed/dragging; without this the page becomes scrollable. `hidden`
|
||||
(not `clip`) keeps the viewport a scroll container so focused inputs can
|
||||
still be scrolled into view when the mobile keyboard opens. */
|
||||
overflow: hidden;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
@@ -471,7 +477,6 @@ body {
|
||||
width: 100%;
|
||||
right: 0;
|
||||
border-radius: 16px 16px 0 0;
|
||||
height: 66vh;
|
||||
top: auto;
|
||||
bottom: 0;
|
||||
}
|
||||
@@ -1555,12 +1560,12 @@ span.icon {
|
||||
}
|
||||
|
||||
@media (width <= 768px) {
|
||||
/* Sidebar/Bottom Sheet is open (Mobile: Bottom 50%) */
|
||||
|
||||
/* Center Y = (height/2) / 2 = height/4 = 25% + half header height */
|
||||
/* Sidebar/Bottom Sheet is open (Mobile). Center the crosshair in the area
|
||||
above the sheet, whose covered height is exposed as --sheet-inset by the
|
||||
map container's syncSheetInset modifier. */
|
||||
.map-container.sidebar-open .map-crosshair {
|
||||
left: 50%; /* Reset desktop shift */
|
||||
top: calc(25% + 30px); /* 30px approx half header height */
|
||||
top: calc((100% - var(--sheet-inset, 50vh)) / 2 + 30px);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1585,16 +1590,55 @@ button.create-place {
|
||||
@media (width <= 768px) {
|
||||
.sidebar {
|
||||
width: 100%;
|
||||
height: 50vh;
|
||||
height: var(--sheet-height, 50vh);
|
||||
box-shadow: 0 -2px 10px rgb(0 0 0 / 10%);
|
||||
border-top-left-radius: 16px;
|
||||
border-top-right-radius: 16px;
|
||||
inset: auto 0 0;
|
||||
transform: translateY(var(--sheet-offset, 0));
|
||||
transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
||||
touch-action: pan-y;
|
||||
}
|
||||
|
||||
/* The bottom-sheet modifier owns the mobile entrance, so the desktop
|
||||
slide-up keyframe must not run here. This selector matches the desktop
|
||||
rule's specificity (0,2,0) and comes later in the file, so it wins. */
|
||||
.sidebar-opening .sidebar {
|
||||
animation: sidebar-slide-up-bottom 0.18s cubic-bezier(0.16, 1, 0.3, 1)
|
||||
forwards;
|
||||
animation: none;
|
||||
}
|
||||
|
||||
/* While a drag is in progress the sheet must track the pointer exactly. */
|
||||
.sidebar.sheet-dragging {
|
||||
transition: none;
|
||||
}
|
||||
|
||||
/* Injected by app/modifiers/bottom-sheet.js as the first child of the sheet.
|
||||
Keep the gap above the bar, but tighten the gap toward the content. */
|
||||
.sheet-handle {
|
||||
display: flex;
|
||||
flex: 0 0 auto;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
height: 16px;
|
||||
padding-top: 9px;
|
||||
background: transparent;
|
||||
cursor: grab;
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
.sheet-handle::before {
|
||||
content: '';
|
||||
width: 40px;
|
||||
height: 4px;
|
||||
border-radius: 999px;
|
||||
background: #ccc;
|
||||
}
|
||||
|
||||
/* The header doubles as a drag surface; disable native scrolling there so
|
||||
the gesture is not cancelled, while .sidebar-content keeps pan-y. */
|
||||
.sidebar-header {
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
.sidebar-content {
|
||||
@@ -2451,17 +2495,6 @@ button.create-place {
|
||||
}
|
||||
}
|
||||
|
||||
/* Snappy slide-up from bottom (Mobile) */
|
||||
@keyframes sidebar-slide-up-bottom {
|
||||
from {
|
||||
transform: translateY(100%);
|
||||
}
|
||||
|
||||
to {
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Lists Index Sidebar Menu */
|
||||
.lists-index-item {
|
||||
width: 100%;
|
||||
|
||||
@@ -6,6 +6,7 @@ import { on } from '@ember/modifier';
|
||||
import Icon from '#components/icon';
|
||||
import { htmlSafe } from '@ember/template';
|
||||
import restoreScroll from '../../modifiers/restore-scroll';
|
||||
import bottomSheet from '../../modifiers/bottom-sheet';
|
||||
|
||||
export default class ListsIndexTemplate extends Component {
|
||||
@service storage;
|
||||
@@ -52,7 +53,7 @@ export default class ListsIndexTemplate extends Component {
|
||||
|
||||
<template>
|
||||
{{#if this.mapUi.isSidebarVisible}}
|
||||
<div class="sidebar">
|
||||
<div class="sidebar" {{bottomSheet detent="medium" onDismiss=this.close}}>
|
||||
<div class="sidebar-header has-back-btn">
|
||||
<button type="button" class="back-btn" {{on "click" this.backToMenu}}>
|
||||
<Icon @name="arrow-left" @size={{20}} @color="#333" />
|
||||
|
||||
@@ -4,6 +4,7 @@ import { action } from '@ember/object';
|
||||
import { on } from '@ember/modifier';
|
||||
import PlaceEditForm from '#components/place-edit-form';
|
||||
import Icon from '#components/icon';
|
||||
import bottomSheet from '../../modifiers/bottom-sheet';
|
||||
|
||||
export default class PlaceNewTemplate extends Component {
|
||||
@service router;
|
||||
@@ -57,7 +58,7 @@ export default class PlaceNewTemplate extends Component {
|
||||
|
||||
<template>
|
||||
{{#if this.mapUi.isSidebarVisible}}
|
||||
<div class="sidebar">
|
||||
<div class="sidebar" {{bottomSheet detent="medium" onDismiss=this.close}}>
|
||||
<div class="sidebar-header">
|
||||
<h2><Icon @name="plus-circle" @size={{20}} @color="#ea4335" />
|
||||
New Place</h2>
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
// Pure geometry and snap math for the mobile bottom sheet.
|
||||
//
|
||||
// Kept free of DOM and service dependencies so the drag/snap behaviour can be
|
||||
// unit tested directly. The modifier in app/modifiers/bottom-sheet.js owns the
|
||||
// DOM and gesture wiring and delegates every decision here.
|
||||
//
|
||||
// The sheet is positioned with `transform: translateY(--sheet-offset)`, where
|
||||
// the offset is the number of pixels pushed below the bottom of the viewport.
|
||||
// An offset of 0 is fully expanded; an offset equal to the sheet height is
|
||||
// fully hidden.
|
||||
|
||||
// Visible height of the sheet at each detent, as a fraction of the viewport.
|
||||
export const DETENT_FRACTIONS = {
|
||||
expanded: 0.92,
|
||||
medium: 0.55,
|
||||
collapsed: 0.3,
|
||||
};
|
||||
|
||||
// The sheet never grows beyond this fraction of the viewport.
|
||||
export const MAX_SHEET_FRACTION = 0.92;
|
||||
|
||||
// Fling speed (px/ms) at or above which a release moves one detent in the
|
||||
// direction of the gesture instead of snapping to the nearest detent.
|
||||
export const FLING_THRESHOLD = 0.5;
|
||||
|
||||
// How far (as a fraction of the sheet height) the user must drag past the
|
||||
// collapsed detent to dismiss the sheet on release.
|
||||
export const DISMISS_EXTRA_FRACTION = 0.15;
|
||||
|
||||
export function clamp(value, min, max) {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
export function computeSheetHeight(
|
||||
viewportHeight,
|
||||
maxFraction = MAX_SHEET_FRACTION
|
||||
) {
|
||||
return Math.round(viewportHeight * maxFraction);
|
||||
}
|
||||
|
||||
// Build the list of reachable detents for a given viewport and sheet height.
|
||||
// Returns entries sorted by ascending offset (expanded first) with any
|
||||
// duplicate offsets removed, so a very short sheet cannot produce two identical
|
||||
// stops.
|
||||
export function computeDetentLayout({
|
||||
viewportHeight,
|
||||
sheetHeight,
|
||||
detents = DETENT_FRACTIONS,
|
||||
}) {
|
||||
const entries = Object.entries(detents).map(([name, fraction]) => {
|
||||
const visibleHeight = Math.min(
|
||||
Math.round(fraction * viewportHeight),
|
||||
sheetHeight
|
||||
);
|
||||
const offset = clamp(sheetHeight - visibleHeight, 0, sheetHeight);
|
||||
return { name, fraction, visibleHeight, offset };
|
||||
});
|
||||
|
||||
entries.sort((a, b) => a.offset - b.offset);
|
||||
|
||||
const seen = new Set();
|
||||
const layout = entries.filter((entry) => {
|
||||
if (seen.has(entry.offset)) return false;
|
||||
seen.add(entry.offset);
|
||||
return true;
|
||||
});
|
||||
|
||||
// Floor rule: a sheet always has at least one reachable open position.
|
||||
if (layout.length === 0) {
|
||||
layout.push({
|
||||
name: 'collapsed',
|
||||
fraction: detents.collapsed,
|
||||
visibleHeight: sheetHeight,
|
||||
offset: 0,
|
||||
});
|
||||
}
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
||||
export function findDetent(layout, name) {
|
||||
return layout.find((detent) => detent.name === name) ?? null;
|
||||
}
|
||||
|
||||
export function nearestDetentIndex(layout, offset) {
|
||||
let nearest = 0;
|
||||
let best = Infinity;
|
||||
layout.forEach((detent, index) => {
|
||||
const distance = Math.abs(detent.offset - offset);
|
||||
if (distance < best) {
|
||||
best = distance;
|
||||
nearest = index;
|
||||
}
|
||||
});
|
||||
return nearest;
|
||||
}
|
||||
|
||||
// Resolve where a released drag should settle.
|
||||
//
|
||||
// `velocity` is in px/ms and positive means downward. Returns a detent name, or
|
||||
// the string 'dismissed' when the sheet should close instead of snapping.
|
||||
export function resolveSnap({
|
||||
offset,
|
||||
velocity,
|
||||
layout,
|
||||
sheetHeight,
|
||||
flingThreshold = FLING_THRESHOLD,
|
||||
dismissExtraFraction = DISMISS_EXTRA_FRACTION,
|
||||
}) {
|
||||
const collapsed = layout[layout.length - 1];
|
||||
const dismissOffset = collapsed.offset + dismissExtraFraction * sheetHeight;
|
||||
|
||||
if (offset >= dismissOffset) return 'dismissed';
|
||||
if (velocity >= flingThreshold && offset > collapsed.offset) {
|
||||
return 'dismissed';
|
||||
}
|
||||
|
||||
const nearest = nearestDetentIndex(layout, offset);
|
||||
if (velocity <= -flingThreshold) {
|
||||
return layout[Math.max(0, nearest - 1)].name;
|
||||
}
|
||||
if (velocity >= flingThreshold) {
|
||||
return layout[Math.min(layout.length - 1, nearest + 1)].name;
|
||||
}
|
||||
return layout[nearest].name;
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
import { module, test } from 'qunit';
|
||||
import {
|
||||
visit,
|
||||
click,
|
||||
currentURL,
|
||||
triggerEvent,
|
||||
waitUntil,
|
||||
} from '@ember/test-helpers';
|
||||
import { setupApplicationTest } from 'marco/tests/helpers';
|
||||
import Service from '@ember/service';
|
||||
import sinon from 'sinon';
|
||||
|
||||
class MockOsmService extends Service {
|
||||
async getNearbyPois() {
|
||||
return [
|
||||
{
|
||||
osmId: '123',
|
||||
lat: 1,
|
||||
lon: 1,
|
||||
osmTags: { name: 'Test Place', amenity: 'cafe' },
|
||||
osmType: 'node',
|
||||
},
|
||||
];
|
||||
}
|
||||
async getPoiById() {
|
||||
return null;
|
||||
}
|
||||
async fetchOsmObject(id, type) {
|
||||
return {
|
||||
osmId: id,
|
||||
osmType: type,
|
||||
lat: 1,
|
||||
lon: 1,
|
||||
osmTags: { name: 'Test Place' },
|
||||
title: 'Test Place',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class MockStorageService extends Service {
|
||||
initialSyncDone = true;
|
||||
savedPlaces = [];
|
||||
findPlaceById() {
|
||||
return null;
|
||||
}
|
||||
isPlaceSaved() {
|
||||
return false;
|
||||
}
|
||||
loadPlacesInBounds() {
|
||||
return [];
|
||||
}
|
||||
get placesInView() {
|
||||
return [];
|
||||
}
|
||||
rs = {
|
||||
on: () => {},
|
||||
};
|
||||
}
|
||||
|
||||
const MOBILE_QUERY = '(max-width: 768px)';
|
||||
|
||||
// @ember/test-helpers 5.4 has no setViewport helper, so simulate the mobile
|
||||
// media query directly. The bottom-sheet modifier reads window.matchMedia at
|
||||
// setup, so this must be installed before the route is visited.
|
||||
function installMobileMatchMedia() {
|
||||
return sinon.stub(window, 'matchMedia').callsFake((query) => ({
|
||||
matches: typeof query === 'string' && query.includes(MOBILE_QUERY),
|
||||
media: query,
|
||||
onchange: null,
|
||||
addEventListener() {},
|
||||
removeEventListener() {},
|
||||
addListener() {},
|
||||
removeListener() {},
|
||||
dispatchEvent() {
|
||||
return false;
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
module('Acceptance | bottom sheet', function (hooks) {
|
||||
setupApplicationTest(hooks);
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
this.owner.register('service:osm', MockOsmService);
|
||||
this.owner.register('service:storage', MockStorageService);
|
||||
});
|
||||
|
||||
test('does not inject the drag handle at desktop width', async function (assert) {
|
||||
await visit('/menu');
|
||||
|
||||
assert.dom('.sidebar').exists('sheet is rendered');
|
||||
assert
|
||||
.dom('[data-test-sheet-handle]')
|
||||
.doesNotExist('no drag handle on desktop');
|
||||
});
|
||||
|
||||
test('injects a handle and a pixel offset on mobile', async function (assert) {
|
||||
const matchMediaStub = installMobileMatchMedia();
|
||||
try {
|
||||
const mapUi = this.owner.lookup('service:map-ui');
|
||||
await visit('/menu');
|
||||
await waitUntil(() => mapUi.sheetDetent !== null);
|
||||
|
||||
assert.dom('[data-test-sheet-handle]').exists('drag handle is injected');
|
||||
|
||||
const sheet = document.querySelector('.sidebar');
|
||||
const offset = sheet.style.getPropertyValue('--sheet-offset');
|
||||
assert.true(
|
||||
/px$/.test(offset),
|
||||
`--sheet-offset is a pixel value (${offset})`
|
||||
);
|
||||
assert.strictEqual(
|
||||
mapUi.sheetDetent,
|
||||
'expanded',
|
||||
'the app menu opens at the expanded detent'
|
||||
);
|
||||
} finally {
|
||||
matchMediaStub.restore();
|
||||
}
|
||||
});
|
||||
|
||||
test('dragging the handle moves the sheet to another detent', async function (assert) {
|
||||
const matchMediaStub = installMobileMatchMedia();
|
||||
try {
|
||||
const mapUi = this.owner.lookup('service:map-ui');
|
||||
await visit('/menu');
|
||||
await waitUntil(() => mapUi.sheetDetent !== null);
|
||||
|
||||
const sheet = document.querySelector('.sidebar');
|
||||
const handle = sheet.querySelector('[data-test-sheet-handle]');
|
||||
const before = parseFloat(sheet.style.getPropertyValue('--sheet-offset'));
|
||||
|
||||
// Drag downward well past the threshold. Pointer events bubble to the
|
||||
// window-level listeners the modifier installs on pointerdown. The first
|
||||
// small move crosses the drag threshold (which rebases the gesture on the
|
||||
// live offset); the second move performs the actual drag.
|
||||
await triggerEvent(handle, 'pointerdown', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 100,
|
||||
button: 0,
|
||||
});
|
||||
await triggerEvent(handle, 'pointermove', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 110,
|
||||
});
|
||||
await triggerEvent(handle, 'pointermove', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 450,
|
||||
});
|
||||
await triggerEvent(handle, 'pointerup', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 450,
|
||||
});
|
||||
|
||||
const after = parseFloat(sheet.style.getPropertyValue('--sheet-offset'));
|
||||
assert.true(after > before, `sheet moved down (${before} -> ${after})`);
|
||||
assert.notStrictEqual(
|
||||
mapUi.sheetDetent,
|
||||
'expanded',
|
||||
'sheet left the expanded detent'
|
||||
);
|
||||
// The inset is published on a deferred animation-frame flush.
|
||||
await waitUntil(() => mapUi.sheetInsetPx > 0);
|
||||
assert.true(mapUi.sheetInsetPx > 0, 'map inset is reported');
|
||||
} finally {
|
||||
matchMediaStub.restore();
|
||||
}
|
||||
});
|
||||
|
||||
test('opening a place from search results does not crash (mobile)', async function (assert) {
|
||||
const matchMediaStub = installMobileMatchMedia();
|
||||
try {
|
||||
const mapUi = this.owner.lookup('service:map-ui');
|
||||
|
||||
await visit('/search?lat=1&lon=1');
|
||||
assert.dom('.place-item').exists('search results are rendered');
|
||||
|
||||
// This is the search→place transition that used to throw the
|
||||
// backtracking assertion (Map re-renders in the same pass that installs
|
||||
// the new sheet's modifier). click() already settles.
|
||||
await click('.place-item');
|
||||
|
||||
assert.dom('.sidebar').exists('the place sheet is rendered');
|
||||
assert.ok(currentURL().includes('/place/'), 'navigated to the place');
|
||||
|
||||
await waitUntil(() => mapUi.sheetInsetPx > 0);
|
||||
assert.true(
|
||||
mapUi.sheetInsetPx > 0,
|
||||
'the map inset reflects the open sheet'
|
||||
);
|
||||
} finally {
|
||||
matchMediaStub.restore();
|
||||
}
|
||||
});
|
||||
|
||||
test('dragging past the collapsed detent dismisses the sheet', async function (assert) {
|
||||
const matchMediaStub = installMobileMatchMedia();
|
||||
try {
|
||||
const mapUi = this.owner.lookup('service:map-ui');
|
||||
await visit('/menu');
|
||||
await waitUntil(() => mapUi.sheetDetent !== null);
|
||||
|
||||
const sheet = document.querySelector('.sidebar');
|
||||
const handle = sheet.querySelector('[data-test-sheet-handle]');
|
||||
|
||||
await triggerEvent(handle, 'pointerdown', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 100,
|
||||
button: 0,
|
||||
});
|
||||
await triggerEvent(handle, 'pointermove', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 110,
|
||||
});
|
||||
await triggerEvent(handle, 'pointermove', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 1400,
|
||||
});
|
||||
await triggerEvent(handle, 'pointerup', {
|
||||
pointerId: 1,
|
||||
pointerType: 'touch',
|
||||
isPrimary: true,
|
||||
clientY: 1400,
|
||||
});
|
||||
|
||||
assert.strictEqual(
|
||||
mapUi.sheetDetent,
|
||||
null,
|
||||
'the remembered detent is cleared on dismiss'
|
||||
);
|
||||
|
||||
// dismiss() waits for the slide-out transition before calling onDismiss,
|
||||
// so settled() is not enough.
|
||||
await waitUntil(() => !document.querySelector('.sidebar'), {
|
||||
timeout: 3000,
|
||||
});
|
||||
assert.dom('.sidebar').doesNotExist('the sheet closed after dismissing');
|
||||
} finally {
|
||||
matchMediaStub.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
import { module, test } from 'qunit';
|
||||
import {
|
||||
computeDetentLayout,
|
||||
computeSheetHeight,
|
||||
findDetent,
|
||||
nearestDetentIndex,
|
||||
resolveSnap,
|
||||
} from 'marco/utils/sheet-detents';
|
||||
|
||||
const VIEWPORT = 800;
|
||||
// 0.92 * 800
|
||||
const SHEET = computeSheetHeight(VIEWPORT); // 736
|
||||
// expanded: 736 - 736 = 0, medium: 736 - 440 = 296, collapsed: 736 - 240 = 496
|
||||
const LAYOUT = computeDetentLayout({
|
||||
viewportHeight: VIEWPORT,
|
||||
sheetHeight: SHEET,
|
||||
});
|
||||
|
||||
module('Unit | Utility | sheet-detents', function () {
|
||||
test('computeSheetHeight caps the sheet to a fraction of the viewport', function (assert) {
|
||||
assert.strictEqual(computeSheetHeight(800), 736);
|
||||
assert.strictEqual(computeSheetHeight(1000, 0.5), 500);
|
||||
});
|
||||
|
||||
test('computeDetentLayout produces ascending offsets with expanded first', function (assert) {
|
||||
assert.deepEqual(
|
||||
LAYOUT.map((detent) => detent.name),
|
||||
['expanded', 'medium', 'collapsed'],
|
||||
'detents are ordered by ascending offset'
|
||||
);
|
||||
assert.strictEqual(findDetent(LAYOUT, 'expanded').offset, 0);
|
||||
assert.strictEqual(findDetent(LAYOUT, 'medium').offset, 296);
|
||||
assert.strictEqual(findDetent(LAYOUT, 'collapsed').offset, 496);
|
||||
});
|
||||
|
||||
test('computeDetentLayout keeps a single clamped detent for short sheets', function (assert) {
|
||||
const layout = computeDetentLayout({
|
||||
viewportHeight: VIEWPORT,
|
||||
sheetHeight: 100,
|
||||
});
|
||||
|
||||
assert.strictEqual(layout.length, 1, 'only one reachable position remains');
|
||||
assert.strictEqual(
|
||||
layout[0].offset,
|
||||
0,
|
||||
'the remaining detent is fully open'
|
||||
);
|
||||
assert.strictEqual(layout[0].visibleHeight, 100);
|
||||
});
|
||||
|
||||
test('nearestDetentIndex picks the closest offset', function (assert) {
|
||||
assert.strictEqual(nearestDetentIndex(LAYOUT, 10), 0, 'near expanded');
|
||||
assert.strictEqual(nearestDetentIndex(LAYOUT, 300), 1, 'near medium');
|
||||
assert.strictEqual(nearestDetentIndex(LAYOUT, 480), 2, 'near collapsed');
|
||||
});
|
||||
|
||||
test('resolveSnap snaps to the nearest detent without velocity', function (assert) {
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 100,
|
||||
velocity: 0,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'expanded'
|
||||
);
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 290,
|
||||
velocity: 0,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'medium'
|
||||
);
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 490,
|
||||
velocity: 0,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'collapsed'
|
||||
);
|
||||
});
|
||||
|
||||
test('resolveSnap flings one detent in the gesture direction', function (assert) {
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 300,
|
||||
velocity: -0.8,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'expanded',
|
||||
'a fast upward flick moves to the next detent up'
|
||||
);
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 290,
|
||||
velocity: 0.8,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'collapsed',
|
||||
'a fast downward flick moves to the next detent down'
|
||||
);
|
||||
});
|
||||
|
||||
test('resolveSnap dismisses past the collapsed threshold', function (assert) {
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 700,
|
||||
velocity: 0,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'dismissed',
|
||||
'dragging well past collapsed dismisses'
|
||||
);
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 500,
|
||||
velocity: 0.6,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'dismissed',
|
||||
'a fast downward flick past collapsed dismisses'
|
||||
);
|
||||
assert.strictEqual(
|
||||
resolveSnap({
|
||||
offset: 500,
|
||||
velocity: 0,
|
||||
layout: LAYOUT,
|
||||
sheetHeight: SHEET,
|
||||
}),
|
||||
'collapsed',
|
||||
'the same offset without velocity just collapses'
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user