homebox/frontend/composables/use-theme.ts
Hayden bd321af29f
chore: developer cleanup (#300)
* new PR tasks

* add homebox to know words

* formatting

* bump deps

* generate db models

* ts errors

* drop id

* fix accessor

* drop unused time field

* change CI

* add expected error

* add type check

* resolve serveral type errors

* hoise in CI
2023-02-17 21:41:01 -09:00

40 lines
839 B
TypeScript

import { ComputedRef } from "vue";
import { DaisyTheme } from "~~/lib/data/themes";
export interface UseTheme {
theme: ComputedRef<DaisyTheme>;
setTheme: (theme: DaisyTheme) => void;
}
const themeRef = ref<DaisyTheme>("garden");
export function useTheme(): UseTheme {
const preferences = useViewPreferences();
themeRef.value = preferences.value.theme;
const setTheme = (newTheme: DaisyTheme) => {
preferences.value.theme = newTheme;
if (htmlEl) {
htmlEl.value?.setAttribute("data-theme", newTheme);
}
themeRef.value = newTheme;
};
const htmlEl = ref<HTMLElement | null>();
onMounted(() => {
if (htmlEl.value) {
return;
}
htmlEl.value = document.querySelector("html");
});
const theme = computed(() => {
return themeRef.value;
});
return { theme, setTheme };
}