1
- import { getColorInfoFromPosition } from '@digdir/designsystemet/color' ;
1
+ import { getColorMetadataByNumber } from '@digdir/designsystemet/color' ;
2
2
import { ColorModal } from '@repo/components' ;
3
3
import cl from 'clsx/lite' ;
4
4
import { useRef } from 'react' ;
5
5
import type { TransformedToken } from 'style-dictionary' ;
6
6
7
- import type { ColorNumber , HexColor } from '@digdir/designsystemet/color' ;
7
+ import type {
8
+ Color ,
9
+ ColorNumber ,
10
+ HexColor ,
11
+ } from '@digdir/designsystemet/color' ;
8
12
9
13
import classes from './TokenColor.module.css' ;
10
14
interface TokenColorProps {
@@ -14,9 +18,8 @@ interface TokenColorProps {
14
18
15
19
/* The original.value is something like "{global.yellow.1}", and we need to get the weight between the last . and } */
16
20
export function getColorNumber ( value : string ) : ColorNumber | undefined {
17
- const firstSplit = value . split ( '.' ) . pop ( ) ?. replace ( '}' , '' ) ;
18
-
19
- const parsed = parseInt ( firstSplit as string ) ;
21
+ const digit = value . match ( / ^ \d + $ / ) ?. [ 0 ] ;
22
+ const parsed = parseInt ( digit || '' ) ;
20
23
21
24
return Number . isNaN ( parsed ) ? undefined : ( parsed as ColorNumber ) ;
22
25
}
@@ -25,30 +28,39 @@ const TokenColor = ({ value, token }: TokenColorProps) => {
25
28
const colorModalRef = useRef < HTMLDialogElement > ( null ) ;
26
29
27
30
const number = getColorNumber ( token . original . $value as string ) ;
28
- const Element = number ? 'button' : 'div' ;
31
+ const color : Color | undefined = number
32
+ ? {
33
+ ...getColorMetadataByNumber ( number ) ,
34
+ number,
35
+ hex : value ,
36
+ }
37
+ : undefined ;
38
+
39
+ const namespace = token . path [ 1 ] ;
29
40
30
- return (
41
+ return color ? (
31
42
< >
32
- { number ? (
33
- < ColorModal
34
- number = { number }
35
- hex = { value }
36
- namespace = { token . path [ 1 ] }
37
- colorModalRef = { colorModalRef }
38
- />
39
- ) : null }
40
- < div className = { classes . test } >
41
- < Element
43
+ < ColorModal
44
+ namespace = { namespace }
45
+ colorModalRef = { colorModalRef }
46
+ color = { color }
47
+ />
48
+ < div className = { classes . colorBox } >
49
+ < button
42
50
style = { { backgroundColor : value } }
43
51
className = { cl ( classes . color , 'ds-focus' ) }
44
- onClick = { ( ) => number && colorModalRef . current ?. showModal ( ) }
45
- aria-label = {
46
- number &&
47
- `Se mer om ${ token . path [ 1 ] } ${ getColorInfoFromPosition ( number ) . displayName } `
48
- }
49
- > </ Element >
52
+ onClick = { ( ) => colorModalRef . current ?. showModal ( ) }
53
+ aria-label = { `Se mer om ${ namespace } ${ color ?. displayName } ` }
54
+ > </ button >
50
55
</ div >
51
56
</ >
57
+ ) : (
58
+ < div className = { classes . colorBox } >
59
+ < div
60
+ style = { { backgroundColor : value } }
61
+ className = { cl ( classes . color , 'ds-focus' ) }
62
+ > </ div >
63
+ </ div >
52
64
) ;
53
65
} ;
54
66
0 commit comments