Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 55 additions & 17 deletions src/components/lab/ColorPicker/ColorPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import * as React from 'react';

import type {HsvaColor} from '@uiw/react-color';
import {Alpha, Hue, Saturation, hsvaToHex, hsvaToHexa} from '@uiw/react-color';
import {Alpha, Hue, Saturation, hsvaToHex, hsvaToHexa, validHex} from '@uiw/react-color';

import {useControlledState} from '../../../hooks/useControlledState';
import {Popup} from '../../Popup';
import type {PopupPlacement} from '../../Popup';
import {Select} from '../../Select';

import {ColorDisplay, ColorPointer, HexInput, RgbInputs} from './components';
import {DEFAULT_COLOR, b} from './constants';
import {Modes} from './types';
import {convertSelectedModeColorToHsva, getTextValueByMode} from './utils';
import {convertSelectedModeColorToHsva, getTextValueByMode, isValidHsva} from './utils';

export interface ColorPickerProps {
/*
Expand Down Expand Up @@ -51,6 +52,20 @@ export interface ColorPickerProps {
compact?: boolean;
}

const POPUP_PLACEMENTS: PopupPlacement = [
'bottom-start',
'bottom-end',
'left-start',
'left-end',
'top-start',
'top-end',
];

const MODE_OPTIONS = Object.values(Modes).map((val) => ({
content: val,
value: val,
}));

export const ColorPicker = ({
size,
value,
Expand Down Expand Up @@ -94,10 +109,21 @@ export const ColorPicker = ({
const updateHsva = React.useCallback(
(updates: Partial<HsvaColor>) => {
const newHsva = {...hsva, ...updates};

// Validate HSVA before applying
if (!isValidHsva(newHsva)) {
return;
}

setHsva(newHsva);

const newHexValue = withAlpha ? hsvaToHexa(newHsva) : hsvaToHex(newHsva);

// Validate HEX before calling onUpdate
if (!validHex(newHexValue)) {
return;
}

isInternalUpdateRef.current = true;
setColor(newHexValue);
},
Expand All @@ -113,13 +139,35 @@ export const ColorPicker = ({
};

const applyInputValue = React.useCallback(() => {
const newHsva = convertSelectedModeColorToHsva(inputValue, modeState, withAlpha);
setHsva(newHsva);
const raw = inputValue.trim();

if (!raw) {
setInputValue(color);
return;
}

if (modeState === Modes.Hex && !validHex(raw)) {
setInputValue(color);
return;
}
const newHsva = convertSelectedModeColorToHsva(raw, modeState, withAlpha);

if (!isValidHsva(newHsva)) {
setInputValue(color);
return;
}

const newHexValue = withAlpha ? hsvaToHexa(newHsva) : hsvaToHex(newHsva);

if (!validHex(newHexValue)) {
setInputValue(color);
return;
}

isInternalUpdateRef.current = true;
setHsva(newHsva);
setColor(newHexValue);
}, [inputValue, modeState, withAlpha, setColor]);
}, [inputValue, modeState, withAlpha, setColor, color]);

return (
<React.Fragment>
Expand All @@ -136,14 +184,7 @@ export const ColorPicker = ({
<Popup
open={isOpen}
className={b('popup')}
placement={[
'bottom-start',
'bottom-end',
'left-start',
'left-end',
'top-start',
'top-end',
]}
placement={POPUP_PLACEMENTS}
anchorElement={anchor}
onOpenChange={setIsOpen}
disableTransition
Expand Down Expand Up @@ -197,10 +238,7 @@ export const ColorPicker = ({

<div className={b('inputs')}>
<Select
options={Object.values(Modes).map((val) => ({
content: val,
value: val,
}))}
options={MODE_OPTIONS}
multiple={false}
value={[modeState]}
onUpdate={(val) => handleModeChange(val[0] as Modes)}
Expand Down
16 changes: 16 additions & 0 deletions src/components/lab/ColorPicker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ import type {HsvaColor, RgbaColor} from '@uiw/react-color';

import {Modes} from './types';

/**
* Validates if an HsvaColor object has valid values (no NaN)
*/
export const isValidHsva = (hsva: HsvaColor): boolean => {
return (
!Number.isNaN(hsva.h) &&
!Number.isNaN(hsva.s) &&
!Number.isNaN(hsva.v) &&
!Number.isNaN(hsva.a) &&
Number.isFinite(hsva.h) &&
Number.isFinite(hsva.s) &&
Number.isFinite(hsva.v) &&
Number.isFinite(hsva.a)
);
};

export const convertSelectedModeColorToHsva = (value: string, mode: Modes, alpha: boolean) => {
switch (mode) {
case Modes.Hex: {
Expand Down
Loading