Skip to content

Commit b3d8eda

Browse files
brianespinosalayershifter
authored andcommitted
breaking(Modal): Fix vertical position with SUI 2.3 (#2690)
* Update SUI css to 2.3.1 * Disable negative top margin calc * Add the ability to apply inline styles to Portal * Top Aligned modal example * Make sure we are not overriding the top margin inline * type defs updated for Portal * Type update for Modal * Refactor to use setProperty instead of string for important * Remove inline-style lib to handle string differently
1 parent 4a90965 commit b3d8eda

File tree

8 files changed

+86
-7
lines changed

8 files changed

+86
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
import { Button, Header, Image, Modal } from 'semantic-ui-react'
3+
4+
const ModalExampleTopAligned = () => (
5+
<Modal trigger={<Button>Show Modal</Button>} centered={false}>
6+
<Modal.Header>Select a Photo</Modal.Header>
7+
<Modal.Content image>
8+
<Image wrapped size='medium' src='/assets/images/avatar/large/rachel.png' />
9+
<Modal.Description>
10+
<Header>Default Profile Image</Header>
11+
<p>We've found the following gravatar image associated with your e-mail address.</p>
12+
<p>Is it okay to use this photo?</p>
13+
</Modal.Description>
14+
</Modal.Content>
15+
</Modal>
16+
)
17+
18+
export default ModalExampleTopAligned

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

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const ModalExamples = () => (
1818
description='A modal can reduce its complexity.'
1919
examplePath='modules/Modal/Types/ModalExampleBasic'
2020
/>
21+
<ComponentExample
22+
title='Top Aligned'
23+
description='A modal can be top aligned.'
24+
examplePath='modules/Modal/Types/ModalExampleTopAligned'
25+
/>
2126
<ComponentExample
2227
title='Scrolling Modal'
2328
description={[

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
"require-dir": "^0.3.2",
136136
"rimraf": "^2.6.1",
137137
"satisfied": "^1.1.1",
138-
"semantic-ui-css": "^2.3.0",
138+
"semantic-ui-css": "^2.3.1",
139139
"simulant": "^0.2.2",
140140
"sinon": "^3.2.0",
141141
"sinon-chai": "^2.13.0",

src/addons/Portal/Portal.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ export interface PortalProps {
101101
/** Controls whether the portal should be prepended to the mountNode instead of appended. */
102102
prepend?: boolean;
103103

104+
/** Any inline styles to the Portal container. */
105+
style?: object;
106+
104107
/** Element to be rendered in-place where the portal is defined. */
105108
trigger?: React.ReactNode;
106109
}

src/addons/Portal/Portal.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class Portal extends Component {
123123
/** Controls whether the portal should be prepended to the mountNode instead of appended. */
124124
prepend: PropTypes.bool,
125125

126+
/** Any inline styles to the Portal container. */
127+
style: PropTypes.object,
128+
126129
/** Element to be rendered in-place where the portal is defined. */
127130
trigger: PropTypes.node,
128131
}
@@ -344,14 +347,15 @@ class Portal extends Component {
344347
if (!this.state.open) return
345348
debug('renderPortal()')
346349

347-
const { children, className, eventPool } = this.props
350+
const { children, className, eventPool, style } = this.props
348351

349352
this.mountPortal()
350353

351354
// Server side rendering
352355
if (!isBrowser()) return null
353356

354357
this.rootNode.className = className || ''
358+
this.rootNode.style = style || ''
355359

356360
// when re-rendering, first remove listeners before re-adding them to the new node
357361
if (this.portalNode) {

src/modules/Modal/Modal.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export interface ModalProps extends PortalProps {
1919
/** A Modal can reduce its complexity */
2020
basic?: boolean;
2121

22+
/** A modal can be vertically centered in the viewport */
23+
centered?: boolean;
24+
2225
/** Primary content. */
2326
children?: React.ReactNode;
2427

src/modules/Modal/Modal.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class Modal extends Component {
4141
/** A modal can reduce its complexity */
4242
basic: PropTypes.bool,
4343

44+
/** A modal can be vertically centered in the viewport */
45+
centered: PropTypes.bool,
46+
4447
/** Primary content. */
4548
children: PropTypes.node,
4649

@@ -140,6 +143,7 @@ class Modal extends Component {
140143
}
141144

142145
static defaultProps = {
146+
centered: true,
143147
dimmer: true,
144148
closeOnDimmerClick: true,
145149
closeOnDocumentClick: false,
@@ -216,6 +220,18 @@ class Modal extends Component {
216220

217221
handleRef = c => (this.ref = c)
218222

223+
handlePortalRef = c => (this.portalRef = c)
224+
225+
setRootNodeStyle = () => {
226+
debug('setRootNodeStyle()')
227+
228+
if (!this.portalRef) return
229+
230+
if (this.portalRef) {
231+
this.portalRef.rootNode.style.setProperty('display', 'flex', 'important')
232+
}
233+
}
234+
219235
setPositionAndClassNames = () => {
220236
const { dimmer } = this.props
221237
let classes
@@ -233,7 +249,10 @@ class Modal extends Component {
233249
if (this.ref) {
234250
const { height } = this.ref.getBoundingClientRect()
235251

236-
const marginTop = -Math.round(height / 2)
252+
// Leaving the old calculation here since we may need it as an older browser fallback
253+
// SEE: https://github.com/Semantic-Org/Semantic-UI/issues/6185#issuecomment-376725956
254+
// const marginTop = -Math.round(height / 2)
255+
const marginTop = null
237256
const scrolling = height >= window.innerHeight
238257

239258
if (this.state.marginTop !== marginTop) {
@@ -251,6 +270,8 @@ class Modal extends Component {
251270
if (!_.isEmpty(newState)) this.setState(newState)
252271

253272
this.animationRequestId = requestAnimationFrame(this.setPositionAndClassNames)
273+
274+
this.setRootNodeStyle()
254275
}
255276

256277
renderContent = (rest) => {
@@ -312,7 +333,7 @@ class Modal extends Component {
312333

313334
render() {
314335
const { open } = this.state
315-
const { closeOnDimmerClick, closeOnDocumentClick, dimmer, eventPool, trigger } = this.props
336+
const { centered, closeOnDimmerClick, closeOnDocumentClick, dimmer, eventPool, trigger } = this.props
316337
const mountNode = this.getMountNode()
317338

318339
// Short circuit when server side rendering
@@ -336,6 +357,7 @@ class Modal extends Component {
336357
: cx(
337358
'ui',
338359
dimmer === 'inverted' && 'inverted',
360+
!centered && 'top aligned',
339361
'page modals dimmer transition visible active',
340362
)
341363

@@ -364,6 +386,7 @@ class Modal extends Component {
364386
onMount={this.handlePortalMount}
365387
onOpen={this.handleOpen}
366388
onUnmount={this.handlePortalUnmount}
389+
ref={this.handlePortalRef}
367390
>
368391
{this.renderContent(rest)}
369392
</Portal>

yarn.lock

+26-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22
# yarn lockfile v1
33

44

5+
"@f/css-default-units@^1.0.1":
6+
version "1.0.1"
7+
resolved "https://registry.yarnpkg.com/@f/css-default-units/-/css-default-units-1.0.1.tgz#5f1758b86a392591d979f6b1024e0080744b6eaa"
8+
dependencies:
9+
"@f/css-unitless" "^1.0.1"
10+
11+
"@f/css-unitless@^1.0.1":
12+
version "1.0.2"
13+
resolved "https://registry.yarnpkg.com/@f/css-unitless/-/css-unitless-1.0.2.tgz#8da6e884a57e4d27798b744584e094862aa503e9"
14+
dependencies:
15+
"@f/hyphenate" "^1.0.0"
16+
17+
"@f/hyphenate@^1.0.0":
18+
version "1.0.0"
19+
resolved "https://registry.yarnpkg.com/@f/hyphenate/-/hyphenate-1.0.0.tgz#dc1633324d05636da20e25fe66d6de01a1ffedd8"
20+
21+
"@f/to-inline-style@^0.1.4":
22+
version "0.1.4"
23+
resolved "https://registry.yarnpkg.com/@f/to-inline-style/-/to-inline-style-0.1.4.tgz#2e7bf7d386e808b93b68473e8d5974a819313711"
24+
dependencies:
25+
"@f/css-default-units" "^1.0.1"
26+
"@f/hyphenate" "^1.0.0"
27+
528
"@types/node@*":
629
version "8.0.53"
730
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"
@@ -6394,9 +6417,9 @@ scss-tokenizer@^0.2.3:
63946417
js-base64 "^2.1.8"
63956418
source-map "^0.4.2"
63966419

6397-
semantic-ui-css@^2.3.0:
6398-
version "2.3.0"
6399-
resolved "https://registry.yarnpkg.com/semantic-ui-css/-/semantic-ui-css-2.3.0.tgz#e96d0d4069499eafff9ba0c84274bc23d2b084e7"
6420+
semantic-ui-css@^2.3.1:
6421+
version "2.3.1"
6422+
resolved "https://registry.yarnpkg.com/semantic-ui-css/-/semantic-ui-css-2.3.1.tgz#a5485c640c98cce29d8ddde3eff3434566a068e0"
64006423
dependencies:
64016424
jquery x.*
64026425

0 commit comments

Comments
 (0)