Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

improvement(react): steadier long-press menus, smoother draggable sheets, tab press feedback, lighter viewport context and desktop buttons

aashu6fc6841155a4
diff --git a/packages/react/src/BottomSheet.tsx b/packages/react/src/BottomSheet.tsx
index 3748f82cdd..1210058950 100644
--- a/packages/react/src/BottomSheet.tsx
+++ b/packages/react/src/BottomSheet.tsx
@@ -9,15 +9,17 @@ import * as DialogPrimitive from "@radix-ui/react-dialog";
import type {
ComponentPropsWithoutRef,
ElementRef,
- HTMLAttributes
+ HTMLAttributes,
+ PointerEvent
} from "react";
-import { forwardRef } from "react";
+import { forwardRef, useRef } from "react";
import { LuChevronLeft, LuX } from "react-icons/lu";
import { ActionPresentationBoundary } from "./ActionPresentation";
import { ClientOnly } from "./ClientOnly";
import { DialogRoot, useDialogDismissable } from "./Modal";
import { cn } from "./utils/cn";
+import { mergeRefs } from "./utils/react";
import { usePhoneOpenAutoFocus, useViewport } from "./Viewport";
/**
@@ -54,6 +56,10 @@ type BottomSheetContentProps = ComponentPropsWithoutRef<
size?: "auto" | "full";
};
+const SHEET_SPRING = "transform 200ms cubic-bezier(0.32, 0.72, 0, 1)";
+/** px per ms: a flick this fast dismisses whatever the distance. */
+const FLICK_VELOCITY = 0.5;
+
const BottomSheetContent = forwardRef<
ElementRef<typeof DialogPrimitive.Content>,
BottomSheetContentProps
@@ -62,16 +68,63 @@ const BottomSheetContent = forwardRef<
const { isPhone } = useViewport();
const onOpenAutoFocus = usePhoneOpenAutoFocus(props.onOpenAutoFocus);
const dismissable = useDialogDismissable();
+ const sheetRef = useRef<HTMLDivElement | null>(null);
+ const closeRef = useRef<HTMLButtonElement>(null);
+ const drag = useRef<{ startY: number; lastY: number; lastT: number } | null>(
+ null
+ );
+ const canDrag = isPhone && dismissable;
+
+ // Phones: drag the grabber down to dismiss, past a third of the sheet or
+ // with a flick; a shorter drag springs back.
+ const onDragStart = (event: PointerEvent<HTMLDivElement>) => {
+ if (!canDrag || !sheetRef.current) return;
+ event.currentTarget.setPointerCapture(event.pointerId);
+ drag.current = {
+ startY: event.clientY,
+ lastY: event.clientY,
+ lastT: event.timeStamp
+ };
+ sheetRef.current.style.transition = "none";
+ };
+ const onDragMove = (event: PointerEvent<HTMLDivElement>) => {
+ const state = drag.current;
+ const sheet = sheetRef.current;
+ if (!state || !sheet) return;
+ const offset = Math.max(0, event.clientY - state.startY);
+ sheet.style.transform = `translateY(${offset}px)`;
+ drag.current = { ...state, lastY: event.clientY, lastT: event.timeStamp };
+ };
+ const onDragEnd = (event: PointerEvent<HTMLDivElement>) => {
+ const state = drag.current;
+ const sheet = sheetRef.current;
+ drag.current = null;
+ if (!state || !sheet) return;
+ const offset = Math.max(0, event.clientY - state.startY);
+ const velocity =
+ (event.clientY - state.lastY) /
+ Math.max(1, event.timeStamp - state.lastT);
+ sheet.style.transition = SHEET_SPRING;
+ if (offset > sheet.offsetHeight / 3 || velocity > FLICK_VELOCITY) {
+ // The exit animation starts from where the finger left the sheet.
+ closeRef.current?.click();
+ } else {
+ sheet.style.transform = "";
+ }
+ };
+
return (
<ClientOnly fallback={null}>
{() => (
<DialogPrimitive.Portal>
<BottomSheetOverlay />
<DialogPrimitive.Content
- ref={ref}
+ ref={mergeRefs(ref, sheetRef)}
className={cn(
"fixed inset-x-0 bottom-0 z-[70] flex flex-col rounded-t-2xl bg-background shadow-lg duration-300",
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",
+ // Phones: a sheet settles in on the iOS curve and leaves faster.
+ "max-md:ease-[cubic-bezier(0.32,0.72,0,1)] max-md:data-[state=open]:[animation-duration:300ms] max-md:data-[state=closed]:[animation-duration:200ms]",
"max-md:max-h-[88dvh] max-md:rounded-t-[14px] max-md:bg-popover max-md:pb-safe",
size === "full" &&
"max-md:h-[calc(100dvh-env(safe-area-inset-top)-12px)] max-md:max-h-none",
@@ -81,14 +134,22 @@ const BottomSheetContent = forwardRef<
onOpenAutoFocus={onOpenAutoFocus}
>
<div
+ aria-hidden
className={cn(
- "mx-auto mt-3 mb-2 h-1.5 w-12 shrink-0 rounded-full bg-muted-foreground/20",
- "max-md:mt-2 max-md:mb-1 max-md:h-[5px] max-md:w-9"
+ "flex shrink-0 justify-center pt-3 pb-2 max-md:pt-2 max-md:pb-1",
+ canDrag && "touch-none cursor-grab active:cursor-grabbing"
)}
- />
+ onPointerDown={onDragStart}
+ onPointerMove={onDragMove}
+ onPointerUp={onDragEnd}
+ onPointerCancel={onDragEnd}
+ >
+ <div className="h-1.5 w-12 rounded-full bg-muted-foreground/20 max-md:h-[5px] max-md:w-9" />
+ </div>
<ActionPresentationBoundary>{children}</ActionPresentationBoundary>
{isPhone && dismissable && (
<DialogPrimitive.Close
+ ref={closeRef}
aria-label={t`Close`}
className="absolute top-3 right-1 flex size-11 items-center justify-center rounded-full text-muted-foreground outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50"
>
diff --git a/packages/react/src/Button.tsx b/packages/react/src/Button.tsx
index 2bd178b9db..a0bde8a7cc 100644
--- a/packages/react/src/Button.tsx
+++ b/packages/react/src/Button.tsx
@@ -15,7 +15,7 @@ import { Spinner } from "./Spinner";
import { cn } from "./utils/cn";
import { hasOpenDialog, isInsideTopmostDialog } from "./utils/dialog";
import { mergeRefs } from "./utils/react";
-import { HitArea } from "./Viewport";
+import { HitArea, useViewport } from "./Viewport";
export const buttonVariants = cva(
[
@@ -171,6 +171,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(
const Comp = asChild ? Slot : "button";
const innerRef = useRef<HTMLButtonElement>(null);
const presentation = useActionPresentation();
+ const { isPhone } = useViewport();
// The badge shows the first binding; the rest are silent alternatives.
const primaryShortcut = Array.isArray(shortcut) ? shortcut[0] : shortcut;
const badgeShortcut =
@@ -256,7 +257,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(
role={asChild ? undefined : "button"}
ref={mergeRefs(ref, innerRef)}
>
- <HitArea />
+ {isPhone && <HitArea />}
{isLoading && (
<Spinner className={cn("size-4 flex-shrink-0", !isIcon && "mr-2")} />
)}
diff --git a/packages/react/src/MenuSheet.tsx b/packages/react/src/MenuSheet.tsx
index a17744837e..80a7a19f5e 100644
--- a/packages/react/src/MenuSheet.tsx
+++ b/packages/react/src/MenuSheet.tsx
@@ -17,6 +17,7 @@ import {
createContext,
forwardRef,
useContext,
+ useEffect,
useId,
useRef,
useState
@@ -109,6 +110,8 @@ export function MenuSheetRoot({
}
const LONG_PRESS_MS = 500;
+/** A held finger drifts; only a move past this many pixels is a scroll. */
+const LONG_PRESS_SLOP_PX = 10;
/**
* A context menu's trigger: a right-click, or a touch held still for half a
@@ -117,33 +120,78 @@ const LONG_PRESS_MS = 500;
export const MenuSheetContextTrigger = forwardRef<
HTMLSpanElement,
ComponentPropsWithoutRef<"span"> & { asChild?: boolean; disabled?: boolean }
->(({ asChild, disabled, onContextMenu, onPointerDown, ...props }, ref) => {
- const { setOpen } = useMenuSheet();
- const timer = useRef<ReturnType<typeof setTimeout>>();
- const clear = () => clearTimeout(timer.current);
- const Comp = asChild ? Slot : "span";
- return (
- <Comp
- ref={ref}
- {...props}
- onContextMenu={(event: MouseEvent<HTMLSpanElement>) => {
- onContextMenu?.(event);
- if (disabled || event.defaultPrevented) return;
- event.preventDefault();
- setOpen(true);
- }}
- onPointerDown={(event: PointerEvent<HTMLSpanElement>) => {
- onPointerDown?.(event);
- if (disabled || event.pointerType !== "touch") return;
- clear();
- timer.current = setTimeout(() => setOpen(true), LONG_PRESS_MS);
- }}
- onPointerUp={clear}
- onPointerMove={clear}
- onPointerCancel={clear}
- />
- );
-});
+>(
+ (
+ {
+ asChild,
+ disabled,
+ className,
+ onClickCapture,
+ onContextMenu,
+ onPointerDown,
+ ...props
+ },
+ ref
+ ) => {
+ const { setOpen } = useMenuSheet();
+ const timer = useRef<ReturnType<typeof setTimeout>>();
+ const start = useRef<{ x: number; y: number } | null>(null);
+ // The click that follows a long-press release must not reach the row.
+ const fired = useRef(false);
+ const clear = () => {
+ clearTimeout(timer.current);
+ start.current = null;
+ };
+ useEffect(() => () => clearTimeout(timer.current), []);
+ const Comp = asChild ? Slot : "span";
+ return (
+ <Comp
+ ref={ref}
+ {...props}
+ className={cn("[-webkit-touch-callout:none] select-none", className)}
+ onContextMenu={(event: MouseEvent<HTMLSpanElement>) => {
+ onContextMenu?.(event);
+ if (disabled || event.defaultPrevented) return;
+ event.preventDefault();
+ setOpen(true);
+ }}
+ onPointerDown={(event: PointerEvent<HTMLSpanElement>) => {
+ onPointerDown?.(event);
+ fired.current = false;
+ if (disabled || event.pointerType !== "touch") return;
+ clear();
+ start.current = { x: event.clientX, y: event.clientY };
+ timer.current = setTimeout(() => {
+ fired.current = true;
+ start.current = null;
+ setOpen(true);
+ }, LONG_PRESS_MS);
+ }}
+ onPointerMove={(event: PointerEvent<HTMLSpanElement>) => {
+ const origin = start.current;
+ if (
+ origin &&
+ Math.hypot(event.clientX - origin.x, event.clientY - origin.y) >
+ LONG_PRESS_SLOP_PX
+ ) {
+ clear();
+ }
+ }}
+ onPointerUp={clear}
+ onPointerCancel={clear}
+ onClickCapture={(event: MouseEvent<HTMLSpanElement>) => {
+ if (fired.current) {
+ fired.current = false;
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
+ onClickCapture?.(event);
+ }}
+ />
+ );
+ }
+);
MenuSheetContextTrigger.displayName = "MenuSheetContextTrigger";
export const MenuSheetContent = forwardRef<
diff --git a/packages/react/src/TabBar.tsx b/packages/react/src/TabBar.tsx
index 0447addc7b..10ee5bbf7e 100644
--- a/packages/react/src/TabBar.tsx
+++ b/packages/react/src/TabBar.tsx
@@ -8,7 +8,7 @@ import { Link } from "react-router";
import { cn } from "./utils/cn";
const tabBarItemClassName =
- "flex min-h-11 min-w-0 flex-1 flex-col items-center justify-center gap-0.5 text-[10.5px] font-medium leading-none outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50";
+ "flex min-h-11 min-w-0 flex-1 flex-col items-center justify-center gap-0.5 text-[10.5px] font-medium leading-none outline-none transition-transform active:scale-[0.98] focus-visible:ring-[3px] focus-visible:ring-ring/50";
/** The phone bottom tab bar. Hidden at md and up. */
export function TabBar({
diff --git a/packages/react/src/Viewport.tsx b/packages/react/src/Viewport.tsx
index ec1854405e..f3c05fa01c 100644
--- a/packages/react/src/Viewport.tsx
+++ b/packages/react/src/Viewport.tsx
@@ -19,7 +19,7 @@ type ViewportProviderProps = {
children: ReactNode;
};
-const Context = createContext<{ initialViewport: Viewport } | null>(null);
+const Context = createContext<Viewport | null>(null);
const readViewport = (): Viewport => {
if (window.matchMedia(PHONE_QUERY).matches) return "phone";
@@ -52,7 +52,7 @@ export const ViewportProvider = ({
}, []);
return (
- <Context.Provider value={{ initialViewport }}>{children}</Context.Provider>
+ <Context.Provider value={initialViewport}>{children}</Context.Provider>
);
};
@@ -67,11 +67,11 @@ export function useViewport(): {
isTablet: boolean;
isDesktop: boolean;
} {
- const context = useContext(Context);
+ const initialViewport = useContext(Context);
const viewport = useSyncExternalStore(
subscribe,
readViewport,
- () => context?.initialViewport ?? "desktop"
+ () => initialViewport ?? "desktop"
);
return useMemo(
() => ({