Skip to content

Commit

Permalink
refactor(projects): refactor ColorPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 13, 2023
1 parent 5d883f5 commit f80c1ae
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 100 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"apifox",
"clsx",
"colord",
"colorpicker",
"consola",
"Destructurable",
"EDITMSG",
"espree",
"execa",
"gridicons",
"heroicons",
"HEXA",
"hexcode",
"iconify",
"INDEXEDDB",
Expand All @@ -26,8 +26,10 @@
"nocheck",
"nprogress",
"ofetch",
"pickr",
"preflights",
"sider",
"simonwep",
"simplebar",
"tada",
"Uncapitalize",
Expand Down
4 changes: 2 additions & 2 deletions packages/materials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
},
"dependencies": {
"@sa/utils": "workspace:*",
"simplebar-vue": "2.3.3",
"vue3-colorpicker": "^2.2.2"
"@simonwep/pickr": "1.9.0",
"simplebar-vue": "2.3.3"
},
"devDependencies": {
"typed-css-modules": "0.8.0"
Expand Down
106 changes: 98 additions & 8 deletions packages/materials/src/libs/color-picker/index.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,115 @@
<script setup lang="ts">
import { computed } from 'vue';
import { ColorPicker } from 'vue3-colorpicker';
import 'vue3-colorpicker/style.css';
import { ref, watch, onMounted, onUnmounted } from 'vue';
import ColorPicker from '@simonwep/pickr';
import '@simonwep/pickr/dist/themes/nano.min.css';
defineOptions({
name: 'ColorPicker'
});
interface Props {
darkMode?: boolean;
color: string;
palettes?: string[];
disabled?: boolean;
}
const props = defineProps<Props>();
const props = withDefaults(defineProps<Props>(), {
palettes: () => [
'#3b82f6',
'#6366f1',
'#8b5cf6',
'#a855f7',
'#0ea5e9',
'#06b6d4',
'#f43f5e',
'#ef4444',
'#ec4899',
'#d946ef',
'#f97316',
'#f59e0b',
'#eab308',
'#84cc16',
'#22c55e',
'#10b981',
'#14b8a6'
]
});
interface Emits {
(e: 'update:color', value: string): void;
}
const emit = defineEmits<Emits>();
const domRef = ref<HTMLElement | null>(null);
const instance = ref<ColorPicker | null>(null);
function handleColorChange(hsva: ColorPicker.HSVaColor) {
const color = hsva.toHEXA().toString();
emit('update:color', color);
}
function initColorPicker() {
if (!domRef.value) return;
instance.value = ColorPicker.create({
el: domRef.value,
theme: 'nano',
swatches: props.palettes,
lockOpacity: true,
default: props.color,
disabled: props.disabled,
components: {
preview: true,
opacity: false,
hue: true,
interaction: {
hex: true,
rgba: true,
input: true
}
}
});
instance.value.on('change', handleColorChange);
}
function updateDisabled(disabled: boolean) {
if (!instance.value) return;
if (disabled) {
instance.value.disable();
} else {
instance.value.enable();
}
}
function destroyColorPicker() {
if (!instance.value) return;
const color = defineModel<string>('color');
instance.value.off('change', handleColorChange);
instance.value.destroyAndRemove();
instance.value = null;
}
watch(
() => props.disabled,
value => {
updateDisabled(value);
}
);
const theme = computed(() => (props.darkMode ? 'black' : 'white'));
onMounted(() => {
initColorPicker();
});
onUnmounted(() => {
destroyColorPicker();
});
</script>

<template>
<ColorPicker v-model:pure-color="color" :theme="theme" />
<div ref="domRef"></div>
</template>

<style scoped></style>
74 changes: 16 additions & 58 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/layouts/modules/theme-drawer/components/setting-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ defineProps<Props>();

<template>
<div class="flex-y-center justify-between w-full">
<span class="text-base_text">{{ label }}</span>
<div>
<span class="text-base_text pr-8px">{{ label }}</span>
<slot name="suffix"></slot>
</div>
<slot></slot>
</div>
</template>
Expand Down
25 changes: 11 additions & 14 deletions src/layouts/modules/theme-drawer/modules/theme-color.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { ColorPicker } from '@sa/materials';
import { useThemeStore } from '@/store/modules/theme';
import { $t } from '@/locales';
import SettingItem from '../components/setting-item.vue';
defineOptions({
Expand All @@ -13,20 +14,16 @@ const themeStore = useThemeStore();
<template>
<ADivider>{{ $t('theme.themeColor.title') }}</ADivider>
<div class="flex-vertical-stretch gap-12px">
<SettingItem :label="$t('theme.themeColor.primary')">
<ColorPicker v-model:color="themeStore.themeColor" :dark-mode="themeStore.darkMode" />
</SettingItem>
<SettingItem :label="$t('theme.themeColor.info')">
<ColorPicker v-model:color="themeStore.otherColor.info" :dark-mode="themeStore.darkMode" />
</SettingItem>
<SettingItem :label="$t('theme.themeColor.success')">
<ColorPicker v-model:color="themeStore.otherColor.success" :dark-mode="themeStore.darkMode" />
</SettingItem>
<SettingItem :label="$t('theme.themeColor.warning')">
<ColorPicker v-model:color="themeStore.otherColor.warning" :dark-mode="themeStore.darkMode" />
</SettingItem>
<SettingItem :label="$t('theme.themeColor.error')">
<ColorPicker v-model:color="themeStore.otherColor.error" :dark-mode="themeStore.darkMode" />
<SettingItem v-for="(_, key) in themeStore.themeColors" :key="key" :label="$t(`theme.themeColor.${key}`)">
<template v-if="key === 'info'" #suffix>
<ACheckbox v-model:checked="themeStore.isCustomizeInfoColor">
{{ $t('theme.themeColor.followPrimary') }}
</ACheckbox>
</template>
<ColorPicker
v-model:color="themeStore.themeColors[key]"
:disabled="key === 'info' && !themeStore.isCustomizeInfoColor"
/>
</SettingItem>
</div>
</template>
Expand Down
2 changes: 1 addition & 1 deletion src/locales/lang/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const local: App.I18n.Schema = {
success: 'Success',
warning: 'Warning',
error: 'Error',
customizeInfo: 'Customize Info'
followPrimary: 'Follow Primary'
}
},
route: {
Expand Down
2 changes: 1 addition & 1 deletion src/locales/lang/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const local: App.I18n.Schema = {
success: '成功色',
warning: '警告色',
error: '错误色',
customizeInfo: '自定义信息色'
followPrimary: '跟随主色'
}
},
route: {
Expand Down
Loading

0 comments on commit f80c1ae

Please sign in to comment.