Skip to content

Commit

Permalink
example theme switch to p-theme-toggle__button class, set label in ja…
Browse files Browse the repository at this point in the history
…vascript
  • Loading branch information
jmuzina committed May 24, 2024
1 parent 5e52e34 commit 67bbe5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 0 additions & 4 deletions scss/docs/example.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ body {
position: fixed;
right: 0;

.u-theme-toggle__button {
text-transform: capitalize;
}

@media only percy {
visibility: hidden !important;
}
Expand Down
13 changes: 11 additions & 2 deletions templates/static/js/example-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ var activeTheme = DEFAULT_COLOR_THEME;
return `js-${themeName}-theme-toggle`;
}

/**
* Converts a string to titlecase (first letter capitalized & subsequent letters lowercase)
* @param {String} str
* @returns {string}
*/
function titleCase(str) {
return `${str.charAt(0).toUpperCase()}${str.slice(1).toLowerCase()}`
}

/**
* Sets a color theme as active; removes all other color themes from active status
* @param {String} themeToSelect
Expand Down Expand Up @@ -128,7 +137,7 @@ var activeTheme = DEFAULT_COLOR_THEME;
if (SUPPORTED_THEMES?.length > 1) {
var themeSwitcherControls = SUPPORTED_THEMES.map(
(themeName) =>
`<button id="${convertThemeNameToButtonIdentifier(themeName)}" class="p-segmented-control__button u-theme-toggle__button is-dense" role="button" aria-selected="${body.classList.contains(convertThemeNameToClassName(themeName))}" data-color-theme-name="${themeName}">${themeName}</button>`,
`<button id="${convertThemeNameToButtonIdentifier(themeName)}" class="p-segmented-control__button p-theme-toggle__button is-dense" role="button" aria-selected="${body.classList.contains(convertThemeNameToClassName(themeName))}" data-color-theme-name="${themeName}">${titleCase(themeName)}</button>`,
);
var themeSwitcherSegmentedControl = fragmentFromString(
`<div class="p-segmented-control u-theme-toggle"><div class="p-segmented-control__list">${themeSwitcherControls.join('')}</div></div>`,
Expand All @@ -147,7 +156,7 @@ var activeTheme = DEFAULT_COLOR_THEME;
body.appendChild(controls);

// Below code relies on the controls already existing in the DOM, so must come after `body.appendChild`.
var themeToggleButtons = document.querySelectorAll('.u-theme-toggle__button');
var themeToggleButtons = document.querySelectorAll('.p-theme-toggle__button');
themeToggleButtons.forEach((themeToggleButton) => {
themeToggleButton.addEventListener('click', () => {
selectColorTheme(themeToggleButton.getAttribute('data-color-theme-name'));
Expand Down

0 comments on commit 67bbe5a

Please sign in to comment.