diff --git a/src/common/ModalDialog/ModalDialog.js b/src/common/ModalDialog/ModalDialog.js new file mode 100644 index 000000000..20128aa25 --- /dev/null +++ b/src/common/ModalDialog/ModalDialog.js @@ -0,0 +1,87 @@ +const React = require('react'); +const PropTypes = require('prop-types'); +const classnames = require('classnames'); +const Button = require('stremio/common/Button'); +const Icon = require('stremio-icons/dom'); +const { Modal } = require('stremio-router'); +const styles = require('./styles'); + +const ModalDialog = ({ className, children, title, buttons, onCloseRequest }) => { + const dispatchCloseRequestEvent = React.useCallback(event => { + if (typeof onCloseRequest === 'function') { + onCloseRequest({ + type: 'closeRequest', + reactEvent: event, + nativeEvent: event.nativeEvent + }); + } + }, [onCloseRequest]); + React.useEffect(() => { + const onKeyDown = (event) => { + if (event.key === 'Escape') { + dispatchCloseRequestEvent(event); + } + }; + window.addEventListener('keydown', onKeyDown); + return () => { + window.removeEventListener('keydown', onKeyDown); + }; + }, [dispatchCloseRequestEvent]); + const onModalContainerMouseDown = React.useCallback(event => { + if (event.target === event.currentTarget) { + dispatchCloseRequestEvent(event); + } + }, [dispatchCloseRequestEvent]); + return ( + +
+ +

{title}

+
+ {children} +
+ { + Array.isArray(buttons) && buttons.length > 0 ? +
+ { + buttons.map((button, key) => ( + + )) + } +
+ : + null + } +
+
+ ); +}; + +ModalDialog.propTypes = { + className: PropTypes.string, + title: PropTypes.string, + buttons: PropTypes.arrayOf(PropTypes.shape({ + label: PropTypes.string, + icon: PropTypes.string, + className: PropTypes.string, + props: PropTypes.object + })), + onCloseRequest: PropTypes.func +}; + +module.exports = ModalDialog; diff --git a/src/common/ModalDialog/index.js b/src/common/ModalDialog/index.js new file mode 100644 index 000000000..7dadbbd9e --- /dev/null +++ b/src/common/ModalDialog/index.js @@ -0,0 +1,3 @@ +const ModalDialog = require('./ModalDialog'); + +module.exports = ModalDialog; \ No newline at end of file diff --git a/src/common/ModalDialog/styles.less b/src/common/ModalDialog/styles.less new file mode 100644 index 000000000..09196210b --- /dev/null +++ b/src/common/ModalDialog/styles.less @@ -0,0 +1,99 @@ +.modal-container { + display: flex; + flex-direction: column; + background-color: var(--color-backgrounddark40); + + .modal-dialog-container { + position: relative; + margin: auto; + padding: 1rem; + background-color: var(--color-surfacelighter); + + .close-button-container { + display: flex; + flex-direction: row; + align-items: center; + justify-content: center; + position: absolute; + top: .2rem; + right: .2rem; + width: 2rem; + height: 2rem; + padding: 0.5rem; + + .icon { + flex: none; + display: block; + width: 100%; + height: 100%; + fill: var(--color-surfacedark); + } + + &:hover { + background-color: var(--color-surfacelight); + + .icon { + fill: var(--color-signal2); + } + } + + &:focus { + .icon { + fill: var(--color-signal2); + } + } + } + + h1 { + font-size: 1.2rem; + } + + .modal-dialog-content { + margin-top: 1rem; + } + + .modal-dialog-buttons { + margin-top: 1rem; + display: flex; + flex-direction: row; + } + } +} + +.action-button { + 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); + + &: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); + } + + .icon { + flex: none; + width: 1rem; + height: 1rem; + margin-right: .5rem; + fill: var(--color-surfacelighter); + } + + &:not(:last-child) { + margin-right: 1rem; + } +} \ No newline at end of file diff --git a/src/common/index.js b/src/common/index.js index 5e0336ad4..9eeca6a39 100644 --- a/src/common/index.js +++ b/src/common/index.js @@ -6,6 +6,7 @@ const MainNavBar = require('./MainNavBar'); const MetaItem = require('./MetaItem'); const MetaPreview = require('./MetaPreview'); const MetaRow = require('./MetaRow'); +const ModalDialog = require('./ModalDialog'); const Multiselect = require('./Multiselect'); const NavBar = require('./NavBar'); const PaginateInput = require('./PaginateInput'); @@ -34,6 +35,7 @@ module.exports = { MetaItem, MetaPreview, MetaRow, + ModalDialog, Multiselect, NavBar, PaginateInput, diff --git a/storybook/stories/ModalDialog/ModalDialog.js b/storybook/stories/ModalDialog/ModalDialog.js new file mode 100644 index 000000000..31f8d3e33 --- /dev/null +++ b/storybook/stories/ModalDialog/ModalDialog.js @@ -0,0 +1,96 @@ + +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/index.js b/storybook/stories/ModalDialog/index.js new file mode 100644 index 000000000..0e5c21118 --- /dev/null +++ b/storybook/stories/ModalDialog/index.js @@ -0,0 +1,2 @@ + +require('./ModalDialog'); \ No newline at end of file diff --git a/storybook/stories/ModalDialog/styles.less b/storybook/stories/ModalDialog/styles.less new file mode 100644 index 000000000..d6a3e426f --- /dev/null +++ b/storybook/stories/ModalDialog/styles.less @@ -0,0 +1,49 @@ +.modal-dialog { + width: 45rem; + + .custom-button { + background-color: var(--color-signal3); + + &:hover { + background-color: var(--color-signal380); + color: var(--color-backgrounddarker); + + .icon { + fill: var(--color-backgrounddarker); + } + } + + .icon { + fill: var(--color-surfacelighter); + width: 1rem; + height: 1rem; + margin-right: 0.5rem; + } + } + + // This is only for the content. Not relevant for the demo + .content-container { + display: flex; + flex-direction: row; + margin: 0 -0.5rem; + + .content-column { + flex: 1 0 0; + padding: 0 0.5rem; + + p { + text-align: justify; + } + } + } +} + +// This is only for the buttons that show the modals. Not relevant for the demo +.show-modal-button { + display: inline-block; + background-color: var(--color-signal4); + width: 15rem; + text-align: center; + padding: 1rem; + margin: .5rem; +} \ No newline at end of file diff --git a/storybook/stories/index.js b/storybook/stories/index.js index 97f1030cf..a3b450370 100644 --- a/storybook/stories/index.js +++ b/storybook/stories/index.js @@ -3,3 +3,4 @@ require('./MetaItem'); require('./ColorPicker'); require('./SharePrompt'); require('./Notification'); +require('./ModalDialog');