Skip to content

Commit

Permalink
feat: introduce new modal component
Browse files Browse the repository at this point in the history
  • Loading branch information
jongomez committed Oct 4, 2023
1 parent a7bfa14 commit c554377
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/lsd-react/src/components/CSSBaseline/CSSBaseline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { TagStyles } from '../Tag/Tag.styles'
import { TextFieldStyles } from '../TextField/TextField.styles'
import { defaultThemes, Theme, withTheme } from '../Theme'
import { TypographyStyles } from '../Typography/Typography.styles'
import { ModalStyles } from '../Modal/Modal.styles'
import { ModalBodyStyles } from '../ModalBody/ModalBody.styles'

const componentStyles: Array<ReturnType<typeof withTheme> | SerializedStyles> =
[
Expand Down Expand Up @@ -66,6 +68,8 @@ const componentStyles: Array<ReturnType<typeof withTheme> | SerializedStyles> =
TableBodyStyles,
TableItemStyles,
TableRowStyles,
ModalStyles,
ModalBodyStyles,
]

export const CSSBaseline: React.FC<{ theme?: Theme }> = ({
Expand Down
18 changes: 18 additions & 0 deletions packages/lsd-react/src/components/Modal/Modal.classes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const modalClasses = {
root: `lsd-modal`,

small: 'lsd-modal--small',
medium: 'lsd-modal--medium',
large: 'lsd-modal--large',
extraSmall: 'lsd-modal--extra-small',

modalContainer: 'lsd-modal__container',

header: 'lsd-modal__header',
title: 'lsd-modal__title',
subtitle: 'lsd-modal__subtitle',

titleAndSubtitleContainer: 'lsd-modal__title-and-subtitle-container',

closeIcon: 'lsd-modal__close-icon',
}
55 changes: 55 additions & 0 deletions packages/lsd-react/src/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState } from 'react'
import { Meta, Story } from '@storybook/react'
import { ModalBody } from '../ModalBody'
import { Modal, ModalProps } from './Modal'
import { Button } from '../Button'

export default {
title: 'Modal',
component: Modal,
argTypes: {
size: {
type: {
name: 'enum',
value: ['extraSmall', 'small', 'medium', 'large'],
},
},
},
} as Meta

export const Root: Story<
ModalProps & {
body: string
}
> = ({ body, ...args }) => {
const [isOpen, setIsOpen] = useState(false) // state to handle modal open/close

return (
<div style={{ width: 400 }}>
<Button onClick={() => setIsOpen(!isOpen)}>Open Modal</Button>
<Modal {...args} isOpen={isOpen} onClose={() => setIsOpen(false)}>
<ModalBody>{body}</ModalBody>
<div style={{ display: 'flex' }}>
<Button
size={args.size === 'extraSmall' ? 'small' : args.size}
style={{
marginRight: 12,
}}
>
Button 1
</Button>
<Button size={args.size === 'extraSmall' ? 'small' : args.size}>
Button 2
</Button>
</div>
</Modal>
</div>
)
}

Root.args = {
size: 'large',
title: 'Title',
subtitle: 'Subtitle goes here',
body: 'Content goes here.',
}
74 changes: 74 additions & 0 deletions packages/lsd-react/src/components/Modal/Modal.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { css } from '@emotion/react'
import { modalClasses } from './Modal.classes'

export const ModalStyles = css`
.${modalClasses.root} {
box-sizing: border-box;
display: flex;
flex-direction: column;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5); /* semi-transparent overlay */
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
.${modalClasses.modalContainer} {
background: rgb(var(--lsd-surface-primary));
padding: 20px;
max-width: 90%;
}
.${modalClasses.header} {
display: flex;
justify-content: space-between;
align-items: center;
}
.${modalClasses.title} {
}
.${modalClasses.subtitle} {
}
.${modalClasses.closeIcon} {
cursor: pointer;
}
.${modalClasses.titleAndSubtitleContainer} {
display: flex;
flex-direction: column;
}
.${modalClasses.large} {
.${modalClasses.modalContainer} {
min-width: 960px;
}
}
.${modalClasses.medium} {
.${modalClasses.modalContainer} {
min-width: 768px;
}
}
.${modalClasses.small} {
.${modalClasses.modalContainer} {
min-width: 614px;
}
}
.${modalClasses.extraSmall} {
.${modalClasses.modalContainer} {
min-width: 490px;
}
}
`
94 changes: 94 additions & 0 deletions packages/lsd-react/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import clsx from 'clsx'
import React from 'react'
import {
CommonProps,
omitCommonProps,
useCommonProps,
} from '../../utils/useCommonProps'
import { modalClasses } from './Modal.classes'
import { CloseIcon } from '../Icons'
import { Typography } from '../Typography'
import { IconButton } from '../IconButton'

export type ModalProps = CommonProps &
Omit<React.HTMLAttributes<HTMLDivElement>, 'label'> & {
isOpen: boolean // new prop to control visibility
size?: 'extraSmall' | 'small' | 'medium' | 'large'
title?: string
subtitle?: string
onClose?: () => void
}

export const Modal: React.FC<ModalProps> & {
classes: typeof modalClasses
} = ({
isOpen,
size = 'large',
title,
subtitle,
onClose,
children,
...props
}) => {
const commonProps = useCommonProps(props)

// Close modal when overlay is clicked
const handleOverlayClick = (e: React.MouseEvent) => {
if (e.target === e.currentTarget && onClose) {
onClose()
}
}

if (!isOpen) {
return null
}

return (
<div
{...omitCommonProps(props)}
className={clsx(
commonProps.className,
modalClasses.root,
modalClasses[size],
)}
onClick={handleOverlayClick}
>
<div className={clsx(modalClasses.modalContainer)}>
<div className={modalClasses.header}>
<div className={modalClasses.titleAndSubtitleContainer}>
{!!title && (
<Typography
className={modalClasses.title}
component="div"
variant={size === 'small' ? 'h6' : 'h5'}
>
{title}
</Typography>
)}

{!!subtitle && (
<Typography
className={modalClasses.title}
variant={size === 'small' ? 'label2' : 'label1'}
component="div"
>
{subtitle}
</Typography>
)}
</div>

<IconButton
onClick={onClose}
className={modalClasses.closeIcon}
size="medium"
>
<CloseIcon color="primary" />
</IconButton>
</div>
{children}
</div>
</div>
)
}

Modal.classes = modalClasses
1 change: 1 addition & 0 deletions packages/lsd-react/src/components/Modal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Modal'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const modalBodyClasses = {
root: `lsd-modal-body`,
}
30 changes: 30 additions & 0 deletions packages/lsd-react/src/components/ModalBody/ModalBody.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Meta, Story } from '@storybook/react'
import { ModalBody, ModalBodyProps } from './ModalBody'

export default {
title: 'ModalBody',
component: ModalBody,
argTypes: {
size: {
table: {
disable: true,
},
},
},
} as Meta

export const Root: Story<
ModalBodyProps & {
body: string
title: string
}
> = ({ body, ...args }) => (
<div style={{ width: 400 }}>
<ModalBody {...args}>{body}</ModalBody>
</div>
)

Root.args = {
title: 'Title',
body: 'A wise man can learn more from a foolish question than a fool can learn from a wise answer.',
}
16 changes: 16 additions & 0 deletions packages/lsd-react/src/components/ModalBody/ModalBody.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { css } from '@emotion/react'
import { modalBodyClasses } from './ModalBody.classes'

export const ModalBodyStyles = css`
.${modalBodyClasses.root} {
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border: 1px solid rgb(var(--lsd-border-primary));
padding: 30px;
margin: 18px 0;
}
`
28 changes: 28 additions & 0 deletions packages/lsd-react/src/components/ModalBody/ModalBody.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import clsx from 'clsx'
import React from 'react'
import {
CommonProps,
omitCommonProps,
useCommonProps,
} from '../../utils/useCommonProps'
import { modalBodyClasses } from './ModalBody.classes'

export type ModalBodyProps = CommonProps &
Omit<React.HTMLAttributes<HTMLDivElement>, 'label'>

export const ModalBody: React.FC<ModalBodyProps> & {
classes: typeof modalBodyClasses
} = ({ children, ...props }) => {
const commonProps = useCommonProps(props)

return (
<div
{...omitCommonProps(props)}
className={clsx(commonProps.className, modalBodyClasses.root)}
>
{children}
</div>
)
}

ModalBody.classes = modalBodyClasses
1 change: 1 addition & 0 deletions packages/lsd-react/src/components/ModalBody/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ModalBody'
2 changes: 2 additions & 0 deletions packages/lsd-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ export * from './components/Tag'
export * from './components/TextField'
export * from './components/Theme'
export * from './components/Typography'
export * from './components/Modal'
export * from './components/ModalBody'

0 comments on commit c554377

Please sign in to comment.