1
1
import { ThemeWithHSLColor } from "../lib/theme" ;
2
- import React , { useCallback , useEffect , useState } from "react" ;
2
+ import React , { useCallback , useEffect , useRef , useState } from "react" ;
3
3
import { Button } from "./ui/button" ;
4
4
import { copy2clipboard , HSL2ComputedColor , setStyleColor } from "../lib/utils" ;
5
5
import { useDebounceCallback } from "../hooks/useDebounceCallback" ;
@@ -13,40 +13,45 @@ export function Item({
13
13
onSave : ( ) => void ;
14
14
} ) {
15
15
const [ color , setColor ] = useState < string > ( "#000" ) ;
16
+ const hslColorRef = useRef ( colord ( color ) . toHsl ( ) ) ;
16
17
17
18
useEffect ( ( ) => {
18
19
setColor ( colord ( theme . color ) . toHex ( ) ) ;
19
20
} , [ theme ] ) ;
21
+
20
22
const updateValue = useCallback (
21
- useDebounceCallback ( ( color : string ) => {
22
- setStyleColor ( theme . variable , colord ( color ) . toHsl ( ) ) ;
23
- onSave ( ) ;
24
- } , 0 ) ,
23
+ useDebounceCallback ( ( ) => {
24
+ setStyleColor ( theme . variable , hslColorRef . current ) ;
25
+ onSave ( ) ; // save to localstorage
26
+ } , 100 ) ,
25
27
[ theme . variable ]
26
28
) ;
29
+
30
+ const title =
31
+ theme . variable + ": " + HSL2ComputedColor ( hslColorRef . current ) + ";" ;
32
+
27
33
return (
28
34
< Button
29
35
variant = { "colorbtn" }
30
- className = " select-none"
36
+ className = "select-none"
31
37
asChild
32
- title = { theme . variable + ": " + HSL2ComputedColor ( theme . color ) + ";" }
33
- onDoubleClick = { ( ) =>
34
- copy2clipboard (
35
- theme . variable + ": " + HSL2ComputedColor ( theme . color ) + ";"
36
- )
37
- }
38
+ title = { title }
39
+ onDoubleClick = { ( ) => copy2clipboard ( title ) }
38
40
>
39
41
< div >
40
42
< div className = "relative overflow-hidden rounded border size-6 cursor-pointer shadow-md drop-shadow-md" >
41
43
< input
42
- onClick = { ( e ) => e . stopPropagation ( ) }
44
+ className = "absolute cursor-pointer inset-1/2 size-[calc(100%+12px)] -translate-x-1/2 -translate-y-1/2 flex-shrink-0 bg-transparent"
43
45
// defaultValue={colord(color).toHex()}
44
- value = { color }
45
46
type = "color"
46
- onChange = { ( e ) => (
47
- setColor ( e . target . value ) , updateValue ( e . target . value )
48
- ) }
49
- className = "absolute cursor-pointer inset-1/2 size-[calc(100%+12px)] -translate-x-1/2 -translate-y-1/2 flex-shrink-0 bg-transparent"
47
+ value = { color }
48
+ onChange = { ( e ) => {
49
+ const clr = e . target . value ;
50
+ hslColorRef . current = colord ( clr ) . toHsl ( ) ;
51
+ setColor ( clr ) ;
52
+ updateValue ( ) ;
53
+ } }
54
+ onClick = { ( e ) => e . stopPropagation ( ) } // disable copy to clipboard
50
55
/>
51
56
</ div >
52
57
< span className = "flex-shrink-0" > { theme . title } </ span >
0 commit comments