Skip to content

Commit

Permalink
#55 - Generic Modal (#68)
Browse files Browse the repository at this point in the history
* adds mobx store

* adds storage service

* modifies storage service

* modifies yarn lock

* adds modal component

* updates generic modal component

* updates generic modal component

* updates generice model styling

* updates props
  • Loading branch information
cksadhukhan authored Dec 29, 2021
1 parent 69864cb commit 6621837
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 1 deletion.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"sb": "^6.1.20",
"styled-components": "^5.2.1",
"styled-jsx": "^3.4.4",
"styled-react-modal": "^2.1.0",
"typescript": "^4.3.2",
"web-vitals": "^1.0.1"
},
Expand Down Expand Up @@ -60,7 +61,10 @@
"@types/lodash": "^4.14.172",
"@types/react": "^17.0.28",
"@types/react-dom": "^17.0.9",
"@types/react-modal": "^3.13.1",
"@types/react-router-dom": "^5.3.1",
"@types/styled-components": "^5.1.13"
"@types/react-transition-group": "^4.4.4",
"@types/styled-components": "^5.1.13",
"@types/styled-react-modal": "^1.2.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ButtonComponentProps } from "../button/button.component.props";
import { TextComponentProps } from "../text/text.component.props";

export interface GenericModalComponentProps {
/**
* to determine if modal should show
*/
show: boolean;

/**
* method to close the modal
*/
onClose?: () => void;

/**
* body of the modal
*/
children: React.ReactNode;

/**
* title of the modal
*/
title?: TextComponentProps;

/**
* button for submit
*/
onSubmit?: ButtonComponentProps;

/**
* button for cancel
*/
onCancel?: ButtonComponentProps;

/**
* hide the buttons and close button
*/
hide?: boolean;

/**
* should modal close if background is clicked
*/
noClose?: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import styled from "styled-components";
import Modal, { BaseModalBackground } from "styled-react-modal";
import { Button } from "..";

export const FadingBackground = styled(BaseModalBackground)`
opacity: ${(props) => props.opacity};
transition: all 0.3s ease-in-out;
`;

export const ModalButton = styled(Button)`
height: 4rem !important;
width: 2rem !important;
margin: 0.5rem !important;
cursor: pointer;
`;

export const StyledModal = Modal.styled`
max-width: 60rem;
display: flex;
flex-direction: column;
background-color: white;
opacity: 1;
padding: 1rem;
transition : all 0.3s ease-in-out;
position: relative;
`;

export const CloseModal = styled.h1`
position: absolute;
top: 1rem;
right: 1rem;
font-size: 2rem;
font-weight: bold;
color: ${(props) => props.theme.colors.textLight};
z-index: 999;
&:hover {
cursor: pointer;
}
`;
92 changes: 92 additions & 0 deletions src/components/primitive/generic-modal/generic-modal.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { useState } from "react";
import { ModalProvider } from "styled-react-modal";
import { GenericModalComponentProps } from "./generic-modal.component.props";
import { Box, Text } from "..";
import {
FadingBackground,
StyledModal,
CloseModal,
ModalButton,
} from "./generic-modal.component.style";

const GenericModal: React.FunctionComponent<GenericModalComponentProps> = (
props: GenericModalComponentProps
) => {
const {
show,
onClose,
title,
onSubmit,
onCancel,
children,
hide,
noClose,
} = props;

const [isOpen, setIsOpen] = useState(show);
const [opacity, setOpacity] = useState(0);

function toggleModal(e: any) {
setOpacity(0);
setIsOpen(!isOpen);
if (onClose) onClose();
}

const handleClose = () => {
if (onClose) onClose();
};

function afterOpen() {
setTimeout(() => {
setOpacity(1);
}, 100);
}

function beforeClose() {
return new Promise((resolve) => {
setOpacity(0);
setTimeout(resolve, 300);
});
}

return (
<ModalProvider backgroundComponent={FadingBackground}>
<StyledModal
isOpen={isOpen}
afterOpen={afterOpen}
beforeClose={beforeClose}
onBackgroundClick={noClose ? () => {} : toggleModal}
onEscapeKeydown={noClose ? () => {} : toggleModal}
backgroundProps={{ opacity }}
>
{!hide && <CloseModal onClick={toggleModal}>X</CloseModal>}
{!!title && (
<Box vCenter hCenter paddingTop={2}>
<Text bold {...title} />
</Box>
)}
<Box paddingVertical={2}>{children}</Box>
{!hide && (
<Box row rightAlign paddingTop={2}>
{!onCancel && (
<ModalButton
label={{ text: "Close", color: "black" }}
variant="ghost"
onClick={handleClose}
/>
)}
{!onSubmit && (
<ModalButton
label={{ text: "Continue" }}
variant="small"
onClick={handleClose}
/>
)}
</Box>
)}
</StyledModal>
</ModalProvider>
);
};

export default GenericModal;
38 changes: 38 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2985,6 +2985,13 @@
dependencies:
"@types/react" "*"

"@types/react-modal@^3.13.1":
version "3.13.1"
resolved "https://registry.yarnpkg.com/@types/react-modal/-/react-modal-3.13.1.tgz#5b9845c205fccc85d9a77966b6e16dc70a60825a"
integrity sha512-iY/gPvTDIy6Z+37l+ibmrY+GTV4KQTHcCyR5FIytm182RQS69G5ps4PH2FxtC7bAQ2QRHXMevsBgck7IQruHNg==
dependencies:
"@types/react" "*"

"@types/react-router-dom@^5.3.1":
version "5.3.2"
resolved "https://registry.yarnpkg.com/@types/react-router-dom/-/react-router-dom-5.3.2.tgz#ebd8e145cf056db5c66eb1dac63c72f52e8542ee"
Expand All @@ -3002,6 +3009,13 @@
"@types/history" "*"
"@types/react" "*"

"@types/react-transition-group@^4.4.4":
version "4.4.4"
resolved "https://registry.yarnpkg.com/@types/react-transition-group/-/react-transition-group-4.4.4.tgz#acd4cceaa2be6b757db61ed7b432e103242d163e"
integrity sha512-7gAPz7anVK5xzbeQW9wFBDg7G++aPLAFY0QaSMOou9rJZpbuI58WAuJrgu+qR92l61grlnCUe7AFX8KGahAgug==
dependencies:
"@types/react" "*"

"@types/react@*":
version "17.0.11"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.11.tgz#67fcd0ddbf5a0b083a0f94e926c7d63f3b836451"
Expand Down Expand Up @@ -3042,6 +3056,15 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==

"@types/styled-components@*":
version "5.1.18"
resolved "https://registry.yarnpkg.com/@types/styled-components/-/styled-components-5.1.18.tgz#1c361130f76a52f1cb4851c2a32aa328267e5b78"
integrity sha512-xPTYmWP7Mxk5TAD3pYsqjwA9G5fAI8e/S51QUJEl7EQD1siKCdiYXIWiH2lzoHRl+QqbQCJMcGv3YTF3OmyPdQ==
dependencies:
"@types/hoist-non-react-statics" "*"
"@types/react" "*"
csstype "^3.0.2"

"@types/styled-components@^5.1.13":
version "5.1.15"
resolved "https://registry.yarnpkg.com/@types/styled-components/-/styled-components-5.1.15.tgz#30855b40aa80b3b4e4c0e43a4af366e7c246d148"
Expand All @@ -3058,6 +3081,14 @@
dependencies:
"@types/react" "*"

"@types/styled-react-modal@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@types/styled-react-modal/-/styled-react-modal-1.2.1.tgz#178a05b482d6fa95d48bc6d2c81ff926f13f76a9"
integrity sha512-wEKR3TtryjVGsTeTYvP1cG5fm7KkxssErlDvTqh+YEB565eJnYBPGAutpJK0ejAPNTb/apn1fKsHomU80NXLew==
dependencies:
"@types/react" "*"
"@types/styled-components" "*"

"@types/tapable@*", "@types/tapable@^1.0.5":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.6.tgz#a9ca4b70a18b270ccb2bc0aaafefd1d486b7ea74"
Expand Down Expand Up @@ -12804,6 +12835,13 @@ styled-jsx@^3.4.4:
stylis "3.5.4"
stylis-rule-sheet "0.0.10"

styled-react-modal@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/styled-react-modal/-/styled-react-modal-2.1.0.tgz#524451deb2faba329f3a004d28733b3e6de27744"
integrity sha512-GgUzhU4oCCA31OFKv/rCIj0DN4FDVRirGWKH+pGsFyvlr7qxeyAsiM09J0Tss107dJ6EUhK+D7U8JzQuq06hZw==
dependencies:
prop-types "^15.7.2"

stylehacks@^4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/stylehacks/-/stylehacks-4.0.3.tgz#6718fcaf4d1e07d8a1318690881e8d96726a71d5"
Expand Down

0 comments on commit 6621837

Please sign in to comment.