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

Convert the ColorPalette component to TypeScript #44632

Merged
merged 43 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b9b3fbc
Convert the ColorPalette component to TypeScript
kienstra Oct 2, 2022
2521cae
Correct the PR number in the CHANGELOG
kienstra Oct 2, 2022
54af17c
Apply Marco's suggestion to remove ts-ignore comments
kienstra Oct 12, 2022
c9f5d8a
Replace complex type with ReactNode, thanks to Marco's suggestion
kienstra Oct 12, 2022
4a602dd
Apply Marco's suggestions for TS verbatim
kienstra Oct 12, 2022
7fe648e
Prevent an error from colors possibly being undefined
kienstra Oct 12, 2022
fcd61ee
Rename Color and MultipleColors to ColorObject and PaletteObject
kienstra Oct 12, 2022
4629f7c
Alphabetize the imports again
kienstra Oct 12, 2022
6ee6987
Remove another needless ts-ignore comment
kienstra Oct 12, 2022
6a7c211
Revert "Prevent an error from colors possibly being undefined"
kienstra Oct 12, 2022
3fc9fa2
Make colors allow undefined
kienstra Oct 12, 2022
1ece323
Merge branch 'trunk' into update/color-palette-component-ts
kienstra Oct 12, 2022
a07c43f
Make actions optional, which I forgot before
kienstra Oct 18, 2022
90efcd9
Merge in trunk, resolve conflict
kienstra Oct 18, 2022
c420232
Commit Marco's changes, including a named export
kienstra Oct 19, 2022
9b770d3
Add default tags, thanks to Marco's idea
kienstra Oct 19, 2022
ea5b1f3
Apply Marco's suggestion to remove ts-ignore
kienstra Oct 19, 2022
a79c187
Apply Marco's suggestions, creating UnforwardedColorProps
kienstra Oct 19, 2022
e729c8f
Merge in trunk, resolve conflict
kienstra Oct 19, 2022
25e0ba2
Fix a linting error, remove needless className
kienstra Oct 19, 2022
8001f14
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 21, 2022
e793921
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 21, 2022
53c8beb
Rename test/index.js to test/indes.tsx, mv snapshot
kienstra Oct 21, 2022
06f7c4e
Add types to test/index.tsx
kienstra Oct 21, 2022
293fafe
Renamce styles.js to styles.ts
kienstra Oct 21, 2022
0af93a8
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 24, 2022
d77ca5b
Revert "Add types to test/index.tsx"
kienstra Oct 24, 2022
626b25d
Paste Marco's description verbatim
kienstra Oct 24, 2022
905c40e
Copy props verbatim from types.ts into README.md
kienstra Oct 24, 2022
eee647c
Update the JSDoc description to be the same as the README.md description
kienstra Oct 24, 2022
0bb6561
Merge branch 'trunk' into update/color-palette-component-ts
kienstra Oct 24, 2022
73c17ea
Remove extra entry for Tooltip
kienstra Oct 24, 2022
bb25a62
Move the CHANGELOG entry up, to Unreleased
kienstra Oct 24, 2022
4fd5d9c
Move Usage section to the top of the README. remove experimental prop…
kienstra Oct 25, 2022
d65a832
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 25, 2022
29984c0
Remove the example of the full props
kienstra Oct 25, 2022
988e940
Change the props format to match CONTRIBUTING.md
kienstra Oct 25, 2022
193fae3
Merge in trunk, resolve conflict
kienstra Oct 25, 2022
2df05c6
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 25, 2022
e88cf95
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 25, 2022
eb8c5c3
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 25, 2022
a2aa2e0
Commit Marco's suggestion: Update packages/components/src/color-palet…
kienstra Oct 25, 2022
f741e4b
Merge branch 'trunk' into update/color-palette-component-ts
ciampo Oct 26, 2022
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 @@ -22,6 +22,7 @@
- `Sandbox`: updated to satisfy `react/exhaustive-deps` eslint rule ([#44378](https://github.com/WordPress/gutenberg/pull/44378))
- `FontSizePicker`: Convert to TypeScript ([#44449](https://github.com/WordPress/gutenberg/pull/44449)).
- `FontSizePicker`: Replace SCSS with Emotion + components ([#44483](https://github.com/WordPress/gutenberg/pull/44483)).
- `ColorPalette`: Convert to TypeScript ([#44632](https://github.com/WordPress/gutenberg/pull/44632)).

## 21.1.0 (2022-09-21)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const BorderControlDropdown = (
/>
</HStack>
) : undefined }
{ /* @ts-ignore colors prop: Type 'Colors | undefined' is not assignable to type 'Color[] | MultipleColors' */ }
<ColorPalette
className={ popoverContentClassName }
value={ color }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
/**
* External dependencies
*/
Expand All @@ -23,6 +22,14 @@ import { Flex, FlexItem } from '../flex';
import { Truncate } from '../truncate';
import { ColorHeading } from './styles';
import DropdownContentWrapper from '../dropdown/dropdown-content-wrapper';
import type {
Color,
MultipleColors,
ColorPaletteProps,
CustomColorPickerDropdownProps,
MultiplePalettesProps,
SinglePaletteProps,
} from './types';

extend( [ namesPlugin, a11yPlugin ] );

Expand All @@ -33,13 +40,14 @@ function SinglePalette( {
onChange,
value,
actions,
} ) {
}: SinglePaletteProps ) {
const colorOptions = useMemo( () => {
return colors.map( ( { color, name }, index ) => {
const colordColor = colord( color );
const isSelected = value === color;

return (
// @ts-ignore Required className prop.
ciampo marked this conversation as resolved.
Show resolved Hide resolved
<CircularOptionPicker.Option
key={ `${ color }-${ index }` }
isSelected={ isSelected }
Expand Down Expand Up @@ -75,6 +83,7 @@ function SinglePalette( {
} );
}, [ colors, value, onChange, clearColor ] );
return (
// @ts-ignore Required children prop.
<CircularOptionPicker
className={ className }
options={ colorOptions }
Expand All @@ -90,7 +99,7 @@ function MultiplePalettes( {
onChange,
value,
actions,
} ) {
}: MultiplePalettesProps ) {
return (
<VStack spacing={ 3 } className={ className }>
{ colors.map( ( { name, colors: colorPalette }, index ) => {
Expand All @@ -117,7 +126,7 @@ export function CustomColorPickerDropdown( {
isRenderedInSidebar,
popoverProps: receivedPopoverProps,
...props
} ) {
}: CustomColorPickerDropdownProps ) {
const popoverProps = useMemo(
() => ( {
shift: true,
Expand Down Expand Up @@ -148,9 +157,9 @@ export function CustomColorPickerDropdown( {
}

export const extractColorNameFromCurrentValue = (
currentValue,
colors = [],
showMultiplePalettes = false
currentValue?: string,
ciampo marked this conversation as resolved.
Show resolved Hide resolved
colors: Color[] | MultipleColors = [],
showMultiplePalettes: boolean = false
) => {
if ( ! currentValue ) {
return '';
Expand All @@ -162,7 +171,9 @@ export const extractColorNameFromCurrentValue = (
: colord( currentValue ).toHex();

// Normalize format of `colors` to simplify the following loop
const colorPalettes = showMultiplePalettes ? colors : [ { colors } ];
const colorPalettes = showMultiplePalettes
? ( colors as MultipleColors )
: [ { colors: colors as Color[] } ];
for ( const { colors: paletteColors } of colorPalettes ) {
for ( const { name: colorName, color: colorValue } of paletteColors ) {
const normalizedColorValue = currentValueIsCssVariable
Expand All @@ -179,7 +190,7 @@ export const extractColorNameFromCurrentValue = (
return __( 'Custom' );
};

export const showTransparentBackground = ( currentValue ) => {
export const showTransparentBackground = ( currentValue?: string ) => {
if ( typeof currentValue === 'undefined' ) {
return true;
}
Expand All @@ -196,10 +207,10 @@ export default function ColorPalette( {
value,
__experimentalHasMultipleOrigins = false,
__experimentalIsRenderedInSidebar = false,
} ) {
}: ColorPaletteProps ) {
ciampo marked this conversation as resolved.
Show resolved Hide resolved
const clearColor = useCallback( () => onChange( undefined ), [ onChange ] );
const showMultiplePalettes =
__experimentalHasMultipleOrigins && colors?.length;
__experimentalHasMultipleOrigins && !! colors?.length;
ciampo marked this conversation as resolved.
Show resolved Hide resolved
const Component = showMultiplePalettes ? MultiplePalettes : SinglePalette;

const renderCustomColorPicker = () => (
Expand All @@ -212,7 +223,7 @@ export default function ColorPalette( {
</DropdownContentWrapper>
);

const colordColor = colord( value );
const colordColor = colord( value ?? '' );
ciampo marked this conversation as resolved.
Show resolved Hide resolved

const valueWithoutLeadingHash = value?.startsWith( '#' )
? value.substring( 1 )
Expand Down Expand Up @@ -287,11 +298,13 @@ export default function ColorPalette( {
<Component
clearable={ clearable }
clearColor={ clearColor }
// @ts-ignore Component can be MultiplePalettes or SinglePalette, which have different colors prop types.
ciampo marked this conversation as resolved.
Show resolved Hide resolved
colors={ colors }
onChange={ onChange }
value={ value }
actions={
!! clearable && (
// @ts-ignore Required className property.
<CircularOptionPicker.ButtonAction
onClick={ clearColor }
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* WordPress dependencies
*/
Expand All @@ -6,11 +11,12 @@ import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import ColorPalette from '../';
import ColorPalette from '..';
import Popover from '../../popover';
import { Provider as SlotFillProvider } from '../../slot-fill';
import type { Color, MultipleColors, ColorPaletteProps } from '../types';

const meta = {
const meta: ComponentMeta< typeof ColorPalette > = {
ciampo marked this conversation as resolved.
Show resolved Hide resolved
title: 'Components/ColorPalette',
component: ColorPalette,
argTypes: {
Expand Down Expand Up @@ -42,14 +48,16 @@ const meta = {
};
export default meta;

const Template = ( args ) => {
const Template: ComponentStory< typeof ColorPalette > = ( args ) => {
const firstColor =
args.colors[ 0 ].color || args.colors[ 0 ].colors[ 0 ].color;
const [ color, setColor ] = useState( firstColor );
( args.colors as Color[] )[ 0 ].color ||
( args.colors as MultipleColors )[ 0 ].colors[ 0 ].color;
const [ color, setColor ] = useState< string | undefined >( firstColor );

return (
<SlotFillProvider>
<ColorPalette { ...args } value={ color } onChange={ setColor } />
{ /* @ts-ignore Property 'Slot' does not exist on type. */ }
kienstra marked this conversation as resolved.
Show resolved Hide resolved
<Popover.Slot />
</SlotFillProvider>
);
Expand Down Expand Up @@ -92,10 +100,11 @@ MultipleOrigins.args = {
],
};

export const CSSVariables = ( args ) => {
export const CSSVariables = ( args: ColorPaletteProps ) => {
return (
<div
style={ {
// @ts-ignore
ciampo marked this conversation as resolved.
Show resolved Hide resolved
'--red': '#f00',
'--yellow': '#ff0',
'--blue': '#00f',
Expand Down
83 changes: 83 additions & 0 deletions packages/components/src/color-palette/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* External dependencies
*/
import type { MouseEventHandler, ReactElement } from 'react';

type OnColorChange = ( newColor?: string ) => void;

export type Color = {
name: string;
color: string;
};

export type MultipleColors = {
name: string;
colors: Color[];
}[];
ciampo marked this conversation as resolved.
Show resolved Hide resolved

type PaletteProps = {
className?: string;
clearColor: () => void;
onChange: OnColorChange;
value?: string;
actions: ( ReactElement | null ) | ( false | ReactElement );
ciampo marked this conversation as resolved.
Show resolved Hide resolved
};

export type SinglePaletteProps = PaletteProps & {
colors: Color[];
};

export type MultiplePalettesProps = PaletteProps & {
colors: MultipleColors;
};

export type CustomColorPickerDropdownProps = {
kienstra marked this conversation as resolved.
Show resolved Hide resolved
isRenderedInSidebar: boolean;
renderContent: () => JSX.Element;
kienstra marked this conversation as resolved.
Show resolved Hide resolved
popoverProps?: string[];
renderToggle: ( props: {
isOpen: boolean;
onToggle: MouseEventHandler< HTMLButtonElement >;
} ) => JSX.Element;
kienstra marked this conversation as resolved.
Show resolved Hide resolved
};

export type ColorPaletteProps = {
/**
* Whether the palette should have a clearing button.
*/
clearable?: boolean;
ciampo marked this conversation as resolved.
Show resolved Hide resolved
/**
* Classes to be applied to the container.
*/
className?: string;
/**
* Array with the colors to be shown.
kienstra marked this conversation as resolved.
Show resolved Hide resolved
*/
colors: MultipleColors | Color[];
/**
* Whether to allow custom color or not.
kienstra marked this conversation as resolved.
Show resolved Hide resolved
*/
disableCustomColors?: boolean;
/**
* Whether the color picker should display the alpha channel
* both in the bottom inputs as well as in the color picker itself.
*/
enableAlpha?: boolean;
/**
* Callback called when a color is selected.
*/
onChange: OnColorChange;
/**
* Currently active value.
*/
value?: string;
/**
* Whether the colors prop is an array of color palettes,
* rather than an array of color objects.
*/
__experimentalHasMultipleOrigins?: boolean;
/**
* Whether this is rendered in the sidebar.
*/
__experimentalIsRenderedInSidebar?: boolean;
};