重构目前所有的复制颜色代码功能使用统一的Hook。
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import { isEqual, isNil } from 'lodash-es';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useCopyToClipboard } from 'react-use';
|
||||
import { isEqual } from 'lodash-es';
|
||||
import { useState } from 'react';
|
||||
import { useCopyColor } from '../hooks/useCopyColor';
|
||||
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 = {
|
||||
@@ -14,35 +13,18 @@ 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]);
|
||||
const copyColor = useCopyColor();
|
||||
|
||||
return (
|
||||
<div className={styles.color_picker}>
|
||||
<div className={styles.color_preview} style={{ backgroundColor: `#${selectedColor}` }} />
|
||||
<div className={styles.color_code} onClick={handleCopyAction}>
|
||||
<div className={styles.color_code} onClick={() => copyColor(selectedColor)}>
|
||||
#{selectedColor}
|
||||
</div>
|
||||
<HSegmentedControl
|
||||
|
@@ -1,38 +1,18 @@
|
||||
import { Icon } from '@iconify/react/dist/iconify.js';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useCopyToClipboard } from 'react-use';
|
||||
import { useMemo, useState } from 'react';
|
||||
import NoColor from '../assets/NoColor.svg';
|
||||
import { useColorFunction } from '../ColorFunctionContext';
|
||||
import { useCopyColor } from '../hooks/useCopyColor';
|
||||
import styles from './ColorStand.module.css';
|
||||
import { HSegmentedControl } from './HSegmentedControl';
|
||||
import { NotificationType, useNotification } from './Notifications';
|
||||
|
||||
type ColorValueProps = {
|
||||
value: string | null;
|
||||
};
|
||||
|
||||
function ColorValue({ value }: ColorValueProps) {
|
||||
const [cpState, copyToClipboard] = useCopyToClipboard();
|
||||
const { showToast } = useNotification();
|
||||
const handleCopy = useCallback(() => {
|
||||
if (!isNil(value)) {
|
||||
copyToClipboard(value);
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
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]);
|
||||
const copyColor = useCopyColor();
|
||||
|
||||
return (
|
||||
<div className={styles.color_value}>
|
||||
@@ -42,7 +22,7 @@ function ColorValue({ value }: ColorValueProps) {
|
||||
<span>Not Available</span>
|
||||
</div>
|
||||
) : (
|
||||
<div className={styles.value} onClick={handleCopy}>
|
||||
<div className={styles.value} onClick={() => copyColor(value)}>
|
||||
{value}
|
||||
</div>
|
||||
)}
|
||||
|
Reference in New Issue
Block a user