frequi_origin/src/components/ThemeSelect.vue

49 lines
1.4 KiB
Vue
Raw Normal View History

2020-05-22 21:02:17 +00:00
<template>
2023-03-23 18:28:13 +00:00
<b-link variant="outline-primary" class="nav-link" @click="toggleNight">
<ThemeLightDark :size="16" />
</b-link>
2020-05-22 21:02:17 +00:00
</template>
2023-03-23 17:23:14 +00:00
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import ThemeLightDark from 'vue-material-design-icons/Brightness6.vue';
import { useSettingsStore } from '@/stores/settings';
2020-05-22 21:02:17 +00:00
2023-03-23 17:23:14 +00:00
const activeTheme = ref('');
const settingsStore = useSettingsStore();
2022-04-17 08:07:48 +00:00
2023-03-23 17:23:14 +00:00
const setTheme = (themeName) => {
// If theme is already active, do nothing.
if (activeTheme.value === themeName) {
return;
}
if (themeName.toLowerCase() === 'bootstrap' || themeName.toLowerCase() === 'bootstrap_dark') {
2023-03-23 18:28:13 +00:00
// const styles = document.getElementsByTagName('style');
2023-03-23 17:23:14 +00:00
document.documentElement.setAttribute(
'data-theme',
themeName.toLowerCase() === 'bootstrap' ? 'light' : 'dark',
);
2023-03-23 18:28:13 +00:00
if (activeTheme.value) {
2023-03-23 17:23:14 +00:00
// Only transition if simple mode is active
document.documentElement.classList.add('ft-theme-transition');
window.setTimeout(() => {
document.documentElement.classList.remove('ft-theme-transition');
}, 1000);
}
}
// Save the theme as localstorage
settingsStore.currentTheme = themeName;
activeTheme.value = themeName;
};
2022-04-17 08:07:48 +00:00
2023-03-23 17:23:14 +00:00
onMounted(() => {
if (settingsStore.currentTheme) setTheme(settingsStore.currentTheme);
});
2023-03-23 17:23:14 +00:00
const toggleNight = () => {
setTheme(activeTheme.value === 'bootstrap' ? 'bootstrap_dark' : 'bootstrap');
};
2020-05-22 21:02:17 +00:00
</script>
<style scoped></style>