-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ColorPicker: Only display detailed inputs (#41222)
- Loading branch information
1 parent
9fe78db
commit 90264d2
Showing
8 changed files
with
111 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/components/src/color-picker/color-copy-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.