builtin.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <script setup lang="ts">
  2. import type { BuiltinThemeType } from '@vben/types';
  3. import { computed, ref } from 'vue';
  4. import { TinyColor, convertToHsl } from '@vben-core/colorful';
  5. import { MdiEditBoxOutline } from '@vben-core/iconify';
  6. import { $t } from '@vben-core/locales';
  7. import {
  8. BUILT_IN_THEME_PRESETS,
  9. type BuiltinThemePreset,
  10. } from '@vben-core/preferences';
  11. defineOptions({
  12. name: 'PreferenceBuiltinTheme',
  13. });
  14. const props = defineProps<{ isDark: boolean }>();
  15. const colorInput = ref();
  16. const modelValue = defineModel<BuiltinThemeType>({ default: 'default' });
  17. const themeColorPrimary = defineModel<string>('themeColorPrimary');
  18. const inputValue = computed(() => {
  19. return new TinyColor(themeColorPrimary.value).toHexString();
  20. });
  21. function typeView(name: BuiltinThemeType) {
  22. switch (name) {
  23. case 'default': {
  24. return $t('preferences.theme.builtin.default');
  25. }
  26. case 'violet': {
  27. return $t('preferences.theme.builtin.violet');
  28. }
  29. case 'pink': {
  30. return $t('preferences.theme.builtin.pink');
  31. }
  32. case 'rose': {
  33. return $t('preferences.theme.builtin.rose');
  34. }
  35. case 'sky-blue': {
  36. return $t('preferences.theme.builtin.sky-blue');
  37. }
  38. case 'deep-blue': {
  39. return $t('preferences.theme.builtin.deep-blue');
  40. }
  41. case 'green': {
  42. return $t('preferences.theme.builtin.green');
  43. }
  44. case 'deep-green': {
  45. return $t('preferences.theme.builtin.deep-green');
  46. }
  47. case 'orange': {
  48. return $t('preferences.theme.builtin.orange');
  49. }
  50. case 'yellow': {
  51. return $t('preferences.theme.builtin.yellow');
  52. }
  53. case 'zinc': {
  54. return $t('preferences.theme.builtin.zinc');
  55. }
  56. case 'neutral': {
  57. return $t('preferences.theme.builtin.neutral');
  58. }
  59. case 'slate': {
  60. return $t('preferences.theme.builtin.slate');
  61. }
  62. case 'gray': {
  63. return $t('preferences.theme.builtin.gray');
  64. }
  65. case 'custom': {
  66. return $t('preferences.theme.builtin.custom');
  67. }
  68. }
  69. }
  70. function handleSelect(theme: BuiltinThemePreset) {
  71. modelValue.value = theme.type;
  72. const primaryColor = props.isDark
  73. ? theme.darkPrimaryColor || theme.primaryColor
  74. : theme.primaryColor;
  75. themeColorPrimary.value = primaryColor || theme.color;
  76. }
  77. function handleInputChange(e: Event) {
  78. const target = e.target as HTMLInputElement;
  79. themeColorPrimary.value = convertToHsl(target.value);
  80. }
  81. function selectColor() {
  82. colorInput.value?.[0]?.click?.();
  83. }
  84. </script>
  85. <template>
  86. <div class="flex w-full flex-wrap justify-between">
  87. <template v-for="theme in BUILT_IN_THEME_PRESETS" :key="theme.type">
  88. <div class="flex cursor-pointer flex-col" @click="handleSelect(theme)">
  89. <div
  90. :class="{
  91. 'outline-box-active': theme.type === modelValue,
  92. }"
  93. class="outline-box flex-center group cursor-pointer"
  94. >
  95. <template v-if="theme.type !== 'custom'">
  96. <div
  97. :style="{ backgroundColor: theme.color }"
  98. class="mx-10 my-2 size-5 rounded-md"
  99. ></div>
  100. </template>
  101. <template v-else>
  102. <div class="size-full px-10 py-2" @click.stop="selectColor">
  103. <div class="flex-center relative size-5 rounded-sm">
  104. <MdiEditBoxOutline
  105. class="absolute z-10 size-5 opacity-60 group-hover:opacity-100"
  106. />
  107. <input
  108. ref="colorInput"
  109. :value="inputValue"
  110. class="absolute inset-0 opacity-0"
  111. type="color"
  112. @input="handleInputChange"
  113. />
  114. </div>
  115. </div>
  116. </template>
  117. </div>
  118. <div class="text-muted-foreground my-2 text-center text-xs">
  119. {{ typeView(theme.type) }}
  120. </div>
  121. </div>
  122. </template>
  123. </div>
  124. </template>