-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class name manager utility for
document.body
element
- Loading branch information
Richard Palmer
committed
Mar 15, 2017
1 parent
c8dbd6a
commit 9df40eb
Showing
5 changed files
with
186 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
.documentBody { | ||
overflow: hidden; | ||
} | ||
|
||
.root { | ||
position: absolute; | ||
height: 100%; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.noScroll { | ||
overflow: hidden; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { canUseDOM } from 'exenv'; | ||
|
||
import css from './BodyClassNameConductor.css'; | ||
|
||
export default class BodyClassNameConductor { | ||
static classNameOriginMap = {}; | ||
|
||
constructor(origin) { | ||
this.origin = origin; | ||
} | ||
|
||
getClassNameOriginMap = className => | ||
BodyClassNameConductor.classNameOriginMap[className] || new Set(); | ||
|
||
getBody = () => { | ||
if (!this.body) this.body = document.querySelector('body'); | ||
return this.body; | ||
} | ||
|
||
addClassNameOrigin = (className) => { | ||
const originList = this.getClassNameOriginMap(className); | ||
originList.add(this.origin); | ||
|
||
BodyClassNameConductor.classNameOriginMap = Object.assign({}, | ||
BodyClassNameConductor.classNameOriginMap, { | ||
[className]: originList, | ||
} | ||
); | ||
}; | ||
|
||
removeClassNameOrigin = (className) => { | ||
const originList = this.getClassNameOriginMap(className); | ||
originList.delete(this.origin); | ||
|
||
BodyClassNameConductor.classNameOriginMap = Object.assign({}, | ||
BodyClassNameConductor.classNameOriginMap, { | ||
[className]: originList, | ||
} | ||
); | ||
}; | ||
|
||
add = (className) => { | ||
if (!canUseDOM) return; | ||
|
||
this.addClassNameOrigin(className); | ||
|
||
if (!this.getBody().classList.contains(css[className])) { | ||
this.getBody().classList.add(css[className]); | ||
} | ||
}; | ||
|
||
remove = (className) => { | ||
if (!canUseDOM) return; | ||
|
||
this.removeClassNameOrigin(className); | ||
|
||
if ( | ||
this.getClassNameOriginMap(className).size <= 0 && | ||
this.getBody().classList.contains(css[className]) | ||
) { | ||
this.getBody().classList.remove(css[className]); | ||
} | ||
}; | ||
|
||
toggle = (className) => { | ||
if (!canUseDOM) return; | ||
|
||
const originMap = this.getClassNameOriginMap(className); | ||
|
||
if (originMap.has(this.origin)) { | ||
this.remove(className); | ||
} else { | ||
this.add(className); | ||
} | ||
}; | ||
} |
102 changes: 102 additions & 0 deletions
102
utils/BodyClassNameConductor/BodyClassNameConductor.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import BodyClassNameConductor from './BodyClassNameConductor'; | ||
|
||
const mocked = { | ||
foo: 'bar', | ||
baz: 'baz', | ||
}; | ||
|
||
jest.mock('./BodyClassNameConductor.css', () => ({ | ||
foo: 'bar', | ||
baz: 'baz', | ||
})); | ||
|
||
it('adds a classname to the body', () => { | ||
const conductor = new BodyClassNameConductor('foo'); | ||
const body = document.querySelector('body'); | ||
conductor.add('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(true); | ||
}); | ||
|
||
it('removes a classname from the body', () => { | ||
const conductor = new BodyClassNameConductor('foo'); | ||
const body = document.querySelector('body'); | ||
|
||
conductor.add('foo'); | ||
conductor.remove('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(false); | ||
}); | ||
|
||
it('toggles a classname on the body', () => { | ||
const conductor = new BodyClassNameConductor('foo'); | ||
const body = document.querySelector('body'); | ||
conductor.toggle('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(true); | ||
|
||
conductor.toggle('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(false); | ||
}); | ||
|
||
it('add multiple classnames to the body', () => { | ||
const conductor = new BodyClassNameConductor('foo'); | ||
const body = document.querySelector('body'); | ||
conductor.add('foo'); | ||
conductor.add('baz'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(true); | ||
expect(body.classList.contains(mocked.baz)).toBe(true); | ||
}); | ||
|
||
it('remove multiple classnames from the body', () => { | ||
const conductor = new BodyClassNameConductor('foo'); | ||
const body = document.querySelector('body'); | ||
conductor.add('foo'); | ||
conductor.add('baz'); | ||
|
||
conductor.remove('foo'); | ||
conductor.remove('baz'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(false); | ||
expect(body.classList.contains(mocked.baz)).toBe(false); | ||
}); | ||
|
||
test('that a classname is only removed when no origins references it', () => { | ||
const conductorA = new BodyClassNameConductor('a'); | ||
const conductorB = new BodyClassNameConductor('b'); | ||
const body = document.querySelector('body'); | ||
|
||
conductorA.add('foo'); | ||
conductorB.add('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(true); | ||
|
||
conductorA.remove('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(true); | ||
|
||
conductorB.remove('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(false); | ||
}); | ||
|
||
test('that a classname is only toggled when no origins references it', () => { | ||
const conductorA = new BodyClassNameConductor('a'); | ||
const conductorB = new BodyClassNameConductor('b'); | ||
const body = document.querySelector('body'); | ||
|
||
conductorA.add('foo'); | ||
conductorB.add('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(true); | ||
|
||
conductorA.toggle('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(true); | ||
|
||
conductorB.toggle('foo'); | ||
|
||
expect(body.classList.contains(mocked.foo)).toBe(false); | ||
}); |