Skip to content

Commit 5c5405b

Browse files
committed
feat(component): support custom tag for component LeetCodeDifficulty
1 parent fd7b2a9 commit 5c5405b

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/components/LeetCodeDifficulty.astro

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
---
2+
import type { HTMLAttributes } from 'astro/types'
23
import kebabCase from 'lodash.kebabcase'
34
4-
export interface Props {
5+
export interface Props extends HTMLAttributes<'a' | 'span'> {
6+
tag?: string
57
difficulty: string
68
}
79
8-
const { difficulty } = Astro.props
10+
const { difficulty, tag = 'a', ...props } = Astro.props
11+
12+
const Tag = tag
13+
14+
const tagProps: Record<string, string> = {}
15+
if (tag === 'a') {
16+
tagProps.href = `/difficulties/${kebabCase(difficulty)}`
17+
}
18+
919
const difficultyStyleMap: Record<string, string> = {
1020
Easy: 'difficulty-easy',
1121
Medium: 'difficulty-medium',
@@ -21,12 +31,17 @@ const getDifficultyCssClass: (difficulty: string) => string = (
2131
}
2232
---
2333

24-
<a
25-
href={`/difficulties/${kebabCase(difficulty)}`}
26-
class:list={['px-2 py-1', getDifficultyCssClass(difficulty)]}
34+
<Tag
35+
{...tagProps}
36+
class:list={[
37+
'px-2 py-1',
38+
getDifficultyCssClass(difficulty),
39+
tag === 'a' ? 'link' : '',
40+
props.class,
41+
]}
2742
>
2843
{difficulty}
29-
</a>
44+
</Tag>
3045

3146
<style lang="scss">
3247
$difficulties: 'easy', 'medium', 'hard';
@@ -37,9 +52,11 @@ const getDifficultyCssClass: (difficulty: string) => string = (
3752
@apply border-2;
3853
@apply border-difficulty-#{$difficulty};
3954
@apply text-difficulty-#{$difficulty};
40-
@apply hover:bg-site-bg;
41-
@apply hover:border-difficulty-#{$difficulty}-hover;
42-
@apply hover:text-difficulty-#{$difficulty}-hover;
55+
&.link {
56+
@apply hover:bg-site-bg;
57+
@apply hover:border-difficulty-#{$difficulty}-hover;
58+
@apply hover:text-difficulty-#{$difficulty}-hover;
59+
}
4360
}
4461
}
4562
</style>

0 commit comments

Comments
 (0)