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

feat: offset support #27

Merged
merged 3 commits into from
Sep 7, 2024
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
1 change: 1 addition & 0 deletions docs/docs/Toaster.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ You can provide default styles for all toasts by passing `style` and `className`
| theme | `light`, `dark` | `light` |
| visibleToasts | Maximum number of visible toasts | `3` |
| position | Place where the toasts will be rendered | `top-center` |
| offset | Offset from the top or bottom | `0` |
| closeButton | Adds a close button to all toasts | `false` |
| invert | Dark toasts in light mode and vice versa. | `false` |
| toastOptions | These will act as default options for all toasts. See [toast()](/toast) for all available options. | `{}` |
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/sonner.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
| dir | ❌ | ✅ |
| richColors | ❌ | ✅ |
| expand | 🕸️ | ✅ |
| offset | | ✅ |
| offset | | ✅ |
| hotkey | 🕸️ | ✅ |
| loadingIcon | ❌ | ✅ |
| pauseWhenPageIsHidden | ❌ | ✅ |
Expand Down
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const App: React.FC = () => {
</NavigationContainer>
<Toaster
position="top-center"
// offset={100}
duration={3000}
swipToDismissDirection="up"
visibleToasts={4}
Expand Down
25 changes: 17 additions & 8 deletions src/animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,42 @@ import type { ToastPosition } from './types';
export const ANIMATION_DURATION = 300;

export const useToastLayoutAnimations = () => {
const { position } = useToastContext();
const { position, offset } = useToastContext();

return React.useMemo(
() => ({
entering: () => {
'worklet';
return getToastEntering({ position });
return getToastEntering({ position, offset });
},
exiting: () => {
'worklet';
return getToastExiting({ position });
return getToastExiting({ position, offset });
},
}),
[position]
[offset, position]
);
};

type GetToastAnimationParams = {
position: ToastPosition;
offset: number;
};

export const getToastEntering = ({ position }: GetToastAnimationParams) => {
export const getToastEntering = ({
position,
offset,
}: GetToastAnimationParams) => {
'worklet';

const animations = {
opacity: withTiming(1, { easing: easeOutCirc }),
transform: [
{ scale: withTiming(1, { easing: easeOutCirc }) },
{
translateY: withTiming(0, { easing: easeOutCirc }),
translateY: withTiming(position === 'top-center' ? offset : -offset, {
easing: easeOutCirc,
}),
},
],
};
Expand All @@ -57,7 +63,10 @@ export const getToastEntering = ({ position }: GetToastAnimationParams) => {
};
};

export const getToastExiting = ({ position }: GetToastAnimationParams) => {
export const getToastExiting = ({
position,
offset,
}: GetToastAnimationParams) => {
'worklet';

const animations = {
Expand All @@ -75,7 +84,7 @@ export const getToastExiting = ({ position }: GetToastAnimationParams) => {
opacity: 1,
transform: [
{
translateY: 0,
translateY: position === 'top-center' ? offset : -offset,
},
],
};
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ToastPosition, ToastSwipeDirection, ToastVariant } from './types';
export const toastDefaultValues: {
duration: number;
position: ToastPosition;
offset: number;
swipeToDismissDirection: ToastSwipeDirection;
variant: ToastVariant;
visibleToasts: number;
Expand All @@ -13,6 +14,7 @@ export const toastDefaultValues: {
} = {
duration: 4000,
position: 'top-center',
offset: 0,
swipeToDismissDirection: 'up',
variant: 'info',
visibleToasts: 3,
Expand Down
3 changes: 3 additions & 0 deletions src/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const Toaster: React.FC<ToasterProps> = (props) => {
export const ToasterUI: React.FC<ToasterProps> = ({
duration = toastDefaultValues.duration,
position = toastDefaultValues.position,
offset = toastDefaultValues.offset,
visibleToasts = toastDefaultValues.visibleToasts,
swipToDismissDirection = toastDefaultValues.swipeToDismissDirection,
closeButton,
Expand Down Expand Up @@ -136,6 +137,7 @@ export const ToasterUI: React.FC<ToasterProps> = ({
() => ({
duration: duration ?? toastDefaultValues.duration,
position: position ?? toastDefaultValues.position,
offset: offset ?? toastDefaultValues.offset,
swipToDismissDirection:
swipToDismissDirection ?? toastDefaultValues.swipeToDismissDirection,
closeButton: closeButton ?? toastDefaultValues.closeButton,
Expand All @@ -149,6 +151,7 @@ export const ToasterUI: React.FC<ToasterProps> = ({
[
duration,
position,
offset,
swipToDismissDirection,
closeButton,
unstyled,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export type ToasterContextType = Required<
| 'styles'
| 'classNames'
| 'icons'
| 'offset'
>
> & {
addToast: AddToastContextHandler;
Expand Down
Loading