Skip to content

Commit 11bd515

Browse files
authored
Allow accessing ref of ScrollContainer's child (mastodon#36265)
1 parent d801cf8 commit 11bd515

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

app/javascript/mastodon/components/scrollable_list.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class ScrollableList extends PureComponent {
399399

400400
if (trackScroll) {
401401
return (
402-
<ScrollContainer scrollKey={scrollKey}>
402+
<ScrollContainer scrollKey={scrollKey} childRef={this.setRef}>
403403
{scrollableArea}
404404
</ScrollContainer>
405405
);

app/javascript/mastodon/containers/scroll_container/index.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import React, { useContext, useEffect, useRef } from 'react';
1+
import React, {
2+
useContext,
3+
useEffect,
4+
useImperativeHandle,
5+
useRef,
6+
} from 'react';
27

38
import { defaultShouldUpdateScroll } from './default_should_update_scroll';
49
import type { ShouldUpdateScrollFn } from './default_should_update_scroll';
@@ -11,6 +16,7 @@ interface ScrollContainerProps {
1116
*/
1217
scrollKey: string;
1318
shouldUpdateScroll?: ShouldUpdateScrollFn;
19+
childRef?: React.ForwardedRef<HTMLElement | undefined>;
1420
children: React.ReactElement;
1521
}
1622

@@ -23,12 +29,20 @@ interface ScrollContainerProps {
2329
export const ScrollContainer: React.FC<ScrollContainerProps> = ({
2430
children,
2531
scrollKey,
32+
childRef,
2633
shouldUpdateScroll = defaultShouldUpdateScroll,
2734
}) => {
2835
const scrollBehaviorContext = useContext(ScrollBehaviorContext);
2936

3037
const containerRef = useRef<HTMLElement>();
3138

39+
/**
40+
* If a childRef is passed, sync it with the containerRef. This
41+
* is necessary because in this component's return statement,
42+
* we're overwriting the immediate child component's ref prop.
43+
*/
44+
useImperativeHandle(childRef, () => containerRef.current, []);
45+
3246
/**
3347
* Register/unregister scrollable element with ScrollBehavior
3448
*/

app/javascript/mastodon/features/status/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ class Status extends ImmutablePureComponent {
602602
)}
603603
/>
604604

605-
<ScrollContainer scrollKey='thread' shouldUpdateScroll={this.shouldUpdateScroll}>
605+
<ScrollContainer scrollKey='thread' shouldUpdateScroll={this.shouldUpdateScroll} childRef={this.setContainerRef}>
606606
<div className={classNames('scrollable item-list', { fullscreen })} ref={this.setContainerRef}>
607607
{ancestors}
608608

0 commit comments

Comments
 (0)