Skip to content

Commit

Permalink
Rename container to rootComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
mpontus committed Feb 5, 2020
1 parent 4e410e0 commit a25a50e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/ModalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { ModalRoot } from "./ModalRoot";
*/
export interface ModalProviderProps {
/**
* Container component for modals that will be passed to ModalRoot
* Container component for modal nodes
*/
container?: React.ComponentType<any>;
rootComponent?: React.ComponentType<any>;

/**
* Subtree that will receive modal context
Expand All @@ -22,7 +22,10 @@ export interface ModalProviderProps {
*
* Provides modal context and renders ModalRoot.
*/
export const ModalProvider = ({ container, children }: ModalProviderProps) => {
export const ModalProvider = ({
rootComponent,
children
}: ModalProviderProps) => {
const [modals, setModals] = useState<Record<string, ModalType>>({});
const showModal = useCallback(
(key: string, modal: ModalType) =>
Expand All @@ -47,7 +50,7 @@ export const ModalProvider = ({ container, children }: ModalProviderProps) => {
<ModalContext.Provider value={contextValue}>
<React.Fragment>
{children}
<ModalRoot modals={modals} container={container} />
<ModalRoot modals={modals} component={rootComponent} />
</React.Fragment>
</ModalContext.Provider>
);
Expand Down
8 changes: 4 additions & 4 deletions src/ModalRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface ModalRootProps {
* used by defualt, specifying a different component can change the way modals
* are rendered across the whole application.
*/
container?: React.ComponentType<any>;
component?: React.ComponentType<any>;
}

/**
Expand Down Expand Up @@ -48,19 +48,19 @@ const ModalRenderer = memo(({ component, ...rest }: ModalRendererProps) =>
* Renders modals using react portal.
*/
export const ModalRoot = memo(
({ modals, container: Container = React.Fragment }: ModalRootProps) => {
({ modals, component: RootComponent = React.Fragment }: ModalRootProps) => {
const [mountNode, setMountNode] = useState<Element | undefined>(undefined);

// This effect will not be ran in the server environment
useEffect(() => setMountNode(document.body));

return mountNode
? ReactDOM.createPortal(
<Container>
<RootComponent>
{Object.keys(modals).map(key => (
<ModalRenderer key={key} component={modals[key]} />
))}
</Container>,
</RootComponent>,
document.body
)
: null;
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/ModalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ModalProvider, useModal } from "..";
import "jest-dom/extend-expect";

describe("custom container prop", () => {
const Container: React.SFC = ({ children }) => (
<div data-testid="custom-container">{children}</div>
const RootComponent: React.SFC = ({ children }) => (
<div data-testid="custom-root">{children}</div>
);

const App = () => {
Expand All @@ -14,17 +14,17 @@ describe("custom container prop", () => {
return <button onClick={showModal}>Show modal</button>;
};

it("should render modals inside custom container", () => {
it("should render modals inside custom root component", () => {
const { getByTestId, getByText } = render(
<ModalProvider container={Container}>
<ModalProvider rootComponent={RootComponent}>
<App />
</ModalProvider>
);

fireEvent.click(getByText("Show modal"));
flushEffects();

expect(getByTestId("custom-container")).toContainElement(
expect(getByTestId("custom-root")).toContainElement(
getByText("This is a modal")
);
});
Expand Down

0 comments on commit a25a50e

Please sign in to comment.