Properties

    1. // boolean to control the state of the popover
    2. isOpen: PropTypes.bool,
    3. autoFocus: PropTypes.bool,
    4. // if modal should be centered vertically in viewport
    5. centered: PropTypes.bool,
    6. // corresponds to bootstrap's modal sizes, ie. 'lg' or 'sm'
    7. size: PropTypes.string,
    8. // callback for toggling isOpen in the controlling component
    9. toggle: PropTypes.func,
    10. role: PropTypes.string, // defaults to "dialog"
    11. // used to reference the ID of the title element in the modal
    12. labelledBy: PropTypes.string,
    13. keyboard: PropTypes.bool,
    14. // control backdrop, see
    15. backdrop: PropTypes.oneOfType([
    16. PropTypes.bool,
    17. PropTypes.oneOf(['static'])
    18. ]),
    19. // if body of modal should be scrollable when content is long
    20. scrollable: PropTypes.bool,
    21. // allows for a node/component to exist next to the modal (outside of it). Useful for external close buttons
    22. // external: PropTypes.node,
    23. // called on componentDidMount
    24. onEnter: PropTypes.func,
    25. // called on componentWillUnmount
    26. onExit: PropTypes.func,
    27. // called when done transitioning in
    28. onOpened: PropTypes.func,
    29. // called when done transitioning out
    30. onClosed: PropTypes.func,
    31. className: PropTypes.string,
    32. wrapClassName: PropTypes.string,
    33. modalClassName: PropTypes.string,
    34. backdropClassName: PropTypes.string,
    35. contentClassName: PropTypes.string,
    36. // boolean to control whether the fade transition occurs (default: true)
    37. fade: PropTypes.bool,
    38. cssModule: PropTypes.object,
    39. // zIndex defaults to 1000.
    40. zIndex: PropTypes.oneOfType([
    41. PropTypes.number,
    42. PropTypes.string,
    43. ]),
    44. // backdropTransition - controls backdrop transition
    45. // timeout is 150ms by default to match bootstrap
    46. // see Fade for more details
    47. backdropTransition: PropTypes.shape(Fade.propTypes),
    48. // modalTransition - controls modal transition
    49. // timeout is 300ms by default to match bootstrap
    50. // see for more details
    51. modalTransition: PropTypes.shape(Fade.propTypes),
    52. innerRef: PropTypes.object,
    53. // if modal should be destructed/removed from DOM after closing
    54. unmountOnClose: PropTypes.bool // defaults to true
    55. }

    Backdrop

    1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
    2. import React from 'react';
    3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input, Label, Form, FormGroup } from 'reactstrap';
    4. class ModalExample extends React.Component {
    5. constructor(props) {
    6. super(props);
    7. this.state = {
    8. modal: false,
    9. backdrop: true
    10. };
    11. this.toggle = this.toggle.bind(this);
    12. this.changeBackdrop = this.changeBackdrop.bind(this);
    13. }
    14. toggle() {
    15. this.setState(prevState => ({
    16. modal: !prevState.modal
    17. }));
    18. }
    19. changeBackdrop(e) {
    20. let value = e.target.value;
    21. if (value !== 'static') {
    22. value = JSON.parse(value);
    23. }
    24. this.setState({ backdrop: value });
    25. }
    26. render() {
    27. return (
    28. <div>
    29. <Form inline onSubmit={(e) => e.preventDefault()}>
    30. <FormGroup>
    31. <Label for="backdrop">Backdrop value</Label>{' '}
    32. <Input type="select" name="backdrop" id="backdrop" onChange={this.changeBackdrop}>
    33. <option value="true">true</option>
    34. <option value="false">false</option>
    35. <option value="static">"static"</option>
    36. </Input>
    37. </FormGroup>
    38. {' '}
    39. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
    40. </Form>
    41. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className} backdrop={this.state.backdrop}>
    42. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
    43. <ModalBody>
    44. 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.
    45. <ModalFooter>
    46. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
    47. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
    48. </ModalFooter>
    49. </Modal>
    50. </div>
    51. }
    52. }
    53. export default ModalExample;

    Nested Modals

    Modals - 图1

    Modals with Custom Transition Timeouts

    1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
    2. import React from 'react';
    3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
    4. class ModalExample extends React.Component {
    5. constructor(props) {
    6. super(props);
    7. this.state = {
    8. modal: false
    9. };
    10. this.toggle = this.toggle.bind(this);
    11. }
    12. toggle() {
    13. this.setState(prevState => ({
    14. modal: !prevState.modal
    15. }));
    16. }
    17. render() {
    18. return (
    19. <div>
    20. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
    21. <Modal isOpen={this.state.modal} modalTransition={{ timeout: 700 }} backdropTransition={{ timeout: 1300 }}
    22. toggle={this.toggle} className={this.props.className}>
    23. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
    24. <ModalBody>
    25. 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.
    26. </ModalBody>
    27. <ModalFooter>
    28. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
    29. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
    30. </ModalFooter>
    31. </Modal>
    32. </div>
    33. );
    34. }
    35. }
    36. export default ModalExample;

    Modals without Fade Effect

    1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
    2. import React from 'react';
    3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
    4. class ModalExample extends React.Component {
    5. constructor(props) {
    6. super(props);
    7. this.state = {
    8. modal: false
    9. };
    10. this.toggle = this.toggle.bind(this);
    11. }
    12. toggle() {
    13. this.setState(prevState => ({
    14. modal: !prevState.modal
    15. }));
    16. }
    17. render() {
    18. return (
    19. <div>
    20. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
    21. <Modal isOpen={this.state.modal} fade={false} toggle={this.toggle} className={this.props.className}>
    22. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
    23. <ModalBody>
    24. 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.
    25. </ModalBody>
    26. <ModalFooter>
    27. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
    28. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
    29. </ModalFooter>
    30. </Modal>
    31. </div>
    32. );
    33. }
    34. }
    35. export default ModalExample;

    Modals with external button

    Modals - 图2

    Modals with custom close icon

    1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
    2. import React from 'react';
    3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
    4. class ModalExample extends React.Component {
    5. constructor(props) {
    6. super(props);
    7. this.state = {
    8. modal: false
    9. };
    10. this.toggle = this.toggle.bind(this);
    11. }
    12. toggle() {
    13. this.setState(prevState => ({
    14. modal: !prevState.modal
    15. }));
    16. }
    17. render() {
    18. <div>
    19. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
    20. <ModalHeader toggle={this.toggle} charCode="Y">Modal title</ModalHeader>
    21. <ModalBody>
    22. 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.
    23. </ModalBody>
    24. <ModalFooter>
    25. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
    26. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
    27. </ModalFooter>
    28. </Modal>
    29. </div>
    30. );
    31. }
    32. }
    33. export default ModalExample;

    Modals with custom close button

    1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
    2. import React from 'react';
    3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
    4. class ModalExample extends React.Component {
    5. constructor(props) {
    6. super(props);
    7. this.state = {
    8. modal: false
    9. };
    10. this.toggle = this.toggle.bind(this);
    11. }
    12. toggle() {
    13. this.setState(prevState => ({
    14. modal: !prevState.modal
    15. }));
    16. }
    17. render() {
    18. const closeBtn = <button className="close" onClick={this.toggle}>&times;</button>;
    19. return (
    20. <div>
    21. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
    22. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
    23. <ModalHeader toggle={this.toggle} close={closeBtn}>Modal title</ModalHeader>
    24. <ModalBody>
    25. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
    26. dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
    27. ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
    28. fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
    29. mollit anim id est laborum.
    30. </ModalBody>
    31. <ModalFooter>
    32. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
    33. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
    34. </ModalFooter>
    35. </Modal>
    36. </div>
    37. );
    38. }
    39. }
    40. export default ModalExample;

    Destructuring

    Modals - 图3

    Focus after close

    1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
    2. import React from 'react';
    3. import { Button, Modal, ModalBody, ModalFooter, Label, Input, FormGroup, Form } from 'reactstrap';
    4. class ModalFocusAfterClose extends React.Component {
    5. constructor() {
    6. super();
    7. this.state = {
    8. open: false,
    9. focusAfterClose: true
    10. };
    11. this.toggle = this.toggle.bind(this);
    12. this.handleSelectChange = this.handleSelectChange.bind(this);
    13. }
    14. toggle() {
    15. this.setState(({ open }) => ({ open: !open }));
    16. }
    17. handleSelectChange({target: { value }}) {
    18. this.setState({ focusAfterClose: JSON.parse(value) });
    19. }
    20. render() {
    21. return (
    22. <div>
    23. <Form inline onSubmit={(e) => e.preventDefault()}>
    24. <FormGroup>
    25. <Label for="focusAfterClose">Focus After Close</Label>
    26. <Input className="mx-2" type="select" id="focusAfterClose" onChange={this.handleSelectChange}>
    27. <option value="true">Yes</option>
    28. <option value="false">No</option>
    29. </Input>
    30. </FormGroup>
    31. <Button color="danger" onClick={this.toggle}>Open</Button>
    32. </Form>
    33. <Modal returnFocusAfterClose={this.state.focusAfterClose} isOpen={this.state.open}>
    34. <ModalBody>
    35. Observe the "Open" button. It will be focused after close when "returnFocusAfterClose" is true and will not be focused if "returnFocusAfterClose" is false.
    36. </ModalBody>
    37. <ModalFooter>
    38. <Button color="primary" onClick={this.toggle}>Close</Button>
    39. </ModalFooter>
    40. </Modal>
    41. </div>
    42. )
    43. }
    44. }