MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useSnapPointTravel

Function useSnapPointTravel

packages/react-aria-components/src/Sheet.tsx:923–965  ·  view source on GitHub ↗
(
  stageRef: React.RefObject<HTMLDivElement | null>,
  sheetRef: React.RefObject<HTMLDivElement | null>,
  axis: Axis,
  after: boolean,
  // Space-separated snap points, so markers are re-measured when they change. Null to disable.
  snapPointsKey: string | null
)

Source from the content-addressed store, hash-verified

921// means snap points can use any CSS length, including percentages of the sheet. Uses layout offsets
922// rather than bounding rects so swipe/stack animation transforms don't affect it.
923function useSnapPointTravel(
924 stageRef: React.RefObject<HTMLDivElement | null>,
925 sheetRef: React.RefObject<HTMLDivElement | null>,
926 axis: Axis,
927 after: boolean,
928 // Space-separated snap points, so markers are re-measured when they change. Null to disable.
929 snapPointsKey: string | null
930): number {
931 let [travel, setTravel] = useState(0);
932 useLayoutEffect(() => {
933 let stage = stageRef.current;
934 let sheet = sheetRef.current;
935 if (snapPointsKey == null || !stage || !sheet) {
936 return;
937 }
938
939 let measure = () => {
940 let stageSize = axis === 'y' ? stage.clientHeight : stage.clientWidth;
941 let max = 0;
942 for (let marker of sheet.querySelectorAll<HTMLElement>('[data-sheet-detent]')) {
943 let offset = 0;
944 let el: HTMLElement | null = marker;
945 while (el && el !== stage) {
946 offset += axis === 'y' ? el.offsetTop : el.offsetLeft;
947 el = el.offsetParent as HTMLElement | null;
948 }
949 // Markers sit at the snap point's start edge for sheets exiting toward the end edge, and at
950 // its end edge for sheets exiting toward the start edge. The smallest snap point travels most.
951 let size = axis === 'y' ? marker.offsetHeight : marker.offsetWidth;
952 max = Math.max(max, after ? stageSize - offset : offset + size);
953 }
954 setTravel(max);
955 };
956
957 measure();
958 let observer = new ResizeObserver(measure);
959 observer.observe(stage);
960 observer.observe(sheet);
961 return () => observer.disconnect();
962 }, [stageRef, sheetRef, axis, after, snapPointsKey]);
963
964 return travel;
965}
966
967export interface SheetBackdropProps
968 extends RenderProps<SheetRenderProps>, GlobalDOMAttributes<HTMLDivElement> {

Callers 1

Sheet.tsxFile · 0.85

Calls 1

measureFunction · 0.85

Tested by

no test coverage detected