Skip to content

Commit e687471

Browse files
committed
refactor: update forwarded refs
1 parent d4af858 commit e687471

File tree

4 files changed

+54
-44
lines changed

4 files changed

+54
-44
lines changed

src/modules/Header/HeaderLogo.tsx

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Box, type BoxProps, Image, Text } from "@chakra-ui/react";
1+
import { Box, type BoxProps, Image, Text, forwardRef } from "@chakra-ui/react";
22

33
export type HeaderLogoProps = BoxProps & {
44
/**
@@ -11,33 +11,41 @@ export type HeaderLogoProps = BoxProps & {
1111
alt: string;
1212
};
1313

14-
export function HeaderLogo({
15-
children,
16-
src,
17-
alt,
18-
title,
19-
...props
20-
}: HeaderLogoProps) {
21-
return (
22-
<Box
23-
as="button"
24-
type="button"
25-
title={title}
26-
display="flex"
27-
alignItems="center"
28-
gap={2}
29-
py={2}
30-
pl={2}
31-
pr={4}
32-
_hover={{
33-
backgroundColor: "surface.800",
34-
}}
35-
{...props}
36-
>
37-
<Image src={src} alt={alt} />
38-
<Text as="span" color="white" fontWeight={700} fontSize="1.1rem">
39-
{title}
40-
</Text>
41-
</Box>
42-
);
43-
}
14+
/**
15+
* HeaderLogo component
16+
* Second parameter of the forwardRef generic has to be of the component bound to `as` prop
17+
*/
18+
export const HeaderLogo = forwardRef<HeaderLogoProps, 'button'>(
19+
({
20+
children,
21+
src,
22+
alt,
23+
title,
24+
as = 'button',
25+
...props
26+
}, ref) => {
27+
return (
28+
<Box
29+
type="button"
30+
title={title}
31+
display="flex"
32+
alignItems="center"
33+
gap={2}
34+
py={2}
35+
pl={2}
36+
pr={4}
37+
_hover={{
38+
backgroundColor: "surface.800",
39+
}}
40+
as={as}
41+
ref={ref}
42+
{...props}
43+
>
44+
<Image src={src} alt={alt} />
45+
<Text as="span" color="white" fontWeight={700} fontSize="1.1rem">
46+
{title}
47+
</Text>
48+
</Box>
49+
);
50+
}
51+
)

src/modules/Header/HeaderMenuButton.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Icon, MenuButton, useMenuContext } from "@chakra-ui/react";
1+
import { Icon, MenuButton, forwardRef, useMenuContext } from "@chakra-ui/react";
22
import { XIcon, type LucideIcon } from "lucide-react";
33

44
export type HeaderMenuButtonProps = {
@@ -9,7 +9,7 @@ const OPEN_MARGIN_IN_PIXEL = 4;
99
const BORDER_WIDTH_IN_PIXEL = 2;
1010
const MAX_CONTAINER_SIZE_IN_PIXEL = 56;
1111

12-
export function HeaderMenuButton({ icon }: HeaderMenuButtonProps) {
12+
export const HeaderMenuButton = forwardRef<HeaderMenuButtonProps, 'button'>(({ icon, as, ...props }, ref) => {
1313
const { isOpen } = useMenuContext();
1414

1515
const containerSize = isOpen
@@ -35,8 +35,11 @@ export function HeaderMenuButton({ icon }: HeaderMenuButtonProps) {
3535
margin: isOpen ? `${OPEN_MARGIN_IN_PIXEL}px` : "0px",
3636
borderRadius: "2px",
3737
}}
38+
as={as}
39+
ref={ref}
40+
{...props}
3841
>
3942
<Icon w={6} height={6} as={isOpen ? XIcon : icon} color="white" />
4043
</MenuButton>
4144
);
42-
}
45+
})
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { MenuItem, type MenuItemProps } from "@chakra-ui/react";
1+
import { MenuItem, type MenuItemProps, forwardRef } from "@chakra-ui/react";
22

33
export type HeaderMenuContentItemProps = MenuItemProps & {};
44

5-
export function HeaderMenuContentItem({ children, ...props }: HeaderMenuContentItemProps) {
5+
export const HeaderMenuContentItem = forwardRef<HeaderMenuContentItemProps, 'button'>(({ children, ...props }, ref) => {
66
return (
77
<MenuItem
88
backgroundColor="black"
@@ -12,9 +12,10 @@ export function HeaderMenuContentItem({ children, ...props }: HeaderMenuContentI
1212
_focusVisible={{
1313
backgroundColor: "surface.800",
1414
}}
15+
ref={ref}
1516
{...props}
1617
>
1718
{children}
1819
</MenuItem>
1920
);
20-
}
21+
})

src/modules/Sidebar/SidebarListItem.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Box, ListItem, type BoxProps } from "@chakra-ui/react";
1+
import { Box, ListItem, type BoxProps, forwardRef } from "@chakra-ui/react";
2+
23

34
export type SidebarListItemProps = BoxProps & {
45
/**
@@ -7,11 +8,7 @@ export type SidebarListItemProps = BoxProps & {
78
isActive?: boolean;
89
};
910

10-
export function SidebarListItem({
11-
children,
12-
isActive,
13-
...props
14-
}: SidebarListItemProps) {
11+
export const SidebarListItem = forwardRef<SidebarListItemProps, 'button'>(({ children, isActive, ...props }, ref) => {
1512
return (
1613
<ListItem>
1714
<Box
@@ -30,10 +27,11 @@ export function SidebarListItem({
3027
backgroundColor: "surface.200",
3128
}}
3229
w="100%"
30+
ref={ref}
3331
{...props}
3432
>
3533
{children}
3634
</Box>
3735
</ListItem>
3836
);
39-
}
37+
})

0 commit comments

Comments
 (0)