Skip to content

Commit

Permalink
Add size option for Modal (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkudrna committed Apr 15, 2020
1 parent 5999309 commit 83db615
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/demo/pages/DemoContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ class DemoContainer extends React.Component {
/>
<h3 id="ui-components-modal" className="typography-size-4 mb-6">Modal</h3>
<Documentation
name="Modal label"
name="Modal"
component={(
<>
<Button
Expand Down Expand Up @@ -1028,7 +1028,7 @@ class DemoContainer extends React.Component {
)}
/>
<Documentation
name="Modal label with loading icon"
name="Small modal with loading icon"
component={(
<>
<Button
Expand All @@ -1048,7 +1048,8 @@ class DemoContainer extends React.Component {
},
]}
closeHandler={() => this.setState({ showModal2: false })}
title="Modal"
size="small"
title="Small modal"
>
<p>Dialog content</p>
</Modal>
Expand Down
15 changes: 15 additions & 0 deletions src/lib/components/ui/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class Modal extends React.Component {
}

render() {
const sizeClass = (size) => {
if (size === 'small') {
return styles.isRootSmall;
}

if (size === 'medium') {
return styles.isRootMedium;
}

return styles.isRootLarge;
};

return (
<div
className={styles.overlay}
Expand All @@ -52,6 +64,7 @@ class Modal extends React.Component {
<div
className={`
${styles.root}
${sizeClass(this.props.size)}
${this.state.isContentOverflowing ? styles.isContentOverflowing : ''}
`.trim()}
onClick={(e) => {
Expand Down Expand Up @@ -115,6 +128,7 @@ Modal.defaultProps = {
actions: [],
closeHandler: null,
id: undefined,
size: 'medium',
};

Modal.propTypes = {
Expand All @@ -132,6 +146,7 @@ Modal.propTypes = {
]).isRequired,
closeHandler: PropTypes.func,
id: PropTypes.string,
size: PropTypes.oneOf(['small', 'medium', 'large']),
title: PropTypes.string.isRequired,
translations: PropTypes.shape({
close: PropTypes.string.isRequired,
Expand Down
13 changes: 12 additions & 1 deletion src/lib/components/ui/Modal/Modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
z-index: $modal-zindex;
display: flex;
flex-direction: column;
width: $modal-width;
max-width: calc(100% - (2 * #{$modal-padding-x}));
max-height: calc(100% - (2 * #{$modal-padding-y}));
overflow-y: auto;
Expand Down Expand Up @@ -79,3 +78,15 @@
.isContentOverflowing .footer {
box-shadow: $modal-footer-box-shadow;
}

.isRootSmall {
width: $modal-size-small-width;
}

.isRootMedium {
width: $modal-size-medium-width;
}

.isRootLarge {
width: $modal-size-large-width;
}
4 changes: 3 additions & 1 deletion src/lib/components/ui/Modal/__tests__/Modal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('rendering', () => {
]}
closeHandler={() => {}}
id="custom-id"
size="large"
title="Modal title"
>
Modal content
Expand All @@ -37,7 +38,7 @@ describe('rendering', () => {
expect(shallowToJson(tree)).toMatchSnapshot();
});

it('renders correctly with all with loading icon props except translations', () => {
it('renders correctly with all props except translations and with a loading icon', () => {
const tree = mount((
<Modal
actions={[
Expand All @@ -49,6 +50,7 @@ describe('rendering', () => {
]}
closeHandler={() => {}}
id="custom-id"
size="small"
title="Modal title"
>
Modal content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports[`rendering renders correctly with all props except translations 1`] = `
}
closeHandler={[Function]}
id="custom-id"
size="large"
title="Modal title"
translations={null}
>
Expand All @@ -26,6 +27,7 @@ exports[`rendering renders correctly with all props except translations 1`] = `
}
closeHandler={[Function]}
id="custom-id"
size="large"
title="Modal title"
translations={
Object {
Expand All @@ -41,7 +43,8 @@ exports[`rendering renders correctly with all props except translations 1`] = `
role="presentation"
>
<div
className="root"
className="root
isRootLarge"
onClick={[Function]}
role="presentation"
>
Expand Down Expand Up @@ -166,7 +169,7 @@ exports[`rendering renders correctly with all props except translations 1`] = `
</WithTranslationContextComponent>
`;

exports[`rendering renders correctly with all with loading icon props except translations 1`] = `
exports[`rendering renders correctly with all props except translations and with a loading icon 1`] = `
<WithTranslationContextComponent
actions={
Array [
Expand All @@ -181,6 +184,7 @@ exports[`rendering renders correctly with all with loading icon props except tra
}
closeHandler={[Function]}
id="custom-id"
size="small"
title="Modal title"
translations={null}
>
Expand All @@ -198,6 +202,7 @@ exports[`rendering renders correctly with all with loading icon props except tra
}
closeHandler={[Function]}
id="custom-id"
size="small"
title="Modal title"
translations={
Object {
Expand All @@ -213,7 +218,8 @@ exports[`rendering renders correctly with all with loading icon props except tra
role="presentation"
>
<div
className="root"
className="root
isRootSmall"
onClick={[Function]}
role="presentation"
>
Expand Down Expand Up @@ -362,6 +368,7 @@ exports[`rendering renders correctly with mandatory props only 1`] = `
<Modal
actions={Array []}
closeHandler={null}
size="medium"
title="Modal title"
translations={
Object {
Expand All @@ -376,7 +383,8 @@ exports[`rendering renders correctly with mandatory props only 1`] = `
role="presentation"
>
<div
className="root"
className="root
isRootMedium"
onClick={[Function]}
role="presentation"
>
Expand Down Expand Up @@ -416,6 +424,7 @@ exports[`rendering renders correctly with translations 1`] = `
<Modal
actions={Array []}
closeHandler={[Function]}
size="medium"
title="Modal title"
translations={
Object {
Expand All @@ -430,7 +439,8 @@ exports[`rendering renders correctly with translations 1`] = `
role="presentation"
>
<div
className="root"
className="root
isRootMedium"
onClick={[Function]}
role="presentation"
>
Expand Down
3 changes: 3 additions & 0 deletions src/lib/components/ui/Modal/_theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ $modal-background: var(--rui-modal-background);
$modal-box-shadow: var(--rui-modal-box-shadow);
$modal-footer-background: var(--rui-modal-footer-background);
$modal-overlay-background: var(--rui-modal-overlay-background);
$modal-size-small-width: var(--rui-modal-size-small-width);
$modal-size-medium-width: var(--rui-modal-size-medium-width);
$modal-size-large-width: var(--rui-modal-size-large-width);
1 change: 0 additions & 1 deletion src/lib/components/ui/Modal/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

// 1. Intentionally using direct color to prevent custom properties from being passed into `rgba` function.

$modal-width: 46rem;
$modal-padding-x: map-get($offset-values, 5);
$modal-padding-y: map-get($offset-values, 3);
$modal-border-radius: $border-radius;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@
--rui-modal-box-shadow: none;
--rui-modal-footer-background: var(--rui-color-gray-100);
--rui-modal-overlay-background: rgba(0, 0, 0, 0.5);
--rui-modal-size-small-width: 20rem;
--rui-modal-size-medium-width: 40rem;
--rui-modal-size-large-width: 60rem;

//
// Toolbar
Expand Down

0 comments on commit 83db615

Please sign in to comment.