Skip to content

Commit

Permalink
feat(createPortal): extend functionality with createPortal
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marosi committed Oct 31, 2019
1 parent 73ecdf9 commit e181c3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/SnackbarProvider.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) => {
Expand All @@ -194,31 +195,32 @@ class SnackbarProvider extends Component {
}, {});

const iconVariant = Object.assign({ ...defaultIconVariant }, { ...this.props.iconVariant });
const notistackElements = Object.entries(categ).map(([origin, snacks]) => (
<SnackbarContainer
key={origin}
dense={dense}
anchorOrigin={snacks[0].anchorOrigin}
className={classes[`containerAnchorOrigin${origin}`]}
>
{snacks.map(snack => (
<SnackbarItem
{...props}
key={snack.key}
dense={dense}
snack={snack}
iconVariant={iconVariant}
classes={getClasses(classes)}
onClose={this.handleCloseSnack}
onExited={this.handleExitedSnack}
/>
))}
</SnackbarContainer>
));

return (
<SnackbarContext.Provider value={contextValue}>
{children}
{Object.entries(categ).map(([origin, snacks]) => (
<SnackbarContainer
key={origin}
dense={dense}
anchorOrigin={snacks[0].anchorOrigin}
className={classes[`containerAnchorOrigin${origin}`]}
>
{snacks.map(snack => (
<SnackbarItem
{...props}
key={snack.key}
dense={dense}
snack={snack}
iconVariant={iconVariant}
classes={getClasses(classes)}
onClose={this.handleCloseSnack}
onExited={this.handleExitedSnack}
/>
))}
</SnackbarContainer>
))}
{domNode ? createPortal(notistackElements, domNode) : notistackElements}
</SnackbarContext.Provider>
);
}
Expand Down Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface SnackbarProviderProps extends Omit<SnackbarProps, 'open' | 'mes
preventDuplicate?: boolean;
dense?: boolean;
action?: SnackbarContentProps['action'] | ((key: OptionsObject['key']) => React.ReactNode);
domNode: HTMLElement;
}

export const SnackbarProvider: React.ComponentType<SnackbarProviderProps>;

0 comments on commit e181c3e

Please sign in to comment.