-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ModalOverlay component (#18161)
- Loading branch information
1 parent
4474359
commit 99bdf84
Showing
10 changed files
with
178 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,40 @@ | ||
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'; | ||
|
||
import { ModalOverlay } from './modal-overlay'; | ||
|
||
# ModalOverlay | ||
|
||
`ModalOverlay` is a transparent overlay that covers the entire screen. It is used to dim the background when a modal is open. | ||
|
||
<Canvas> | ||
<Story id="components-componentlibrary-modaloverlay--default-story" /> | ||
</Canvas> | ||
|
||
## Props | ||
|
||
The `ModalOverlay` accepts all props below as well as all [Box](/docs/components-ui-box--default-story#props) component props | ||
|
||
<ArgsTable of={ModalOverlay} /> | ||
|
||
### On Click | ||
|
||
Use the `onClick` prop to handle clicks on the overlay | ||
|
||
<Canvas> | ||
<Story id="components-componentlibrary-modaloverlay--on-click" /> | ||
</Canvas> | ||
|
||
```jsx | ||
import React, { useState } from 'react'; | ||
import { ModalOverlay } from '../../component-library'; | ||
|
||
const [open, setOpen] = useState(false); | ||
const handleOnClick = () => { | ||
setOpen(!open); | ||
}; | ||
|
||
<button onClick={handleOnClick}>Show modal overlay</button>; | ||
{ | ||
open && <ModalOverlay onClick={handleOnClick} />; | ||
} | ||
``` |
9 changes: 9 additions & 0 deletions
9
ui/components/component-library/modal-overlay/__snapshots__/modal-overlay.test.tsx.snap
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,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ModalOverlay should match snapshot 1`] = ` | ||
<div> | ||
<div | ||
class="box mm-modal-overlay box--flex-direction-row box--width-full box--height-full box--background-color-overlay-default" | ||
/> | ||
</div> | ||
`; |
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,2 @@ | ||
export { ModalOverlay } from './modal-overlay'; | ||
export type { ModalOverlayProps } from './modal-overlay.types'; |
7 changes: 7 additions & 0 deletions
7
ui/components/component-library/modal-overlay/modal-overlay.scss
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,7 @@ | ||
.mm-modal-overlay { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
} |
44 changes: 44 additions & 0 deletions
44
ui/components/component-library/modal-overlay/modal-overlay.stories.tsx
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,44 @@ | ||
import React, { useState } from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import { ModalOverlay } from './modal-overlay'; | ||
|
||
import README from './README.mdx'; | ||
|
||
export default { | ||
title: 'Components/ComponentLibrary/ModalOverlay', | ||
component: ModalOverlay, | ||
parameters: { | ||
docs: { | ||
page: README, | ||
}, | ||
}, | ||
argTypes: { | ||
className: { | ||
control: 'text', | ||
}, | ||
onClick: { | ||
action: 'onClick', | ||
}, | ||
}, | ||
} as ComponentMeta<typeof ModalOverlay>; | ||
|
||
const Template: ComponentStory<typeof ModalOverlay> = (args) => ( | ||
<ModalOverlay {...args} /> | ||
); | ||
|
||
export const DefaultStory = Template.bind({}); | ||
DefaultStory.storyName = 'Default'; | ||
|
||
export const OnClick: ComponentStory<typeof ModalOverlay> = (args) => { | ||
const [open, setOpen] = useState(false); | ||
const handleOnClick = () => { | ||
setOpen(!open); | ||
}; | ||
return ( | ||
<> | ||
<button onClick={handleOnClick}>Show modal overlay</button> | ||
{open && <ModalOverlay {...args} onClick={handleOnClick} />} | ||
</> | ||
); | ||
}; |
32 changes: 32 additions & 0 deletions
32
ui/components/component-library/modal-overlay/modal-overlay.test.tsx
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,32 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import { ModalOverlay } from './modal-overlay'; | ||
|
||
describe('ModalOverlay', () => { | ||
it('should render ModalOverlay without error', () => { | ||
const { getByTestId } = render( | ||
<ModalOverlay data-testid="modal-overlay" />, | ||
); | ||
expect(getByTestId('modal-overlay')).toBeDefined(); | ||
expect(getByTestId('modal-overlay')).toHaveClass('mm-modal-overlay'); | ||
}); | ||
it('should match snapshot', () => { | ||
const { container } = render(<ModalOverlay />); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
it('should render with and additional className', () => { | ||
const { getByTestId } = render( | ||
<ModalOverlay data-testid="modal-overlay" className="test-class" />, | ||
); | ||
expect(getByTestId('modal-overlay')).toHaveClass('test-class'); | ||
}); | ||
it('should fire the onClick function when clicked', () => { | ||
const onClick = jest.fn(); | ||
const { getByTestId } = render( | ||
<ModalOverlay data-testid="modal-overlay" onClick={onClick} />, | ||
); | ||
getByTestId('modal-overlay').click(); | ||
expect(onClick).toHaveBeenCalled(); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
ui/components/component-library/modal-overlay/modal-overlay.tsx
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,28 @@ | ||
import React from 'react'; | ||
import classnames from 'classnames'; | ||
|
||
import { | ||
BackgroundColor, | ||
BLOCK_SIZES, | ||
} from '../../../helpers/constants/design-system'; | ||
|
||
import Box from '../../ui/box/box'; | ||
|
||
import { ModalOverlayProps } from './modal-overlay.types'; | ||
|
||
export const ModalOverlay: React.FC<ModalOverlayProps> = ({ | ||
onClick, | ||
className = '', | ||
...props | ||
}) => ( | ||
<Box | ||
className={classnames('mm-modal-overlay', className)} | ||
backgroundColor={BackgroundColor.overlayDefault} | ||
width={BLOCK_SIZES.FULL} | ||
height={BLOCK_SIZES.FULL} | ||
onClick={onClick} | ||
{...props} | ||
/> | ||
); | ||
|
||
export default ModalOverlay; |
13 changes: 13 additions & 0 deletions
13
ui/components/component-library/modal-overlay/modal-overlay.types.ts
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,13 @@ | ||
import { BoxProps } from '../../ui/box/box.d'; | ||
|
||
export interface ModalOverlayProps extends BoxProps { | ||
/** | ||
* onClick handler for the overlay | ||
* Not necessary when used with Modal and closeOnClickOutside is true | ||
*/ | ||
onClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void; | ||
/** | ||
* Additional className to add to the ModalOverlay | ||
*/ | ||
className?: string; | ||
} |