Skip to content

Commit b644c3d

Browse files
committed
feat: toast position and docs
1 parent b9cff3b commit b644c3d

4 files changed

Lines changed: 134 additions & 16 deletions

File tree

src/components/Toast/Toast.mdx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Paramount exposes a convenience hook `useToast` you can use to create toasts fro
3434
notify({
3535
title: 'Title',
3636
description: 'Description',
37+
duration: 100000,
3738
})
3839
}
3940
title="Open toast"
@@ -43,6 +44,38 @@ Paramount exposes a convenience hook `useToast` you can use to create toasts fro
4344
</ToastProvider>
4445
</Playground>
4546

46-
## Options
47+
## ToastProvider Props
48+
49+
_Note_: In order for the `position="bottom"` prop to match the `offset` exactly, try to keep the title and message short. This is due to derived height of the `Toast` being "hard-coded" when `position="bottom"`
50+
51+
<Props of={ToastProvider} />
52+
53+
## Toast Options
4754

4855
<Props of={Toast} />
56+
57+
## Customization
58+
59+
Use `getStyles` prop on **ToastProvider**
60+
61+
```tsx
62+
ToastStyles {
63+
containerStyle: ViewStyle;
64+
wrapperStyle: ViewStyle;
65+
}
66+
67+
getStyles={(ToastProviderProps, Theme) => ToastProviderStyles}
68+
```
69+
70+
Markup
71+
72+
```tsx
73+
<>
74+
{children}
75+
<View containerStyle>
76+
<View wrapperStyle>
77+
<Alert />
78+
</View>
79+
</View>
80+
</>
81+
```

src/components/Toast/Toast.styles.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { Platform, ViewStyle } from 'react-native';
22

33
import { Theme } from '../../theme/Theme';
4+
import { ToastProviderProps } from './ToastProvider';
45

56
export interface ToastStyles {
67
containerStyle: ViewStyle;
78
wrapperStyle: ViewStyle;
89
}
9-
export type GetToastStyles = (props: {}, theme: Theme) => ToastStyles;
10+
11+
export type GetToastStyles = (
12+
props: ToastProviderProps,
13+
theme: Theme,
14+
) => ToastStyles;
1015

1116
// @ts-ignore: Compat with web
1217
export const getToastStyles: GetToastStyles = (props, theme) => {
18+
const { position = 'top' } = props;
19+
1320
return {
1421
containerStyle: {
1522
left: 32,
@@ -21,7 +28,13 @@ export const getToastStyles: GetToastStyles = (props, theme) => {
2128
// @ts-ignore: Compat with web
2229
position: Platform.OS === 'web' ? 'fixed' : 'absolute',
2330
right: 32,
24-
top: 10,
31+
...(position === 'top'
32+
? {
33+
top: 0,
34+
}
35+
: {
36+
bottom: 68,
37+
}),
2538
zIndex: 100,
2639
},
2740
wrapperStyle: {

src/components/Toast/ToastProvider.tsx

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,22 @@ import { ToastContext } from './ToastContext';
1212

1313
const AnimatedView = animated(View);
1414

15+
export type ToastPosition = 'top' | 'bottom';
16+
1517
export interface ToastProviderProps {
16-
children?: React.ReactNode;
18+
/**
19+
* Position from which to display the toast from
20+
* @default top
21+
*/
22+
position?: ToastPosition;
23+
24+
/**
25+
* Offset toast from the edge of the container (screen).
26+
* @default 16
27+
*/
28+
offset?: number;
29+
30+
/** Callback to get element styles. */
1731
getStyles?: ReplaceReturnType<GetToastStyles, DeepPartial<ToastStyles>>;
1832
}
1933

@@ -49,19 +63,43 @@ const reducer = (state: ToastProviderState, action: Action) => {
4963
}
5064
};
5165

52-
export const ToastProvider = (props: ToastProviderProps) => {
53-
const { children, getStyles } = props;
66+
const getTransitionConfig = (offset: number, position: ToastPosition) => {
67+
if (position === 'top') {
68+
return {
69+
config: springDefaultConfig,
70+
71+
enter: { translateY: offset },
72+
from: { translateY: -500 },
73+
leave: { translateY: -500 },
74+
};
75+
}
76+
77+
return {
78+
config: springDefaultConfig,
79+
80+
enter: { translateY: -offset },
81+
from: { translateY: 500 },
82+
leave: { translateY: 500 },
83+
};
84+
};
85+
86+
export const ToastProvider: React.FunctionComponent<
87+
ToastProviderProps
88+
> = props => {
89+
const { children, offset = 16, position = 'top', getStyles } = props;
5490
const idCounterRef = React.useRef(0);
5591
// Use reducer because we want access previous value of state
5692
const [state, dispatch] = React.useReducer(reducer, initialState);
5793

5894
const theme = useTheme();
5995

96+
console.log(position, 'position');
97+
6098
const { containerStyle, wrapperStyle } = mergeStyles(
6199
getToastStyles,
62100
getStyles,
63101
theme.components.getToastStyles,
64-
)({}, theme);
102+
)({ offset, position }, theme);
65103

66104
const createToastInstance = (toastSettings: ToastSettings): ToastInstance => {
67105
const uniqueId = ++idCounterRef.current;
@@ -98,13 +136,11 @@ export const ToastProvider = (props: ToastProviderProps) => {
98136
return toastInstance;
99137
}, []);
100138

101-
const transitions = useTransition(state.toasts, toast => toast.id, {
102-
config: springDefaultConfig,
103-
104-
enter: { translateY: 10 },
105-
from: { translateY: -500 },
106-
leave: { translateY: -500 },
107-
});
139+
const transitions = useTransition(
140+
state.toasts,
141+
toast => toast.id,
142+
getTransitionConfig(offset, position),
143+
);
108144

109145
return (
110146
<ToastContext.Provider

tests/__snapshots__/snapshot.test.js.snap

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32619,7 +32619,7 @@ exports[`KitchenSink_1 1`] = `
3261932619
"maxWidth": "560px",
3262032620
"position": "fixed",
3262132621
"right": "32px",
32622-
"top": "10px",
32622+
"top": "0px",
3262332623
"zIndex": 100,
3262432624
}
3262532625
}
@@ -40333,7 +40333,7 @@ exports[`Toast_2 1`] = `
4033340333
"maxWidth": "560px",
4033440334
"position": "fixed",
4033540335
"right": "32px",
40336-
"top": "10px",
40336+
"top": "0px",
4033740337
"zIndex": 100,
4033840338
}
4033940339
}
@@ -40343,6 +40343,42 @@ exports[`Toast_2 1`] = `
4034340343

4034440344
exports[`Toast_3 1`] = `<div />`;
4034540345

40346+
exports[`Toast_4 1`] = `<div />`;
40347+
40348+
exports[`Toast_5 1`] = `
40349+
<pre>
40350+
<code
40351+
className="language-tsx"
40352+
>
40353+
ToastStyles {
40354+
containerStyle: ViewStyle;
40355+
wrapperStyle: ViewStyle;
40356+
}
40357+
40358+
getStyles={(ToastProviderProps, Theme) =&gt; ToastProviderStyles}
40359+
40360+
</code>
40361+
</pre>
40362+
`;
40363+
40364+
exports[`Toast_6 1`] = `
40365+
<pre>
40366+
<code
40367+
className="language-tsx"
40368+
>
40369+
&lt;&gt;
40370+
{children}
40371+
&lt;View containerStyle&gt;
40372+
&lt;View wrapperStyle&gt;
40373+
&lt;Alert /&gt;
40374+
&lt;/View&gt;
40375+
&lt;/View&gt;
40376+
&lt;/&gt;
40377+
40378+
</code>
40379+
</pre>
40380+
`;
40381+
4034640382
exports[`Visible_0 1`] = `
4034740383
<h1>
4034840384
Visible

0 commit comments

Comments
 (0)