Skip to content

Commit

Permalink
Use new color picker props; Fix some issue on the color picker compon…
Browse files Browse the repository at this point in the history
…ent.
  • Loading branch information
jorgefilipecosta committed Oct 12, 2021
1 parent 17bdf97 commit d1bc697
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 183 deletions.
7 changes: 0 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
"@types/requestidlecallback": "0.3.4",
"@types/semver": "7.3.8",
"@types/sprintf-js": "1.1.2",
"@types/tinycolor2": "1.4.3",
"@types/uuid": "8.3.1",
"@wordpress/babel-plugin-import-jsx-pragma": "file:packages/babel-plugin-import-jsx-pragma",
"@wordpress/babel-plugin-makepot": "file:packages/babel-plugin-makepot",
Expand Down
1 change: 0 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"react-use-gesture": "^9.0.0",
"reakit": "^1.3.8",
"rememo": "^3.0.0",
"tinycolor2": "^1.4.2",
"uuid": "^8.3.0"
},
"peerDependencies": {
Expand Down
33 changes: 12 additions & 21 deletions packages/components/src/color-picker/color-display.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import colorize, { ColorFormats } from 'tinycolor2';
import { colord, extend, Colord } from 'colord';

/**
* WordPress dependencies
Expand All @@ -20,13 +20,13 @@ import type { ColorType } from './types';
import { space } from '../ui/utils/space';

interface ColorDisplayProps {
color: ColorFormats.HSLA;
color: Colord;
colorType: ColorType;
enableAlpha: boolean;
}

interface DisplayProps {
color: ColorFormats.HSLA;
color: Colord;
enableAlpha: boolean;
}

Expand All @@ -50,7 +50,7 @@ const ValueDisplay = ( { values }: ValueDisplayProps ) => (
);

const HslDisplay = ( { color, enableAlpha }: DisplayProps ) => {
const { h, s, l, a } = colorize( color ).toHsl();
const { h, s, l, a } = color.toHsl();

const values: Values = [
[ Math.floor( h ), 'H' ],
Expand All @@ -65,7 +65,7 @@ const HslDisplay = ( { color, enableAlpha }: DisplayProps ) => {
};

const RgbDisplay = ( { color, enableAlpha }: DisplayProps ) => {
const { r, g, b, a } = colorize( color ).toRgb();
const { r, g, b, a } = color.toRgb();

const values: Values = [
[ r, 'R' ],
Expand All @@ -81,13 +81,7 @@ const RgbDisplay = ( { color, enableAlpha }: DisplayProps ) => {
};

const HexDisplay = ( { color, enableAlpha }: DisplayProps ) => {
const colorized = colorize( color );
const colorWithoutHash = ( enableAlpha
? colorized.toHex8String()
: colorized.toHexString()
)
.slice( 1 )
.toUpperCase();
const colorWithoutHash = color.toHex().slice( 1 ).toUpperCase();
return (
<FlexItem>
<Text color="blue">#</Text>
Expand All @@ -114,32 +108,29 @@ export const ColorDisplay = ( {
enableAlpha,
}: ColorDisplayProps ) => {
const [ copiedColor, setCopiedColor ] = useState< string | null >( null );
const copyTimer = useRef< number | undefined >();
const copyTimer = useRef< ReturnType< typeof setTimeout > | undefined >();
const props = { color, enableAlpha };
const Component = getComponent( colorType );
const copyRef = useCopyToClipboard< HTMLDivElement >(
() => {
switch ( colorType ) {
case 'hsl': {
return colorize( color ).toHslString();
return color.toHslString();
}
case 'rgb': {
return colorize( color ).toRgbString();
return color.toRgbString();
}
default:
case 'hex': {
const colorized = colorize( color );
return enableAlpha
? colorized.toHex8String()
: colorized.toHexString();
return color.toHex();
}
}
},
() => {
if ( copyTimer.current ) {
clearTimeout( copyTimer.current );
}
setCopiedColor( colorize( color ).toHex8String() );
setCopiedColor( color.toHex() );
copyTimer.current = setTimeout( () => {
setCopiedColor( null );
copyTimer.current = undefined;
Expand All @@ -158,7 +149,7 @@ export const ColorDisplay = ( {
<Tooltip
content={
<Text color="white">
{ copiedColor === colorize( color ).toHex8String()
{ copiedColor === color.toHex()
? __( 'Copied!' )
: __( 'Copy' ) }
</Text>
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/color-picker/color-input.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import type { ColorFormats } from 'tinycolor2';
import { colord, extend, Colord } from 'colord';

/**
* Internal dependencies
Expand All @@ -12,8 +12,8 @@ import { HexInput } from './hex-input';

interface ColorInputProps {
colorType: 'hsl' | 'hex' | 'rgb';
color: ColorFormats.HSLA;
onChange: ( value: ColorFormats.HSLA ) => void;
color: Colord;
onChange: ( nextColor: Colord ) => void;
enableAlpha: boolean;
}

Expand Down
50 changes: 27 additions & 23 deletions packages/components/src/color-picker/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { Ref } from 'react';
import type { ColorFormats } from 'tinycolor2';
import { Ref, useCallback } from 'react';
import { colord, extend, Colord } from 'colord';
import namesPlugin from 'colord/plugins/names';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useState, useMemo } from '@wordpress/element';
import { settings } from '@wordpress/icons';
import { useDebounce } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -36,11 +37,13 @@ import { useControlledValue } from '../utils/hooks';

import type { ColorType } from './types';

extend( [ namesPlugin ] );

export interface ColorPickerProps {
enableAlpha?: boolean;
color?: ColorFormats.HSL | ColorFormats.HSLA;
onChange?: ( color: ColorFormats.HSL | ColorFormats.HSLA ) => void;
defaultValue?: ColorFormats.HSL | ColorFormats.HSLA;
color?: string;
onChange?: ( color: string ) => void;
defaultValue?: string;
copyFormat?: ColorType;
}

Expand All @@ -50,12 +53,6 @@ const options = [
{ label: 'Hex', value: 'hex' as const },
];

const getSafeColor = (
color: ColorFormats.HSL | ColorFormats.HSLA | undefined
): ColorFormats.HSLA => {
return color ? { a: 1, ...color } : { h: 0, s: 0, l: 100, a: 1 };
};

const ColorPicker = (
props: WordPressComponentProps< ColorPickerProps, 'div', false >,
forwardedRef: Ref< any >
Expand All @@ -75,17 +72,24 @@ const ColorPicker = (
defaultValue,
} );

// Use a safe default value for the color and remove the possibility of `undefined`.
const safeColordColor = useMemo( () => {
return color ? colord( color ) : colord( '#fff' );
}, [ color ] );

// Debounce to prevent rapid changes from conflicting with one another.
const debouncedSetColor = useDebounce( setColor );

const handleChange = (
nextValue: ColorFormats.HSLA | ColorFormats.HSL
) => {
debouncedSetColor( nextValue );
};

// Use a safe default value for the color and remove the possibility of `undefined`.
const safeColor = getSafeColor( color );
const handleChange = useCallback(
( nextValue: Colord ) => {
debouncedSetColor(
nextValue.alpha() < 1
? nextValue.toRgbString()
: nextValue.toHex()
);
},
[ debouncedSetColor ]
);

const [ showInputs, setShowInputs ] = useState< boolean >( false );
const [ colorType, setColorType ] = useState< ColorType >(
Expand All @@ -96,7 +100,7 @@ const ColorPicker = (
<ColorfulWrapper ref={ forwardedRef } { ...divProps }>
<Picker
onChange={ handleChange }
color={ safeColor }
color={ safeColordColor }
enableAlpha={ enableAlpha }
/>
<AuxiliaryColorArtefactWrapper>
Expand All @@ -113,7 +117,7 @@ const ColorPicker = (
/>
) : (
<ColorDisplay
color={ safeColor }
color={ safeColordColor }
colorType={ copyFormat || colorType }
enableAlpha={ enableAlpha }
/>
Expand All @@ -134,7 +138,7 @@ const ColorPicker = (
{ showInputs && (
<ColorInput
colorType={ colorType }
color={ safeColor }
color={ safeColordColor }
onChange={ handleChange }
enableAlpha={ enableAlpha }
/>
Expand Down
19 changes: 8 additions & 11 deletions packages/components/src/color-picker/hex-input.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import colorize, { ColorFormats } from 'tinycolor2';
import { colord, Colord } from 'colord';

/**
* WordPress dependencies
Expand All @@ -17,22 +17,19 @@ import { space } from '../ui/utils/space';
import { ColorHexInputControl } from './styles';

interface HexInputProps {
color: ColorFormats.HSLA;
onChange: ( value: ColorFormats.HSLA ) => void;
color: Colord;
onChange: ( nextColor: Colord ) => void;
enableAlpha: boolean;
}

export const HexInput = ( { color, onChange, enableAlpha }: HexInputProps ) => {
const handleValidate = ( value: string ) => {
if ( ! colorize( value ).isValid() ) {
if ( ! colord( '#' + value ).isValid() ) {
throw new Error( 'Invalid hex color input' );
}
};

const colorized = colorize( color );
const value = enableAlpha
? colorized.toHex8String()
: colorized.toHexString();
const value = color.toHex();

return (
<ColorHexInputControl
Expand All @@ -47,9 +44,9 @@ export const HexInput = ( { color, onChange, enableAlpha }: HexInputProps ) => {
</Spacer>
}
value={ value.slice( 1 ).toUpperCase() }
onChange={ ( nextValue ) =>
onChange( colorize( nextValue ).toHsl() )
}
onChange={ ( nextValue ) => {
onChange( colord( '#' + nextValue ) );
} }
onValidate={ handleValidate }
maxLength={ enableAlpha ? 8 : 6 }
label={ __( 'Hex color' ) }
Expand Down
Loading

0 comments on commit d1bc697

Please sign in to comment.