Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: theme switching when document.startViewTransition is undefined #9

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions src/layouts/Navbar/components/ThemeChange/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { logger } from "@kirklin/logger";
import { themeList } from "./data";
import type { CustomTheme } from "~/layouts/Navbar/components/ThemeChange/types";

Expand Down Expand Up @@ -42,34 +43,44 @@ const mode = useColorMode<CustomTheme>({

const changeTheme = (event: MouseEvent, theme: CustomTheme) => {
const isSameTheme = mode.value === theme;
const x = event.clientX;
const y = event.clientY;
const endRadius = Math.hypot(
Math.max(x, innerWidth - x),
Math.max(y, innerHeight - y),
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const transition = document.startViewTransition();
transition.ready.then(() => {
const clipPath = [
`circle(0px at ${x}px ${y}px)`,
`circle(${endRadius}px at ${x}px ${y}px)`,
];
document.documentElement.animate(
{
clipPath: isSameTheme ? [...clipPath].reverse() : clipPath,
},
{
duration: 500,
easing: "ease-in",
pseudoElement: isSameTheme
? "::view-transition-old(root)"
: "::view-transition-new(root)",
},
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (document.startViewTransition === undefined) {
throw new Error("document.startViewTransition is undefined, please update your browser to the latest version or use a modern browser.");
}
const x = event.clientX;
const y = event.clientY;
const endRadius = Math.hypot(
Math.max(x, innerWidth - x),
Math.max(y, innerHeight - y),
);
});
mode.value = theme;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const transition = document.startViewTransition();
transition.ready.then(() => {
const clipPath = [
`circle(0px at ${x}px ${y}px)`,
`circle(${endRadius}px at ${x}px ${y}px)`,
];
document.documentElement.animate(
{
clipPath: isSameTheme ? [...clipPath].reverse() : clipPath,
},
{
duration: 500,
easing: "ease-in",
pseudoElement: isSameTheme
? "::view-transition-old(root)"
: "::view-transition-new(root)",
},
);
});
} catch (error: unknown) {
logger.error(`Failed to change theme : ${error instanceof Error ? error.message : ""}`);
} finally {
mode.value = theme;
}
};
</script>

Expand Down