Files
webawesome/docs/assets/scripts/preset-theme.js
Lindsay M e046015ed5 Theme cleanup (#414)
* formatting, docs tweaks

* FA theme cleanup

* Revert removal of `--wa-form-control-height-*`

* Classic theme cleanup

* Use consistent selectors for dark mode

* Clean slate for additional themes

* Retire depth stylesheets

* Move header styles for themer out of themes

* Missed instance of dark mode selector

* Migration theme cleanup

* Brutalist theme cleanup

* Changelog for new themes

* Playful theme cleanup

* Formatting

* Default theme update

* Add tests for form theming

* Fix test typo

* Change misnamed `multiplier` properties to `scale`

* Active theme cleanup

* Mellow theme cleanup

* Cleanup unused FA styles

* Glassy theme progress

* Add checked styles to glassy

* Fix typo

* Final FA theme cleanup

* Rename FA theme to 'Awesome', allow in alpha

* Final brutalist theme cleanup

* A few more brutalist tweaks

* One last brutalist tweak

* Final mellow theme cleanup

* Final Tailwind theme cleanup

* Final active theme cleanup

* Some classic theme cleanup

* Remove unused `--wa-form-control-height-*`

* Rename `--wa-form-control-resting-color` to `border-color`

* Touch up callout `appearance` styles

* Add more themes to alpha

* Add changelog for removal of `--wa-form-control-height-*`

* Clean up colors

* Final classic theme cleanup

* Add new themes to alpha build

* Re-add teal (used by Awesome theme)

* sync mobile/desktop theme pickers and don't dup IDs

* Remove `color/standard.css` (duplicates `/themes/default/color.css`)

* add view transition

* Add premium theme for later cleanup

* Accommodate new tag `appearance` styles

* Revise `--wa-form-control-height-*` changelog entry

Co-authored-by: Lea Verou <lea@verou.me>

* Improve `--wa-border-width-scale` description

Co-authored-by: Lea Verou <lea@verou.me>

* Better border docs

* Premium tweaks

---------

Co-authored-by: Cory LaViska <cory@abeautifulsite.net>
Co-authored-by: Lea Verou <lea@verou.me>
2025-01-07 15:41:57 -05:00

100 lines
3.3 KiB
JavaScript

(() => {
function applyThemeChange(stylesheet, newStylesheet) {
newStylesheet.rel = 'stylesheet';
newStylesheet.id = stylesheet.id;
requestAnimationFrame(() => {
stylesheet.remove();
});
}
function setPresetTheme(newPresetTheme) {
presetTheme = newPresetTheme;
localStorage.setItem('presetTheme', presetTheme);
const stylesheet = document.getElementById('theme-stylesheet');
const newStylesheet = Object.assign(document.createElement('link'), {
href: `/dist/styles/themes/${presetTheme}.css`,
rel: 'preload',
as: 'style',
});
newStylesheet.addEventListener(
'load',
() => {
const canUseViewTransitions =
document.startViewTransition && !window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (canUseViewTransitions) {
document.startViewTransition(() => applyThemeChange(stylesheet, newStylesheet));
} else {
applyThemeChange(stylesheet, newStylesheet);
}
},
{ once: true },
);
document.head.append(newStylesheet);
updateSelection();
document.documentElement.classList.toggle(`wa-theme-${presetTheme}-dark`, window.isDark());
}
function updateSelection(container = document) {
const menus = container.querySelectorAll('.preset-theme-selector wa-menu');
if (!menus) return;
[...menus].forEach(menu => {
// Clear all checked states
[...menu.querySelectorAll('wa-menu-item')].forEach(async item => {
await customElements.whenDefined(item.localName);
await item.updateComplete;
item.checked = false; // Reset all items to unchecked first
});
// Then set only the selected item as checked
const selectedItem = menu.querySelector(`wa-menu-item[value="${presetTheme}"]`);
if (selectedItem) {
customElements.whenDefined(selectedItem.localName).then(async () => {
await selectedItem.updateComplete;
selectedItem.checked = true;
container.querySelector('.preset-theme-selector__text').textContent = selectedItem.innerText;
});
}
});
}
let presetTheme = window.getPresetTheme();
// Selection is not preserved when changing page, so update when opening dropdown
document.addEventListener('wa-show', event => {
const presetThemeSelector = event.target.closest('.preset-theme-selector');
if (!presetThemeSelector) return;
updateSelection();
});
// Listen for selections
document.addEventListener('wa-select', event => {
const menu = event.target.closest('.preset-theme-selector wa-menu');
if (!menu) return;
setPresetTheme(event.detail.item.value);
});
// Update the color scheme when the preference changes
window.matchMedia('(prefers-preset-theme: dark)').addEventListener('change', () => setPresetTheme(presetTheme));
updateSelection();
/**
* Without this, there's a flash of the incorrect preset theme.
*/
function updateSelectionBeforeTurboLoad(e) {
const newElement = e.detail.newBody || e.detail.newFrame || e.detail.newStream;
if (!newElement) {
return;
}
updateSelection(newElement);
}
['turbo:before-render', 'turbo:before-stream-render', 'turbo:before-frame-render'].forEach(eventName => {
document.addEventListener(eventName, updateSelectionBeforeTurboLoad);
});
})();