Skip to content

Commit

Permalink
feat(toast): add portal
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrsavk committed Jan 21, 2021
1 parent 9fed95d commit 439148d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/toast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@alfalab/core-components-popover": "^1.4.0",
"@alfalab/core-components-portal": "^1.4.0",
"@alfalab/core-components-toast-plate": "^1.0.0",
"@alfalab/hooks": "^1.1.1",
"classnames": "^2.2.6"
Expand Down
40 changes: 29 additions & 11 deletions packages/toast/src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import mergeRefs from 'react-merge-refs';
import { useClickOutside, usePrevious } from '@alfalab/hooks';
import { ToastPlate, ToastPlateProps } from '@alfalab/core-components-toast-plate';
import { Popover, PopoverProps } from '@alfalab/core-components-popover';
import { Portal } from '@alfalab/core-components-portal';

import styles from './index.module.css';

export type ToastProps = ToastPlateProps &
Pick<PopoverProps, 'anchorElement' | 'position' | 'offset' | 'open'> & {
Pick<PopoverProps, 'anchorElement' | 'position' | 'offset' | 'open' | 'getPortalContainer'> & {
/**
* Через сколько исчезнет компонент (ms)
*/
Expand All @@ -21,7 +22,16 @@ export type ToastProps = ToastPlateProps &

export const Toast = forwardRef<HTMLDivElement, ToastProps>(
(
{ anchorElement, position, offset, open, onClose, autoCloseDelay = 3000, ...restProps },
{
anchorElement,
position,
offset,
open,
autoCloseDelay = 3000,
onClose,
getPortalContainer,
...restProps
},
ref,
) => {
const plateRef = useRef<HTMLDivElement>(null);
Expand All @@ -45,17 +55,25 @@ export const Toast = forwardRef<HTMLDivElement, ToastProps>(
};
}, [autoCloseDelay, open, prevOpen, onClose]);

if (anchorElement) {
return (
<Popover
open={open}
anchorElement={anchorElement}
position={position}
offset={offset}
popperClassName={styles.popover}
transition={{ timeout: 150 }}
>
<ToastPlate {...restProps} ref={mergeRefs([ref, plateRef])} onClose={onClose} />
</Popover>
);
}

return (
<Popover
open={open}
anchorElement={anchorElement}
position={position}
offset={offset}
popperClassName={styles.popover}
transition={{ timeout: 150 }}
>
<Portal getPortalContainer={getPortalContainer}>
<ToastPlate {...restProps} ref={mergeRefs([ref, plateRef])} onClose={onClose} />
</Popover>
</Portal>
);
},
);
21 changes: 0 additions & 21 deletions packages/toast/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,4 @@
border: none;
border-radius: var(--toast-plate-border-radius);
transition-timing-function: ease-in-out;
transition-property: opacity, transform;

&.entering {
opacity: 0;
transform: scale(0.98);
}

&.entered {
opacity: 1;
transform: scale(1);
}

&.exiting {
opacity: 0;
transform: scale(0.98);
}

&.exited {
opacity: 0;
transform: scale(0.98);
}
}
2 changes: 1 addition & 1 deletion packages/toast/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"@alfalab/core-components-*": ["../*/src"]
}
},
"references": [{ "path": "../toast-plate" }, { "path": "../popover" }]
"references": [{ "path": "../toast-plate" }, { "path": "../popover" }, { "path": "../portal" }]
}

0 comments on commit 439148d

Please sign in to comment.