forked from reactjs/react-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[added] Support using multiple document.body classes
* update `bodyOpenClassName` prop to handle adding and removing multiple class names * update String#includes polyfill to work properly * ensure shared classes on `document.body` persist on one modal close if multiple modals are open * create new helper for adding/removing class names from body element * remove unmaintained and obsolete `element-class` library * rename refCount private variable `modals` to `classListMap` * create `get` method on refCount helper for public access to the class list count
- Loading branch information
Showing
7 changed files
with
93 additions
and
27 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,10 +1,22 @@ | ||
### CSS Classes | ||
|
||
Sometimes it may be preferable to use CSS classes rather than inline styles. You can use the `className` and `overlayClassName` props to specify a given CSS class for each of those. | ||
You can override the default class that is added to `document.body` when the modal is open by defining a property `bodyOpenClassName`. | ||
Sometimes it may be preferable to use CSS classes rather than inline styles. | ||
|
||
It's required that `bodyOpenClassName` must be `constant string`, otherwise we would end up with a complex system to manage which class name | ||
should appear or be removed from `document.body` from which modal (if using multiple modals simultaneously). | ||
You can use the `className` and `overlayClassName` props to specify a given CSS | ||
class for each of those. | ||
|
||
You can override the default class that is added to `document.body` when the | ||
modal is open by defining a property `bodyOpenClassName`. | ||
|
||
It's required that `bodyOpenClassName` must be `constant string`, otherwise we | ||
would end up with a complex system to manage which class name should appear or | ||
be removed from `document.body` from which modal (if using multiple modals | ||
simultaneously). | ||
|
||
`bodyOpenClassName` can support adding multiple classes to `document.body` when | ||
the modal is open. Add as many class names as you desire, delineated by spaces. | ||
|
||
Note: If you provide those props all default styles will not be applied, leaving | ||
all styles under control of the CSS class. | ||
|
||
Note: If you provide those props all default styles will not be applied, leaving all styles under control of the CSS class. | ||
The `portalClassName` can also be used however there are no styles by default applied |
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
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,20 @@ | ||
import * as refCount from './refCount'; | ||
|
||
export function add (bodyClass) { | ||
// Increment class(es) on refCount tracker and add class(es) to body | ||
bodyClass | ||
.split(' ') | ||
.map(refCount.add) | ||
.forEach(className => document.body.classList.add(className)); | ||
} | ||
|
||
export function remove (bodyClass) { | ||
const classListMap = refCount.get(); | ||
// Decrement class(es) from the refCount tracker | ||
// and remove unused class(es) from body | ||
bodyClass | ||
.split(' ') | ||
.map(refCount.remove) | ||
.filter(className => classListMap[className] === 0) | ||
.forEach(className => document.body.classList.remove(className)); | ||
} |
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,23 +1,26 @@ | ||
const modals = {}; | ||
const classListMap = {}; | ||
|
||
export function get() { | ||
return classListMap; | ||
} | ||
|
||
export function add(bodyClass) { | ||
// Set variable and default if none | ||
if (!modals[bodyClass]) { | ||
modals[bodyClass] = 0; | ||
if (!classListMap[bodyClass]) { | ||
classListMap[bodyClass] = 0; | ||
} | ||
modals[bodyClass] += 1; | ||
classListMap[bodyClass] += 1; | ||
return bodyClass; | ||
} | ||
|
||
export function remove(bodyClass) { | ||
if (modals[bodyClass]) { | ||
modals[bodyClass] -= 1; | ||
if (classListMap[bodyClass]) { | ||
classListMap[bodyClass] -= 1; | ||
} | ||
} | ||
|
||
export function count(bodyClass) { | ||
return modals[bodyClass]; | ||
return bodyClass; | ||
} | ||
|
||
export function totalCount() { | ||
return Object.keys(modals).reduce((acc, curr) => acc + modals[curr], 0); | ||
return Object.keys(classListMap) | ||
.reduce((acc, curr) => acc + classListMap[curr], 0); | ||
} |