Skip to content

Commit

Permalink
fix(Modal): add/remove dimmer classes in raf (#2010)
Browse files Browse the repository at this point in the history
* fix(Modal): add/remove dimmer classes in raf

* test(Modal): abstract assertBodyContains

* fix(Modal): add/remove dimmer classes properly
  • Loading branch information
levithomason authored Sep 30, 2017
1 parent 5a37f28 commit 9c97ed9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
32 changes: 15 additions & 17 deletions src/modules/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,8 @@ class Modal extends Component {

handlePortalMount = (e) => {
debug('handlePortalMount()')
const { dimmer } = this.props
const mountNode = this.getMountNode()

if (dimmer) {
debug('adding dimmer')
mountNode.classList.add('dimmable')
mountNode.classList.add('dimmed')

if (dimmer === 'blurring') {
debug('adding blurred dimmer')
mountNode.classList.add('blurring')
}
}

this.setPosition()
this.setPositionAndClassNames()

const { onMount } = this.props
if (onMount) onMount(e, this.props)
Expand Down Expand Up @@ -241,9 +228,20 @@ class Modal extends Component {

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

setPosition = () => {
setPositionAndClassNames = () => {
const { dimmer } = this.props
const mountNode = this.getMountNode()

if (dimmer) {
mountNode.classList.add('dimmable')
mountNode.classList.add('dimmed')

if (dimmer === 'blurring') {
mountNode.classList.add('blurring')
}
}

if (this.ref) {
const mountNode = this.getMountNode()
const { height } = this.ref.getBoundingClientRect()

const marginTop = -Math.round(height / 2)
Expand All @@ -268,7 +266,7 @@ class Modal extends Component {
if (Object.keys(newState).length > 0) this.setState(newState)
}

this.animationRequestId = requestAnimationFrame(this.setPosition)
this.animationRequestId = requestAnimationFrame(this.setPositionAndClassNames)
}

renderContent = (rest) => {
Expand Down
38 changes: 12 additions & 26 deletions test/specs/modules/Modal/Modal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ModalActions from 'src/modules/Modal/ModalActions'
import ModalDescription from 'src/modules/Modal/ModalDescription'
import Portal from 'src/addons/Portal/Portal'

import { assertNodeContains, assertBodyContains, domEvent, sandbox } from 'test/utils'
import { assertNodeContains, assertBodyClasses, assertBodyContains, domEvent, sandbox } from 'test/utils'
import * as common from 'test/specs/commonTests'

// ----------------------------------------
Expand All @@ -20,20 +20,6 @@ let wrapper
const wrapperMount = (...args) => (wrapper = mount(...args))
const wrapperShallow = (...args) => (wrapper = shallow(...args))

const assertBodyClasses = (...rest) => {
const hasClasses = typeof rest[rest.length - 1] === 'boolean' ? rest.pop() : true

rest.forEach((className) => {
const didFind = document.body.classList.contains(className)
const message = [
`document.body ${didFind ? 'has' : 'does not have'} class "${className}".`,
`It has class="${document.body.classList}"`,
].join(' ')

didFind.should.equal(hasClasses, message)
})
}

describe('Modal', () => {
beforeEach(() => {
wrapper = undefined
Expand Down Expand Up @@ -242,13 +228,13 @@ describe('Modal', () => {

describe('true', () => {
it('adds/removes body classes "dimmable dimmed" on mount/unmount', () => {
assertBodyClasses('dimmable', 'dimmed', false)
assertBodyClasses('dimmable dimmed', false)

wrapperMount(<Modal open dimmer />)
assertBodyClasses('dimmable', 'dimmed')
assertBodyClasses('dimmable dimmed')

wrapper.unmount()
assertBodyClasses('dimmable', 'dimmed', false)
assertBodyClasses('dimmable dimmed', false)
})

it('adds a dimmer to the body', () => {
Expand All @@ -260,24 +246,24 @@ describe('Modal', () => {
describe('false', () => {
it('does not render a dimmer', () => {
wrapperMount(<Modal open dimmer={false} />)
assertBodyClasses('dimmable', 'dimmed', 'blurring', false)
assertBodyClasses('dimmable dimmed blurring', false)
})

it('does not add any dimmer classes to the body', () => {
wrapperMount(<Modal open dimmer={false} />)
assertBodyClasses('dimmable', 'dimmed', 'blurring', false)
assertBodyClasses('dimmable dimmed blurring', false)
})
})

describe('blurring', () => {
it('adds/removes body classes "dimmable dimmed blurring" on mount/unmount', () => {
assertBodyClasses('dimmable', 'dimmed', 'blurring', false)
assertBodyClasses('dimmable dimmed blurring', false)

wrapperMount(<Modal open dimmer='blurring' />)
assertBodyClasses('dimmable', 'dimmed', 'blurring')
assertBodyClasses('dimmable dimmed blurring')

wrapper.unmount()
assertBodyClasses('dimmable', 'dimmed', 'blurring', false)
assertBodyClasses('dimmable dimmed blurring', false)
})

it('adds a dimmer to the body', () => {
Expand All @@ -288,13 +274,13 @@ describe('Modal', () => {

describe('inverted', () => {
it('adds/removes body classes "dimmable dimmed" on mount/unmount', () => {
assertBodyClasses('dimmable', 'dimmed', false)
assertBodyClasses('dimmable dimmed', false)

wrapperMount(<Modal open dimmer />)
assertBodyClasses('dimmable', 'dimmed')
assertBodyClasses('dimmable dimmed')

wrapper.unmount()
assertBodyClasses('dimmable', 'dimmed', false)
assertBodyClasses('dimmable dimmed', false)
})

it('adds an inverted dimmer to the body', () => {
Expand Down
22 changes: 22 additions & 0 deletions test/utils/assertBodyClasses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Assert whether or not the document.body has the specified HTML classes.
*
* @param {string|string[]} classes An array of strings string of HTML classes
* @param {boolean} [hasClasses=true] Indicating whether to assert "has" or "does not have" classes
*/
const assertBodyClasses = (classes, hasClasses = true) => {
const classesArr = [].concat(classes).join(' ').split(' ')

classesArr.forEach((className) => {
const didFind = document.body.classList.contains(className)

const message = [
`document.body should ${hasClasses ? 'have' : 'not have'} class "${className}",`,
`but it ${didFind ? 'has' : 'does not have'} class="${document.body.classList}"`,
].join(' ')

didFind.should.equal(hasClasses, message)
})
}

export default assertBodyClasses
1 change: 1 addition & 0 deletions test/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as assertBodyClasses } from './assertBodyClasses'
export * from './assertNodeContains'
export { default as consoleUtil } from './consoleUtil'
export { default as domEvent } from './domEvent'
Expand Down

0 comments on commit 9c97ed9

Please sign in to comment.