An opinionated toast component for React.
Improve keyboard navigation (#104)
João Pedro Magalhães2f6b2b9d014f
diff --git a/src/index.tsx b/src/index.tsxindex 21a37c8..a31e555 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -395,6 +395,8 @@ const Toaster = (props: ToasterProps) => {const listRef = React.useRef<HTMLOListElement>(null);const hotkeyLabel = hotkey.join('+').replace(/Key/g, '').replace(/Digit/g, '');+ const lastFocusedElementRef = React.useRef<HTMLElement>(null);+ const isFocusWithinRef = React.useRef(false);const removeToast = React.useCallback((toast: ToastT) => setToasts((toasts) => toasts.filter(({ id }) => id !== toast.id)),@@ -475,6 +477,18 @@ const Toaster = (props: ToasterProps) => {return () => document.removeEventListener('keydown', handleKeyDown);}, [hotkey]);+ React.useEffect(() => {+ if (listRef.current) {+ return () => {+ if (lastFocusedElementRef.current) {+ lastFocusedElementRef.current.focus({ preventScroll: true });+ lastFocusedElementRef.current = null;+ isFocusWithinRef.current = false;+ }+ };+ }+ }, [listRef.current]);+if (!toasts.length) return null;return (@@ -498,6 +512,21 @@ const Toaster = (props: ToasterProps) => {...style,} as React.CSSProperties}+ onBlur={(event) => {+ if (isFocusWithinRef.current && !event.currentTarget.contains(event.relatedTarget)) {+ isFocusWithinRef.current = false;+ if (lastFocusedElementRef.current) {+ lastFocusedElementRef.current.focus({ preventScroll: true });+ lastFocusedElementRef.current = null;+ }+ }+ }}+ onFocus={(event) => {+ if (!isFocusWithinRef.current) {+ isFocusWithinRef.current = true;+ lastFocusedElementRef.current = event.relatedTarget as HTMLElement;+ }+ }}onMouseEnter={() => setExpanded(true)}onMouseMove={() => setExpanded(true)}onMouseLeave={() => {diff --git a/src/styles.css b/src/styles.cssindex 7a1c5d9..e5e1935 100644--- a/src/styles.css+++ b/src/styles.css@@ -185,6 +185,9 @@z-index: 1;transition: opacity 100ms, background 200ms, border-color 200ms;}+[data-sonner-toast] [data-close-button]:focus-visible {+ box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1), 0 0 0 2px rgba(0, 0, 0, 0.2);+}[data-sonner-toast] [data-disabled='true'] {cursor: not-allowed;@@ -193,6 +196,12 @@[data-sonner-toast]:hover [data-close-button] {opacity: 1;}+[data-sonner-toast]:focus [data-close-button] {+ opacity: 1;+}+[data-sonner-toast]:focus-within [data-close-button] {+ opacity: 1;+}[data-sonner-toast]:hover [data-close-button]:hover {background: var(--gray2);diff --git a/test/src/app/page.tsx b/test/src/app/page.tsxindex 07295ae..71edde8 100644--- a/test/src/app/page.tsx+++ b/test/src/app/page.tsx@@ -71,7 +71,9 @@ export default function Home() {toast.custom((t) => (<div><h1>jsx</h1>- <button onClick={() => toast.dismiss(t)}>Dismiss</button>+ <button data-testid="dismiss-button" onClick={() => toast.dismiss(t)}>+ Dismiss+ </button></div>))}diff --git a/test/tests/basic.spec.ts b/test/tests/basic.spec.tsindex 275d3ea..f5d5e52 100644--- a/test/tests/basic.spec.ts+++ b/test/tests/basic.spec.ts@@ -77,4 +77,14 @@ test.describe('Basic functionality', () => {await page.mouse.up();await expect(page.getByTestId('dismiss-el')).toHaveCount(1);});++ test('return focus to the previous focused element', async ({ page }) => {+ await page.getByTestId('custom').focus();+ await page.keyboard.press('Enter');+ await expect(page.locator('[data-sonner-toast]')).toHaveCount(1);+ await page.getByTestId('dismiss-button').focus();+ await page.keyboard.press('Enter');+ await expect(page.locator('[data-sonner-toast]')).toHaveCount(0);+ await expect(page.getByTestId('custom')).toBeFocused();+ });});