diff --git a/packages/bpk-component-carousel/src/utils.test.tsx b/packages/bpk-component-carousel/src/utils.test.tsx deleted file mode 100644 index f1ab796866..0000000000 --- a/packages/bpk-component-carousel/src/utils.test.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import { renderHook } from '@testing-library/react-hooks'; - -import { useScrollToInitialImage } from './utils'; - -describe('useScrollToInitialImage', () => { - it('should scroll to initial image on mount', () => { - const imagesRef = { - current: [document.createElement('div'), document.createElement('div')], - }; - const initialImageIndex = 0; - - renderHook(() => useScrollToInitialImage(initialImageIndex, imagesRef)); - - expect( - imagesRef.current[initialImageIndex].scrollIntoView, - ).toHaveBeenCalledTimes(1); - }); -}); diff --git a/packages/bpk-component-carousel/src/utils.tsx b/packages/bpk-component-carousel/src/utils.tsx index 97771715e0..7d6aa3096d 100644 --- a/packages/bpk-component-carousel/src/utils.tsx +++ b/packages/bpk-component-carousel/src/utils.tsx @@ -25,27 +25,15 @@ export function useScrollToInitialImage( initialImageIndex: number, imagesRef: MutableRefObject>, ) { - const handleIntersecting = (index: number) => { - const element = imagesRef.current[index]; + useEffect(() => { + const element = imagesRef.current[initialImageIndex]; if (element) { element.scrollIntoView({ block: 'nearest', inline: 'start', }); } - }; - - const observe = useIntersectionObserver(handleIntersecting, { - root: null, - threshold: 0.1, - }); - - useEffect(() => { - const element = imagesRef.current[initialImageIndex]; - if (element) { - observe(element); - } - }, [initialImageIndex, imagesRef, observe]); + }, [initialImageIndex, imagesRef]); } export function useIntersectionObserver( diff --git a/packages/bpk-component-mobile-scroll-container/src/BpkMobileScrollContainer.js b/packages/bpk-component-mobile-scroll-container/src/BpkMobileScrollContainer.js index f82b6537da..d9910b8dad 100644 --- a/packages/bpk-component-mobile-scroll-container/src/BpkMobileScrollContainer.js +++ b/packages/bpk-component-mobile-scroll-container/src/BpkMobileScrollContainer.js @@ -140,38 +140,35 @@ class BpkMobileScrollContainer extends Component { }, 100); setScrollIndicatorClassName = () => { - requestAnimationFrame(() => { - const classNames = computeScrollIndicatorClassName( - this.scrollerEl, - this.props.leadingIndicatorClassName, - this.props.trailingIndicatorClassName, - ); - - if (!classNames) { - return; - } - - this.setState(() => ({ - scrollIndicatorClassName: classNames.join(' '), - })); - }); + const classNames = computeScrollIndicatorClassName( + this.scrollerEl, + this.props.leadingIndicatorClassName, + this.props.trailingIndicatorClassName, + ); + + if (!classNames) { + return; + } + + this.setState(() => ({ + scrollIndicatorClassName: classNames.join(' '), + })); }; setScrollBarAwareHeight = () => { if (this.props.showScrollbar) { return; } - requestAnimationFrame(() => { - const computedHeight = computeScrollBarAwareHeight( - this.scrollerEl, - this.innerEl, - ); - - if (!computedHeight) { - return; - } - this.setState(() => ({ computedHeight })); - }); + const computedHeight = computeScrollBarAwareHeight( + this.scrollerEl, + this.innerEl, + ); + + if (!computedHeight) { + return; + } + + this.setState(() => ({ computedHeight })); }; render() {