Skip to content

Commit 40405c7

Browse files
committed
feat: add prop "anchor"
1 parent bba5f0b commit 40405c7

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ ReactDOM.render(
2222
)
2323
```
2424

25+
#### ToastsManager props:
26+
27+
* **anchor** - values: ["left", "right"] default: "left"
28+
2529
#### Show standard info/warn/error toasts:
2630

2731
```javascript

Diff for: src/ToastsManager.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ const toLeftAnimation = keyframes`
2828
}
2929
`;
3030

31+
const toRightAnimation = keyframes`
32+
from {
33+
transform: translate(0, 0);
34+
}
35+
to {
36+
transform: translate(500px, 0);
37+
}
38+
`;
39+
3140
const Wrapper = styled.div`
3241
position: fixed;
3342
left: 16px;
@@ -51,6 +60,11 @@ const ToastContainer = styled.div`
5160
transition: transform 0.3s ease-out;
5261
transform: translate(0, -${({ bottomOffset }) => bottomOffset}px);
5362
63+
${is('isRight')`
64+
left: unset;
65+
right: 0;
66+
`};
67+
5468
${is('isInvisible')`
5569
visibility: hidden;
5670
`};
@@ -61,9 +75,7 @@ const ToastWrapper = styled.div`
6175
animation: ${fromBottomAnimation} 0.4s ease-out;
6276
`};
6377
64-
${is('isHiding')`
65-
animation: ${toLeftAnimation} 0.3s ease-in forwards;
66-
`};
78+
${({ animation }) => (animation ? `animation: ${animation} 0.3s ease-in forwards` : '')};
6779
`;
6880

6981
let instance;
@@ -112,11 +124,13 @@ export default class ToastsManager extends PureComponent {
112124

113125
static propTypes = {
114126
className: PropTypes.string,
127+
anchor: PropTypes.oneOf(['left', 'right']),
115128
renderToast: PropTypes.func,
116129
};
117130

118131
static defaultProps = {
119132
className: undefined,
133+
anchor: 'left',
120134
renderToast: undefined,
121135
};
122136

@@ -380,9 +394,12 @@ export default class ToastsManager extends PureComponent {
380394
}
381395

382396
renderToasts() {
383-
const { renderToast } = this.props;
397+
const { renderToast, anchor } = this.props;
384398
const { currentToasts, bottomOffsets } = this.state;
385399

400+
const isRight = anchor === 'right';
401+
const hideAnimation = isRight ? toRightAnimation : toLeftAnimation;
402+
386403
return currentToasts.map(({ id, type, text, renderer, isHiding }) => {
387404
const bottomOffset = bottomOffsets[id];
388405
const isOffsetCalculated = bottomOffset !== undefined;
@@ -394,9 +411,13 @@ export default class ToastsManager extends PureComponent {
394411
key={id}
395412
ref={this.toastsRefs[id]}
396413
isInvisible={!isOffsetCalculated}
414+
isRight={isRight}
397415
bottomOffset={bottomOffset}
398416
>
399-
<ToastWrapper isAppearing={isOffsetCalculated} isHiding={isHiding}>
417+
<ToastWrapper
418+
isAppearing={isOffsetCalculated}
419+
animation={isHiding ? hideAnimation : null}
420+
>
400421
{render ? (
401422
render({ type, text, onClose: () => this.onCloseClick(id) })
402423
) : (

0 commit comments

Comments
 (0)