From c894833cef0583c20fbfb8c62dda4527d6613fc9 Mon Sep 17 00:00:00 2001 From: Christian Moore Date: Fri, 29 Nov 2024 08:20:37 +1100 Subject: [PATCH] Fix focus theft on initial render --- .../subcomponents/GenericTile/GenericTile.tsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/components/src/Tile/subcomponents/GenericTile/GenericTile.tsx b/packages/components/src/Tile/subcomponents/GenericTile/GenericTile.tsx index 53e57b9980a..5746e56a8ea 100644 --- a/packages/components/src/Tile/subcomponents/GenericTile/GenericTile.tsx +++ b/packages/components/src/Tile/subcomponents/GenericTile/GenericTile.tsx @@ -53,14 +53,24 @@ export const GenericTile = ({ ...restProps }: GenericTileProps): JSX.Element => { const [isFlipped, setIsFlipped] = useState(false) + + const isMountingRef = useRef(false) const infoButtonRef = useRef(null) const infoButtonReturnRef = useRef(null) useEffect(() => { - if (isFlipped) { - infoButtonReturnRef.current!.focus() + isMountingRef.current = true + }, []) + + useEffect(() => { + if (!isMountingRef.current) { + if (isFlipped) { + infoButtonReturnRef.current!.focus() + } else { + infoButtonRef.current!.focus() + } } else { - infoButtonRef.current!.focus() + isMountingRef.current = false } }, [isFlipped])