Skip to content

Commit a048a72

Browse files
committed
fix collapse toggle in weapon/decoration tree rows
The [-] toggle marker rendered on tree rows with children had no click handler after the migration off Stimulus, so clicking it did nothing. Add collapse state to WeaponTreeRow so it hides/shows children and flips to [+]/[-], without triggering navigation on rows that are also links.
1 parent f9c8a5f commit a048a72

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

‎src/components/WeaponTreeRow.tsx‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { useState } from "react";
34
import Link from "next/link";
45
import Icon from "./Icon";
56
import { cn } from "@/lib/cn";
@@ -33,6 +34,8 @@ export default function WeaponTreeRow({
3334
basePath,
3435
activeSlug,
3536
}: WeaponTreeRowProps) {
37+
const [collapsed, setCollapsed] = useState(false);
38+
3639
const isExternal = node.type !== sectionType && (WEAPON_TYPES as readonly string[]).includes(node.type);
3740
const href = node.rarity
3841
? isExternal
@@ -41,6 +44,14 @@ export default function WeaponTreeRow({
4144
: undefined;
4245
const isActive = activeSlug === node.slug;
4346

47+
const onToggleClick = (event: { preventDefault(): void; stopPropagation(): void }) => {
48+
// Rows can be links (a parent weapon still has its own page), so stop
49+
// the click from also triggering navigation.
50+
event.preventDefault();
51+
event.stopPropagation();
52+
setCollapsed((value) => !value);
53+
};
54+
4455
const onPointerEnter = href
4556
? () => {
4657
const file = bundleForHref(node, isExternal ? `/blacksmith/${node.type}/` : basePath);
@@ -66,7 +77,20 @@ export default function WeaponTreeRow({
6677
/>
6778
)}
6879
<p className={styles.name}>{node.name}</p>
69-
{node.children && <p className={styles.toggle}>[-]</p>}
80+
{node.children && (
81+
<p
82+
className={styles.toggle}
83+
role="button"
84+
tabIndex={0}
85+
aria-label={collapsed ? "Expand" : "Collapse"}
86+
onClick={onToggleClick}
87+
onKeyDown={(event) => {
88+
if (event.key === "Enter" || event.key === " ") onToggleClick(event);
89+
}}
90+
>
91+
{collapsed ? "[+]" : "[-]"}
92+
</p>
93+
)}
7094
</>
7195
);
7296

@@ -88,7 +112,7 @@ export default function WeaponTreeRow({
88112
</span>
89113
)}
90114

91-
{node.children && (
115+
{node.children && !collapsed && (
92116
<ul className={node.children.length > 1 ? styles["multiple-children"] : undefined}>
93117
{node.children.map((child) => (
94118
<WeaponTreeRow

0 commit comments

Comments
 (0)