diff --git a/src/components/ColorPicker.tsx b/src/components/ColorPicker.tsx index e480ff9..cffebb5 100644 --- a/src/components/ColorPicker.tsx +++ b/src/components/ColorPicker.tsx @@ -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(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 (
-
+
copyColor(selectedColor)}> #{selectedColor}
{ - 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 (
@@ -42,7 +22,7 @@ function ColorValue({ value }: ColorValueProps) { Not Available
) : ( -
+
copyColor(value)}> {value}
)} diff --git a/src/hooks/useCopyColor.ts b/src/hooks/useCopyColor.ts index 69c1e69..b96a7c2 100644 --- a/src/hooks/useCopyColor.ts +++ b/src/hooks/useCopyColor.ts @@ -7,6 +7,7 @@ export function useCopyColor() { const { showToast } = useNotification(); const [cpState, copyToClipboard] = useCopyToClipboard(); const copyAction = useCallback((color: string) => { + if (isNil(color)) return; if (color.startsWith('#')) { copyToClipboard(color); } else { diff --git a/src/page-components/harmonies/HarmonyPreview.tsx b/src/page-components/harmonies/HarmonyPreview.tsx index 47ac03d..dc77602 100644 --- a/src/page-components/harmonies/HarmonyPreview.tsx +++ b/src/page-components/harmonies/HarmonyPreview.tsx @@ -1,8 +1,7 @@ import cx from 'clsx'; -import { constant, flatten, isEqual, isNil, take, times } from 'lodash-es'; -import { useEffect, useMemo } from 'react'; -import { useCopyToClipboard } from 'react-use'; -import { NotificationType, useNotification } from '../../components/Notifications'; +import { constant, flatten, isEqual, take, times } from 'lodash-es'; +import { useMemo } from 'react'; +import { useCopyColor } from '../../hooks/useCopyColor'; import { HarmonyColor } from '../../models'; import styles from './HarmonyPreview.module.css'; @@ -11,8 +10,7 @@ type HarmonyPreviewProps = { }; export function HarmonyPreview({ colors = [] }: HarmonyPreviewProps) { - const [cpState, copyToClipboard] = useCopyToClipboard(); - const { showToast } = useNotification(); + const copyColor = useCopyColor(); const extendedColors = useMemo(() => { const sortedColors = colors.sort((a, b) => -(a.ratio - b.ratio)); if (sortedColors.length >= 4) { @@ -24,19 +22,6 @@ export function HarmonyPreview({ colors = [] }: HarmonyPreviewProps) { ]); }, [colors]); - 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 (
Harmony Preview
@@ -49,7 +34,7 @@ export function HarmonyPreview({ colors = [] }: HarmonyPreviewProps) {
{ratio > 0 && `Ratio: ${ratio}`}
- {ratio > 0 && copyToClipboard(`#${color}`)}>#{color}} + {ratio > 0 && copyColor(color)}>#{color}}
))}