feat(color-picker): dynamic trigger background color (#793)

* feat(color-picker): dynamic trigger background color

* fix(color-picker): fix color shake problem

* fix(color-picker): fix trigger can not update when is black

* fix(color-picker): fix trigger can not abandon color when click cancel
This commit is contained in:
GaoNeng 2023-11-13 15:27:51 +08:00 committed by GitHub
parent b538c831a6
commit 6e2363472a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 18 deletions

View File

@ -6,10 +6,12 @@ export const onConfirm = (
triggerBg: IColorSelectPanelRef<string>, triggerBg: IColorSelectPanelRef<string>,
res: IColorSelectPanelRef<string>, res: IColorSelectPanelRef<string>,
emit, emit,
isShow: IColorSelectPanelRef<boolean> isShow: IColorSelectPanelRef<boolean>,
prev: IColorPickerRef<string>
) => { ) => {
return (color: string) => { return (color: string) => {
res.value = color prev.value = res.value;
res.value = color
hex.value = res.value hex.value = res.value
triggerBg.value = res.value triggerBg.value = res.value
emit('confirm', res.value) emit('confirm', res.value)
@ -23,41 +25,44 @@ export const onCancel = (
emit, emit,
isShow: IColorSelectPanelRef<boolean>, isShow: IColorSelectPanelRef<boolean>,
hex: IColorSelectPanelRef<string>, hex: IColorSelectPanelRef<string>,
color: Color color: Color, prev: IColorPickerRef<string>
) => { ) => {
return () => { return () => {
if (isShow.value) { if (isShow.value) {
res.value = triggerBg.value res.value = prev.value;
hex.value = triggerBg.value hex.value = prev.value;
triggerBg.value = prev.value;
color.reset(hex.value) color.reset(hex.value)
emit('cancel') emit('cancel')
} }
isShow.value = false isShow.value = false
} }
} }
export const onColorUpdate = (color: Color, res: IColorSelectPanelRef<string>, triggerBg?: IColorPickerRef<string>) => { export const onColorUpdate = (color: Color, res: IColorSelectPanelRef<string>, triggerBg: IColorPickerRef<string>) => {
res.value = color.getHex() res.value = color.getHex()
triggerBg.value = color.getHex();
} }
export const onHSVUpdate = (color: Color, res: IColorSelectPanelRef<string>, hex: IColorSelectPanelRef<string>) => { export const onHSVUpdate = (color: Color, res: IColorSelectPanelRef<string>, hex: IColorSelectPanelRef<string>, triggerBg: IColorSelectPanelRef<string>) => {
return { return {
onHueUpdate: (hue: number) => { onHueUpdate: (hue: number) => {
color.set({ h: hue }) color.set({ h: hue })
onColorUpdate(color, res) onColorUpdate(color, res, triggerBg)
hex.value = color.getHex() hex.value = color.getHex()
}, },
onSVUpdate: ({ s, v }: { s: number; v: number }) => { onSVUpdate: ({ s, v }: { s: number; v: number }) => {
color.set({ s, v }) color.set({ h: color.get('h'), s, v })
onColorUpdate(color, res) onColorUpdate(color, res, triggerBg)
hex.value = color.getHex();
} }
} }
} }
export const onAlphaUpdate = (color: Color, res: IColorSelectPanelRef<string>) => { export const onAlphaUpdate = (color: Color, res: IColorSelectPanelRef<string>, triggerBg: IColorSelectPanelRef<string>) => {
return { return {
update: (alpha: number) => { update: (alpha: number) => {
color.set({ a: alpha }) color.set({ a: alpha })
onColorUpdate(color, res) onColorUpdate(color, res, triggerBg)
} }
} }
} }

View File

@ -19,6 +19,7 @@ export const renderless = (props, context, { emit }) => {
const { modelValue, visible, predefine, size, history } = context.toRefs(props) const { modelValue, visible, predefine, size, history } = context.toRefs(props)
const hex = context.ref(modelValue.value ?? 'transparent') const hex = context.ref(modelValue.value ?? 'transparent')
const res = context.ref(modelValue.value ?? 'transparent') const res = context.ref(modelValue.value ?? 'transparent')
const prev = context.ref(modelValue.value ?? 'transparent')
const triggerBg = context.ref(modelValue.value ?? 'transparent') const triggerBg = context.ref(modelValue.value ?? 'transparent')
const isShow = context.ref(visible?.value ?? false) const isShow = context.ref(visible?.value ?? false)
const cursor: Ref<HTMLElement> = context.ref() const cursor: Ref<HTMLElement> = context.ref()
@ -53,21 +54,22 @@ export const renderless = (props, context, { emit }) => {
hex.value = newValue hex.value = newValue
res.value = newValue res.value = newValue
triggerBg.value = newValue triggerBg.value = newValue
prev.value = newValue;
color.reset(hex.value) color.reset(hex.value)
}) })
context.watch(visible, (visible) => { context.watch(visible, (visible) => {
isShow.value = visible isShow.value = visible
}) })
const { onHueUpdate, onSVUpdate } = onHSVUpdate(color, res, hex) const { onHueUpdate, onSVUpdate } = onHSVUpdate(color, res, hex, triggerBg)
const { update } = onAlphaUpdate(color, res) const { update } = onAlphaUpdate(color, res, triggerBg)
const api = { const api = {
state, state,
changeVisible, changeVisible,
onHueUpdate, onHueUpdate,
onSVUpdate, onSVUpdate,
onConfirm: onConfirm(hex, triggerBg, res, emit, isShow), onConfirm: onConfirm(hex, triggerBg, res, emit, isShow, prev),
onCancel: onCancel(res, triggerBg, emit, isShow, hex, color), onCancel: onCancel(res, triggerBg, emit, isShow, hex, color, prev),
onAlphaUpdate: update, onAlphaUpdate: update,
cursor cursor
} }

View File

@ -58,8 +58,8 @@ import CollapseItem from '@opentiny/vue-collapse-item'
import Input from '@opentiny/vue-input' import Input from '@opentiny/vue-input'
import { renderless, api } from '@opentiny/vue-renderless/color-select-panel/vue' import { renderless, api } from '@opentiny/vue-renderless/color-select-panel/vue'
import { props, setup, defineComponent, directive } from '@opentiny/vue-common' import { props, setup, defineComponent, directive } from '@opentiny/vue-common'
import HueSelect from './components/hue-select' import HueSelect from './components/hue-select.vue'
import AlphaSelect from './components/alpha-select' import AlphaSelect from './components/alpha-select.vue'
import '@opentiny/vue-theme/color-select-panel/index.less' import '@opentiny/vue-theme/color-select-panel/index.less'
import Clickoutside from '@opentiny/vue-renderless/common/deps/clickoutside' import Clickoutside from '@opentiny/vue-renderless/common/deps/clickoutside'
import { t } from '@opentiny/vue-locale' import { t } from '@opentiny/vue-locale'