import TestCase from '../../TestCase'; import Fixture from '../../Fixture'; import ScrollIntoViewCaseComplex from './ScrollIntoViewCaseComplex'; import ScrollIntoViewCaseSimple from './ScrollIntoViewCaseSimple'; import ScrollIntoViewTargetElement from './ScrollIntoViewTargetElement'; const React = window.React; const {Fragment, useRef, useState, useEffect} = React; const ReactDOM = window.ReactDOM; function Controls({ alignToTop, setAlignToTop, scrollVertical, exampleType, setExampleType, }) { return (
); } export default function ScrollIntoViewCase() { const [exampleType, setExampleType] = useState('simple'); const [alignToTop, setAlignToTop] = useState(true); const [caseInViewport, setCaseInViewport] = useState(false); const fragmentRef = useRef(null); const testCaseRef = useRef(null); const noChildRef = useRef(null); const scrollContainerRef = useRef(null); const scrollVertical = () => { fragmentRef.current.scrollIntoView(alignToTop); }; const scrollVerticalNoChildren = () => { noChildRef.current.scrollIntoView(alignToTop); }; useEffect(() => { const observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { setCaseInViewport(true); } else { setCaseInViewport(false); } }); }); testCaseRef.current.observeUsing(observer); const lastRef = testCaseRef.current; return () => { lastRef.unobserveUsing(observer); observer.disconnect(); }; }); return (
  • Toggle alignToTop and click the buttons to scroll
  • When the Fragment has children:

    In order to handle the case where children are split between multiple scroll containers, we call scrollIntoView on each child in reverse order.

    When the Fragment does not have children:

    The Fragment still represents a virtual space. We can scroll to the nearest edge by selecting the host sibling before if alignToTop=false, or after if alignToTop=true|undefined. We'll fall back to the other sibling or parent in the case that the preferred sibling target doesn't exist.

    {exampleType === 'simple' && ( )} {exampleType === 'horizontal' && (
    )} {exampleType === 'multiple' && (
    )} {exampleType === 'empty' && ( )} ); }