Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColorPicker: TypeScript refactor #49214

Merged
merged 19 commits into from
Mar 24, 2023
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `Animate`: Convert to TypeScript ([#49243](https://github.com/WordPress/gutenberg/pull/49243)).
- `CustomGradientPicker`: Convert to TypeScript ([#48929](https://github.com/WordPress/gutenberg/pull/48929)).
- `ColorPicker`: Convert to TypeScript ([#49214](https://github.com/WordPress/gutenberg/pull/49214)).
- `GradientPicker`: Convert to TypeScript ([#48316](https://github.com/WordPress/gutenberg/pull/48316)).

### Enhancements
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/color-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ The current color value to display in the picker. Must be a hex or hex8 string.

### `onChange`: `(hex8Color: string) => void`

Fired when the color changes. Always passes a hex8 color string.
Fired when the color changes. Always passes a hex or hex8 color string.
Copy link
Contributor Author

@chad1008 chad1008 Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onChange previously always passed a hex8 string, but since #35562, Colord.toHex() will return a hex or a hex8 (hex8 only if the color's alpha is < 1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thank you!


- Required: No

### `enableAlpha`: `boolean`

Defaults to `false`. When `true` the color picker will display the alpha channel both in the bottom inputs as well as in the color picker itself.
When `true` the color picker will display the alpha channel both in the bottom inputs as well as in the color picker itself.

- Required: No
- Default: `false`
Expand Down
13 changes: 1 addition & 12 deletions packages/components/src/color-picker/color-input.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
/**
* External dependencies
*/
import type { Colord } from 'colord';

/**
* Internal dependencies
*/
import { RgbInput } from './rgb-input';
import { HslInput } from './hsl-input';
import { HexInput } from './hex-input';

interface ColorInputProps {
colorType: 'hsl' | 'hex' | 'rgb';
color: Colord;
onChange: ( nextColor: Colord ) => void;
enableAlpha: boolean;
}
import type { ColorInputProps } from './types';

export const ColorInput = ( {
colorType,
Expand Down
27 changes: 9 additions & 18 deletions packages/components/src/color-picker/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import {
useContextSystem,
contextConnect,
WordPressComponentProps,
} from '../ui/context';
import { useContextSystem, contextConnect } from '../ui/context';
import {
ColorfulWrapper,
SelectControl,
Expand All @@ -32,26 +28,18 @@ import { ColorInput } from './color-input';
import { Picker } from './picker';
import { useControlledValue } from '../utils/hooks';

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

extend( [ namesPlugin ] );

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

const options = [
{ label: 'RGB', value: 'rgb' as const },
{ label: 'HSL', value: 'hsl' as const },
{ label: 'Hex', value: 'hex' as const },
];

const ColorPicker = (
props: WordPressComponentProps< ColorPickerProps, 'div', false >,
const UnconnectedColorPicker = (
props: ColorPickerProps,
forwardedRef: ForwardedRef< any >
) => {
const {
Expand Down Expand Up @@ -124,6 +112,9 @@ const ColorPicker = (
);
};

const ConnectedColorPicker = contextConnect( ColorPicker, 'ColorPicker' );
export const ColorPicker = contextConnect(
UnconnectedColorPicker,
'ColorPicker'
);

export default ConnectedColorPicker;
export default ColorPicker;
9 changes: 2 additions & 7 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 { colord, Colord } from 'colord';
import { colord } from 'colord';

/**
* WordPress dependencies
Expand All @@ -17,12 +17,7 @@ import { Spacer } from '../spacer';
import { space } from '../ui/utils/space';
import { COLORS } from '../utils/colors-values';
import type { StateReducer } from '../input-control/reducer/state';

interface HexInputProps {
color: Colord;
onChange: ( nextColor: Colord ) => void;
enableAlpha: boolean;
}
import type { HexInputProps } from './types';

export const HexInput = ( { color, onChange, enableAlpha }: HexInputProps ) => {
const handleChange = ( nextValue: string | undefined ) => {
Expand Down
9 changes: 2 additions & 7 deletions packages/components/src/color-picker/hsl-input.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
/**
* External dependencies
*/
import { colord, Colord } from 'colord';
import { colord } from 'colord';

/**
* Internal dependencies
*/
import { InputWithSlider } from './input-with-slider';

interface HslInputProps {
color: Colord;
onChange: ( nextColor: Colord ) => void;
enableAlpha: boolean;
}
import type { HslInputProps } from './types';

export const HslInput = ( { color, onChange, enableAlpha }: HslInputProps ) => {
const { h, s, l, a } = color.toHsl();
Expand Down
10 changes: 1 addition & 9 deletions packages/components/src/color-picker/input-with-slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import { Spacer } from '../spacer';
import { space } from '../ui/utils/space';
import { RangeControl, NumberControlWrapper } from './styles';
import { COLORS } from '../utils/colors-values';

interface InputWithSliderProps {
min: number;
max: number;
value: number;
label: string;
abbreviation: string;
onChange: ( value: number ) => void;
}
import type { InputWithSliderProps } from './types';

export const InputWithSlider = ( {
min,
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/color-picker/legacy-adapter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
* Internal dependencies
*/
import ColorPicker from './component';
import type { LegacyAdapterProps } from './types';
import { useDeprecatedProps } from './use-deprecated-props';

type LegacyAdapterProps = Parameters< typeof useDeprecatedProps >[ 0 ];

export const LegacyAdapter = ( props: LegacyAdapterProps ) => {
return <ColorPicker { ...useDeprecatedProps( props ) } />;
};
11 changes: 5 additions & 6 deletions packages/components/src/color-picker/picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
* External dependencies
*/
import { RgbStringColorPicker, RgbaStringColorPicker } from 'react-colorful';
import { colord, Colord } from 'colord';
import { colord } from 'colord';

/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
interface PickerProps {
color: Colord;
enableAlpha: boolean;
onChange: ( nextColor: Colord ) => void;
}
/**
* Internal dependencies
*/
import type { PickerProps } from './types';

export const Picker = ( { color, enableAlpha, onChange }: PickerProps ) => {
const Component = enableAlpha
Expand Down
9 changes: 2 additions & 7 deletions packages/components/src/color-picker/rgb-input.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
/**
* External dependencies
*/
import { colord, Colord } from 'colord';
import { colord } from 'colord';

/**
* Internal dependencies
*/
import { InputWithSlider } from './input-with-slider';

interface RgbInputProps {
color: Colord;
onChange: ( nextColor: Colord ) => void;
enableAlpha: boolean;
}
import type { RgbInputProps } from './types';

export const RgbInput = ( { color, onChange, enableAlpha }: RgbInputProps ) => {
const { r, g, b, a } = color.toRgb();
Expand Down
41 changes: 0 additions & 41 deletions packages/components/src/color-picker/stories/index.js

This file was deleted.

51 changes: 51 additions & 0 deletions packages/components/src/color-picker/stories/index.tsx
chad1008 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { ColorPicker } from '../component';

const meta: ComponentMeta< typeof ColorPicker > = {
ciampo marked this conversation as resolved.
Show resolved Hide resolved
component: ColorPicker,
title: 'Components/ColorPicker',
argTypes: {
as: { control: { type: null } },
color: { control: { type: null } },
},
parameters: {
actions: { argTypesRegex: '^on.*' },
controls: {
expanded: true,
},
docs: { source: { state: 'open' } },
},
};
export default meta;

const Template: ComponentStory< typeof ColorPicker > = ( {
onChange,
chad1008 marked this conversation as resolved.
Show resolved Hide resolved
...props
} ) => {
const [ color, setColor ] = useState< string | undefined >();

return (
<ColorPicker
{ ...props }
color={ color }
onChange={ ( ...changeArgs ) => {
onChange?.( ...changeArgs );
setColor( ...changeArgs );
} }
/>
);
};

export const Default = Template.bind( {} );
Loading