ModalDialog overflow styles fixed

This commit is contained in:
NikolaBorislavovHristov 2019-11-18 15:47:21 +02:00
parent f0e252ed63
commit 41e910e1f0
9 changed files with 205 additions and 207 deletions

View file

@ -6,21 +6,10 @@ const Icon = require('stremio-icons/dom');
const { Modal } = require('stremio-router');
const styles = require('./styles');
const ModalDialog = ({ className, children, title, buttons, onCloseRequest }) => {
React.useEffect(() => {
const onKeyDown = (event) => {
if (event.key === 'Escape') {
onCloseRequest({
type: 'close',
nativeEvent: event
});
}
};
window.addEventListener('keydown', onKeyDown);
return () => {
window.removeEventListener('keydown', onKeyDown);
};
}, [onCloseRequest]);
const ModalDialog = ({ className, title, buttons, children, onCloseRequest }) => {
const onModalDialogContainerMouseDown = React.useCallback((event) => {
event.nativeEvent.closeModalDialogPrevented = true;
}, []);
const closeButtonOnClick = React.useCallback((event) => {
onCloseRequest({
type: 'close',
@ -28,55 +17,79 @@ const ModalDialog = ({ className, children, title, buttons, onCloseRequest }) =>
nativeEvent: event.nativeEvent
});
}, [onCloseRequest]);
const onModalContainerMouseDown = React.useCallback((event) => {
if (event.target === event.currentTarget) {
onCloseRequest({
type: 'close',
reactEvent: event,
nativeEvent: event.nativeEvent
});
}
React.useEffect(() => {
const onCloseEvent = (event) => {
if (!event.closeModalDialogPrevented && typeof onCloseRequest === 'function') {
const closeEvent = {
type: 'close',
nativeEvent: event
};
switch (event.type) {
case 'resize':
onCloseRequest(closeEvent);
break;
case 'keydown':
if (event.key === 'Escape') {
onCloseRequest(closeEvent);
}
break;
case 'mousedown':
if (event.target !== document.documentElement) {
onCloseRequest(closeEvent);
}
break;
}
}
};
window.addEventListener('resize', onCloseEvent);
window.addEventListener('keydown', onCloseEvent);
window.addEventListener('mousedown', onCloseEvent);
return () => {
window.removeEventListener('resize', onCloseEvent);
window.removeEventListener('keydown', onCloseEvent);
window.removeEventListener('mousedown', onCloseEvent);
};
}, [onCloseRequest]);
return (
<Modal className={styles['modal-container']} onMouseDown={onModalContainerMouseDown}>
<div className={classnames(className, styles['modal-dialog-container'])}>
<Button className={styles['close-button-container']} title={'Close'} onClick={closeButtonOnClick}>
<Icon className={styles['icon']} icon={'ic_x'} />
</Button>
{
typeof title === 'string' && title.length > 0 ?
<h1>{title}</h1>
:
null
}
<Modal className={classnames(className, styles['modal-container'])}>
<div className={styles['modal-dialog-container']} onMouseDown={onModalDialogContainerMouseDown}>
<div className={styles['header-container']}>
{
typeof title === 'string' && title.length > 0 ?
<div className={styles['title-container']} title={title}>{title}</div>
:
null
}
<Button className={styles['close-button-container']} title={'Close'} onClick={closeButtonOnClick}>
<Icon className={styles['icon']} icon={'ic_x'} />
</Button>
</div>
<div className={styles['modal-dialog-content']}>
{children}
</div>
{
Array.isArray(buttons) && buttons.length > 0 ?
<div className={styles['modal-dialog-buttons']}>
{
buttons.map((button, key) => (
<Button className={classnames(button.className, styles['action-button'])} {...button.props} key={key}>
{
Array.isArray(buttons) && buttons.length > 0 ?
<div className={styles['buttons-container']}>
{buttons.map(({ className, label, icon, props }, index) => (
<Button title={label} {...props} key={index} className={classnames(className, styles['action-button'])}>
{
typeof button.icon === 'string' && button.icon.length > 0 ?
<Icon className={styles['icon']} icon={button.icon} />
typeof icon === 'string' && icon.length > 0 ?
<Icon className={styles['icon']} icon={icon} />
:
null
}
{
typeof button.label === 'string' && button.label.length > 0 ?
button.label
typeof label === 'string' && label.length > 0 ?
<div className={styles['label']}>{label}</div>
:
null
}
</Button>
))
}
</div>
:
null
}
))}
</div>
:
null
}
</div>
</div>
</Modal>
);
@ -86,11 +99,15 @@ ModalDialog.propTypes = {
className: PropTypes.string,
title: PropTypes.string,
buttons: PropTypes.arrayOf(PropTypes.shape({
className: PropTypes.string,
label: PropTypes.string,
icon: PropTypes.string,
className: PropTypes.string,
props: PropTypes.object
})),
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]),
onCloseRequest: PropTypes.func
};

View file

@ -1,104 +1,117 @@
.modal-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: var(--color-backgrounddark40);
.modal-dialog-container {
position: relative;
margin: auto;
padding: 1rem;
flex: none;
display: flex;
flex-direction: column;
max-width: 80%;
max-height: 80%;
background-color: var(--color-surfacelighter);
.close-button-container {
.header-container {
flex: none;
align-self: stretch;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
position: absolute;
top: .2rem;
right: .2rem;
width: 2rem;
height: 2rem;
padding: 0.5rem;
justify-content: flex-end;
padding: 0.2rem 0.2rem 0;
.icon {
.title-container {
flex: 1;
align-self: center;
max-height: 2.4em;
padding: 0 0.5rem;
font-size: 1.2rem;
color: var(--color-backgrounddarker);
}
.close-button-container {
flex: none;
display: block;
width: 100%;
height: 100%;
fill: var(--color-surfacedark);
}
&:hover {
background-color: var(--color-surfacelight);
align-self: flex-start;
width: 2rem;
height: 2rem;
padding: 0.5rem;
.icon {
fill: var(--color-signal2);
display: block;
width: 100%;
height: 100%;
fill: var(--color-surfacedark);
}
&:hover, &:focus {
background-color: var(--color-surfacelight);
.icon {
fill: var(--color-surfacedarker);
}
}
&:focus {
outline-color: var(--color-surfacedark);
}
}
&:focus {
.icon {
fill: var(--color-signal2);
}
}
}
h1 {
margin-bottom: 1rem;
font-size: 1.2rem;
}
.modal-dialog-content {
padding: 1rem;
flex: 1;
align-self: stretch;
margin: 1rem 0.5rem;
padding: 0 0.5rem;
overflow-y: auto;
>:not(:first-child) {
.buttons-container {
margin-top: 1rem;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
}
.modal-dialog-buttons {
margin: 1rem;
display: flex;
flex-direction: row;
}
}
}
.action-button {
flex: 1;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
flex: 1 0 0;
padding: 1rem;
text-align: center;
color: var(--color-surfacelighter);
background-color: var(--color-signal5);
&:focus {
outline-width: calc(1.5 * var(--focus-outline-size));
outline-color: var(--color-surfacelighter);
outline-offset: calc(-2 * var(--focus-outline-size));
}
&:hover {
filter: brightness(1.2);
}
&:focus {
outline: calc(1.5 * var(--focus-outline-size)) solid var(--color-surfacelighter);
outline-offset: calc(-2 * var(--focus-outline-size));
}
&:global(.disabled) {
color: var(--color-surfacedarker);
background-color: var(--color-surfacedark);
&:not(:last-child) {
margin-right: 1rem;
}
.icon {
flex: none;
width: 1rem;
height: 1rem;
width: 1.2rem;
height: 1.2rem;
margin-right: .5rem;
fill: var(--color-surfacelighter);
}
&:not(:last-child) {
margin-right: 2rem;
.label {
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
max-height: 3.6em;
font-size: 1.1rem;
text-align: center;
color: var(--color-surfacelighter);
}
}

View file

@ -1,96 +0,0 @@
const React = require('react');
const { storiesOf } = require('@storybook/react');
const { action } = require('@storybook/addon-actions');
const { ModalDialog } = require('stremio/common');
const styles = require('./styles');
const useBinaryState = require('stremio/common/useBinaryState');
storiesOf('ModalDialog', module).add('ModalDialog', () => {
const [modalVisible, showModal, hideModal] = useBinaryState(false);
const [modalBVisible, showModalB, hideModalB] = useBinaryState(true);
const modalDummyContents = (
<div className={styles['content-container']}>
<div className={styles['content-column']}>
<h2>What is Lorem Ipsum?</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
<div className={styles['content-column']}>
<h2>Where does it come from?</h2>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
</div>
</div>
);
/*
Every Button has the following properties:
label (String/React component) - the contents of the button.
icon (String) - icon class name. It will be shown to the left of the button's text
className (String) - Custom className applied along side the default one. Used for custom styles
props (Object) - the properties applied to the button itself. If a className is supplied here it will override all other class names for this Button
*/
const oneButton = [
{
label: 'Show many buttons', icon: 'ic_ellipsis', props: {
onClick: React.useCallback(() => setButtons(manyButtons), [setButtons])
}
},
]
const manyButtons = [
{
label: 'One',
icon: 'ic_back_ios',
props: {
onClick: React.useCallback(() => setButtons(oneButton), [setButtons])
}
},
{
label: 'A disabled button with a long name',
props: {
disabled: true,
tabIndex: -1, // We don't need keyboard focus on disabled elements
onClick: action('The onClick on disabled buttons should not be called!')
}
},
{
label: 'A button with a long name, icon and custom class',
className: styles['custom-button'],
props: {
onClick: action('A button with a long name and icon clicked')
}
},
{}
];
const [buttons, setButtons] = React.useState(oneButton);
return (
<div>
<button className={styles['show-modal-button']} onClick={showModal}>Show dialog without buttons</button>
{
modalVisible
?
<ModalDialog className={styles['modal-dialog']} title={'Test dialog without buttons'} visible={modalVisible} onCloseRequest={hideModal}>
{modalDummyContents}
</ModalDialog>
:
null
}
<button className={styles['show-modal-button']} onClick={showModalB}>Show dialog with buttons</button>
{
modalBVisible
?
<ModalDialog className={styles['modal-dialog']} title={'Test dialog with buttons'} buttons={buttons} visible={modalBVisible} onCloseRequest={hideModalB}>
{modalDummyContents}
</ModalDialog>
:
null
}
</div>
);
});

View file

@ -0,0 +1,18 @@
const React = require('react');
const { storiesOf } = require('@storybook/react');
const { action } = require('@storybook/addon-actions');
const { ModalDialog } = require('stremio/common');
const styles = require('./styles');
storiesOf('ModalDialog', module).add('ModalDialogWithActionButtons', () => (
<ModalDialog title={'ModalDialogWithActionButtons'}
onCloseRequest={action('onCloseRequest')}
buttons={[
{ label: 'Accept', props: { onClick: action('Accept') } },
{ label: 'Addons', icon: 'ic_addons', className: styles['addons-button-container'], props: { onClick: action('Addons') } },
]}>
<div style={{ height: 1000, background: 'red', color: 'white' }}>
1000px height content
</div>
</ModalDialog>
));

View file

@ -0,0 +1 @@
require('./ModalDialogWithActionButtons');

View file

@ -0,0 +1,31 @@
:import('~stremio/common/ModalDialog/styles.less') {
modal-dialog-icon: icon;
modal-dialog-label: label;
}
.addons-button-container {
background-color: transparent;
border: 2px solid gray;
.modal-dialog-icon {
fill: gray;
}
.modal-dialog-label {
color: gray;
}
&:hover, &:focus {
filter: none;
outline: none;
border-color: black;
.modal-dialog-icon {
fill: black;
}
.modal-dialog-label {
color: black;
}
}
}

View file

@ -0,0 +1,13 @@
const React = require('react');
const { storiesOf } = require('@storybook/react');
const { action } = require('@storybook/addon-actions');
const { ModalDialog } = require('stremio/common');
storiesOf('ModalDialog', module).add('SampleModalDialog', () => (
<ModalDialog title={'SampleModalDialog'} onCloseRequest={action('onCloseRequest')}>
<div style={{ height: 100, background: 'red', color: 'white' }}>
100px height content
</div>
</ModalDialog>
));

View file

@ -0,0 +1 @@
require('./SampleModalDialog');

View file

@ -1,2 +1,2 @@
require('./ModalDialog');
require('./SampleModalDialog');
require('./ModalDialogWithActionButtons');