Skip to content

Commit 1e89a4e

Browse files
authored
feat: add options to configure offsets on mobile (#526)
1 parent 24808f1 commit 1e89a4e

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const VISIBLE_TOASTS_AMOUNT = 3;
2222

2323
// Viewport padding
2424
const VIEWPORT_OFFSET = '32px';
25+
const MOBILE_VIEWPORT_X_OFFSET = '16px';
26+
const MOBILE_VIEWPORT_Y_OFFSET = '20px';
2527

2628
// Default lifetime of a toasts (in ms)
2729
const TOAST_LIFETIME = 4000;
@@ -109,7 +111,7 @@ const Toast = (props: ToastProps) => {
109111
const [y, x] = position.split('-');
110112
const toastsHeightBefore = React.useMemo(() => {
111113
return heights.reduce((prev, curr, reducerIndex) => {
112-
// Calculate offset up until current toast
114+
// Calculate offset up until current toast
113115
if (reducerIndex >= heightIndex) {
114116
return prev;
115117
}
@@ -482,6 +484,8 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re
482484
closeButton,
483485
className,
484486
offset,
487+
mobileXOffset,
488+
mobileYOffset,
485489
theme = 'light',
486490
richColors,
487491
duration,
@@ -676,6 +680,10 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re
676680
{
677681
'--front-toast-height': `${heights[0]?.height || 0}px`,
678682
'--offset': typeof offset === 'number' ? `${offset}px` : offset || VIEWPORT_OFFSET,
683+
'--mobile-x-offset':
684+
typeof mobileXOffset === 'number' ? `${mobileXOffset}px` : mobileXOffset || MOBILE_VIEWPORT_X_OFFSET,
685+
'--mobile-y-offset':
686+
typeof mobileYOffset === 'number' ? `${mobileYOffset}px` : mobileYOffset || MOBILE_VIEWPORT_Y_OFFSET,
679687
'--width': `${TOAST_WIDTH}px`,
680688
'--gap': `${gap}px`,
681689
...style,

src/styles.css

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,37 +369,36 @@
369369
@media (max-width: 600px) {
370370
[data-sonner-toaster] {
371371
position: fixed;
372-
--mobile-offset: 16px;
373-
right: var(--mobile-offset);
374-
left: var(--mobile-offset);
372+
right: var(--mobile-x-offset);
373+
left: var(--mobile-x-offset);
375374
width: 100%;
376375
}
377376

378377
[data-sonner-toaster][dir='rtl'] {
379-
left: calc(var(--mobile-offset) * -1);
378+
left: calc(var(--mobile-x-offset) * -1);
380379
}
381380

382381
[data-sonner-toaster] [data-sonner-toast] {
383382
left: 0;
384383
right: 0;
385-
width: calc(100% - var(--mobile-offset) * 2);
384+
width: calc(100% - var(--mobile-x-offset) * 2);
386385
}
387386

388387
[data-sonner-toaster][data-x-position='left'] {
389-
left: var(--mobile-offset);
388+
left: var(--mobile-x-offset);
390389
}
391390

392391
[data-sonner-toaster][data-y-position='bottom'] {
393-
bottom: 20px;
392+
bottom: max(var(--mobile-y-offset), env(safe-area-inset-bottom));
394393
}
395394

396395
[data-sonner-toaster][data-y-position='top'] {
397-
top: 20px;
396+
top: max(var(--mobile-y-offset), env(safe-area-inset-top));
398397
}
399398

400399
[data-sonner-toaster][data-x-position='center'] {
401-
left: var(--mobile-offset);
402-
right: var(--mobile-offset);
400+
left: var(--mobile-x-offset);
401+
right: var(--mobile-x-offset);
403402
transform: none;
404403
}
405404
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ export interface ToasterProps {
120120
className?: string;
121121
style?: React.CSSProperties;
122122
offset?: string | number;
123+
mobileXOffset?: string | number;
124+
mobileYOffset?: string | number;
123125
dir?: 'rtl' | 'ltr' | 'auto';
124126
/**
125127
* @deprecated Please use the `icons` prop instead:

website/src/pages/toaster.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Changes the directionality of the toast's text.
5858
| position | Place where the toasts will be rendered | `bottom-right` |
5959
| closeButton | Adds a close button to all toasts | `false` |
6060
| offset | Offset from the edges of the screen. | `32px` |
61+
| mobileXOffset | Offset from the left/right edges of the screen on mobile. | `16px` |
62+
| mobileYOffset | Offset from the top/bottom edges of the screen on mobile. | `20px` |
6163
| dir | Directionality of toast's text | `ltr` |
6264
| hotkey | Keyboard shortcut that will move focus to the toaster area. | `⌥/alt + T` |
6365
| invert | Dark toasts in light mode and vice versa. | `false` |

0 commit comments

Comments
 (0)