Pin phone sheets to the visible viewport, and let people correct login and organizer errors.
This commit is contained in:
1 parent
27d74e510c
commit
39807ef452
17 files changed
+236
-16
No files matched your search
@@ -1,22 +1,40 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
/**
|
||||
* Visible viewport height, including the shrink when a phone keyboard opens.
|
||||
* Visible viewport box, including the shrink and offset when a phone keyboard opens.
|
||||
*
|
||||
* iOS Safari reports the keyboard by shrinking `visualViewport.height` and
|
||||
* shifting `offsetTop`. A `fixed` overlay has to use both, or its footer stays
|
||||
* in the layout viewport behind the keyboard.
|
||||
*
|
||||
* @param enabled When false, skip listeners and return `undefined`.
|
||||
* @returns Pixel height of `visualViewport`, or `undefined` when disabled.
|
||||
* @returns Height and top offset of `visualViewport`, or `undefined` when disabled.
|
||||
*
|
||||
* @example
|
||||
* const viewportHeight = useVisualViewportHeight(isOpen);
|
||||
* const viewport = useVisualViewportHeight(isOpen);
|
||||
*/
|
||||
export function useVisualViewportHeight(enabled: boolean): number | undefined {
|
||||
const [height, setHeight] = useState<number | undefined>(undefined);
|
||||
export function useVisualViewportHeight(
|
||||
enabled: boolean,
|
||||
): { height: number; offsetTop: number } | undefined {
|
||||
const [frame, setFrame] = useState<
|
||||
{ height: number; offsetTop: number } | undefined
|
||||
>(undefined);
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
const vp = window.visualViewport;
|
||||
const sync = () => {
|
||||
setHeight(vp?.height ?? window.innerHeight);
|
||||
const next = {
|
||||
height: vp?.height ?? window.innerHeight,
|
||||
offsetTop: vp?.offsetTop ?? 0,
|
||||
};
|
||||
setFrame((prev) =>
|
||||
prev &&
|
||||
prev.height === next.height &&
|
||||
prev.offsetTop === next.offsetTop
|
||||
? prev
|
||||
: next,
|
||||
);
|
||||
};
|
||||
sync();
|
||||
vp?.addEventListener("resize", sync);
|
||||
@@ -29,5 +47,5 @@ export function useVisualViewportHeight(enabled: boolean): number | undefined {
|
||||
};
|
||||
}, [enabled]);
|
||||
|
||||
return enabled ? height : undefined;
|
||||
return enabled ? frame : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user