@shopgate/engage
Version:
Shopgate's ENGAGE library.
5 lines • 997 B
JavaScript
import{useCallback}from'react';import{getCSSCustomProp}from'@shopgate/engage/styles';import{hasWebBridge}from"../helpers/bridge";/**
* @param {Object} ref A ref to the scroll container
* @param {number} [offset=10] Optional offset for the scroll operation
* @returns {Object}
*/export var useScrollTo=function useScrollTo(ref){var offset=arguments.length>1&&arguments[1]!==undefined?arguments[1]:10;var scrollTo=useCallback(function(selector){if(!(ref===null||ref===void 0?void 0:ref.current)){return;}var firstElement=ref.current.querySelector(selector);if(firstElement){if(hasWebBridge()){var scrollOffset=offset;var appBarHeight=getCSSCustomProp('--app-bar-height')||0;var _firstElement$getBoun2=firstElement.getBoundingClientRect(),top=_firstElement$getBoun2.top;var scrollTop=top+window.pageYOffset-parseInt(appBarHeight,10)-scrollOffset;window.scroll({top:scrollTop,behavior:'smooth'});}else{firstElement.scrollIntoView({behavior:'smooth'});}}},[offset,ref]);return{scrollTo:scrollTo};};