-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use count instead of instances
- Loading branch information
Andrew Fuller
committed
Jun 24, 2017
1 parent
8f30171
commit a78049b
Showing
4 changed files
with
51 additions
and
65 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
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,6 @@ | ||
import ExecutionEnvironment from 'exenv'; | ||
|
||
const EE = ExecutionEnvironment; | ||
|
||
export const SafeHTMLElement = EE.canUseDOM ? window.HTMLElement : {}; | ||
export const AppElement = EE.canUseDOM ? document.body : { appendChild() {} }; |
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,63 +1,28 @@ | ||
import elementClass from 'element-class'; | ||
|
||
import * as ariaAppHider from './ariaAppHider'; | ||
|
||
const modals = {}; | ||
|
||
function totalCount() { | ||
// Find all modal types and filter out empty arrays | ||
const values = Object.keys(modals) | ||
.map(key => modals[key]) | ||
.filter(el => el.length) || []; | ||
return values.length; | ||
} | ||
|
||
export function add(element) { | ||
const { props } = element; | ||
const identifier = props.id || props.contentLabel || props; | ||
const bodyClassName = props.bodyOpenClassName; | ||
|
||
export function add(bodyClass) { | ||
// Set variable and default if none | ||
let modalReference = modals[bodyClassName]; | ||
if (!modalReference) { | ||
modals[bodyClassName] = []; | ||
modalReference = modals[bodyClassName]; | ||
} | ||
|
||
// Add reference to modal for specified key if it does not exist already | ||
if (modalReference.indexOf(identifier) === -1) { | ||
modalReference.push(identifier); | ||
if (!modals[bodyClass]) { | ||
modals[bodyClass] = 0; | ||
} | ||
modals[bodyClass] += 1; | ||
elementClass(document.body).add(bodyClass); | ||
} | ||
|
||
if (modalReference.length > 0) { | ||
elementClass(document.body).add(bodyClassName); | ||
export function remove(bodyClass) { | ||
if (modals[bodyClass]) { | ||
modals[bodyClass] -= 1; | ||
} | ||
|
||
// Add aria-hidden to appELement | ||
if (props.ariaHideApp) { | ||
ariaAppHider.hide(props.appElement); | ||
// Remove class if no more modals are open | ||
if (modals[bodyClass] === 0) { | ||
elementClass(document.body).remove(bodyClass); | ||
} | ||
} | ||
|
||
export function remove(element) { | ||
const { props } = element; | ||
const identifier = props.id || props.contentLabel || props; | ||
const bodyClassName = props.bodyOpenClassName; | ||
const modalReference = modals[bodyClassName]; | ||
|
||
// Remove className if no more references | ||
if (modalReference) { | ||
const index = modalReference.indexOf(identifier); | ||
if (index !== -1) { | ||
modalReference.splice(index, 1); | ||
} | ||
if (modalReference.length === 0) { | ||
elementClass(document.body).remove(bodyClassName); | ||
} | ||
} | ||
|
||
// Reset aria-hidden attribute if all modals have been removed | ||
if (props.ariaHideApp && totalCount() < 1) { | ||
ariaAppHider.show(props.appElement); | ||
} | ||
export function totalCount() { | ||
const count = Object.keys(modals) | ||
.reduce((acc, curr) => acc + modals[curr], 0); | ||
return count; | ||
} |