Skip to content

Commit 1b330ed

Browse files
committed
feat: 🎸 increase tag color randomness
1 parent 157d7c8 commit 1b330ed

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

‎app/posts/[slug]/tag.tsx‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { cva } from 'class-variance-authority'
22

33
import { Badge } from '@/components/ui/badge'
4-
import { cn } from '@/lib/utils'
54

65
const tagVariants = cva('font-semibold cursor-default border-2 h-6', {
76
variants: {
@@ -10,30 +9,34 @@ const tagVariants = cva('font-semibold cursor-default border-2 h-6', {
109
fuchsia: 'bg-fuchsia-900 border-fuchsia-800 text-fuchsia-300',
1110
violet: 'bg-violet-900 border-violet-800 text-violet-300',
1211
emerald: 'bg-emerald-900 border-emerald-800 text-emerald-300',
12+
cyan: 'bg-cyan-900 border-cyan-800 text-cyan-300',
1313
},
1414
},
1515
})
16-
const COLOR_OPTS: TagColorOptions[] = ['emerald', 'fuchsia', 'blue', 'violet']
16+
const COLOR_OPTS: TagColorOptions[] = ['emerald', 'fuchsia', 'blue', 'violet', 'cyan']
1717

18-
type TagColorOptions = 'blue' | 'fuchsia' | 'violet' | 'emerald'
18+
type TagColorOptions = 'blue' | 'fuchsia' | 'violet' | 'emerald' | 'cyan'
1919

2020
interface TagProps {
2121
text: string
2222
}
2323

2424
export function Tag({ text }: TagProps) {
25-
const hashString = (str: string) => {
26-
return str
27-
.split('')
28-
.map((char) => char.charCodeAt(0))
29-
.reduce((a, b) => a + b, 0)
30-
}
31-
32-
const textToColor = (str: string) => COLOR_OPTS[hashString(str) % COLOR_OPTS.length]
25+
const convertTextToColorVariant = (str: string) => COLOR_OPTS[hashString(str) % COLOR_OPTS.length]
3326

3427
return (
35-
<Badge variant="outline" className={cn(tagVariants({ variant: textToColor(text) }))}>
28+
<Badge variant="outline" className={tagVariants({ variant: convertTextToColorVariant(text) })}>
3629
#{text}
3730
</Badge>
3831
)
3932
}
33+
34+
function hashString(str: string): number {
35+
return str
36+
.toLowerCase()
37+
.split('')
38+
.reduce((hash, char) => {
39+
const charCode = char.charCodeAt(0)
40+
return ((hash << 5) + hash + charCode) >>> 0
41+
}, 5381)
42+
}

0 commit comments

Comments
 (0)