Allow accessing ref of ScrollContainer's child (#36265)

This commit is contained in:
diondiondion 2025-09-25 18:14:49 +02:00 committed by GitHub
parent d801cf8e59
commit 11bd515648
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 3 deletions

View File

@ -399,7 +399,7 @@ class ScrollableList extends PureComponent {
if (trackScroll) { if (trackScroll) {
return ( return (
<ScrollContainer scrollKey={scrollKey}> <ScrollContainer scrollKey={scrollKey} childRef={this.setRef}>
{scrollableArea} {scrollableArea}
</ScrollContainer> </ScrollContainer>
); );

View File

@ -1,4 +1,9 @@
import React, { useContext, useEffect, useRef } from 'react'; import React, {
useContext,
useEffect,
useImperativeHandle,
useRef,
} from 'react';
import { defaultShouldUpdateScroll } from './default_should_update_scroll'; import { defaultShouldUpdateScroll } from './default_should_update_scroll';
import type { ShouldUpdateScrollFn } from './default_should_update_scroll'; import type { ShouldUpdateScrollFn } from './default_should_update_scroll';
@ -11,6 +16,7 @@ interface ScrollContainerProps {
*/ */
scrollKey: string; scrollKey: string;
shouldUpdateScroll?: ShouldUpdateScrollFn; shouldUpdateScroll?: ShouldUpdateScrollFn;
childRef?: React.ForwardedRef<HTMLElement | undefined>;
children: React.ReactElement; children: React.ReactElement;
} }
@ -23,12 +29,20 @@ interface ScrollContainerProps {
export const ScrollContainer: React.FC<ScrollContainerProps> = ({ export const ScrollContainer: React.FC<ScrollContainerProps> = ({
children, children,
scrollKey, scrollKey,
childRef,
shouldUpdateScroll = defaultShouldUpdateScroll, shouldUpdateScroll = defaultShouldUpdateScroll,
}) => { }) => {
const scrollBehaviorContext = useContext(ScrollBehaviorContext); const scrollBehaviorContext = useContext(ScrollBehaviorContext);
const containerRef = useRef<HTMLElement>(); const containerRef = useRef<HTMLElement>();
/**
* If a childRef is passed, sync it with the containerRef. This
* is necessary because in this component's return statement,
* we're overwriting the immediate child component's ref prop.
*/
useImperativeHandle(childRef, () => containerRef.current, []);
/** /**
* Register/unregister scrollable element with ScrollBehavior * Register/unregister scrollable element with ScrollBehavior
*/ */

View File

@ -602,7 +602,7 @@ class Status extends ImmutablePureComponent {
)} )}
/> />
<ScrollContainer scrollKey='thread' shouldUpdateScroll={this.shouldUpdateScroll}> <ScrollContainer scrollKey='thread' shouldUpdateScroll={this.shouldUpdateScroll} childRef={this.setContainerRef}>
<div className={classNames('scrollable item-list', { fullscreen })} ref={this.setContainerRef}> <div className={classNames('scrollable item-list', { fullscreen })} ref={this.setContainerRef}>
{ancestors} {ancestors}