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

Styles for action and cancel buttons #214

Merged
merged 3 commits into from
Nov 5, 2023
Merged
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
8 changes: 8 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface ToastProps {
closeButton: boolean;
interacting: boolean;
style?: React.CSSProperties;
cancelButtonStyle?: React.CSSProperties;
actionButtonStyle?: React.CSSProperties;
duration?: number;
className?: string;
unstyled?: boolean;
Expand All @@ -63,6 +65,8 @@ const Toast = (props: ToastProps) => {
removeToast,
closeButton,
style,
cancelButtonStyle,
actionButtonStyle,
className = '',
descriptionClassName = '',
duration: durationFromToaster,
Expand Down Expand Up @@ -346,6 +350,7 @@ const Toast = (props: ToastProps) => {
<button
data-button
data-cancel
style={cancelButtonStyle}
onClick={() => {
if (!dismissible) return;
deleteToast();
Expand All @@ -360,6 +365,7 @@ const Toast = (props: ToastProps) => {
{toast.action ? (
<button
data-button=""
style={actionButtonStyle}
onClick={(event) => {
toast.action?.onClick(event);
if (event.defaultPrevented) return;
Expand Down Expand Up @@ -612,6 +618,8 @@ const Toaster = (props: ToasterProps) => {
interacting={interacting}
position={position}
style={toastOptions?.style}
cancelButtonStyle={toastOptions?.cancelButtonStyle}
actionButtonStyle={toastOptions?.actionButtonStyle}
removeToast={removeToast}
toasts={toasts}
heights={heights}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ interface ToastOptions {
className?: string;
descriptionClassName?: string;
style?: React.CSSProperties;
cancelButtonStyle?: React.CSSProperties;
actionButtonStyle?: React.CSSProperties;
duration?: number;
unstyled?: boolean;
}
Expand Down
28 changes: 25 additions & 3 deletions test/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ export default function Home({ searchParams }: any) {
>
Render Custom Toast
</button>
<button
data-testid="custom-cancel-button-toast"
className="button"
onClick={() =>
toast('My Custom Cancel Button', {
cancel: {
label: 'Cancel',
onClick: () => console.log('Cancel'),
},
})
}
>
Render Custom Cancel Button
</button>
<button data-testid="infinity-toast" className="button" onClick={() => toast('My Toast', { duration: Infinity })}>
Render Infinity Toast
</button>
Expand Down Expand Up @@ -132,8 +146,8 @@ export default function Home({ searchParams }: any) {
onClick={() => {
const toastId = toast('My Unupdated Toast', {
duration: 10000,
});
toast('My Updated Toast', {
});
toast('My Updated Toast', {
id: toastId,
duration: 10000,
});
Expand All @@ -143,7 +157,15 @@ export default function Home({ searchParams }: any) {
</button>
{showAutoClose ? <div data-testid="auto-close-el" /> : null}
{showDismiss ? <div data-testid="dismiss-el" /> : null}
<Toaster position={searchParams.position || 'bottom-right'} theme={theme} dir={searchParams.dir || 'auto'} />
<Toaster
position={searchParams.position || 'bottom-right'}
toastOptions={{
actionButtonStyle: { backgroundColor: 'rgb(219, 239, 255)' },
cancelButtonStyle: { backgroundColor: 'rgb(254, 226, 226)' },
}}
theme={theme}
dir={searchParams.dir || 'auto'}
/>
</>
);
}
Expand Down
16 changes: 15 additions & 1 deletion test/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test.beforeEach(async ({ page }) => {
test.describe('Basic functionality', () => {
test('toast is rendered and disappears after the default timeout', async ({ page }) => {
await page.getByTestId('default-button').click();
await expect(page.locator('[data-sonner-toast]')).toHaveCount(0);
await expect(page.locator('[data-sonner-toast]')).toHaveCount(1);
await expect(page.locator('[data-sonner-toast]')).toHaveCount(0);
});

Expand Down Expand Up @@ -165,4 +165,18 @@ test.describe('Basic functionality', () => {
await expect(page.getByText('My Unupdated Toast')).toHaveCount(0);
await expect(page.getByText('My Updated Toast')).toHaveCount(1);
});

test('cancel button is rendered with custom styles', async ({ page }) => {
await page.getByTestId('custom-cancel-button-toast').click();
const button = await page.locator('[data-cancel]');

await expect(button).toHaveCSS('background-color', 'rgb(254, 226, 226)');
});

test('action button is rendered with custom styles', async ({ page }) => {
await page.getByTestId('action').click();
const button = await page.locator('[data-button]');

await expect(button).toHaveCSS('background-color', 'rgb(219, 239, 255)');
});
});