Modals
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} className={className}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Properties
Modal.propTypes = {
// boolean to control the state of the popover
isOpen: PropTypes.bool,
autoFocus: PropTypes.bool,
// if modal should be centered vertically in viewport
centered: PropTypes.bool,
// corresponds to bootstrap's modal sizes, ie. 'lg' or 'sm'
size: PropTypes.string,
// callback for toggling isOpen in the controlling component
toggle: PropTypes.func,
role: PropTypes.string, // defaults to "dialog"
// used to reference the ID of the title element in the modal
labelledBy: PropTypes.string,
keyboard: PropTypes.bool,
// control backdrop, see http://v4-alpha.getbootstrap.com/components/modal/#options
backdrop: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['static'])
]),
// if body of modal should be scrollable when content is long
scrollable: PropTypes.bool,
// allows for a node/component to exist next to the modal (outside of it). Useful for external close buttons
external: PropTypes.node,
// called on componentDidMount
onEnter: PropTypes.func,
// called on componentWillUnmount
onExit: PropTypes.func,
// called when done transitioning in
onOpened: PropTypes.func,
// called when done transitioning out
onClosed: PropTypes.func,
className: PropTypes.string,
wrapClassName: PropTypes.string,
modalClassName: PropTypes.string,
backdropClassName: PropTypes.string,
contentClassName: PropTypes.string,
// boolean to control whether the fade transition occurs (default: true)
fade: PropTypes.bool,
cssModule: PropTypes.object,
// zIndex defaults to 1000.
zIndex: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
// backdropTransition - controls backdrop transition
// timeout is 150ms by default to match bootstrap
// see [Fade](/components/fade/) for more details
backdropTransition: PropTypes.shape(Fade.propTypes),
// modalTransition - controls modal transition
// timeout is 300ms by default to match bootstrap
// see [Fade](/components/fade/) for more details
modalTransition: PropTypes.shape(Fade.propTypes),
innerRef: PropTypes.object,
// if modal should be destructed/removed from DOM after closing
unmountOnClose: PropTypes.bool, // defaults to true
// if the element which triggered the modal to open should focused after the modal closes (see example somewhere below)
returnFocusAfterClose: PropTypes.bool, // defaults to true
// container to append the modal to
container: PropTypes.oneOfType([PropTypes.string, PropTypes.func, DOMElement]),
trapFocus: PropTypes.bool // Traps focus within modal
}
Backdrop
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input, Label, Form, FormGroup } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const [backdrop, setBackdrop] = useState(true);
const [keyboard, setKeyboard] = useState(true);
const toggle = () => setModal(!modal);
const changeBackdrop = e => {
let value = e.target.value;
if (value !== 'static') {
value = JSON.parse(value);
}
setBackdrop(value);
}
const changeKeyboard = e => {
setKeyboard(e.currentTarget.checked);
}
return (
<div>
<Form inline onSubmit={(e) => e.preventDefault()}>
<FormGroup>
<Label for="backdrop">Backdrop value</Label>{' '}
<Input type="select" name="backdrop" id="backdrop" onChange={changeBackdrop}>
<option value="true">true</option>
<option value="false">false</option>
<option value="static">"static"</option>
</Input>
</FormGroup>
<FormGroup className="mx-2" check>
<Label check>
<Input type="checkbox" checked={keyboard} onChange={changeKeyboard} /> Keyboard
</Label>
</FormGroup>
{' '}
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
</Form>
<Modal isOpen={modal} toggle={toggle} className={className} backdrop={backdrop} keyboard={keyboard}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Nested Modals
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const [nestedModal, setNestedModal] = useState(false);
const [closeAll, setCloseAll] = useState(false);
const toggle = () => setModal(!modal);
const toggleNested = () => {
setNestedModal(!nestedModal);
setCloseAll(false);
}
const toggleAll = () => {
setNestedModal(!nestedModal);
setCloseAll(true);
}
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} className={className}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<br />
<Button color="success" onClick={toggleNested}>Show Nested Modal</Button>
<Modal isOpen={nestedModal} toggle={toggleNested} onClosed={closeAll ? toggle : undefined}>
<ModalHeader>Nested Modal title</ModalHeader>
<ModalBody>Stuff and things</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggleNested}>Done</Button>{' '}
<Button color="secondary" onClick={toggleAll}>All Done</Button>
</ModalFooter>
</Modal>
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Modals with Custom Transition Timeouts
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} modalTransition={{ timeout: 700 }} backdropTransition={{ timeout: 1300 }}
toggle={toggle} className={className}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Modals without Fade Effect
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} fade={false} toggle={toggle} className={className}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Modals with external button
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
const externalCloseBtn = <button className="close" style={{ position: 'absolute', top: '15px', right: '15px' }} onClick={toggle}>×</button>;
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} className={className} external={externalCloseBtn}>
<ModalHeader>Modal title</ModalHeader>
<ModalBody>
<b>Look at the top right of the page/viewport!</b><br />
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Modals with custom close icon
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} className={className}>
<ModalHeader toggle={toggle} charCode="Y">Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Modals with custom close button
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
const closeBtn = <button className="close" onClick={toggle}>×</button>;
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} className={className}>
<ModalHeader toggle={toggle} close={closeBtn}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Destructuring
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input, Label, Form, FormGroup } from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const [unmountOnClose, setUnmountOnClose] = useState(true);
const toggle = () => setModal(!modal);
const changeUnmountOnClose = e => {
let value = e.target.value;
setUnmountOnClose(JSON.parse(value));
}
return (
<div>
<Form inline onSubmit={(e) => e.preventDefault()}>
<FormGroup>
<Label for="unmountOnClose">UnmountOnClose value</Label>{' '}
<Input type="select" name="unmountOnClose" id="unmountOnClose" onChange={changeUnmountOnClose}>
<option value="true">true</option>
<option value="false">false</option>
</Input>
</FormGroup>
{' '}
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
</Form>
<Modal isOpen={modal} toggle={toggle} className={className} unmountOnClose={unmountOnClose}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
<Input type="textarea" placeholder="Write something (data should remain in modal if unmountOnClose is set to false)" rows={5} />
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Focus after close
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Modal, ModalBody, ModalFooter, Label, Input, FormGroup, Form } from 'reactstrap';
const ModalFocusAfterClose = (props) => {
const [open, setOpen] = useState(false);
const [focusAfterClose, setFocusAfterClose] = useState(true);
const toggle = () => setOpen(!open);
const handleSelectChange = ({target: { value }}) => {
setFocusAfterClose(JSON.parse(value));
}
return (
<div>
<Form inline onSubmit={(e) => e.preventDefault()}>
<FormGroup>
<Label for="focusAfterClose">Focus After Close</Label>
<Input className="mx-2" type="select" id="focusAfterClose" onChange={handleSelectChange}>
<option value="true">Yes</option>
<option value="false">No</option>
</Input>
</FormGroup>
<Button color="danger" onClick={toggle}>Open</Button>
</Form>
<Modal returnFocusAfterClose={focusAfterClose} isOpen={open}>
<ModalBody>
Observe the "Open" button. It will be focused after close when "returnFocusAfterClose" is true and will not be focused if "returnFocusAfterClose" is false.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Close</Button>
</ModalFooter>
</Modal>
</div>
)
}
export default ModalFocusAfterClose;