-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8d2e45
commit ee87bc6
Showing
7 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './modal' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Story } from '@storybook/react/types-6-0' | ||
import { useState } from 'react' | ||
|
||
import { Modal } from './modal' | ||
import { Box } from '../box' | ||
import { Button } from '../button' | ||
import { Typography } from '../typography' | ||
|
||
export default { | ||
title: 'Modal', | ||
component: Modal, | ||
} | ||
|
||
const Template: Story = (args) => { | ||
const [isOpen, setIsOpen] = useState(false) | ||
|
||
const toggleModal = () => { | ||
setIsOpen(!isOpen) | ||
} | ||
return ( | ||
<Box> | ||
<Button onClick={toggleModal} color="secondary" variant="outlined"> | ||
Open Modal | ||
</Button> | ||
<Modal onRequestClose={toggleModal} isOpen={isOpen} {...args}> | ||
<Box | ||
csx={{ | ||
root: (theme) => ({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
width: '300px', | ||
height: '100px', | ||
padding: '1rem 2rem', | ||
background: theme.palette.neutral[10], | ||
}), | ||
}} | ||
> | ||
<Typography variant="body" csx={{ root: { flex: '1' } }}> | ||
This is a modal | ||
</Typography> | ||
<Button | ||
csx | ||
onClick={toggleModal} | ||
color="error" | ||
variant="outlined" | ||
> | ||
Close Modal | ||
</Button> | ||
</Box> | ||
</Modal> | ||
</Box> | ||
) | ||
} | ||
|
||
export const Default = Template.bind({}) | ||
Default.args = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import ReactModal, { Props as ReactModalProps } from 'react-modal' | ||
import cx from 'clsx' | ||
|
||
import { | ||
createStyledComponent, | ||
ThemeCSSStyles, | ||
getThemeCSSObject, | ||
createStyle, | ||
hex2rgba, | ||
themeTernaryOperator as tto, | ||
} from '../styles' | ||
import { attachSignatureToComponent } from '../utils' | ||
import { MODAL } from '../constants/component-ids' | ||
|
||
import type { Ref } from '../utils' | ||
|
||
export interface ModalProps extends Omit<ReactModalProps, 'as' | 'ref'> { | ||
innerRef?: Ref | ||
/** | ||
* To extend the styles applied to the components | ||
*/ | ||
classes?: { | ||
/** | ||
* Applied to root component | ||
*/ | ||
root?: string | ||
/** | ||
* Applied to content container | ||
*/ | ||
content?: string | ||
} | ||
/** | ||
* To override the applied styles. | ||
*/ | ||
csx?: { | ||
/** | ||
* Applied to root component | ||
*/ | ||
root?: ThemeCSSStyles | ||
/** | ||
* Applied to content container | ||
*/ | ||
content?: ThemeCSSStyles | ||
} | ||
} | ||
|
||
const ModalRoot = createStyledComponent<'table', ModalProps>( | ||
'table', | ||
(theme, props) => { | ||
const { csx = {}, isExtendStyleFromThemeVars = true } = props | ||
const { themeVars, ...themePropsForThemeVarFn } = theme | ||
const { | ||
typography: { body }, | ||
palette: { neutral }, | ||
} = themePropsForThemeVarFn | ||
|
||
return { | ||
...body, | ||
...(isExtendStyleFromThemeVars && | ||
themeVars.table(themePropsForThemeVarFn, props)), | ||
...getThemeCSSObject(csx.root, theme), | ||
} | ||
} | ||
) | ||
|
||
const useStyles = createStyle((theme) => ({ | ||
root: (props: ModalProps) => { | ||
const { isOpen = false, csx = {} } = props | ||
const { themeVars, ...themePropsForThemeVarFn } = theme | ||
const { | ||
palette: { | ||
common: { white, black }, | ||
}, | ||
themeType, | ||
} = themePropsForThemeVarFn | ||
|
||
const rgba = hex2rgba(tto(themeType, black, white), 0.05) | ||
|
||
return { | ||
alignItems: 'center', | ||
background: rgba.rgba, | ||
bottom: 0, | ||
display: isOpen ? 'flex' : 'none', | ||
justifyContent: 'center', | ||
left: 0, | ||
position: 'fixed', | ||
right: 0, | ||
top: 0, | ||
...themeVars.modal(themePropsForThemeVarFn, props), | ||
...getThemeCSSObject(csx.root, theme), | ||
} | ||
}, | ||
|
||
content: (props: ModalProps) => { | ||
const { csx = {} } = props | ||
const { | ||
palette: { neutral }, | ||
} = theme | ||
|
||
return { | ||
color: neutral[90], | ||
...getThemeCSSObject(csx.root, theme), | ||
} | ||
}, | ||
})) | ||
|
||
export const Modal = (props: ModalProps): JSX.Element => { | ||
const { | ||
innerRef, | ||
classes: _classes = {}, | ||
className, | ||
children, | ||
...rest | ||
} = props | ||
|
||
const classes = useStyles(props) | ||
|
||
return ( | ||
<ReactModal | ||
className={cx(classes.content, _classes.content)} | ||
overlayClassName={cx(classes.root, className, _classes.root)} | ||
ref={innerRef} | ||
{...rest} | ||
> | ||
{children} | ||
</ReactModal> | ||
) | ||
} | ||
|
||
attachSignatureToComponent(Modal, MODAL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters