gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<script lang="ts" setup>
import { computed, nextTick } from 'vue';
 
import { VbenButton } from '..\..\..\..\..\@core\ui-kit\shadcn-ui\src';
 
interface Props {
  /**
   * 类型
   */
  type?: 'icon' | 'normal';
}
 
defineOptions({
  name: 'ThemeToggleButton',
});
 
const props = withDefaults(defineProps<Props>(), {
  type: 'normal',
});
 
const isDark = defineModel<boolean>();
 
const theme = computed(() => {
  return isDark.value ? 'light' : 'dark';
});
 
const bindProps = computed(() => {
  const type = props.type;
 
  return type === 'normal'
    ? {
        variant: 'heavy' as const,
      }
    : {
        class: 'rounded-full',
        size: 'icon' as const,
        style: { padding: '7px' },
        variant: 'icon' as const,
      };
});
 
function toggleTheme(event: MouseEvent) {
  const isAppearanceTransition =
    // @ts-expect-error - startViewTransition is not available in the current DOM lib target
    document.startViewTransition &&
    !window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  if (!isAppearanceTransition || !event) {
    isDark.value = !isDark.value;
    return;
  }
  const x = event.clientX;
  const y = event.clientY;
  const endRadius = Math.hypot(
    Math.max(x, innerWidth - x),
    Math.max(y, innerHeight - y),
  );
  const transition = document.startViewTransition(async () => {
    isDark.value = !isDark.value;
    await nextTick();
  });
  transition.ready.then(() => {
    const clipPath = [
      `circle(0px at ${x}px ${y}px)`,
      `circle(${endRadius}px at ${x}px ${y}px)`,
    ];
    const animate = document.documentElement.animate(
      {
        clipPath: isDark.value ? [...clipPath].toReversed() : clipPath,
      },
      {
        duration: 450,
        easing: 'ease-in',
        pseudoElement: isDark.value
          ? '::view-transition-old(root)'
          : '::view-transition-new(root)',
      },
    );
    animate.onfinish = () => {
      transition.skipTransition();
    };
  });
}
</script>
 
<template>
  <VbenButton
    :aria-label="theme"
    :class="[`is-${theme}`]"
    aria-live="polite"
    class="theme-toggle cursor-pointer border-none bg-none hover:animate-[shrink_0.3s_ease-in-out]"
    v-bind="bindProps"
    @click.stop="toggleTheme"
  >
    <svg aria-hidden="true" height="24" viewBox="0 0 24 24" width="24">
      <mask id="theme-toggle-moon" class="theme-toggle__moon">
        <rect fill="white" height="100%" width="100%" x="0" y="0" />
        <circle cx="40" cy="8" fill="black" r="11" />
      </mask>
      <circle
        id="sun"
        class="theme-toggle__sun fill-foreground/90"
        cx="12"
        cy="12"
        mask="url(#theme-toggle-moon)"
        r="11"
      />
      <g class="theme-toggle__sun-beams stroke-foreground/90 stroke-2">
        <line x1="12" x2="12" y1="1" y2="3" />
        <line x1="12" x2="12" y1="21" y2="23" />
        <line x1="4.22" x2="5.64" y1="4.22" y2="5.64" />
        <line x1="18.36" x2="19.78" y1="18.36" y2="19.78" />
        <line x1="1" x2="3" y1="12" y2="12" />
        <line x1="21" x2="23" y1="12" y2="12" />
        <line x1="4.22" x2="5.64" y1="19.78" y2="18.36" />
        <line x1="18.36" x2="19.78" y1="5.64" y2="4.22" />
      </g>
    </svg>
  </VbenButton>
</template>
 
<style scoped>
@reference "@vben/tailwind-config/theme";
 
.theme-toggle__moon > circle {
  transition: transform 0.5s cubic-bezier(0, 0, 0.3, 1);
}
 
.theme-toggle__sun {
  stroke: none;
  transform-origin: center center;
  transition: transform 1.6s cubic-bezier(0.25, 0, 0.2, 1);
}
 
.theme-toggle__sun-beams {
  transform-origin: center center;
  transition:
    transform 1.6s cubic-bezier(0.5, 1.5, 0.75, 1.25),
    opacity 0.6s cubic-bezier(0.25, 0, 0.3, 1);
}
 
.theme-toggle.is-light .theme-toggle__sun {
  @apply scale-50;
}
 
.theme-toggle.is-light .theme-toggle__sun-beams {
  transform: rotateZ(0.25turn);
}
 
.theme-toggle.is-dark .theme-toggle__moon > circle {
  transform: translateX(-20px);
}
 
.theme-toggle.is-dark .theme-toggle__sun-beams {
  @apply opacity-0;
}
 
.theme-toggle:hover > svg .theme-toggle__sun {
  fill: hsl(var(--foreground));
}
 
.theme-toggle:hover > svg .theme-toggle__sun-beams {
  stroke: hsl(var(--foreground));
}
</style>