@@ -4,83 +4,83 @@ import { oklch2hex } from "colorizr";
4
4
import { useEffect , useRef , useState } from "react" ;
5
5
import clsx from "clsx" ;
6
6
import {
7
- Button ,
8
- Tooltip ,
9
- TooltipPanel ,
10
- TooltipTrigger ,
7
+ Button ,
8
+ Tooltip ,
9
+ TooltipPanel ,
10
+ TooltipTrigger ,
11
11
} from "@headlessui/react" ;
12
12
13
13
export function Color ( { name, shade } : { name : string ; shade : string } ) {
14
- const [ justCopied , setJustCopied ] = useState ( false ) ;
15
-
16
- const buttonRef = useRef < HTMLButtonElement > ( null ) ;
17
-
18
- const colorVariableName = `--color-${ name } -${ shade } ` ;
19
-
20
- const copyHexToClipboard = ( ) => {
21
- if ( ! buttonRef . current ) {
22
- return ;
23
- }
24
-
25
- const styleValue = buttonRef . current
26
- . computedStyleMap ( )
27
- . get ( colorVariableName ) ;
28
-
29
- if ( ! styleValue ) {
30
- return ;
31
- }
32
-
33
- const oklchWithCSSFunctionalNotation = styleValue . toString ( ) ;
34
-
35
- // oklch(0.808 0.114 19.571) to 0.808 0.114 19.571
36
- const oklch = oklchWithCSSFunctionalNotation . slice ( 6 , - 1 ) ;
37
-
38
- // 0.808 0.114 19.571 to [0.808, 0.114, 19.571]
39
- const oklchTuple = oklch . split ( " " ) . map ( Number ) as [ number , number , number ] ;
40
-
41
- const hex = oklch2hex ( oklchTuple ) ;
42
-
43
- navigator . clipboard . writeText ( hex ) ;
44
-
45
- setJustCopied ( true ) ;
46
- } ;
47
-
48
- useEffect ( ( ) => {
49
- const timeout = setTimeout ( ( ) => {
50
- if ( ! justCopied ) {
51
- return ;
52
- }
53
-
54
- setJustCopied ( false ) ;
55
- } , 1300 ) ;
56
-
57
- return ( ) => clearTimeout ( timeout ) ;
58
- } , [ justCopied ] ) ;
59
-
60
- return (
61
- < Tooltip as = "div" showDelayMs = { 100 } hideDelayMs = { 0 } className = "relative" >
62
- < TooltipTrigger >
63
- < Button
64
- ref = { buttonRef }
65
- type = "button"
66
- onClick = { copyHexToClipboard }
67
- onTouchEnd = { copyHexToClipboard }
68
- style = { { backgroundColor : `var(${ colorVariableName } )` } }
69
- className = { clsx (
70
- "h-full w-full cursor-pointer aspect-1/1 rounded-sm outline -outline-offset-1 outline-black/10 sm:rounded-md dark:outline-white/10 flex items-center justify-center" ,
71
- ) }
72
- />
73
- </ TooltipTrigger >
74
- < TooltipPanel
75
- as = "div"
76
- anchor = "top"
77
- className = "pointer-events-none z-10 translate-y-2 rounded-full border border-gray-950 bg-gray-950/90 py-0.5 pr-2 pb-1 pl-3 text-center font-mono text-xs/6 font-medium whitespace-nowrap text-white opacity-100 inset-ring inset-ring-white/10 transition-[opacity] starting:opacity-0"
78
- >
79
- { justCopied ? "Copied!" : "Click to copy" }
80
- </ TooltipPanel >
81
- </ Tooltip >
82
- ) ;
83
- /*
84
-
85
- */
14
+ const [ justCopied , setJustCopied ] = useState ( false ) ;
15
+
16
+ const buttonRef = useRef < HTMLButtonElement > ( null ) ;
17
+
18
+ const colorVariableName = `--color-${ name } -${ shade } ` ;
19
+
20
+ const copyHexToClipboard = ( ) => {
21
+ if ( ! buttonRef . current ) {
22
+ return ;
23
+ }
24
+
25
+ const styleValue = buttonRef . current
26
+ . computedStyleMap ( )
27
+ . get ( colorVariableName ) ;
28
+
29
+ if ( ! styleValue ) {
30
+ return ;
31
+ }
32
+
33
+ const oklchWithCSSFunctionalNotation = styleValue . toString ( ) ;
34
+
35
+ // oklch(0.808 0.114 19.571) to 0.808 0.114 19.571
36
+ const oklch = oklchWithCSSFunctionalNotation . slice ( 6 , - 1 ) ;
37
+
38
+ // 0.808 0.114 19.571 to [0.808, 0.114, 19.571]
39
+ const oklchTuple = oklch . split ( " " ) . map ( Number ) as [ number , number , number ] ;
40
+
41
+ const hex = oklch2hex ( oklchTuple ) ;
42
+
43
+ navigator . clipboard . writeText ( hex ) ;
44
+
45
+ setJustCopied ( true ) ;
46
+ } ;
47
+
48
+ useEffect ( ( ) => {
49
+ const timeout = setTimeout ( ( ) => {
50
+ if ( ! justCopied ) {
51
+ return ;
52
+ }
53
+
54
+ setJustCopied ( false ) ;
55
+ } , 1300 ) ;
56
+
57
+ return ( ) => clearTimeout ( timeout ) ;
58
+ } , [ justCopied ] ) ;
59
+
60
+ return (
61
+ < Tooltip as = "div" showDelayMs = { 100 } hideDelayMs = { 0 } className = "relative" >
62
+ < TooltipTrigger >
63
+ < Button
64
+ ref = { buttonRef }
65
+ type = "button"
66
+ onClick = { copyHexToClipboard }
67
+ onTouchEnd = { copyHexToClipboard }
68
+ style = { { backgroundColor : `var(${ colorVariableName } )` } }
69
+ className = { clsx (
70
+ "h-full w-full cursor-pointer aspect-1/1 rounded-sm outline -outline-offset-1 outline-black/10 sm:rounded-md dark:outline-white/10 flex items-center justify-center" ,
71
+ ) }
72
+ />
73
+ </ TooltipTrigger >
74
+ < TooltipPanel
75
+ as = "div"
76
+ anchor = "top"
77
+ className = "pointer-events-none z-10 translate-y-2 rounded-full border border-gray-950 bg-gray-950/90 py-0.5 pr-2 pb-1 pl-3 text-center font-mono text-xs/6 font-medium whitespace-nowrap text-white opacity-100 inset-ring inset-ring-white/10 transition-[opacity] starting:opacity-0"
78
+ >
79
+ { justCopied ? "Copied!" : "Click to copy" }
80
+ </ TooltipPanel >
81
+ </ Tooltip >
82
+ ) ;
83
+ /*
84
+
85
+ */
86
86
}
0 commit comments