UNPKG

@react-md/utils

Version:
29 lines 1.13 kB
import { findSizingContainer } from "./positioning/findSizingContainer"; /** * Attempts to scroll an element into view within another container element if * needed. If either the container or element are `null`, nothing will happen. * * @param container - The container element that should be scrolled if the child * element is not within view. * @param element - The element that should be visible within the container * element's scroll area. */ export function scrollIntoView(container, element) { element = findSizingContainer(element); if (!container || !element) { return; } var offsetTop = element.offsetTop; if (element.offsetParent !== container) { offsetTop -= container.offsetTop; } var elementBottom = offsetTop + element.offsetHeight; var containerBottom = container.offsetHeight + container.scrollTop; if (elementBottom > containerBottom) { container.scrollTop = elementBottom - container.offsetHeight; } else if (offsetTop < container.scrollTop) { container.scrollTop = offsetTop; } } //# sourceMappingURL=scrollIntoView.js.map