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

Function scrollDetentIntoView

packages/react-aria-components/src/Sheet.tsx:1116–1152  ·  view source on GitHub ↗
(
  scroller: HTMLElement,
  marker: HTMLElement,
  axis: Axis,
  align: 'start' | 'end',
  smooth = false
)

Source from the content-addressed store, hash-verified

1114}
1115
1116function scrollDetentIntoView(
1117 scroller: HTMLElement,
1118 marker: HTMLElement,
1119 axis: Axis,
1120 align: 'start' | 'end',
1121 smooth = false
1122) {
1123 let behavior: ScrollBehavior = smooth && !reduceMotion() ? 'smooth' : 'auto';
1124 let markerRect = marker.getBoundingClientRect();
1125 let scrollerRect = scroller.getBoundingClientRect();
1126 let style = getComputedStyle(marker);
1127 if (axis === 'y') {
1128 if (align === 'start') {
1129 let margin = parseFloat(style.scrollMarginTop) || 0;
1130 // Rest the marker's start edge (less its scroll-margin) at the scrollport's start.
1131 let top = markerRect.top - scrollerRect.top + scroller.scrollTop - margin;
1132 scroller.scrollTo({top, behavior});
1133 } else {
1134 let margin = parseFloat(style.scrollMarginBottom) || 0;
1135 // Rest the marker's end edge (plus its scroll-margin) at the scrollport's end.
1136 let top =
1137 markerRect.bottom - scrollerRect.top + scroller.scrollTop + margin - scroller.clientHeight;
1138 scroller.scrollTo({top, behavior});
1139 }
1140 } else {
1141 if (align === 'start') {
1142 let margin = parseFloat(style.scrollMarginLeft) || 0;
1143 let left = markerRect.left - scrollerRect.left + scroller.scrollLeft - margin;
1144 scroller.scrollTo({left, behavior});
1145 } else {
1146 let margin = parseFloat(style.scrollMarginRight) || 0;
1147 let left =
1148 markerRect.right - scrollerRect.left + scroller.scrollLeft + margin - scroller.clientWidth;
1149 scroller.scrollTo({left, behavior});
1150 }
1151 }
1152}
1153
1154function calcToPx(calc: string) {
1155 if (!calc.startsWith('calc(')) {

Callers 1

scrollToRestFunction · 0.85

Calls 1

reduceMotionFunction · 0.85

Tested by

no test coverage detected