Skip to content

Commit

Permalink
[MIRROR] React cleanup (#1965)
Browse files Browse the repository at this point in the history
* React cleanup (#82607)

## About The Pull Request
- No defaultHooks in react. Might fix issues where pages were not
scrollable on hover.
- createRef in a functional component. should be useref

## Why It's Good For The Game
Code improvement

* React cleanup

---------

Co-authored-by: Jeremiah <[email protected]>
  • Loading branch information
2 people authored and StealsThePRs committed Apr 14, 2024
1 parent d4a333c commit d710d3e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
18 changes: 12 additions & 6 deletions tgui/packages/tgui/components/Autofocus.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { createRef, PropsWithChildren, useEffect } from 'react';
import { PropsWithChildren, useEffect, useRef } from 'react';

export const Autofocus = (props: PropsWithChildren) => {
const ref = createRef<HTMLDivElement>();
/** Used to force the window to steal focus on load. Children optional */
export function Autofocus(props: PropsWithChildren) {
const { children } = props;
const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
setTimeout(() => {
const timer = setTimeout(() => {
ref.current?.focus();
}, 1);

return () => {
clearTimeout(timer);
};
}, []);

return (
<div ref={ref} tabIndex={-1}>
{props.children}
{children}
</div>
);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,6 @@ const KeybindingName = (props: { keybinding: Keybinding }) => {
);
};

KeybindingName.defaultHooks = {
onComponentShouldUpdate: (lastProps, nextProps) => {
return lastProps.keybinding !== nextProps.keybinding;
},
};

const ResetToDefaultButton = (props: { keybindingId: string }) => {
const { act } = useBackend<PreferencesMenuData>();

Expand Down
21 changes: 16 additions & 5 deletions tgui/packages/tgui/layouts/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { classes } from 'common/react';
import { useEffect, useRef } from 'react';

import {
BoxProps,
Expand Down Expand Up @@ -40,6 +41,20 @@ type ContentProps = Partial<{

function LayoutContent(props: ContentProps) {
const { className, scrollable, children, ...rest } = props;
const node = useRef<HTMLDivElement>(null);

useEffect(() => {
const self = node.current;

if (self && scrollable) {
addScrollableNode(self);
}
return () => {
if (self && scrollable) {
removeScrollableNode(self);
}
};
}, []);

return (
<div
Expand All @@ -49,16 +64,12 @@ function LayoutContent(props: ContentProps) {
className,
computeBoxClassName(rest),
])}
ref={node}
{...computeBoxProps(rest)}
>
{children}
</div>
);
}

LayoutContent.defaultHooks = {
onComponentDidMount: (node) => addScrollableNode(node),
onComponentWillUnmount: (node) => removeScrollableNode(node),
};

Layout.Content = LayoutContent;

0 comments on commit d710d3e

Please sign in to comment.