-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from justeat/module
Added toggle function with unit tests.
- Loading branch information
Showing
9 changed files
with
684 additions
and
9 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,3 +1,6 @@ | ||
{ | ||
"presets": ["es2015"] | ||
"presets": [ | ||
"es2015", | ||
"babili" | ||
] | ||
} |
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,67 @@ | ||
/** | ||
* @overview Fozzie vanilla JS toggle library. | ||
* | ||
* @module toggle | ||
*/ | ||
|
||
import $ from 'qwery'; | ||
|
||
|
||
/** | ||
* Handles the toggle element click events | ||
* | ||
* @property {object} toggle Toggle an element's visibility. | ||
* @property {object} show Shows an element. | ||
* @property {object} hide Hides an element. | ||
*/ | ||
const toggles = { | ||
toggle (element) { | ||
element.classList.toggle('is-hidden'); | ||
}, | ||
|
||
show (element) { | ||
element.classList.remove('is-hidden'); | ||
}, | ||
|
||
hide (element) { | ||
element.classList.add('is-hidden'); | ||
} | ||
}; | ||
|
||
/** | ||
* Handles the toggle element click events | ||
* | ||
* @param {string|string[]} targets | ||
*/ | ||
const handleToggles = targets => { | ||
if (!Array.isArray(targets)) { | ||
handleToggles(targets.split(' ')); | ||
return; | ||
} | ||
|
||
targets.forEach(target => { | ||
const parts = target.split(':'); | ||
|
||
if (parts.length === 1) { | ||
parts.unshift('toggle'); | ||
} | ||
|
||
$(`[data-toggle-name~=${parts[1]}]`) | ||
.forEach(toggles[parts[0]]); | ||
}); | ||
|
||
}; | ||
|
||
/** | ||
* Bind the toggle element click events | ||
*/ | ||
export default () => { | ||
$('[data-toggle-target]') | ||
.forEach(toggle => { | ||
toggle.addEventListener('click', e => { | ||
e.preventDefault(); | ||
|
||
handleToggles(toggle.getAttribute('data-toggle-target')); | ||
}); | ||
}); | ||
}; |
Empty file.
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,101 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`hide applies hidden class 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"is-hidden\\"></div> | ||
<button data-toggle-target=\\"hide:test\\"></button> | ||
" | ||
`; | ||
exports[`hide can handle multiple specified targets 1`] = ` | ||
" | ||
<div data-toggle-name=\\"one\\" class=\\"is-hidden\\"></div> | ||
<div data-toggle-name=\\"two\\" class=\\"is-hidden\\"></div> | ||
<button data-toggle-target=\\"hide:one hide:two\\"></button> | ||
" | ||
`; | ||
exports[`hide can handle multiple targets 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"is-hidden\\"></div> | ||
<div data-toggle-name=\\"test\\" class=\\"is-hidden\\"></div> | ||
<button data-toggle-target=\\"hide:test\\"></button> | ||
" | ||
`; | ||
exports[`hide does nothing when element is already hidden 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"is-hidden\\"></div> | ||
<button data-toggle-target=\\"hide:test\\"></button> | ||
" | ||
`; | ||
exports[`mixed toggles shows and hides elements correctly 1`] = ` | ||
" | ||
<div data-toggle-name=\\"one\\" class=\\"is-hidden\\"></div> | ||
<div data-toggle-name=\\"two\\" class=\\"\\"></div> | ||
<div data-toggle-name=\\"three\\" class=\\"\\"></div> | ||
<div data-toggle-name=\\"four\\" class=\\"is-hidden\\"></div> | ||
<button data-toggle-target=\\"one two show:three hide:four\\"></button> | ||
" | ||
`; | ||
exports[`show can handle multiple specified targets 1`] = ` | ||
" | ||
<div data-toggle-name=\\"one\\" class=\\"\\"></div> | ||
<div data-toggle-name=\\"two\\" class=\\"\\"></div> | ||
<button data-toggle-target=\\"show:one show:two\\"></button> | ||
" | ||
`; | ||
exports[`show can handle multiple targets 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"\\"></div> | ||
<div data-toggle-name=\\"test\\" class=\\"\\"></div> | ||
<button data-toggle-target=\\"show:test\\"></button> | ||
" | ||
`; | ||
exports[`show does nothing when element is already shown 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"\\"></div> | ||
<button data-toggle-target=\\"show:test\\"></button> | ||
" | ||
`; | ||
exports[`show removes hidden class 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"\\"></div> | ||
<button data-toggle-target=\\"show:test\\"></button> | ||
" | ||
`; | ||
exports[`toggle applies hidden class 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"is-hidden\\"></div> | ||
<button data-toggle-target=\\"test\\"></button> | ||
" | ||
`; | ||
exports[`toggle can handle multiple specified targets 1`] = ` | ||
" | ||
<div data-toggle-name=\\"one\\" class=\\"\\"></div> | ||
<div data-toggle-name=\\"two\\" class=\\"is-hidden\\"></div> | ||
<button data-toggle-target=\\"one two\\"></button> | ||
" | ||
`; | ||
exports[`toggle can handle multiple targets 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"is-hidden\\"></div> | ||
<div data-toggle-name=\\"test\\" class=\\"\\"></div> | ||
<button data-toggle-target=\\"test\\"></button> | ||
" | ||
`; | ||
exports[`toggle removes hidden class 1`] = ` | ||
" | ||
<div data-toggle-name=\\"test\\" class=\\"\\"></div> | ||
<button data-toggle-target=\\"test\\"></button> | ||
" | ||
`; |
Oops, something went wrong.