Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to dismiss a toast #90

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/components/close.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { styled } from 'goober';

export interface CloseTheme {
primary?: string;
secondary?: string;
}

export const CloseIcon = styled('div')<CloseTheme>`
width: 20px;
opacity: 1;
height: 20px;
border-radius: 10px;
background: ${(p) => p.primary || 'transparent'};;
position: relative;
transform: rotate(45deg);
transition: background-color 200ms ease-out;

&:after,
&:before {
content: '';
animation-delay: 150ms;
position: absolute;
border-radius: 3px;
opacity: 1;
background: ${(p) => p.secondary || '#000'};
bottom: 9px;
left: 4px;
height: 2px;
width: 12px;
}

&:before {
transform: rotate(90deg);
}
`;
7 changes: 6 additions & 1 deletion src/components/toast-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { styled, keyframes } from 'goober';
import { Toast, ToastPosition, resolveValue, Renderable } from '../core/types';
import { ToastIcon } from './toast-icon';
import { prefersReducedMotion } from '../core/utils';
import { CloseIcon } from "./close";

const enterAnimation = (factor: number) => `
0% {transform: translate3d(0,${factor * -200}%,0) scale(.6); opacity:.5;}
Expand Down Expand Up @@ -49,6 +50,7 @@ interface ToastBarProps {
icon: Renderable;
message: Renderable;
}) => Renderable;
onDismiss: () => void;
}

const getAnimationStyle = (
Expand All @@ -70,8 +72,9 @@ const getAnimationStyle = (
};

export const ToastBar: React.FC<ToastBarProps> = React.memo(
({ toast, position, style, children }) => {
({ toast, position, style, children, onDismiss }) => {
const animationStyle: React.CSSProperties = toast.height

? getAnimationStyle(
toast.position || position || 'top-center',
toast.visible
Expand All @@ -84,6 +87,7 @@ export const ToastBar: React.FC<ToastBarProps> = React.memo(
{resolveValue(toast.message, toast)}
</Message>
);
const closeIcon = !!toast.canDismiss ? <CloseIcon onClick={onDismiss} /> : null;

return (
<ToastBarBase
Expand All @@ -103,6 +107,7 @@ export const ToastBar: React.FC<ToastBarProps> = React.memo(
<>
{icon}
{message}
{closeIcon}
</>
)}
</ToastBarBase>
Expand Down
9 changes: 8 additions & 1 deletion src/components/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ToastPosition,
ToastWrapperProps,
} from '../core/types';
import { toast } from "../core/toast";
import { useToaster } from '../core/use-toaster';
import { prefersReducedMotion } from '../core/utils';
import { ToastBar } from './toast-bar';
Expand Down Expand Up @@ -131,7 +132,13 @@ export const Toaster: React.FC<ToasterProps> = ({
) : children ? (
children(t)
) : (
<ToastBar toast={t} position={toastPosition} />
<ToastBar
toast={t}
position={toastPosition}
onDismiss={() => {
toast.dismiss(t.id);
}}
/>
)}
</ToastWrapper>
);
Expand Down
1 change: 1 addition & 0 deletions src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const createToast = (
pauseDuration: 0,
...opts,
id: opts?.id || genId(),
canDismiss: opts?.canDismiss,
});

const createHandler =
Expand Down
2 changes: 2 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface Toast {
createdAt: number;
visible: boolean;
height?: number;
canDismiss?: boolean;
}

export type ToastOptions = Partial<
Expand All @@ -65,6 +66,7 @@ export type ToastOptions = Partial<
| 'style'
| 'position'
| 'iconTheme'
| 'canDismiss'
>
>;

Expand Down