Skip to content

Commit

Permalink
Fixed color picker bug in tag
Browse files Browse the repository at this point in the history
  • Loading branch information
GhitaNektt committed Jun 28, 2024
1 parent 27023cc commit fec9015
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 109 deletions.
59 changes: 23 additions & 36 deletions packages/alto/src/_dev/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,43 @@
import 'uno.css';
import '../assets/main.css';
import { ref } from 'vue';
import { Status } from '~/components';
import type { StatusObject } from '~/components/Status/types';
import { ColorInput, ColorPicker, Tag } from '~/components';
import type { TagItemValue } from '../types';

Check failure on line 6 in packages/alto/src/_dev/App.vue

View workflow job for this annotation

GitHub Actions / Run linters

`../types` type import should occur before import of `~/components`
const staticFruit = ref<StatusObject>({
color: '#fffad2',
label: 'Banana 🍌',
labelColor: '#555022',
});
const colorv3 = ref('#A8B1FFFF');
const color = ref('#A8B1FFFF');
const fruits = [
{
color: '#ffdecb',
label: 'Peach 🍑',
labelColor: '#35192b',
value: 'peach',
},
{
color: '#fffad2',
label: 'Banana 🍌',
labelColor: '#555022',
value: 'banana',
},
{
color: '#cbffd3',
label: 'Kiwi 🥝',
labelColor: '#2c4730',
value: 'kiwi',
},
];
function updateColor(newColor: string) {
color.value = newColor;
}
const favoriteFruit = ref<StatusObject>(fruits[0]);
const preferredLanguages = ref<TagItemValue[]>([
{ label: 'Pink', hexColor: '#F49FBC' },
]);
</script>

<template>
<div class="container">
<!-- Single status badge -->
<Status :status="staticFruit" />

<!-- Dropdown of badges -->
<Status v-model="favoriteFruit" :status="fruits" />
{{ favoriteFruit }}
<Tag
v-model="preferredLanguages"

Check warning on line 23 in packages/alto/src/_dev/App.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expected indentation of 6 spaces but found 4 spaces
placeholder="Your favorite colors"

Check warning on line 24 in packages/alto/src/_dev/App.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expected indentation of 6 spaces but found 4 spaces
type="color"

Check warning on line 25 in packages/alto/src/_dev/App.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expected indentation of 6 spaces but found 4 spaces
/>

Check warning on line 26 in packages/alto/src/_dev/App.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expected indentation of 4 spaces but found 2 spaces
<ColorInput :model-value="color" @update:modelValue="updateColor" />

Check warning on line 27 in packages/alto/src/_dev/App.vue

View workflow job for this annotation

GitHub Actions / Run linters

v-on event '@update:modelValue' must be hyphenated
</div>
<div class="container">
<ColorPicker :preserve-transparency="false" :model-value="colorv3" />
<ColorPicker :model-value="colorv3" />
</div>
</template>

<style scoped>
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: row;
margin: 30px;
padding: 10%;
background-color: var(--gray-200);
gap: 0.5rem;
}
</style>
139 changes: 67 additions & 72 deletions packages/alto/src/components/Color/ColorPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const props = withDefaults(
defineProps<ColorPickerProps>(),
{
preserveTransparency: true,
swatches: () => ['#B8256EFF', '#25B86AFF', '#127EE3FF', '#F2990EFF', '#CC2929FF'],
swatches: () => ['#B8256E', '#25B86A', '#127EE3', '#F2990E', '#CC2929'],
},
);
Expand All @@ -27,57 +27,57 @@ const alphaValue = ref(1);
let isDragging = false;
const setColor = (pickedColor: string) => {
if (props.preserveTransparency) {
const alpha = Math.floor(alphaValue.value * 255).toString(16).padStart(2, '0').toUpperCase();
color.value = `${pickedColor}${alpha}`;
inputColor.value = `${pickedColor}${alpha}`;
}
else {
color.value = `${pickedColor}`;
inputColor.value = `${pickedColor}`;
let newColor = pickedColor;
if (props.preserveTransparency || pickedColor.length === 6) {
const alpha = Math.floor(alphaValue.value * 255)
.toString(16)
.padStart(2, '0')
.toUpperCase();
newColor += alpha;

Check failure on line 38 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expected indentation of 4 spaces but found 6
}
color.value = newColor;
inputColor.value = newColor;
};
const setDraggableDivColor = (newX: number, newY: number) => {
const context = canvas.value!.getContext('2d');
if (context) {
const x = newX === 0 ? newX : newX - 1;
const y = newY === 0 ? newY : newY - 1;
const pixelData = context.getImageData(x, y, 1, 1).data;
const red = pixelData[0].toString(16).padStart(2, '0').toUpperCase();
const green = pixelData[1].toString(16).padStart(2, '0').toUpperCase();
const blue = pixelData[2].toString(16).padStart(2, '0').toUpperCase();
const pickedColor = `#${red}${green}${blue}`;
setColor(pickedColor);
emit('update:modelValue', color.value);
}
};
const context = canvas.value?.getContext('2d');
if (!context) return;

Check failure on line 47 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expect newline after if

Check failure on line 47 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expected { after 'if' condition
const setDraggableDivCoordinates = (event: MouseEvent | TouchEvent) => {
if (draggableDiv.value) {
const rect = canvasContainer.value!.getBoundingClientRect();
let x = 0;
let y = 0;
if (event instanceof MouseEvent) {
x = event.clientX - rect.left;
y = event.clientY - rect.top;
}
else if (event instanceof TouchEvent) {
x = event.touches[0].clientX - rect.left;
y = event.touches[0].clientY - rect.top;
}
const maxX = canvasContainer.value!.clientWidth;
const maxY = canvasContainer.value!.clientHeight;
const newX = Math.min(Math.max(0, x), maxX);
const newY = Math.min(Math.max(0, y), maxY);
const x = Math.max(newX - 1, 0);
const y = Math.max(newY - 1, 0);
const [r, g, b] = context.getImageData(x, y, 1, 1).data;
draggableDiv.value.style.left = `calc(${(newX / 224) * 100}% - 6px)`;
draggableDiv.value.style.top = `calc(${(newY / 224) * 100}% - 6px)`;
const toHex = (value: number) => value.toString(16).padStart(2, '0').toUpperCase();
const pickedColor = `#${toHex(r)}${toHex(g)}${toHex(b)}`;
setDraggableDivColor(newX, newY);
}
setColor(pickedColor);
emit('update:modelValue', color.value);
};
const setDraggableDivCoordinates = (event: MouseEvent | TouchEvent) => {
const div = draggableDiv.value;
const container = canvasContainer.value;
if (!div || !container) return;

Check failure on line 63 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expect newline after if

Check failure on line 63 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Expected { after 'if' condition
const rect = container.getBoundingClientRect();
const { clientX, clientY } = 'touches' in event ? event.touches[0] : event;

Check failure on line 67 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Trailing spaces not allowed
const x = clientX - rect.left;
const y = clientY - rect.top;
const maxX = container.clientWidth;
const maxY = container.clientHeight;

Check failure on line 73 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Trailing spaces not allowed
const newX = Math.min(Math.max(0, x), maxX);
const newY = Math.min(Math.max(0, y), maxY);
div.style.left = `calc(${(newX / 224) * 100}% - 6px)`;
div.style.top = `calc(${(newY / 224) * 100}% - 6px)`;
setDraggableDivColor(newX, newY);
};
const startDrag = (event: MouseEvent | TouchEvent) => {
Expand Down Expand Up @@ -145,24 +145,31 @@ onMounted(() => {
emit('update:modelValue', props.modelValue);
const stopPropagation = (event: MouseEvent) => event.stopPropagation();
if (props.preserveTransparency) {
alphaSlider.value.addEventListener('input', updateAlpha);
alphaSlider.value.addEventListener('touchmove', updateAlpha);
alphaSlider.value!.addEventListener('mousemove', (event: MouseEvent) => {
event.stopPropagation();
});
color.value = inputColor.value = `${props.modelValue}FF`;

Check failure on line 152 in packages/alto/src/components/Color/ColorPicker.vue

View workflow job for this annotation

GitHub Actions / Run linters

Trailing spaces not allowed
if (alphaSlider.value) {
alphaSlider.value.addEventListener('input', updateAlpha);
alphaSlider.value.addEventListener('touchmove', updateAlpha);
alphaSlider.value.addEventListener('mousemove', stopPropagation);
}
}
if (colorSlider.value) {
colorSlider.value.addEventListener('input', updateColor);
colorSlider.value.addEventListener('touchmove', updateColor);
colorSlider.value.addEventListener('mousemove', stopPropagation);
}
colorSlider.value!.addEventListener('input', updateColor);
colorSlider.value!.addEventListener('touchmove', updateColor);
colorSlider.value!.addEventListener('mousemove', (event: MouseEvent) => {
event.stopPropagation();
});
window.addEventListener('mouseup', stopDrag);
window.addEventListener('mousemove', drag);
canvasContainer.value!.addEventListener('touchmove', drag);
canvasContainer.value!.addEventListener('touchend', stopDrag);
if (canvasContainer.value) {
canvasContainer.value.addEventListener('touchmove', drag);
canvasContainer.value.addEventListener('touchend', stopDrag);
}
});
onUnmounted(() => {
Expand Down Expand Up @@ -292,25 +299,13 @@ onUnmounted(() => {
transition: color 0.05s linear;
border-radius: 0.3em;
outline: none;
appearance: none;
background-image:
linear-gradient(
45deg,
v-bind(color.slice(0, -2)) 25%,
transparent 25%,
transparent 75%,
v-bind(color.slice(0, -2)) 75%
),
linear-gradient(
45deg,
v-bind(color.slice(0, -2)) 25%,
transparent 25%,
transparent 75%,
v-bind(color.slice(0, -2)) 75%
),
linear-gradient(45deg, v-bind(color.slice(0, -2)) 25%, transparent 25%, transparent 75%, v-bind(color.slice(0, -2)) 75%),
linear-gradient(45deg, v-bind(color.slice(0, -2)) 25%, transparent 25%, transparent 75%, v-bind(color.slice(0, -2)) 75%),
linear-gradient(to right, var(--gray-100) 25%, v-bind(color.slice(0, -2)) 85%);
background-position: 0 0, 2px 2px, 0 0;
background-size: 4px 4px, 4px 4px, 100% 100%;
appearance: none;
}
.color-picker .sliders .color-range {
Expand Down
2 changes: 1 addition & 1 deletion packages/alto/src/components/Color/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function hexToHue(hex: string) {
export function getSliderValueFromColor(hexColorWithHash: string) {
const hexColor = hexColorWithHash.startsWith('#') ? hexColorWithHash.slice(1) : hexColorWithHash;

let alpha = '';
let alpha = 'FF';
if (hexColor.length === 8) {
alpha = hexColor.slice(6, 8);
}
Expand Down

0 comments on commit fec9015

Please sign in to comment.