Skip to content

An opinionated toast component for React.

Fix tests (#131)

Emil Kowalskid36ffd4aa9f8
diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml
index 55b5b2b..ee48659 100644
--- a/.github/workflows/playwright.yml
+++ b/.github/workflows/playwright.yml
@@ -14,7 +14,7 @@ jobs:
with:
node-version: 16
- run: npm install pnpm -g
- - run: pnpm install
+ - run: pnpm install --no-frozen-lockfile
- run: pnpm build
- run: pnpm playwright install --with-deps
- run: pnpm test || exit 1
diff --git a/package.json b/package.json
index 335c261..c62803e 100644
--- a/package.json
+++ b/package.json
@@ -14,7 +14,7 @@
"dev:website": "turbo run dev --filter=website...",
"dev:test": "turbo run dev --filter=test...",
"format": "prettier --write .",
- "test": "playwright test "
+ "test": "playwright test"
},
"keywords": [
"react",
@@ -44,7 +44,6 @@
"turbo": "1.6",
"typescript": "^4.8.4"
},
- "type": "module",
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
diff --git a/src/index.tsx b/src/index.tsx
index de6349d..cb41632 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -78,6 +78,7 @@ const Toast = (props: ToastProps) => {
const isFront = index === 0;
const isVisible = index + 1 <= visibleToasts;
const toastType = toast.type;
+ const dismissible = toast.dismissible !== false;
const toastClassname = toast.className || '';
const toastDescriptionClassname = toast.descriptionClassName || '';
@@ -220,6 +221,7 @@ const Toast = (props: ToastProps) => {
data-index={index}
data-front={isFront}
data-swiping={swiping}
+ data-dismissible={dismissible}
data-type={toastType}
data-invert={invert}
data-swipe-out={swipeOut}
@@ -236,7 +238,7 @@ const Toast = (props: ToastProps) => {
} as React.CSSProperties
}
onPointerDown={(event) => {
- if (disabled) return;
+ if (disabled || !dismissible) return;
dragStartTime.current = new Date();
setOffsetBeforeRemove(offset.current);
// Ensure we maintain correct pointer capture even when going outside of the toast (e.g. when swiping)
@@ -246,7 +248,7 @@ const Toast = (props: ToastProps) => {
pointerStartRef.current = { x: event.clientX, y: event.clientY };
}}
onPointerUp={() => {
- if (swipeOut) return;
+ if (swipeOut || !dismissible) return;
pointerStartRef.current = null;
const swipeAmount = Number(toastRef.current?.style.getPropertyValue('--swipe-amount').replace('px', '') || 0);
@@ -266,7 +268,7 @@ const Toast = (props: ToastProps) => {
setSwiping(false);
}}
onPointerMove={(event) => {
- if (!pointerStartRef.current) return;
+ if (!pointerStartRef.current || !dismissible) return;
const yPosition = event.clientY - pointerStartRef.current.y;
const xPosition = event.clientX - pointerStartRef.current.x;
@@ -291,7 +293,7 @@ const Toast = (props: ToastProps) => {
data-disabled={disabled}
data-close-button
onClick={
- disabled
+ disabled || !dismissible
? undefined
: () => {
deleteToast();
@@ -339,6 +341,7 @@ const Toast = (props: ToastProps) => {
data-button
data-cancel
onClick={() => {
+ if (!dismissible) return;
deleteToast();
if (toast.cancel?.onClick) {
toast.cancel.onClick();
@@ -518,6 +521,7 @@ const Toaster = (props: ToasterProps) => {
}, [listRef.current]);
if (!toasts.length) return null;
+ console.log({ expanded, interacting });
return (
// Remove item from normal navigation flow, only available via hotkey
@@ -551,6 +555,10 @@ const Toaster = (props: ToasterProps) => {
}
}}
onFocus={(event) => {
+ const isNotDismissible = event.target instanceof HTMLElement && event.target.dataset.dismissible === 'false';
+
+ if (isNotDismissible) return;
+
if (!isFocusWithinRef.current) {
isFocusWithinRef.current = true;
lastFocusedElementRef.current = event.relatedTarget as HTMLElement;
@@ -564,7 +572,10 @@ const Toaster = (props: ToasterProps) => {
setExpanded(false);
}
}}
- onPointerDown={() => {
+ onPointerDown={(event) => {
+ const isNotDismissible = event.target instanceof HTMLElement && event.target.dataset.dismissible === 'false';
+
+ if (isNotDismissible) return;
setInteracting(true);
}}
onPointerUp={() => setInteracting(false)}
diff --git a/src/state.ts b/src/state.ts
index 0099cf0..beea8e7 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -37,18 +37,19 @@ class Observer {
const alreadyExists = this.toasts.find((toast) => {
return toast.id === id;
});
+ const dismissible = data.dismissible === undefined ? true : data.dismissible;
if (alreadyExists) {
this.toasts = this.toasts.map((toast) => {
if (toast.id === id) {
this.publish({ ...toast, ...data, id, title: message });
- return { ...toast, ...data, id, title: message };
+ return { ...toast, ...data, id, dismissible, title: message };
}
return toast;
});
} else {
- this.addToast({ title: message, ...rest, id });
+ this.addToast({ title: message, ...rest, dismissible, id });
}
return id;
diff --git a/src/types.ts b/src/types.ts
index f603b03..5dbf965 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -18,6 +18,7 @@ export interface ToastT {
icon?: React.ReactNode;
jsx?: React.ReactNode;
invert?: boolean;
+ dismissible?: boolean;
description?: React.ReactNode;
duration?: number;
delete?: boolean;
diff --git a/test/src/app/page.tsx b/test/src/app/page.tsx
index 648b3bc..1ece9eb 100644
--- a/test/src/app/page.tsx
+++ b/test/src/app/page.tsx
@@ -115,6 +115,17 @@ export default function Home({ searchParams }: any) {
>
Render Toast With onAutoClose callback
</button>
+ <button
+ data-testid="non-dismissible-toast"
+ className="button"
+ onClick={() =>
+ toast('My Toast', {
+ dismissible: false,
+ })
+ }
+ >
+ Non-dismissible Toast
+ </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'} />
diff --git a/test/tests/basic.spec.ts b/test/tests/basic.spec.ts
index d84fbf6..688689a 100644
--- a/test/tests/basic.spec.ts
+++ b/test/tests/basic.spec.ts
@@ -42,6 +42,21 @@ test.describe('Basic functionality', () => {
await expect(page.locator('[data-sonner-toast]')).toHaveCount(0);
});
+ test('dismissible toast is not removed when dragged', async ({ page }) => {
+ await page.getByTestId('non-dismissible-toast').click();
+ const toast = page.locator('[data-sonner-toast]');
+ const dragBoundingBox = await toast.boundingBox();
+
+ if (!dragBoundingBox) return;
+ await page.mouse.move(dragBoundingBox.x + dragBoundingBox.width / 2, dragBoundingBox.y);
+
+ await page.mouse.down();
+ await page.mouse.move(0, dragBoundingBox.y + 300);
+
+ await page.mouse.up();
+ await expect(page.getByTestId('non-dismissible-toast')).toHaveCount(1);
+ });
+
test('toast is removed after swiping up', async ({ page }) => {
await page.goto('/?position=top-left');
await page.getByTestId('default-button').click();
diff --git a/website/src/components/CodeBlock/code-block.module.css b/website/src/components/CodeBlock/code-block.module.css
index 66e0b00..efb57e2 100644
--- a/website/src/components/CodeBlock/code-block.module.css
+++ b/website/src/components/CodeBlock/code-block.module.css
@@ -37,6 +37,10 @@
transition: background 200ms, box-shadow 200ms, opacity 200ms;
}
+.copyButton:focus-visible {
+ opacity: 1;
+}
+
.copyButton:hover {
background: var(--gray1);
}
diff --git a/website/src/components/Hero/index.tsx b/website/src/components/Hero/index.tsx
index 28c77b9..5fe997e 100644
--- a/website/src/components/Hero/index.tsx
+++ b/website/src/components/Hero/index.tsx
@@ -18,6 +18,7 @@ export const Hero = () => {
onClick={() => {
toast('Sonner', {
description: 'An opinionated toast component for React.',
+ dismissible: false,
});
}}
className={styles.button}