Skip to content

Commit

Permalink
ColorPicker: Only display detailed inputs (#41222)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw authored May 27, 2022
1 parent 9fe78db commit 90264d2
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 214 deletions.
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 @@

- `SelectControl`: Add `__nextHasNoMarginBottom` prop for opting into the new margin-free styles ([#41269](https://github.com/WordPress/gutenberg/pull/41269)).
- `ColorPicker`: Strip leading hash character from hex values pasted into input. ([#41223](https://github.com/WordPress/gutenberg/pull/41223))
- `ColorPicker`: Display detailed color inputs by default. ([#41222](https://github.com/WordPress/gutenberg/pull/41222))

### Internal

Expand Down
76 changes: 76 additions & 0 deletions packages/components/src/color-picker/color-copy-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* WordPress dependencies
*/
import { useCopyToClipboard } from '@wordpress/compose';
import { useState, useEffect, useRef } from '@wordpress/element';
import { copy } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { CopyButton } from './styles';
import { Text } from '../text';
import { Tooltip } from '../ui/tooltip';

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

export const ColorCopyButton = ( props: ColorCopyButtonProps ) => {
const { color, colorType } = props;
const [ copiedColor, setCopiedColor ] = useState< string | null >( null );
const copyTimer = useRef< ReturnType< typeof setTimeout > | undefined >();
const copyRef = useCopyToClipboard< HTMLDivElement >(
() => {
switch ( colorType ) {
case 'hsl': {
return color.toHslString();
}
case 'rgb': {
return color.toRgbString();
}
default:
case 'hex': {
return color.toHex();
}
}
},
() => {
if ( copyTimer.current ) {
clearTimeout( copyTimer.current );
}
setCopiedColor( color.toHex() );
copyTimer.current = setTimeout( () => {
setCopiedColor( null );
copyTimer.current = undefined;
}, 3000 );
}
);
useEffect( () => {
// Clear copyTimer on component unmount.
return () => {
if ( copyTimer.current ) {
clearTimeout( copyTimer.current );
}
};
}, [] );

return (
<Tooltip
content={
<Text color="white">
{ copiedColor === color.toHex()
? __( 'Copied!' )
: __( 'Copy' ) }
</Text>
}
placement="bottom"
>
<CopyButton
isSmall
ref={ copyRef }
icon={ copy }
showTooltip={ false }
/>
</Tooltip>
);
};
169 changes: 0 additions & 169 deletions packages/components/src/color-picker/color-display.tsx

This file was deleted.

56 changes: 18 additions & 38 deletions packages/components/src/color-picker/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import namesPlugin from 'colord/plugins/names';
* WordPress dependencies
*/
import { useCallback, useState, useMemo } from '@wordpress/element';
import { settings } from '@wordpress/icons';
import { useDebounce } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';

Expand All @@ -27,9 +26,8 @@ import {
ColorfulWrapper,
SelectControl,
AuxiliaryColorArtefactWrapper,
DetailsControlButton,
} from './styles';
import { ColorDisplay } from './color-display';
import { ColorCopyButton } from './color-copy-button';
import { ColorInput } from './color-input';
import { Picker } from './picker';
import { useControlledValue } from '../utils/hooks';
Expand Down Expand Up @@ -85,7 +83,6 @@ const ColorPicker = (
[ debouncedSetColor ]
);

const [ showInputs, setShowInputs ] = useState< boolean >( false );
const [ colorType, setColorType ] = useState< ColorType >(
copyFormat || 'hex'
);
Expand All @@ -99,44 +96,27 @@ const ColorPicker = (
/>
<AuxiliaryColorArtefactWrapper>
<HStack justify="space-between">
{ showInputs ? (
<SelectControl
options={ options }
value={ colorType }
onChange={ ( nextColorType ) =>
setColorType( nextColorType as ColorType )
}
label={ __( 'Color format' ) }
hideLabelFromVision
/>
) : (
<ColorDisplay
color={ safeColordColor }
colorType={ copyFormat || colorType }
enableAlpha={ enableAlpha }
/>
) }
<DetailsControlButton
isSmall
onClick={ () => setShowInputs( ! showInputs ) }
icon={ settings }
isPressed={ showInputs }
label={
showInputs
? __( 'Hide detailed inputs' )
: __( 'Show detailed inputs' )
<SelectControl
options={ options }
value={ colorType }
onChange={ ( nextColorType ) =>
setColorType( nextColorType as ColorType )
}
label={ __( 'Color format' ) }
hideLabelFromVision
/>
</HStack>
<Spacer margin={ 4 } />
{ showInputs && (
<ColorInput
colorType={ colorType }
<ColorCopyButton
color={ safeColordColor }
onChange={ handleChange }
enableAlpha={ enableAlpha }
colorType={ copyFormat || colorType }
/>
) }
</HStack>
<Spacer margin={ 4 } />
<ColorInput
colorType={ colorType }
color={ safeColordColor }
onChange={ handleChange }
enableAlpha={ enableAlpha }
/>
</AuxiliaryColorArtefactWrapper>
</ColorfulWrapper>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/color-picker/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ export const ColorfulWrapper = styled.div`
${ inputHeightStyle }
`;

export const DetailsControlButton = styled( Button )`
export const CopyButton = styled( Button )`
&&&&& {
min-width: ${ space( 6 ) };
padding: 0;
> svg {
margin-right: 0;
}
}
`;

Expand Down
9 changes: 9 additions & 0 deletions packages/components/src/color-picker/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
/**
* External dependencies
*/
import type { Colord } from 'colord';

export type ColorType = 'rgb' | 'hsl' | 'hex';
export type ColorCopyButtonProps = {
color: Colord;
colorType: ColorType;
};
2 changes: 1 addition & 1 deletion packages/compose/src/hooks/use-copy-to-clipboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function useUpdatedRef( value ) {
* @return {import('react').Ref<TElementType>} A ref to assign to the target element.
*/
export default function useCopyToClipboard( text, onSuccess ) {
// Store the dependencies as refs and continuesly update them so they're
// Store the dependencies as refs and continuously update them so they're
// fresh when the callback is called.
const textRef = useUpdatedRef( text );
const onSuccessRef = useUpdatedRef( onSuccess );
Expand Down
Loading

0 comments on commit 90264d2

Please sign in to comment.