From e181c3e17b1a90894acc08d64e30783ed0a43eb6 Mon Sep 17 00:00:00 2001 From: Peter Marosi Date: Wed, 9 Oct 2019 13:37:19 +0200 Subject: [PATCH] feat(createPortal): extend functionality with createPortal --- src/SnackbarProvider.js | 50 +++++++++++++++++++++++------------------ src/index.d.ts | 1 + 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/SnackbarProvider.js b/src/SnackbarProvider.js index e910a07b..a4a1e82f 100644 --- a/src/SnackbarProvider.js +++ b/src/SnackbarProvider.js @@ -1,4 +1,5 @@ import React, { Component } from 'react'; +import { createPortal } from 'react-dom'; import PropTypes from 'prop-types'; import Slide from '@material-ui/core/Slide'; import SnackbarContext from './SnackbarContext'; @@ -181,7 +182,7 @@ class SnackbarProvider extends Component { }; render() { - const { classes, children, maxSnack, dense, ...props } = this.props; + const { classes, children, maxSnack, dense, domNode, ...props } = this.props; const { contextValue } = this.state; const categ = this.state.snacks.reduce((acc, current) => { @@ -194,31 +195,32 @@ class SnackbarProvider extends Component { }, {}); const iconVariant = Object.assign({ ...defaultIconVariant }, { ...this.props.iconVariant }); + const notistackElements = Object.entries(categ).map(([origin, snacks]) => ( + + {snacks.map(snack => ( + + ))} + + )); return ( {children} - {Object.entries(categ).map(([origin, snacks]) => ( - - {snacks.map(snack => ( - - ))} - - ))} + {domNode ? createPortal(notistackElements, domNode) : notistackElements} ); } @@ -348,6 +350,10 @@ SnackbarProvider.propTypes = { PropTypes.number, PropTypes.shape({ enter: PropTypes.number, exit: PropTypes.number }), ]), + /** + * Valid and exist HTML Node element, used to target `React.createPortal` + */ + domNode: PropTypes.instanceOf(Element), }; SnackbarProvider.defaultProps = { diff --git a/src/index.d.ts b/src/index.d.ts index f3dd092d..a2e24990 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -47,6 +47,7 @@ export interface SnackbarProviderProps extends Omit React.ReactNode); + domNode: HTMLElement; } export const SnackbarProvider: React.ComponentType;