File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 1
1
import Emoji from "react-emoji-render"
2
2
import {
3
3
Avatar ,
4
+ ColorMode ,
4
5
Flex ,
5
6
HStack ,
6
7
SimpleGrid ,
7
8
SimpleGridProps ,
8
9
Stack ,
9
10
Text ,
11
+ useColorMode ,
10
12
} from "@chakra-ui/react"
11
13
12
14
import type { GHIssue } from "@/lib/types"
13
15
16
+ import { getContrastYIQ } from "@/lib/utils/getContrastYIQ"
17
+
14
18
import InlineLink from "../Link"
15
19
import Tag from "../Tag"
16
20
17
21
type IssuesListProps = SimpleGridProps & {
18
22
issues : GHIssue [ ]
19
23
}
20
24
25
+ function pickTagColorsByColorMode ( color : string , colorMode : ColorMode ) {
26
+ if ( colorMode === "dark" ) {
27
+ return {
28
+ color : `#${ color } ` ,
29
+ bgColor : `#${ color } 22` ,
30
+ }
31
+ } else {
32
+ return {
33
+ color : getContrastYIQ ( `#${ color } ` ) ,
34
+ bgColor : `#${ color } ` ,
35
+ }
36
+ }
37
+ }
38
+
21
39
const IssuesList = ( { issues, ...props } : IssuesListProps ) => {
40
+ const { colorMode } = useColorMode ( )
41
+
22
42
return (
23
43
< SimpleGrid columns = { [ 1 , null , 2 ] } spacing = { 4 } { ...props } >
24
44
{ issues . map ( ( issue ) => (
@@ -51,8 +71,7 @@ const IssuesList = ({ issues, ...props }: IssuesListProps) => {
51
71
< Tag
52
72
key = { label . id }
53
73
label = { < Emoji text = { label . name } /> }
54
- color = { `#${ label . color } ` }
55
- bgColor = { `#${ label . color } 22` }
74
+ { ...pickTagColorsByColorMode ( label . color , colorMode ) }
56
75
/>
57
76
)
58
77
} ) }
Original file line number Diff line number Diff line change
1
+ export const getContrastYIQ = ( hexcolor : string ) : "black" | "white" => {
2
+ // Convert hex to RGB first
3
+ let r = 0 ,
4
+ g = 0 ,
5
+ b = 0
6
+ if ( hexcolor . length == 4 ) {
7
+ r = parseInt ( hexcolor [ 1 ] + hexcolor [ 1 ] , 16 )
8
+ g = parseInt ( hexcolor [ 2 ] + hexcolor [ 2 ] , 16 )
9
+ b = parseInt ( hexcolor [ 3 ] + hexcolor [ 3 ] , 16 )
10
+ } else if ( hexcolor . length == 7 ) {
11
+ r = parseInt ( hexcolor [ 1 ] + hexcolor [ 2 ] , 16 )
12
+ g = parseInt ( hexcolor [ 3 ] + hexcolor [ 4 ] , 16 )
13
+ b = parseInt ( hexcolor [ 5 ] + hexcolor [ 6 ] , 16 )
14
+ }
15
+
16
+ // Calculate luminance
17
+ const luminance = ( 0.299 * r + 0.587 * g + 0.114 * b ) / 255
18
+
19
+ // Return black for bright colors, white for dark colors
20
+ return luminance > 0.5 ? "black" : "white"
21
+ }
You can’t perform that action at this time.
0 commit comments