Repository navigation
List View: Replace useFixedWindowList with a colocated window hook #87065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 5 commits
df06fc9
044bdf1
d1201b2
52692be
fdb2694
691dcdc
18a39f9
afd5306
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,6 @@ | ||
| import type { CSSProperties, ForwardedRef, KeyboardEvent } from 'react'; | ||
| import clsx from 'clsx'; | ||
| import { | ||
| useInstanceId, | ||
| useMergeRefs, | ||
| useRefEffect, | ||
| __experimentalUseFixedWindowList as useFixedWindowList, | ||
| } from '@wordpress/compose'; | ||
| import { useInstanceId, useMergeRefs, useRefEffect } from '@wordpress/compose'; | ||
| import { isShallowEqual } from '@wordpress/is-shallow-equal'; | ||
| import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components'; | ||
| import { VisuallyHidden } from '@wordpress/ui'; | ||
|
|
@@ -33,6 +28,7 @@ import useListViewClientIds from './use-list-view-client-ids'; | |
| import useListViewCollapseItems from './use-list-view-collapse-items'; | ||
| import useListViewDropZone from './use-list-view-drop-zone'; | ||
| import useListViewExpandSelectedItem from './use-list-view-expand-selected-item'; | ||
| import useListViewWindow from './use-list-view-window'; | ||
| import { store as blockEditorStore } from '../../store'; | ||
| import { BlockSettingsDropdown } from '../block-settings-menu/block-settings-dropdown'; | ||
| import { BLOCK_LIST_ITEM_HEIGHT, focusListItem } from './utils'; | ||
|
|
@@ -135,20 +131,6 @@ function ListViewComponent( | |
|
|
||
| const { getBlock, getSelectedBlockClientIds } = | ||
| useSelect( blockEditorStore ); | ||
| const { visibleBlockCount } = useSelect( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new hook doesn't need the total row count, so List View no longer subscribes to the global block count. |
||
| ( select ) => { | ||
| const { getGlobalBlockCount, getClientIdsOfDescendants } = | ||
| select( blockEditorStore ); | ||
| const draggedBlockCount = | ||
| draggedClientIds?.length > 0 | ||
| ? getClientIdsOfDescendants( draggedClientIds ).length + 1 | ||
| : 0; | ||
| return { | ||
| visibleBlockCount: getGlobalBlockCount() - draggedBlockCount, | ||
| }; | ||
| }, | ||
| [ draggedClientIds ] | ||
| ); | ||
|
|
||
| const { updateBlockSelection } = useBlockSelection(); | ||
|
|
||
|
|
@@ -185,6 +167,7 @@ function ListViewComponent( | |
| updateExpansion, | ||
| } ); | ||
| const elementRef = useRef< HTMLElement >( null ); | ||
| const { ref: listWindowRef, fixedListWindow } = useListViewWindow(); | ||
|
|
||
| // Allow handling of copy, cut, and paste events. | ||
| const clipBoardRef = useClipboardHandler( { | ||
|
|
@@ -248,6 +231,7 @@ function ListViewComponent( | |
| clipBoardRef, | ||
| focusOnMountRef, | ||
| elementRef, | ||
| listWindowRef, | ||
| dropZoneRef, | ||
| ref, | ||
| ] ); | ||
|
|
@@ -366,22 +350,6 @@ function ListViewComponent( | |
| ] | ||
| ); | ||
|
|
||
| const [ fixedListWindow ] = useFixedWindowList( | ||
| elementRef, | ||
| BLOCK_LIST_ITEM_HEIGHT, | ||
| visibleBlockCount, | ||
| { | ||
| // Ensure that the windowing logic is recalculated when the expanded state changes. | ||
| // This is necessary because expanding a collapsed block in a short list view can | ||
| // switch the list view to a tall list view with a scrollbar, and vice versa. | ||
| // When this happens, the windowing logic needs to be recalculated to ensure that | ||
| // the correct number of blocks are rendered, by rechecking for a scroll container. | ||
| expandedState: expansionState, | ||
| useWindowing: true, | ||
| windowOverscan: 40, | ||
| } | ||
| ); | ||
|
|
||
| // If there are no blocks to show and we're not showing the appender, do not render the list view. | ||
| if ( ! clientIdsTree.length && ! showAppender ) { | ||
| return null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import { useRefEffect } from '@wordpress/compose'; | ||
| import { getScrollContainer } from '@wordpress/dom'; | ||
| import { flushSync, useRef, useState } from '@wordpress/element'; | ||
| import { BLOCK_LIST_ITEM_HEIGHT } from './utils'; | ||
| import type { FixedListWindow } from './types'; | ||
|
|
||
| // Every window change re-renders the whole tree, so the window only moves once | ||
| // fewer than the minimum rows are left past the viewport, and then moves far | ||
| // enough for the next stretch of scrolling. | ||
| const MIN_BUFFER_ROWS = 10; | ||
| const MAX_BUFFER_ROWS = 40; | ||
|
Comment on lines
+10
to
+11
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were tuned with a perf probe. A minimum of 20 or 30 dropped more frames (28 or 50 against 19). A maximum of 70 halved the dropped frames, but each one got longer (54ms against 42ms). |
||
|
|
||
| function getVisibleRows( list: HTMLElement, scrollContainer: Element ) { | ||
| const { documentElement } = list.ownerDocument; | ||
| const { top, bottom } = | ||
| scrollContainer === documentElement | ||
| ? { top: 0, bottom: documentElement.clientHeight } | ||
| : scrollContainer.getBoundingClientRect(); | ||
| const listTop = list.getBoundingClientRect().top; | ||
|
|
||
| return { | ||
| first: Math.max( | ||
| 0, | ||
| Math.floor( ( top - listTop ) / BLOCK_LIST_ITEM_HEIGHT ) | ||
| ), | ||
| last: Math.max( | ||
| 0, | ||
| Math.ceil( ( bottom - listTop ) / BLOCK_LIST_ITEM_HEIGHT ) | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| function coversRows( | ||
| { start, end }: FixedListWindow, | ||
| first: number, | ||
| last: number, | ||
| spareRows: number | ||
| ) { | ||
| return ( | ||
| ( start === 0 || first - start >= spareRows ) && end - last >= spareRows | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tracks which rows of the list view should be rendered. | ||
| * | ||
| * @return The ref for the list element, and the window of rows to render. | ||
| */ | ||
| export default function useListViewWindow() { | ||
| // The list can't be measured without forcing a layout while it opens, but | ||
| // it never shows more rows than fit in the browser window. | ||
| const [ fixedListWindow, setFixedListWindow ] = useState< FixedListWindow >( | ||
| () => ( { | ||
| start: 0, | ||
| end: Math.ceil( window.innerHeight / BLOCK_LIST_ITEM_HEIGHT ), | ||
| } ) | ||
| ); | ||
|
Comment on lines
+52
to
+57
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reposting, since the earlier comment got detached when this was inlined. Sizing the first window from |
||
| // The listeners below have to know which rows are rendered to tell | ||
| // whether a row in view is missing. | ||
| const listWindowRef = useRef( fixedListWindow ); | ||
|
|
||
| const ref = useRefEffect( ( list: HTMLElement ) => { | ||
| const { ownerDocument } = list; | ||
| const defaultView = ownerDocument.defaultView as Window; | ||
| let observedContainer: Element | undefined; | ||
|
|
||
| // The container can change while the list is open: a popover only | ||
| // clips its content after the first layout, and a list starts to | ||
| // overflow as it grows. | ||
| const observeContainer = ( scrollContainer: Element ) => { | ||
| if ( scrollContainer !== observedContainer ) { | ||
| observedContainer = scrollContainer; | ||
| resizeObserver.disconnect(); | ||
| resizeObserver.observe( scrollContainer ); | ||
| } | ||
| }; | ||
|
|
||
| const updateWindow = ( spareRows: number ) => { | ||
| const scrollContainer = | ||
| getScrollContainer( list ) ?? ownerDocument.documentElement; | ||
| observeContainer( scrollContainer ); | ||
|
|
||
| const { first, last } = getVisibleRows( list, scrollContainer ); | ||
| const listWindow = listWindowRef.current; | ||
| if ( coversRows( listWindow, first, last, spareRows ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // A row can only be unmounted when the window moves, so this is | ||
| // the one moment the focused row has to be known. | ||
| const focusedRow = | ||
| ownerDocument.activeElement?.closest< HTMLElement >( | ||
| '[data-block]' | ||
| ); | ||
|
Comment on lines
+76
to
+79
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first attempt kept the focused block in state and passed it through context, which re-rendered the whole tree on every focus move. Reading it here costs nothing, since the tree already re-renders when the window moves. A scroll probe measured the same main-thread work with and without the pin. Trunk has the same focus drop, so this isn't a regression fix for the new hook. |
||
| const nextWindow = { | ||
| start: Math.max( 0, first - MAX_BUFFER_ROWS ), | ||
| end: last + MAX_BUFFER_ROWS, | ||
| focusedClientId: | ||
| focusedRow && list.contains( focusedRow ) | ||
| ? focusedRow.dataset.block | ||
| : undefined, | ||
| }; | ||
| listWindowRef.current = nextWindow; | ||
|
|
||
| // Scroll events run before paint, so rows that are already in view | ||
| // have to be committed here to show up in this frame. | ||
| if ( coversRows( listWindow, first, last, 0 ) ) { | ||
| setFixedListWindow( nextWindow ); | ||
| } else { | ||
| flushSync( () => setFixedListWindow( nextWindow ) ); | ||
| } | ||
|
Comment on lines
+92
to
+96
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always using |
||
| }; | ||
|
|
||
| const fillViewport = () => updateWindow( 0 ); | ||
| const resizeObserver = new ResizeObserver( fillViewport ); | ||
|
|
||
| // Scroll events don't bubble, but capturing them here covers whichever | ||
| // ancestor ends up scrolling the list. | ||
| const onScroll = ( event: Event ) => { | ||
| if ( ( event.target as Node ).contains( list ) ) { | ||
| updateWindow( MIN_BUFFER_ROWS ); | ||
| } | ||
| }; | ||
|
|
||
| ownerDocument.addEventListener( 'scroll', onScroll, { | ||
| capture: true, | ||
| passive: true, | ||
| } ); | ||
| defaultView.addEventListener( 'resize', fillViewport ); | ||
|
|
||
| // The observer first reports after layout, so the first measurement | ||
| // doesn't force one while the list opens. | ||
| resizeObserver.observe( list ); | ||
|
Mamaduka marked this conversation as resolved.
Resolved
|
||
|
|
||
| return () => { | ||
| resizeObserver.disconnect(); | ||
| defaultView.removeEventListener( 'resize', fillViewport ); | ||
| ownerDocument.removeEventListener( 'scroll', onScroll, { | ||
| capture: true, | ||
| } ); | ||
| }; | ||
| }, [] ); | ||
|
|
||
| return { ref, fixedListWindow }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
itemInViewwas a leftover from theuseFixedWindowListAPI. A plain range does the same job and measured no difference in the scroll probe.