Skip to content

Commit 7874292

Browse files
lewisblackwoodlevithomason
authored andcommitted
docs(Modal): Add controlled modal example (#769)
* Add controlled modal example * docs(Modal): fix open prop description
1 parent 504eab3 commit 7874292

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { Component } from 'react'
2+
import { Button, Header, Icon, Modal } from 'semantic-ui-react'
3+
4+
export default class ModalExampleControlled extends Component {
5+
state = { modalOpen: false }
6+
7+
handleOpen = (e) => this.setState({
8+
modalOpen: true,
9+
})
10+
11+
handleClose = (e) => this.setState({
12+
modalOpen: false,
13+
})
14+
15+
render() {
16+
return (
17+
<Modal
18+
trigger={<Button onClick={this.handleOpen}>Show Modal</Button>}
19+
open={this.state.modalOpen}
20+
onClose={this.handleClose}
21+
basic
22+
size='small'
23+
>
24+
<Header icon='browser' content='Cookies policy' />
25+
<Modal.Content>
26+
<h3>This website uses cookies to ensure the best user experience.</h3>
27+
</Modal.Content>
28+
<Modal.Actions>
29+
<Button color='green' onClick={this.handleClose} inverted>
30+
<Icon name='checkmark' /> Got it
31+
</Button>
32+
</Modal.Actions>
33+
</Modal>
34+
)
35+
}
36+
}

docs/app/Examples/modules/Modal/Types/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const ModalExamples = () => (
3434
description='Multiple modals can be displayed on top of one another.'
3535
examplePath='modules/Modal/Types/ModalExampleMultiple'
3636
/>
37+
<ComponentExample
38+
title='Controlled'
39+
description='A modal can be a controlled component'
40+
examplePath='modules/Modal/Types/ModalExampleControlled'
41+
/>
3742
</ExampleSection>
3843
)
3944

src/modules/Modal/Modal.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class Modal extends Component {
6969
/** Called when the portal is unmounted from the DOM */
7070
onUnmount: PropTypes.func,
7171

72+
/** Controls whether or not the Modal is displayed. */
73+
open: PropTypes.bool,
74+
7275
/** A modal can vary in size */
7376
size: PropTypes.oneOf(_meta.props.size),
7477

@@ -169,7 +172,7 @@ class Modal extends Component {
169172
}
170173

171174
render() {
172-
const { basic, children, className, dimmer, mountNode, size } = this.props
175+
const { basic, children, className, dimmer, mountNode, open, size } = this.props
173176

174177
// Short circuit when server side rendering
175178
if (!isBrowser) return null
@@ -225,6 +228,7 @@ class Modal extends Component {
225228
onMount={this.handlePortalMount}
226229
onOpen={this.handleOpen}
227230
onUnmount={this.handlePortalUnmount}
231+
open={open}
228232
>
229233
{modalJSX}
230234
</Portal>

0 commit comments

Comments
 (0)