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

Function scrollDetentIntoView

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

Source from the content-addressed store, hash-verified

1156}
1157
1158function scrollDetentIntoView(
1159 scroller: HTMLElement,
1160 marker: HTMLElement,
1161 axis: Axis,
1162 align: 'start' | 'end',
1163 smooth = false
1164) {
1165 let behavior: ScrollBehavior = smooth && !reduceMotion() ? 'smooth' : 'auto';
1166 let markerRect = marker.getBoundingClientRect();
1167 let scrollerRect = scroller.getBoundingClientRect();
1168 let style = getComputedStyle(marker);
1169 if (axis === 'y') {
1170 if (align === 'start') {
1171 let margin = parseFloat(style.scrollMarginTop) || 0;
1172 // Rest the marker's start edge (less its scroll-margin) at the scrollport's start.
1173 let top = markerRect.top - scrollerRect.top + scroller.scrollTop - margin;
1174 scroller.scrollTo({top, behavior});
1175 } else {
1176 let margin = parseFloat(style.scrollMarginBottom) || 0;
1177 // Rest the marker's end edge (plus its scroll-margin) at the scrollport's end.
1178 let top =
1179 markerRect.bottom - scrollerRect.top + scroller.scrollTop + margin - scroller.clientHeight;
1180 scroller.scrollTo({top, behavior});
1181 }
1182 } else {
1183 if (align === 'start') {
1184 let margin = parseFloat(style.scrollMarginLeft) || 0;
1185 let left = markerRect.left - scrollerRect.left + scroller.scrollLeft - margin;
1186 scroller.scrollTo({left, behavior});
1187 } else {
1188 let margin = parseFloat(style.scrollMarginRight) || 0;
1189 let left =
1190 markerRect.right - scrollerRect.left + scroller.scrollLeft + margin - scroller.clientWidth;
1191 scroller.scrollTo({left, behavior});
1192 }
1193 }
1194}
1195
1196function calcToPx(calc: string) {
1197 if (!calc.startsWith('calc(')) {

Callers 1

scrollToRestFunction · 0.85

Calls 1

reduceMotionFunction · 0.85

Tested by

no test coverage detected