Skip to content

Commit 791d69b

Browse files
authored
fix: add ref support to snackbar (#499)
1 parent 776ba18 commit 791d69b

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/components/experimental/Snackbar/Snackbar.tsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ReactNode, type ReactElement } from 'react';
1+
import React, { forwardRef, ReactNode, type ReactElement } from 'react';
22
import styled from 'styled-components';
33
import { SpaceProps, LayoutProps, PositionProps, FlexboxProps } from 'styled-system';
44

@@ -32,28 +32,31 @@ const DismissButton = styled(IconButton)`
3232
padding: 0;
3333
`;
3434

35-
interface SnackbarProps extends SpaceProps, LayoutProps, PositionProps, FlexboxProps {
35+
interface SnackbarProps
36+
extends SpaceProps,
37+
LayoutProps,
38+
PositionProps,
39+
FlexboxProps,
40+
React.HTMLAttributes<HTMLDivElement> {
3641
children: ReactNode;
3742
hasDismissButton?: boolean;
3843
onDismiss?: () => void;
3944
}
4045

41-
const Snackbar = ({
42-
children,
43-
hasDismissButton = false,
44-
onDismiss = null,
45-
...restProps
46-
}: SnackbarProps): ReactElement => (
47-
<Container {...restProps}>
48-
{children}
49-
{hasDismissButton && (
50-
<DismissButton
51-
data-testid="snackbar-close-icon"
52-
Icon={() => <XCrossIcon size={24} color={getSemanticValue('inverse-on-surface')} />}
53-
onPress={onDismiss}
54-
/>
55-
)}
56-
</Container>
46+
const Snackbar = forwardRef<HTMLDivElement, SnackbarProps>(
47+
({ children, hasDismissButton = false, onDismiss = null, ...restProps }, ref): ReactElement => (
48+
<Container ref={ref} {...restProps}>
49+
{children}
50+
{hasDismissButton && (
51+
<DismissButton
52+
data-testid="snackbar-close-icon"
53+
Icon={() => <XCrossIcon size={24} color={getSemanticValue('inverse-on-surface')} />}
54+
onPress={onDismiss}
55+
aria-label="close"
56+
/>
57+
)}
58+
</Container>
59+
)
5760
);
5861

5962
export { Snackbar, SnackbarProps };

0 commit comments

Comments
 (0)