Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]: tag coloring #357

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions src/components/Tag.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
import { colors } from "../styles/colors"
import styled from "@emotion/styled"
import { useRouter } from "next/router"
import React from "react"

const colorArray = [
colors.light.red4,
colors.light.amber4,
colors.light.green4,
colors.light.blue4,
colors.light.indigo4,
colors.light.purple4,
colors.light.pink4,
]

type Props = {
children: string
}

const hashStringToColor = (str: string, colorsArray: string[]) => {
let hash = 0
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash)
}
const index = Math.abs(hash) % colorsArray.length

return colorsArray[index]
}

const Tag: React.FC<Props> = ({ children }) => {
const router = useRouter()

const handleClick = (value: string) => {
router.push(`/?tag=${value}`)
}
return (
<StyledWrapper onClick={() => handleClick(children)}>
{children}
</StyledWrapper>
)

const StyledTag = styled.div`
background-color: ${hashStringToColor(children, colorArray)};
color: ${colors.light.gray10};
padding: 0.25rem 0.5rem;
border-radius: 50px;
font-size: 0.75rem;
line-height: 1rem;
font-weight: 400;
cursor: pointer;
`

return <StyledTag onClick={() => handleClick(children)}>{children}</StyledTag>
}

export default Tag

const StyledWrapper = styled.div`
padding-top: 0.25rem;
padding-bottom: 0.25rem;
padding-left: 0.5rem;
padding-right: 0.5rem;
border-radius: 50px;
font-size: 0.75rem;
line-height: 1rem;
font-weight: 400;
color: ${({ theme }) => theme.colors.gray10};
background-color: ${({ theme }) => theme.colors.gray5};
cursor: pointer;
`
7 changes: 4 additions & 3 deletions src/routes/Detail/PostDetail/PostHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ const PostHeader: React.FC<Props> = ({ data }) => {
<div className="mid">
{data.tags && (
<div className="tags">
{data.tags.map((tag: string) => (
<Tag key={tag}>{tag}</Tag>
))}
{data.tags &&
data.tags.map((tag: string, idx: number) => (
<Tag key={idx}>{tag}</Tag>
))}
</div>
)}
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/routes/Feed/PostList/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ const PostCard: React.FC<Props> = ({ data }) => {
/>
</div>
)}
<div data-thumb={!!data.thumbnail} data-category={!!category} className="content">
<div
data-thumb={!!data.thumbnail}
data-category={!!category}
className="content"
>
<header className="top">
<h2>{data.title}</h2>
</header>
Expand Down
16 changes: 16 additions & 0 deletions src/styles/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import {
greenDark,
indigo,
indigoDark,
amber,
amberDark,
grayA,
grayDarkA,
purple,
purpleDark,
pink,
pinkDark,
} from "@radix-ui/colors"

export type Colors = typeof colors.light & typeof colors.dark
Expand All @@ -20,12 +28,20 @@ export const colors = {
...blue,
...red,
...green,
...amber,
...grayA,
...purple,
...pink,
},
dark: {
...indigoDark,
...grayDark,
...blueDark,
...redDark,
...greenDark,
...amberDark,
...grayDarkA,
...purpleDark,
...pinkDark,
},
}