diff --git a/tgui/packages/tgui/components/Section.tsx b/tgui/packages/tgui/components/Section.tsx index 372a8ae52ae..6640f5a99e0 100644 --- a/tgui/packages/tgui/components/Section.tsx +++ b/tgui/packages/tgui/components/Section.tsx @@ -5,7 +5,7 @@ */ import { BoxProps, computeBoxClassName, computeBoxProps } from './Box'; -import { ReactNode, RefObject, createRef, useEffect } from 'react'; +import { forwardRef, ReactNode, RefObject, useEffect } from 'react'; import { addScrollableNode, removeScrollableNode } from '../events'; import { canRender, classes } from 'common/react'; @@ -16,68 +16,72 @@ export type SectionProps = Partial<{ scrollable: boolean; scrollableHorizontal: boolean; title: ReactNode; - /** @member Allows external control of scrolling. */ - scrollableRef: RefObject; /** @member Callback function for the `scroll` event */ onScroll: ((this: GlobalEventHandlers, ev: Event) => any) | null; }> & BoxProps; -export const Section = (props: SectionProps) => { - const { - className, - title, - buttons, - fill, - fitted, - scrollable, - scrollableHorizontal, - children, - onScroll, - ...rest - } = props; +export const Section = forwardRef( + (props: SectionProps, ref: RefObject) => { + const { + className, + title, + buttons, + fill, + fitted, + scrollable, + scrollableHorizontal, + children, + onScroll, + ...rest + } = props; - const scrollableRef = props.scrollableRef || createRef(); - const hasTitle = canRender(title) || canRender(buttons); + const hasTitle = canRender(title) || canRender(buttons); + + useEffect(() => { + if (!ref?.current) return; - useEffect(() => { - if (scrollable || scrollableHorizontal) { - addScrollableNode(scrollableRef.current as HTMLElement); - if (onScroll && scrollableRef.current) { - scrollableRef.current.onscroll = onScroll; - } - } - return () => { if (scrollable || scrollableHorizontal) { - removeScrollableNode(scrollableRef.current as HTMLElement); + addScrollableNode(ref.current); + if (onScroll && ref.current) { + ref.current.onscroll = onScroll; + } } - }; - }, []); + return () => { + if (!ref?.current) return; - return ( -
- {hasTitle && ( -
- {title} -
{buttons}
-
- )} -
-
- {children} + if (scrollable || scrollableHorizontal) { + removeScrollableNode(ref.current); + } + }; + }, []); + + return ( +
+ {hasTitle && ( +
+ {title} +
{buttons}
+
+ )} +
+
+ {children} +
-
- ); -}; + ); + }, +); diff --git a/tgui/packages/tgui/interfaces/NtosMessenger/ChatScreen.tsx b/tgui/packages/tgui/interfaces/NtosMessenger/ChatScreen.tsx index 0c1f2f89cce..6724bd359a1 100644 --- a/tgui/packages/tgui/interfaces/NtosMessenger/ChatScreen.tsx +++ b/tgui/packages/tgui/interfaces/NtosMessenger/ChatScreen.tsx @@ -349,7 +349,7 @@ export class ChatScreen extends Component { fill fitted title={`${recipient.name} (${recipient.job})`} - scrollableRef={this.scrollRef} + ref={this.scrollRef} > {!!(messages.length > 0 && canReply) && ( diff --git a/tgui/packages/tgui/interfaces/PaperSheet.tsx b/tgui/packages/tgui/interfaces/PaperSheet.tsx index 86ab67ab7a3..3f6bf0de671 100644 --- a/tgui/packages/tgui/interfaces/PaperSheet.tsx +++ b/tgui/packages/tgui/interfaces/PaperSheet.tsx @@ -956,7 +956,7 @@ export class PreviewView extends Component { fill fitted scrollable - scrollableRef={scrollableRef} + ref={scrollableRef} onScroll={handleOnScroll} >