-
Notifications
You must be signed in to change notification settings - Fork 921
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 #5242 from uswds/jm-banner-init
USWDS - Banner: Add accordion dependency
- Loading branch information
Showing
3 changed files
with
44 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,43 @@ | ||
const behavior = require("../../uswds-core/src/js/utils/behavior"); | ||
const { CLICK } = require("../../uswds-core/src/js/events"); | ||
const { prefix: PREFIX } = require("../../uswds-core/src/js/config"); | ||
const accordion = require("../../usa-accordion/src/index"); | ||
|
||
const HEADER = `.${PREFIX}-banner__header`; | ||
const EXPANDED_CLASS = `${PREFIX}-banner__header--expanded`; | ||
|
||
/** | ||
* Toggle expanded banner class. | ||
* | ||
* @param {*} event - The clicked banner button. | ||
*/ | ||
const toggleBanner = function toggleEl(event) { | ||
event.preventDefault(); | ||
this.closest(HEADER).classList.toggle(EXPANDED_CLASS); | ||
event.target.closest(HEADER).classList.toggle(EXPANDED_CLASS); | ||
}; | ||
|
||
module.exports = behavior({ | ||
[CLICK]: { | ||
[`${HEADER} [aria-controls]`]: toggleBanner, | ||
const banner = behavior( | ||
{ | ||
[CLICK]: { | ||
[`${HEADER} [aria-controls]`]: toggleBanner, | ||
}, | ||
}, | ||
}); | ||
{ | ||
init(root) { | ||
// Initialize accordion if it hasn't already. | ||
// Required for modular import of Banner. | ||
if (!accordion.hasInit) { | ||
accordion.on(root); | ||
accordion.hasInit = true; | ||
} | ||
}, | ||
teardown(root) { | ||
if (accordion.hasInit) { | ||
accordion.off(root); | ||
accordion.hasInit = false; | ||
} | ||
}, | ||
} | ||
); | ||
|
||
module.exports = banner; |
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