The tallest detent now fills the viewport down from the top of the search field, so a fully expanded sheet covers the search field and avatar while leaving that little strip visible above. - DETENT_FRACTIONS.expanded is now the whole sheet; the sheet height is the viewport minus a top gap (replacing the 0.92 fraction cap). - The modifier measures that gap from the search field, relative to the sheet's container (the viewport top is not the app's top), falling back to 8px. - Update the sheet-detents unit test and read the real --sheet-height in the stacked-sheets acceptance test.
126 lines
4.0 KiB
JavaScript
126 lines
4.0 KiB
JavaScript
// 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.
|
|
// The expanded stop is the whole sheet, whose height is the viewport minus the
|
|
// small gap kept above it (see computeSheetHeight).
|
|
export const DETENT_FRACTIONS = {
|
|
expanded: 1,
|
|
medium: 0.55,
|
|
collapsed: 0.3,
|
|
};
|
|
|
|
// 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);
|
|
}
|
|
|
|
// The sheet fills the viewport down from `topGap` pixels, so a fully expanded
|
|
// sheet reaches up to the top of the search field while leaving that strip
|
|
// visible above it.
|
|
export function computeSheetHeight(viewportHeight, topGap = 0) {
|
|
return Math.max(0, Math.round(viewportHeight - topGap));
|
|
}
|
|
|
|
// 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;
|
|
}
|