ColorPicker中增加复制当前颜色HEX值功能。

This commit is contained in:
徐涛 2024-12-31 15:28:24 +08:00
parent 016237ff6b
commit e1b366adb0
2 changed files with 26 additions and 3 deletions

View File

@ -14,6 +14,7 @@
font-size: var(--font-size-xs);
line-height: var(--font-size-xs);
text-transform: uppercase;
cursor: pointer;
}
}
}

View File

@ -1,9 +1,11 @@
import { isEqual } from 'lodash-es';
import { useState } from 'react';
import { isEqual, isNil } from 'lodash-es';
import { useCallback, useEffect, useState } from 'react';
import { useCopyToClipboard } from 'react-use';
import { ColorComponentInput } from './ColorComponentInput';
import styles from './ColorPicker.module.css';
import { HSegmentedControl } from './HSegmentedControl';
import { HslAssemble } from './HslAsssemble';
import { NotificationType, useNotification } from './Notifications';
import { RGBAssemble } from './RGBAssemble';
type ColorPickerProps = {
@ -12,17 +14,37 @@ type ColorPickerProps = {
};
export function ColorPicker({ color, onSelect }: ColorPickerProps) {
const [cpState, copyToClipboard] = useCopyToClipboard();
const { showToast } = useNotification();
const [pickMode, setMode] = useState<'rgb' | 'hsl' | 'input'>('rgb');
const [selectedColor, setSelectedColor] = useState<string>(color ?? '000000');
const handleColorSelect = (color: string) => {
setSelectedColor(color);
onSelect?.(color);
};
const handleCopyAction = useCallback(() => {
copyToClipboard(`#${selectedColor}`);
}, [selectedColor]);
useEffect(() => {
if (!isNil(cpState.error)) {
showToast(NotificationType.ERROR, 'Failed to copy to clipboard', 'tabler:alert-circle', 3000);
} else if (!isNil(cpState.value)) {
showToast(
NotificationType.SUCCESS,
`${cpState.value} has been copied to clipboard.`,
'tabler:circle-check',
3000,
);
}
}, [cpState]);
return (
<div className={styles.color_picker}>
<div className={styles.color_preview} style={{ backgroundColor: `#${selectedColor}` }} />
<div className={styles.color_code}>#{selectedColor}</div>
<div className={styles.color_code} onClick={handleCopyAction}>
#{selectedColor}
</div>
<HSegmentedControl
options={[
{ label: 'RGB', value: 'rgb' },