mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
Settle the form sheets on one anatomy
Field labels no longer lose the space before '(optional)' or set the colon apart from a unit, and a question label carries no colon after its question mark. Cancel next to a primary is the outline button in every form, the illness sheet's selects span the field width, and the reminder sheet drops a description that repeated the page subtitle. On a phone a sheet now opens with focus on itself rather than its first field, so a date or name field no longer raises the keyboard over a sheet nobody has read yet.
This commit is contained in:
1 parent
0b881fe5b7
commit
03f374794e
15 files changed
+84
-31
No files matched your search
@@ -105,7 +105,7 @@ vi.mock("@/components/ui/dialog", () => ({
|
||||
),
|
||||
}));
|
||||
|
||||
import { ResponsiveSheet } from "../responsive-sheet";
|
||||
import { keepKeyboardDownOnOpen, ResponsiveSheet } from "../responsive-sheet";
|
||||
|
||||
describe("<ResponsiveSheet>", () => {
|
||||
it("renders the Dialog branch on `md+` viewports", () => {
|
||||
@@ -324,3 +324,34 @@ describe("<ResponsiveSheet>", () => {
|
||||
expect(onOpenChange).toHaveBeenCalledWith(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("keepKeyboardDownOnOpen (phone branch open focus)", () => {
|
||||
function setup(activeInside: boolean) {
|
||||
const focus = vi.fn();
|
||||
const preventDefault = vi.fn();
|
||||
const active = { id: "active" };
|
||||
const content = {
|
||||
focus,
|
||||
contains: (node: unknown) => activeInside && node === active,
|
||||
};
|
||||
vi.stubGlobal("document", { activeElement: active });
|
||||
keepKeyboardDownOnOpen({
|
||||
preventDefault,
|
||||
currentTarget: content,
|
||||
} as unknown as Event);
|
||||
vi.unstubAllGlobals();
|
||||
return { focus, preventDefault };
|
||||
}
|
||||
|
||||
it("focuses the sheet itself instead of its first field", () => {
|
||||
const { focus, preventDefault } = setup(false);
|
||||
expect(preventDefault).toHaveBeenCalledOnce();
|
||||
expect(focus).toHaveBeenCalledWith({ preventScroll: true });
|
||||
});
|
||||
|
||||
it("leaves a field the form focused on purpose alone", () => {
|
||||
const { focus, preventDefault } = setup(true);
|
||||
expect(preventDefault).toHaveBeenCalledOnce();
|
||||
expect(focus).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -131,6 +131,23 @@ const CONTENT_WIDTH_CLASS: Record<
|
||||
* the form's chrome is unchanged because both branches expose the
|
||||
* same header / body / footer slot shape.
|
||||
*/
|
||||
/**
|
||||
* Phone branch only: on open, focus the sheet itself instead of its first
|
||||
* field. Radix's default lands on the first tabbable element, and when that
|
||||
* is a text-like field (a date, a name) the phone raises its keyboard over a
|
||||
* sheet the person has not read yet, and the sheet jumps up under it. Focus
|
||||
* still moves into the sheet (the content is focusable and traps focus), a
|
||||
* field a form focuses on purpose (`autoFocus`, a mount effect) keeps it, and
|
||||
* the desktop dialog keeps the default.
|
||||
*/
|
||||
export function keepKeyboardDownOnOpen(event: Event): void {
|
||||
event.preventDefault();
|
||||
const content = event.currentTarget as HTMLElement | null;
|
||||
if (content && !content.contains(document.activeElement)) {
|
||||
content.focus({ preventScroll: true });
|
||||
}
|
||||
}
|
||||
|
||||
export function ResponsiveSheet({
|
||||
open,
|
||||
onOpenChange,
|
||||
@@ -153,6 +170,7 @@ export function ResponsiveSheet({
|
||||
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||
<SheetContent
|
||||
onCloseAutoFocus={onCloseAutoFocus}
|
||||
onOpenAutoFocus={keepKeyboardDownOnOpen}
|
||||
side="bottom"
|
||||
showCloseButton={showCloseButton}
|
||||
data-slot="responsive-sheet-content"
|
||||
|
||||
Reference in new issue
Block a user