Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename to enableAlpha and add a README
  • Loading branch information
sarayourfriend committed Aug 6, 2021
commit f81bcbc93b64a9b7d2f6b8b8c27bbd3effede541
51 changes: 51 additions & 0 deletions packages/components/src/ui/color-picker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# ColorPicker

<div class="callout callout-alert">
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes.
</div>

`ColorPicker` is a color picking component based on `react-colorful`. It lets you pick a color visually or by manipulating the individual RGB(A), HSL(A) and Hex(8) color values.

## Usage

```jsx
import { ColorPicker } from '@wordpress/components/ui';

function Example() {
const [color, setColor] = useState();
return (
<ColorPicker
color={color}
onChange={setColor}
enableAlpha
defaultValue="#000"
/>
);
}
```

## Props

### `color`

**Type**: `string`

The current color value to display in the picker. Must be a hex or hex8 string.

### `onChange`

**Type**: `(hex8Color: string) => void`

Fired when the color changes. Always passes a hex8 color string.

### `enableAlpha`

**Type**: `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.
Comment on lines +41 to +45
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: should we use the same format we've been using for newer components?

Suggested change
### `enableAlpha`
**Type**: `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.
### `enableAlpha`: `boolean`
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`


### `defaultValue`

**Type**: `string | undefined`

An optional default value to use for the color picker.
16 changes: 8 additions & 8 deletions packages/components/src/ui/color-picker/color-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import { space } from '../utils/space';
interface ColorDisplayProps {
color: string;
colorType: ColorType;
disableAlpha: boolean;
enableAlpha: boolean;
}

interface DisplayProps {
color: string;
disableAlpha: boolean;
enableAlpha: boolean;
}

type Values = [ number, string ][];
Expand All @@ -49,22 +49,22 @@ const ValueDisplay = ( { values }: ValueDisplayProps ) => (
</>
);

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

const values: Values = [
[ Math.floor( h ), 'H' ],
[ Math.round( s * 100 ), 'S' ],
[ Math.round( l * 100 ), 'L' ],
];
if ( ! disableAlpha ) {
if ( enableAlpha ) {
values.push( [ Math.round( a * 100 ), 'A' ] );
}

return <ValueDisplay values={ values } />;
};

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

const values: Values = [
Expand All @@ -73,7 +73,7 @@ const RgbDisplay = ( { color, disableAlpha }: DisplayProps ) => {
[ b, 'B' ],
];

if ( ! disableAlpha ) {
if ( enableAlpha ) {
values.push( [ Math.round( a * 100 ), 'A' ] );
}

Expand Down Expand Up @@ -105,10 +105,10 @@ const getComponent = ( colorType: ColorType ) => {
export const ColorDisplay = ( {
color,
colorType,
disableAlpha,
enableAlpha,
}: ColorDisplayProps ) => {
const [ copiedColor, setCopiedColor ] = useState< string | null >( null );
const props = { color, disableAlpha };
const props = { color, enableAlpha };
const Component = getComponent( colorType );
const copyRef = useCopyToClipboard< HTMLDivElement >( color, () =>
setCopiedColor( color )
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/ui/color-picker/color-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ interface ColorInputProps {
colorType: 'hsl' | 'hex' | 'rgb';
color: string;
onChange: ( value: string ) => void;
disableAlpha: boolean;
enableAlpha: boolean;
}

export const ColorInput = ( {
colorType,
color,
onChange,
disableAlpha,
enableAlpha,
}: ColorInputProps ) => {
const props = { color, onChange, disableAlpha };
const props = { color, onChange, enableAlpha };
switch ( colorType ) {
case 'hsl':
return <HslInput { ...props } />;
Expand Down
10 changes: 5 additions & 5 deletions packages/components/src/ui/color-picker/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { useControlledValue } from '../../utils/hooks';

import type { ColorType } from './types';
interface ColorPickerProps {
disableAlpha?: boolean;
enableAlpha?: boolean;
color?: string;
onChange?: ( hex8Color: string ) => void;
defaultValue?: string;
Expand All @@ -48,7 +48,7 @@ const ColorPicker = (
forwardedRef: Ref< any >
) => {
const {
disableAlpha = true,
enableAlpha = false,
color: colorProp,
onChange,
defaultValue,
Expand All @@ -69,7 +69,7 @@ const ColorPicker = (
const [ showInputs, setShowInputs ] = useState< boolean >( false );
const [ colorType, setColorType ] = useState< ColorType >( 'rgb' );

const Picker = disableAlpha ? HexColorPicker : RgbaColorPicker;
const Picker = enableAlpha ? RgbaColorPicker : HexColorPicker;

return (
<ColorfulWrapper ref={ forwardedRef }>
Expand All @@ -87,7 +87,7 @@ const ColorPicker = (
<ColorDisplay
color={ safeColor }
colorType={ colorType }
disableAlpha={ disableAlpha }
enableAlpha={ enableAlpha }
/>
) }
<Button
Expand All @@ -102,7 +102,7 @@ const ColorPicker = (
colorType={ colorType }
color={ safeColor }
onChange={ debouncedSetColor }
disableAlpha={ disableAlpha }
enableAlpha={ enableAlpha }
/>
) }
</ColorfulWrapper>
Expand Down
10 changes: 3 additions & 7 deletions packages/components/src/ui/color-picker/hex-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ import { space } from '../utils/space';
interface HexInputProps {
color: string;
onChange: ( value: string ) => void;
disableAlpha: boolean;
enableAlpha: boolean;
}

export const HexInput = ( {
color,
onChange,
disableAlpha,
}: HexInputProps ) => {
export const HexInput = ( { color, onChange, enableAlpha }: HexInputProps ) => {
const handleValidate = ( value: string ) => {
if ( ! colorize( value ).isValid() ) {
throw new Error( 'Invalid hex color input' );
Expand All @@ -41,7 +37,7 @@ export const HexInput = ( {
onChange( colorize( nextValue ).toHex8String() )
}
onValidate={ handleValidate }
maxLength={ disableAlpha ? 6 : 8 }
maxLength={ enableAlpha ? 8 : 6 }
/>
);
};
10 changes: 3 additions & 7 deletions packages/components/src/ui/color-picker/hsl-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import { InputWithSlider } from './input-with-slider';
interface HslInputProps {
color: string;
onChange: ( color: string ) => void;
disableAlpha: boolean;
enableAlpha: boolean;
}

export const HslInput = ( {
color,
onChange,
disableAlpha,
}: HslInputProps ) => {
export const HslInput = ( { color, onChange, enableAlpha }: HslInputProps ) => {
const { h, s, l, a } = colorize( color ).toHsl();

return (
Expand Down Expand Up @@ -57,7 +53,7 @@ export const HslInput = ( {
)
}
/>
{ ! disableAlpha && (
{ enableAlpha && (
<InputWithSlider
min={ 0 }
max={ 100 }
Expand Down
10 changes: 3 additions & 7 deletions packages/components/src/ui/color-picker/rgb-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import { InputWithSlider } from './input-with-slider';
interface RgbInputProps {
color: string;
onChange: ( color: string ) => void;
disableAlpha: boolean;
enableAlpha: boolean;
}

export const RgbInput = ( {
color,
onChange,
disableAlpha,
}: RgbInputProps ) => {
export const RgbInput = ( { color, onChange, enableAlpha }: RgbInputProps ) => {
const { r, g, b, a } = colorize( color ).toRgb();

return (
Expand Down Expand Up @@ -53,7 +49,7 @@ export const RgbInput = ( {
onChange( colorize( { r, g, b: nextB, a } ).toHex8String() )
}
/>
{ ! disableAlpha && (
{ enableAlpha && (
<InputWithSlider
min={ 0 }
max={ 100 }
Expand Down
12 changes: 10 additions & 2 deletions packages/components/src/ui/color-picker/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { useState } from '@wordpress/element';
*/
import { ColorPicker } from '..';
import { Flex } from '../../../flex';
import { Spacer } from '../../../spacer';
import { space } from '../../utils/space';

export default {
component: ColorPicker,
Expand All @@ -22,11 +24,17 @@ export default {
const Example = () => {
const [ color, setColor ] = useState( '#fff' );
const props = {
disableAlpha: boolean( 'disableAlpha', true ),
enableAlpha: boolean( 'enableAlpha', false ),
};

return (
<Flex gap={ 8 } align="flex-start">
<Flex
as={ Spacer }
gap={ space( 2 ) }
justify="space-around"
align="flex-start"
marginTop={ space( 10 ) }
>
<ColorPicker { ...props } color={ color } onChange={ setColor } />
<ColorPicker { ...props } color={ color } onChange={ setColor } />
</Flex>
Expand Down