Skip to content

Commit 778aea7

Browse files
committed
refactor: styles to "beginner" ToC variant
1 parent 40935cd commit 778aea7

File tree

5 files changed

+109
-47
lines changed

5 files changed

+109
-47
lines changed

app/[locale]/what-is-ethereum/page.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,9 @@ const Page = async ({ params }: { params: Promise<{ locale: Lang }> }) => {
166166
<ListenToPlayer slug="what-is-ethereum" />
167167
</div>
168168

169-
<div
170-
className={cn(
171-
"row-start-1 lg:col-start-2 lg:row-span-2",
172-
"[&_a]:leading-base [&_ul]:list-decimal [&_ul]:border-s-0 [&_ul]:text-base",
173-
"[&_li>a]:inline [&_li]:list-item [&_ul]:list-inside [&_ul]:ps-0",
174-
"[&_li:hover]:text-primary-hover [&_li:hover_[data-label='label']]:text-primary-hover",
175-
"[&_[data-label='marker']]:!hidden [&_li:has([data-state-active='true'])]:text-primary",
176-
// `aside` targets desktop version
177-
"[&_aside]:sticky [&_aside]:top-[7.25rem]",
178-
"[&_aside]:h-fit [&_aside]:shrink-0 [&_aside]:gap-0 [&_aside]:space-y-2.5 [&_aside]:rounded-2xl [&_aside]:bg-accent-a/10 [&_aside]:px-3 [&_aside]:py-2",
179-
"[&_aside]:min-w-80 [&_aside]:max-w-72 [&_aside]:p-8 [&_aside]:text-body-medium",
180-
"[&_[data-label='label']]:font-bold [&_[data-label='label']]:normal-case [&_aside_[data-label='label']]:text-lg",
181-
// `button` targets mobile version
182-
"[&_button>span]:flex-none [&_button]:mb-16 [&_button]:justify-center [&_button]:rounded-lg [&_button]:border-border [&_button]:bg-accent-a/10 [&_button]:text-lg [&_button]:font-bold"
183-
)}
184-
>
185-
<TableOfContents items={tocItems} isMobile className="lg:hidden" />
186-
<TableOfContents items={tocItems} className="max-lg:hidden" />
169+
<div className="row-start-1 lg:col-start-2 lg:row-span-2">
170+
<TableOfContents variant="beginner" items={tocItems} isMobile />
171+
<TableOfContents variant="beginner" items={tocItems} />
187172
</div>
188173

189174
<div className="max-w-[50rem] space-y-14 lg:col-start-1 lg:row-start-2">

src/components/TableOfContents/ItemsList.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1+
import { cva, type VariantProps } from "class-variance-authority"
2+
13
import type { ToCItem } from "@/lib/types"
24

35
import ToCLink from "@/components/TableOfContents/TableOfContentsLink"
46

7+
const variants = cva("mb-2 last:mb-0", {
8+
variants: {
9+
variant: {
10+
default: "",
11+
beginner:
12+
"[&:has([data-state-active='true'])]:text-primary hover:text-primary-hover list-item",
13+
},
14+
},
15+
defaultVariants: {
16+
variant: "default",
17+
},
18+
})
19+
520
export type ItemsListProps = {
621
items: Array<ToCItem>
722
depth: number
823
maxDepth: number
924
activeHash?: string
10-
}
25+
} & VariantProps<typeof variants>
1126

1227
const ItemsList = ({
1328
items,
1429
depth,
1530
maxDepth,
1631
activeHash,
32+
variant,
1733
...rest
1834
}: ItemsListProps) => {
1935
if (depth > maxDepth) return null
@@ -23,8 +39,13 @@ const ItemsList = ({
2339
{items.map((item, index) => {
2440
const { title, items } = item
2541
return (
26-
<li key={index} className="mb-2 last:mb-0" {...rest}>
27-
<ToCLink depth={depth} item={item} activeHash={activeHash} />
42+
<li key={index} className={variants({ variant })} {...rest}>
43+
<ToCLink
44+
variant={variant}
45+
depth={depth}
46+
item={item}
47+
activeHash={activeHash}
48+
/>
2849
{items && (
2950
<ul key={title} className="m-0 mt-2 list-none gap-2 ps-2 text-sm">
3051
<ItemsList

src/components/TableOfContents/TableOfContentsLink.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
1+
import { cva, type VariantProps } from "class-variance-authority"
2+
13
import type { ToCItem } from "@/lib/types"
24

35
import { cn } from "@/lib/utils/cn"
46

57
import { BaseLink } from "../ui/Link"
68

9+
const variants = cva(
10+
"group relative inline-block w-full py-0.5 text-body-medium no-underline lg:w-auto",
11+
{
12+
variants: {
13+
variant: {
14+
default: "",
15+
beginner: "[&_[data-label='marker']]:!hidden inline leading-base",
16+
},
17+
},
18+
defaultVariants: {
19+
variant: "default",
20+
},
21+
}
22+
)
23+
724
export type TableOfContentsLinkProps = {
825
depth: number
926
item: ToCItem
1027
activeHash?: string
11-
}
28+
} & VariantProps<typeof variants>
1229

1330
const Link = ({
1431
depth,
1532
item: { title, url },
1633
activeHash,
34+
variant,
1735
}: TableOfContentsLinkProps) => {
1836
const isActive = activeHash === url
1937

2038
return (
2139
<BaseLink
2240
data-state-active={isActive}
2341
href={url}
24-
className={cn(
25-
"group relative inline-block w-full py-0.5 text-body-medium no-underline lg:w-auto",
26-
isActive && "visited text-primary"
27-
)}
42+
className={variants({
43+
variant,
44+
className: isActive && "visited text-primary",
45+
})}
2846
>
2947
<div
3048
data-label="marker"

src/components/TableOfContents/TableOfContentsMobile.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"use client"
2+
3+
import { cva, type VariantProps } from "class-variance-authority"
14
import { ChevronDown } from "lucide-react"
25

36
import type { ToCItem } from "@/lib/types"
@@ -13,12 +16,25 @@ import ItemsListMobile from "./ItemsListMobile"
1316

1417
import { useTranslation } from "@/hooks/useTranslation"
1518

19+
const variants = cva("flex w-full justify-between lg:hidden", {
20+
variants: {
21+
variant: {
22+
default: "",
23+
beginner:
24+
"[&>span]:flex-none mb-16 justify-center rounded-lg border-border bg-accent-a/10 text-lg font-bold",
25+
},
26+
},
27+
defaultVariants: {
28+
variant: "default",
29+
},
30+
})
31+
1632
export type TableOfContentsMobileProps = {
1733
items?: Array<ToCItem>
1834
maxDepth?: number
19-
}
35+
} & VariantProps<typeof variants>
2036

21-
const Mobile = ({ items, maxDepth }: TableOfContentsMobileProps) => {
37+
const Mobile = ({ items, maxDepth, variant }: TableOfContentsMobileProps) => {
2238
const { t } = useTranslation("common")
2339

2440
if (!items) {
@@ -28,11 +44,7 @@ const Mobile = ({ items, maxDepth }: TableOfContentsMobileProps) => {
2844
return (
2945
<DropdownMenu>
3046
<DropdownMenuTrigger asChild>
31-
<Button
32-
isSecondary
33-
variant="outline"
34-
className="flex w-full justify-between lg:hidden"
35-
>
47+
<Button isSecondary variant="outline" className={variants({ variant })}>
3648
<span className="flex-1 text-center">{t("on-this-page")}</span>
3749
<ChevronDown />
3850
</Button>

src/components/TableOfContents/index.tsx

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use client"
22

3+
import { cva, type VariantProps } from "class-variance-authority"
4+
35
import type { ToCItem } from "@/lib/types"
46

57
import Github from "@/components/icons/github.svg"
@@ -14,7 +16,39 @@ import { useActiveHash } from "@/hooks/useActiveHash"
1416
import { useTranslation } from "@/hooks/useTranslation"
1517
import { usePathname } from "@/i18n/routing"
1618

17-
export type TableOfContentsProps = {
19+
const variants = cva(
20+
"sticky hidden flex-col items-start overflow-y-auto lg:flex",
21+
{
22+
variants: {
23+
variant: {
24+
default:
25+
"top-19 min-w-48 max-w-[25%] p-4 pe-0 h-[calc(100vh-80px)] gap-4",
26+
beginner: cn(
27+
"top-[7.25rem] min-w-80 max-w-72 lg:p-8 px-3 py-2",
28+
"h-fit shrink-0 space-y-2.5 rounded-2xl bg-accent-a/10 text-body-medium",
29+
"[&_ul]:list-decimal [&_ul]:border-s-0 [&_ul]:text-base [&_ul]:list-inside [&_ul]:ps-0"
30+
),
31+
},
32+
},
33+
defaultVariants: {
34+
variant: "default",
35+
},
36+
}
37+
)
38+
39+
const labelVariants = cva("text-body-medium", {
40+
variants: {
41+
variant: {
42+
default: "uppercase",
43+
beginner: "font-bold text-lg",
44+
},
45+
},
46+
defaultVariants: {
47+
variant: "default",
48+
},
49+
})
50+
51+
export interface TableOfContentsProps extends VariantProps<typeof variants> {
1852
items: Array<ToCItem>
1953
maxDepth?: number
2054
editPath?: string
@@ -30,13 +64,12 @@ const TableOfContents = ({
3064
hideEditButton = false,
3165
isMobile = false,
3266
className,
67+
variant,
3368
...rest
3469
}: TableOfContentsProps) => {
3570
const pathname = usePathname()
3671
const { t } = useTranslation("common")
37-
3872
const titleIds: Array<string> = []
39-
4073
if (!isMobile) {
4174
const getTitleIds = (items: Array<ToCItem>, depth: number): void => {
4275
// Return early if maxDepth hit
@@ -56,17 +89,11 @@ const TableOfContents = ({
5689
return null
5790
}
5891
if (isMobile) {
59-
return <Mobile items={items} maxDepth={maxDepth} />
92+
return <Mobile variant={variant} items={items} maxDepth={maxDepth} />
6093
}
6194

6295
return (
63-
<aside
64-
className={cn(
65-
"sticky top-19 hidden h-[calc(100vh-80px)] min-w-48 max-w-[25%] flex-col items-start gap-4 overflow-y-auto p-4 pe-0 lg:flex",
66-
className
67-
)}
68-
{...rest}
69-
>
96+
<aside className={variants({ variant, className })} {...rest}>
7097
{!hideEditButton && editPath && (
7198
<ButtonLink
7299
href={editPath}
@@ -81,15 +108,14 @@ const TableOfContents = ({
81108
{t("edit-page")}
82109
</ButtonLink>
83110
)}
84-
<div data-label="label" className="uppercase text-body-medium">
85-
{t("on-this-page")}
86-
</div>
111+
<div className={labelVariants({ variant })}>{t("on-this-page")}</div>
87112
<ul className="m-0 mb-2 mt-2 list-none gap-2 border-s border-s-body-medium ps-4 pt-0 text-sm">
88113
<ItemsList
89114
items={items}
90115
depth={0}
91116
maxDepth={maxDepth ? maxDepth : 1}
92117
activeHash={activeHash}
118+
variant={variant}
93119
/>
94120
</ul>
95121
</aside>

0 commit comments

Comments
 (0)