diff --git a/packages/dye/components/Canvas/ColorCanvas.vue b/packages/dye/components/Canvas/ColorCanvas.vue index b1e1197..cb998a1 100644 --- a/packages/dye/components/Canvas/ColorCanvas.vue +++ b/packages/dye/components/Canvas/ColorCanvas.vue @@ -18,8 +18,8 @@ const emit = defineEmits<{ }>() const props = defineProps<{ - getRef: () => Ref - setRef: (el: any) => void + colorCanvas: () => Ref + setColorCanvas: (el: any) => void color: { value: string name: string @@ -39,14 +39,14 @@ function colorChange(e: MouseEvent, click = false) { if (click) mousedown.value = true if (offCanvas(e, click)) return if (isActiveCanvas(e.target)) return - const hex = canvasPixelColor(e, props.getRef().value) + const hex = canvasPixelColor(e, props.colorCanvas().value) updateCanvas(hex) mouseOn.value = true } //when outside canvas const { mouseOn } = outsideCanvas({ - canvas: props.getRef(), + canvas: props.colorCanvas(), updateCanvas }) @@ -56,11 +56,11 @@ function getHue(color: string = props.color.value) { } const { width, height } = responsiveCanvas({ - canvas: props.getRef(), + canvas: props.colorCanvas(), updateCanvas: () => { const hue = getHue() const data = { hue, saturation: 100, lightness: 100 } - fillColorCanvas(data, props.getRef().value) + fillColorCanvas(data, props.colorCanvas().value) } }) @@ -83,7 +83,7 @@ watch(width, () => {
(), { compactSize: 50 }) +const [colorCanvas, setColorCanvas] = useColorCanvas() const pickerRef = ref(null) -const colorCanvas = ref(null) - const color = ref({ name: 'red', value: props.default @@ -49,18 +48,9 @@ watch(color, (c) => { }).apply({ target: pickerRef.value }) }) -function getRef(): Ref { - return colorCanvas -} - -function setRef(el: HTMLCanvasElement) { - colorCanvas.value = el -} - function handleChange(hex?: hexType, mounted = false) { if (!hex) return - const get = colorName(hex.color) - const { name, value } = get() + const { name, value } = colorName(hex.color)() color.value = { name, value } if (mounted) return @@ -83,10 +73,15 @@ const compactSize = ref(props.compactSize) v-on-click-outside="() => (compact = true)" > - +
diff --git a/packages/dye/composables/canvas.ts b/packages/dye/composables/canvas.ts index e76ba16..8e75f4e 100644 --- a/packages/dye/composables/canvas.ts +++ b/packages/dye/composables/canvas.ts @@ -169,3 +169,9 @@ export function getDimentions(canvas: HTMLCanvasElement, frame = { height: 100, } } } + +type UCCP = [() => Ref, (el: HTMLCanvasElement) => void] +export function useColorCanvas(): UCCP { + const colorCanvas = ref(null) + return [() => colorCanvas, (el: HTMLCanvasElement) => (colorCanvas.value = el)] +}