Skip to content

Commit

Permalink
feat: add close param
Browse files Browse the repository at this point in the history
  • Loading branch information
neroli committed Oct 30, 2023
1 parent 0712c4b commit c5a3e65
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
70 changes: 38 additions & 32 deletions packages/vibrant-components/src/lib/Toast/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,46 @@ import { Body } from '../Body';
import { HStack } from '../HStack';
import { Paper } from '../Paper';
import { Pressable } from '../Pressable';
import { useToastAction } from '../ToastProvider';
import { VStack } from '../VStack';
import { withToastVariation } from './ToastProps';

export const Toast = withToastVariation(
({ innerRef, IconComponent, color, title, buttonText, onButtonClick, testId = 'toast', ...restProps }) => (
<HStack ref={innerRef} {...restProps} px={20} width="100%" alignHorizontal="center" data-testid={testId}>
<Paper
elevationLevel={1}
rounded="sm"
backgroundColor="inverseSurface"
maxWidth={816}
pointerEvents="auto"
width={['100%', 'auto']}
>
<HStack px={16} py={12} alignVertical="center" width={['100%', 'auto']}>
{IconComponent && (
<VStack mr={8} flexShrink={0}>
<IconComponent size={18} fill={color} />
</VStack>
)}
<Body level={2} wordBreak="keep-all" color="onInverseSurface">
{title}
</Body>
{isDefined(buttonText) && onButtonClick && (
<VStack flexShrink={0} ml="auto">
<Pressable interactions={['focus', 'active']} ml={12} onClick={onButtonClick}>
<Body level={2} color="onViewInformative" weight="medium">
{buttonText}
</Body>
</Pressable>
</VStack>
)}
</HStack>
</Paper>
</HStack>
)
({ innerRef, IconComponent, color, title, buttonText, onButtonClick, testId = 'toast', ...restProps }) => {
const { closeToast } = useToastAction();
const onCloseButtonClick = () => onButtonClick?.({ close: closeToast });

return (
<HStack ref={innerRef} {...restProps} px={20} width="100%" alignHorizontal="center" data-testid={testId}>
<Paper
elevationLevel={1}
rounded="sm"
backgroundColor="inverseSurface"
maxWidth={816}
pointerEvents="auto"
width={['100%', 'auto']}
>
<HStack px={16} py={12} alignVertical="center" width={['100%', 'auto']}>
{IconComponent && (
<VStack mr={8} flexShrink={0}>
<IconComponent size={18} fill={color} />
</VStack>
)}
<Body level={2} wordBreak="keep-all" color="onInverseSurface">
{title}
</Body>
{isDefined(buttonText) && onButtonClick && (
<VStack flexShrink={0} ml="auto">
<Pressable interactions={['focus', 'active']} ml={12} onClick={onCloseButtonClick}>
<Body level={2} color="onViewInformative" weight="medium">
{buttonText}
</Body>
</Pressable>
</VStack>
)}
</HStack>
</Paper>
</HStack>
);
}
);
2 changes: 1 addition & 1 deletion packages/vibrant-components/src/lib/Toast/ToastProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ToastProps = {
} & (
| {
buttonText: string;
onButtonClick: () => void;
onButtonClick: (_: { close: () => void }) => void;
}
| {
buttonText?: never;
Expand Down

0 comments on commit c5a3e65

Please sign in to comment.