From 41e910e1f0fb87a28827dc2e155e6444c2751640 Mon Sep 17 00:00:00 2001 From: NikolaBorislavovHristov Date: Mon, 18 Nov 2019 15:47:21 +0200 Subject: [PATCH] ModalDialog overflow styles fixed --- src/common/ModalDialog/ModalDialog.js | 121 ++++++++++------- src/common/ModalDialog/styles.less | 127 ++++++++++-------- storybook/stories/ModalDialog/ModalDialog.js | 96 ------------- .../ModalDialogWithActionButtons.js | 18 +++ .../ModalDialogWithActionButtons/index.js | 1 + .../ModalDialogWithActionButtons/styles.less | 31 +++++ .../SampleModalDialog/SampleModalDialog.js | 13 ++ .../ModalDialog/SampleModalDialog/index.js | 1 + storybook/stories/ModalDialog/index.js | 4 +- 9 files changed, 205 insertions(+), 207 deletions(-) delete mode 100644 storybook/stories/ModalDialog/ModalDialog.js create mode 100644 storybook/stories/ModalDialog/ModalDialogWithActionButtons/ModalDialogWithActionButtons.js create mode 100644 storybook/stories/ModalDialog/ModalDialogWithActionButtons/index.js create mode 100644 storybook/stories/ModalDialog/ModalDialogWithActionButtons/styles.less create mode 100644 storybook/stories/ModalDialog/SampleModalDialog/SampleModalDialog.js create mode 100644 storybook/stories/ModalDialog/SampleModalDialog/index.js diff --git a/src/common/ModalDialog/ModalDialog.js b/src/common/ModalDialog/ModalDialog.js index bb632fc6b..c6e5522e4 100644 --- a/src/common/ModalDialog/ModalDialog.js +++ b/src/common/ModalDialog/ModalDialog.js @@ -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 ( - -
- - { - typeof title === 'string' && title.length > 0 ? -

{title}

- : - null - } + +
+
+ { + typeof title === 'string' && title.length > 0 ? +
{title}
+ : + null + } + +
{children} -
- { - Array.isArray(buttons) && buttons.length > 0 ? -
- { - buttons.map((button, key) => ( - - )) - } -
- : - null - } + ))} +
+ : + null + } +
); @@ -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 }; diff --git a/src/common/ModalDialog/styles.less b/src/common/ModalDialog/styles.less index 9eb93b01f..4700d70fc 100644 --- a/src/common/ModalDialog/styles.less +++ b/src/common/ModalDialog/styles.less @@ -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); } } \ No newline at end of file diff --git a/storybook/stories/ModalDialog/ModalDialog.js b/storybook/stories/ModalDialog/ModalDialog.js deleted file mode 100644 index 31f8d3e33..000000000 --- a/storybook/stories/ModalDialog/ModalDialog.js +++ /dev/null @@ -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 = ( -
-
-

What is Lorem Ipsum?

-

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.

-
-
-

Where does it come from?

-

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.

-
-
- ); - - /* - - 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 ( -
- - { - modalVisible - ? - - {modalDummyContents} - - : - null - } - - - { - modalBVisible - ? - - {modalDummyContents} - - : - null - } -
- ); -}); \ No newline at end of file diff --git a/storybook/stories/ModalDialog/ModalDialogWithActionButtons/ModalDialogWithActionButtons.js b/storybook/stories/ModalDialog/ModalDialogWithActionButtons/ModalDialogWithActionButtons.js new file mode 100644 index 000000000..0b2f7cfae --- /dev/null +++ b/storybook/stories/ModalDialog/ModalDialogWithActionButtons/ModalDialogWithActionButtons.js @@ -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', () => ( + +
+ 1000px height content +
+
+)); \ No newline at end of file diff --git a/storybook/stories/ModalDialog/ModalDialogWithActionButtons/index.js b/storybook/stories/ModalDialog/ModalDialogWithActionButtons/index.js new file mode 100644 index 000000000..2da4e4115 --- /dev/null +++ b/storybook/stories/ModalDialog/ModalDialogWithActionButtons/index.js @@ -0,0 +1 @@ +require('./ModalDialogWithActionButtons'); diff --git a/storybook/stories/ModalDialog/ModalDialogWithActionButtons/styles.less b/storybook/stories/ModalDialog/ModalDialogWithActionButtons/styles.less new file mode 100644 index 000000000..febf9ea6e --- /dev/null +++ b/storybook/stories/ModalDialog/ModalDialogWithActionButtons/styles.less @@ -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; + } + } +} \ No newline at end of file diff --git a/storybook/stories/ModalDialog/SampleModalDialog/SampleModalDialog.js b/storybook/stories/ModalDialog/SampleModalDialog/SampleModalDialog.js new file mode 100644 index 000000000..980810b0e --- /dev/null +++ b/storybook/stories/ModalDialog/SampleModalDialog/SampleModalDialog.js @@ -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', () => ( + +
+ 100px height content +
+
+)); \ No newline at end of file diff --git a/storybook/stories/ModalDialog/SampleModalDialog/index.js b/storybook/stories/ModalDialog/SampleModalDialog/index.js new file mode 100644 index 000000000..a95c16af6 --- /dev/null +++ b/storybook/stories/ModalDialog/SampleModalDialog/index.js @@ -0,0 +1 @@ +require('./SampleModalDialog'); diff --git a/storybook/stories/ModalDialog/index.js b/storybook/stories/ModalDialog/index.js index 0e5c21118..89d26593b 100644 --- a/storybook/stories/ModalDialog/index.js +++ b/storybook/stories/ModalDialog/index.js @@ -1,2 +1,2 @@ - -require('./ModalDialog'); \ No newline at end of file +require('./SampleModalDialog'); +require('./ModalDialogWithActionButtons');