Skip to content

Commit

Permalink
lots of type and props improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
CarelessCourage committed Jan 29, 2024
1 parent 0cd8765 commit 9453ce1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
5 changes: 4 additions & 1 deletion packages/dye/components/Canvas/ColorCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
import { fillColorCanvas } from '../../composables/gradient'
import Handle from '../Handle.vue'
const emit = defineEmits(['change'])
const emit = defineEmits<{
(e: 'change', hex: hexType): void
}>()
const props = defineProps<{
getRef: () => Ref<HTMLCanvasElement | null>
setRef: (el: any) => void
Expand Down
19 changes: 11 additions & 8 deletions packages/dye/components/Canvas/HueCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ interface Hsl {
l: number
}
const emit = defineEmits(['change'])
const emit = defineEmits<{
(e: 'change', props: { hex: hexType; mounted: boolean }): void
}>()
const props = defineProps<{
width: number
colorCanvas: () => Ref<HTMLCanvasElement | null>
Expand All @@ -44,7 +47,6 @@ function hueGradient(
hsl: Hsl = { h: 0, s: 1, l: 0.5 }
) {
const gradient = ctx.createLinearGradient(0, 0, 0, height)
console.log('rex: ', hsl)
for (var hue = 0; hue <= 360; hue++) {
var hslColor = `hsl(${hue}, ${hsl.s}%, ${hsl.l}%)`
gradient.addColorStop(hue / 360, hslColor)
Expand Down Expand Up @@ -79,9 +81,9 @@ function hueChange(e: MouseEvent, click = false) {
mouseOn.value = true
}
function updateCanvas(hex: hexType) {
function updateCanvas(hex: hexType, mounted = false) {
if (!hex) return
emit('change', hex)
emit('change', { hex, mounted })
fillColorCanvas({ hue: hex.color }, props.colorCanvas().value)
position.value = {
x: position.value.x,
Expand Down Expand Up @@ -116,16 +118,17 @@ onMounted(() => {
fillHueCanvas()
setCenterHandle()
var color = colord(props.color.value)
const color = colord(props.color.value)
const hsl = color.toHsl()
updateCanvas({
const hex = {
color: props.color.value,
position: {
x: 0,
y: huePercent(hsl.h, canvasHeight.value)
}
})
}
updateCanvas(hex, true)
})
</script>

Expand Down
43 changes: 35 additions & 8 deletions packages/dye/components/DyePicker.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { colord } from 'colord'
import type { Colord } from 'colord'
import { vOnClickOutside } from '@vueuse/components'
import { ref } from 'vue'
Expand All @@ -9,11 +10,31 @@ import Pallet from './Pallet.vue'
import ColorCanvas from './Canvas/ColorCanvas.vue'
import HueCanvas from './Canvas/HueCanvas.vue'
const emit = defineEmits(['change'])
const props = defineProps<{
default: string
const emit = defineEmits<{
(
e: 'change',
color: {
name: string
value: Colord
position: { x: number; y: number }
}
): void
}>()
interface Props {
default: string
compact: boolean
compactSize: number
hueWidth: number
}
const props = withDefaults(defineProps<Props>(), {
default: '#ff0000',
compact: true,
compactSize: 50,
hueWidth: 25
})
const pallet = ref<HTMLElement | null>(null)
const colorCanvas = ref<HTMLCanvasElement | null>(null)
Expand All @@ -30,22 +51,23 @@ const color = ref({
value: props.default
})
function handleChange(hex?: hexType) {
function handleChange(hex?: hexType, mounted = false) {
if (!hex) return
const get = colorName(hex.color)
const { name, value } = get()
color.value = { name, value }
if (mounted) return
emit('change', {
name,
value: colord(value),
position: hex.position
})
}
const compact = ref(true)
const compactSize = ref(50)
const hueWidth = ref(25)
const compact = ref(props.compact)
const compactSize = ref(props.compactSize)
const hueWidth = ref(props.hueWidth)
</script>

<template>
Expand All @@ -62,7 +84,12 @@ const hueWidth = ref(25)
</slot>
</div>
<ColorCanvas @change="handleChange" :getRef="getRef" :setRef="setRef" :color="color" />
<HueCanvas @change="handleChange" :colorCanvas="getRef" :color="color" :width="hueWidth" />
<HueCanvas
@change="(props) => handleChange(props.hex, props.mounted)"
:colorCanvas="getRef"
:color="color"
:width="hueWidth"
/>
</div>
</template>

Expand Down
2 changes: 1 addition & 1 deletion packages/dye/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@umbrajs/dye",
"private": false,
"version": "0.0.0",
"version": "0.0.002",
"description": "Vue color picker using umbra",
"author": "Samuel M. Bednarz<https://github.com/CarelessCourage>",
"license": "MIT",
Expand Down
9 changes: 8 additions & 1 deletion packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ const height = ref('8rem')
<template>
<div class="page container">
<h1 class="display">Umbra</h1>
<DyePicker default="#36576a" />
<DyePicker
default="#36576a"
@change="
(value) => {
console.log(value.name)
}
"
/>
<button @click="() => inverse()">Invert</button>
<div class="ranges">
<LabelsGroups />
Expand Down

0 comments on commit 9453ce1

Please sign in to comment.