diff --git a/src/components/ColorPicker.module.css b/src/components/ColorPicker.module.css index 8bfc002..61f0678 100644 --- a/src/components/ColorPicker.module.css +++ b/src/components/ColorPicker.module.css @@ -14,6 +14,7 @@ font-size: var(--font-size-xs); line-height: var(--font-size-xs); text-transform: uppercase; + cursor: pointer; } } } diff --git a/src/components/ColorPicker.tsx b/src/components/ColorPicker.tsx index 59a0c2e..e480ff9 100644 --- a/src/components/ColorPicker.tsx +++ b/src/components/ColorPicker.tsx @@ -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(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 (
-
#{selectedColor}
+
+ #{selectedColor} +