Skip to content

Commit

Permalink
[DevTools] feat[Tree]: set initial scroll offset when inspected eleme…
Browse files Browse the repository at this point in the history
…nt index is set (facebook#31968)

Stacked on facebook#31956. See [commit on
top](facebook@ecb8df4).

Use `initialScrollOffset` prop for `FixedSizeList` from `react-window`.
This happens when user selects an element in built-in Elements panel in
DevTools, and then opens Components panel from React DevTools - elements
will be synced and corresponding React Element will be pre-selected, we
just have to scroll to its position now.
  • Loading branch information
hoxyq authored Jan 9, 2025
1 parent d634548 commit f2813ee
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export type ItemData = {
treeFocused: boolean,
};

function calculateInitialScrollOffset(
inspectedElementIndex: number | null,
elementHeight: number,
): number | void {
if (inspectedElementIndex === null) {
return undefined;
}

if (inspectedElementIndex < 3) {
return undefined;
}

// Make 3 elements on top of the inspected one visible
return (inspectedElementIndex - 3) * elementHeight;
}

export default function Tree(): React.Node {
const dispatch = useContext(TreeDispatcherContext);
const {
Expand Down Expand Up @@ -401,6 +417,10 @@ export default function Tree(): React.Node {
<FixedSizeList
className={styles.List}
height={height}
initialScrollOffset={calculateInitialScrollOffset(
inspectedElementIndex,
lineHeight,
)}
innerElementType={InnerElementType}
itemCount={numElements}
itemData={itemData}
Expand Down

0 comments on commit f2813ee

Please sign in to comment.