An opinionated toast component for React.
feat: Add RTL support (#110)
Abdulelahd803cf564476
diff --git a/README.md b/README.mdindex 95b4eb1..d1941a6 100644--- a/README.md+++ b/README.md@@ -153,6 +153,16 @@ You can change the position through the `position` prop on the `<Toaster />` com<Toaster position="top-center" />```+### Direction++You can change the direction through the `dir` prop on the `<Toaster />` component. Default is `ltr`.++```jsx+// rtl, ltr, auto++<Toaster dir="rtl" />+```+### ExpandedToasts can also be expanded by default through the `expand` prop. You can also change the amount of visible toasts which is 3 by default.diff --git a/src/index.tsx b/src/index.tsxindex c900e38..de56250 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -362,6 +362,18 @@ const Toast = (props: ToastProps) => {);};+function getDocumentDirection(): ToasterProps['dir'] {+ if (typeof window === 'undefined') return 'ltr';++ const dirAttribute = document.documentElement.getAttribute('dir');++ if (dirAttribute === 'auto' || !dirAttribute) {+ return window.getComputedStyle(document.documentElement).direction as ToasterProps['dir'];+ }++ return dirAttribute as ToasterProps['dir'];+}+const Toaster = (props: ToasterProps) => {const {invert,@@ -377,6 +389,7 @@ const Toaster = (props: ToasterProps) => {style,visibleToasts = VISIBLE_TOASTS_AMOUNT,toastOptions,+ dir = getDocumentDirection(),} = props;const [toasts, setToasts] = React.useState<ToastT[]>([]);const [heights, setHeights] = React.useState<HeightT[]>([]);@@ -495,6 +508,7 @@ const Toaster = (props: ToasterProps) => {// Remove item from normal navigation flow, only available via hotkey<section aria-label={`Notifications ${hotkeyLabel}`} tabIndex={-1}><ol+ dir={dir === 'auto' ? getDocumentDirection() : dir}tabIndex={-1}ref={listRef}className={className}diff --git a/src/styles.css b/src/styles.cssindex e5e1935..8a1a996 100644--- a/src/styles.css+++ b/src/styles.css@@ -1,3 +1,29 @@+html[dir='ltr'],+[data-sonner-toaster][dir='ltr'] {+ --toast-icon-margin-start: -3px;+ --toast-icon-margin-end: 4px;+ --toast-svg-margin-start: -1px;+ --toast-svg-margin-end: 0px;+ --toast-button-margin-start: auto;+ --toast-button-margin-end: 0;+ --toast-close-button-start: 0;+ --toast-close-button-end: unset;+ --toast-close-button-transform: translate(-35%, -35%);+}++html[dir='rtl'],+[data-sonner-toaster][dir='rtl'] {+ --toast-icon-margin-start: 4px;+ --toast-icon-margin-end: -3px;+ --toast-svg-margin-start: 0px;+ --toast-svg-margin-end: -1px;+ --toast-button-margin-start: 0;+ --toast-button-margin-end: auto;+ --toast-close-button-start: unset;+ --toast-close-button-end: 0;+ --toast-close-button-transform: translate(35%, -35%);+}+[data-sonner-toaster] {position: fixed;width: var(--width);@@ -112,8 +138,8 @@justify-content: flex-start;align-items: center;flex-shrink: 0;- margin-left: -3px;- margin-right: 4px;+ margin-left: var(--toast-icon-margin-start);+ margin-right: var(--toast-icon-margin-end);}[data-sonner-toast][data-promise='true'] [data-icon] > svg {@@ -128,7 +154,8 @@}[data-sonner-toast] [data-icon] svg {- margin-left: -1px;+ margin-left: var(--toast-svg-margin-start);+ margin-right: var(--toast-svg-margin-end);}[data-sonner-toast] [data-content] {@@ -145,7 +172,8 @@font-size: 12px;color: var(--normal-bg);background: var(--normal-text);- margin-left: auto;+ margin-left: var(--toast-button-margin-start);+ margin-right: var(--toast-button-margin-end);border: none;cursor: pointer;outline: none;@@ -157,7 +185,8 @@}[data-sonner-toast] [data-button]:first-of-type {- margin-left: auto;+ margin-left: var(--toast-button-margin-start);+ margin-right: var(--toast-button-margin-end);}[data-sonner-toast] [data-cancel] {@@ -167,7 +196,8 @@[data-sonner-toast] [data-close-button] {position: absolute;- left: 0;+ left: var(--toast-close-button-start);+ right: var(--toast-close-button-end);top: 0;height: 20px;width: 20px;@@ -178,13 +208,14 @@background: var(--gray1);color: var(--gray12);border: 1px solid var(--gray4);- transform: translate(-35%, -35%);+ transform: var(--toast-close-button-transform);border-radius: 50%;opacity: 0;cursor: pointer;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);}diff --git a/src/types.ts b/src/types.tsindex 5cf043a..f603b03 100644--- a/src/types.ts+++ b/src/types.ts@@ -64,6 +64,7 @@ export interface ToasterProps {className?: string;style?: React.CSSProperties;offset?: string | number;+ dir?: 'rtl' | 'ltr' | 'auto';}export enum SwipeStateTypes {diff --git a/test/src/app/page.tsx b/test/src/app/page.tsxindex 4bc0061..648b3bc 100644--- a/test/src/app/page.tsx+++ b/test/src/app/page.tsx@@ -117,7 +117,7 @@ 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} />+ <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.tsindex 55c19a8..d84fbf6 100644--- a/test/tests/basic.spec.ts+++ b/test/tests/basic.spec.ts@@ -121,4 +121,27 @@ test.describe('Basic functionality', () => {await expect(page.locator('[data-sonner-toast]')).toHaveCount(0);await expect(page.getByTestId('custom')).toBeFocused();});++ test("toaster's dir prop is reflected correctly", async ({ page }) => {+ await page.goto('/?dir=rtl');+ await page.getByTestId('default-button').click();+ await expect(page.locator('[data-sonner-toaster]')).toHaveAttribute('dir', 'rtl');+ });++ test("toaster respects the HTML's dir attribute", async ({ page }) => {+ await page.evaluate(() => {+ document.documentElement.setAttribute('dir', 'rtl');+ });+ await page.getByTestId('default-button').click();+ await expect(page.locator('[data-sonner-toaster]')).toHaveAttribute('dir', 'rtl');+ });++ test("toaster respects its own dir attribute over HTML's", async ({ page }) => {+ await page.goto('/?dir=ltr');+ await page.evaluate(() => {+ document.documentElement.setAttribute('dir', 'rtl');+ });+ await page.getByTestId('default-button').click();+ await expect(page.locator('[data-sonner-toaster]')).toHaveAttribute('dir', 'ltr');+ });});diff --git a/website/src/components/Other/other.module.css b/website/src/components/Other/other.module.cssindex 169ea7e..ee3fe65 100644--- a/website/src/components/Other/other.module.css+++ b/website/src/components/Other/other.module.css@@ -1,3 +1,13 @@+ol[dir='ltr'] .headlessClose {+ --headless-close-start: unset;+ --headless-close-end: 6px;+}++ol[dir='rtl'] .headlessClose {+ --headless-close-start: 6px;+ --headless-close-end: unset;+}+.headless {padding: 16px;width: 356px;@@ -32,7 +42,8 @@display: flex;justify-content: center;align-items: center;- right: 6px;+ left: var(--headless-close-start);+ right: var(--headless-close-end);color: var(--gray10);padding: 0;background: transparent;