Skip to content

Commit 7602b21

Browse files
authored
highlight currently selected (but not expanded) section in var(--prime) (sveltejs#76)
1 parent 6af23ba commit 7602b21

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/routes/tutorial/[slug]/_/Menu/Menu.svelte

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
let is_open = false;
1717
let search = '';
1818
19+
let expanded_part = current.part.slug;
20+
let expanded_chapter = current.chapter.slug;
21+
1922
const duration = browser && matchMedia('(prefers-reduced-motion: reduce)').matches ? 0 : 200;
2023
2124
$: regex = new RegExp(`\\b${search.length >= 2 ? search : ''}`, 'i');
@@ -88,23 +91,35 @@
8891
{#each filtered as part (part.slug)}
8992
<li
9093
class="part"
94+
class:expanded={part.slug === expanded_part}
95+
aria-current={part.slug === current.part.slug ? 'step' : undefined}
9196
transition:slide|local={{ duration }}
92-
class:expanded={part.slug === current.part.slug}
9397
>
94-
<a sveltekit:prefetch href="/tutorial/{part.first}" data-label={part.label}>
98+
<button
99+
on:click={() => {
100+
if (expanded_part !== part.slug) {
101+
expanded_part = part.slug;
102+
expanded_chapter = part.chapters[0].slug;
103+
}
104+
}}
105+
>
95106
Part {part.label}: {part.title}
96-
</a>
107+
</button>
97108

98-
{#if search.length >= 2 || part.slug === current.part.slug}
109+
{#if search.length >= 2 || part.slug === expanded_part}
99110
<ul class="chapter" transition:slide|local={{ duration }}>
100111
{#each part.chapters as chapter (chapter.slug)}
101-
<li class="chapter" class:expanded={chapter.slug === current.chapter.slug}>
112+
<li
113+
class="chapter"
114+
class:expanded={chapter.slug === expanded_chapter}
115+
aria-current={chapter.slug === current.chapter.slug ? 'step' : undefined}
116+
>
102117
<img src={arrow} alt="Arrow icon" />
103-
<a sveltekit:prefetch href="/tutorial/{chapter.first}" data-label={chapter.label}
104-
>{chapter.title}</a
105-
>
118+
<button on:click={() => (expanded_chapter = chapter.slug)}>
119+
{chapter.title}
120+
</button>
106121

107-
{#if search.length >= 2 || chapter.slug === current.chapter.slug}
122+
{#if search.length >= 2 || chapter.slug === expanded_chapter}
108123
<ul transition:slide|local={{ duration }}>
109124
{#each chapter.sections as section (section.slug)}
110125
<li
@@ -267,20 +282,21 @@
267282
position: relative;
268283
}
269284
270-
li[aria-current='page'] a {
285+
li[aria-current='page'] a,
286+
li[aria-current='step']:not(.expanded) > button {
271287
color: var(--prime);
272288
}
273289
274290
li img {
275291
position: absolute;
276292
left: -2rem;
277-
top: 0.3rem;
293+
top: 0.1rem;
278294
width: 2rem;
279295
height: 2rem;
280296
transition: transform 0.2s;
281297
}
282298
283-
li.expanded > a {
299+
li.expanded > button {
284300
font-weight: bold;
285301
}
286302
@@ -298,7 +314,8 @@
298314
--color: var(--second);
299315
}
300316
301-
a {
317+
a,
318+
button {
302319
color: var(--second);
303320
padding: 0 0 0 0.5rem;
304321
margin: 0 0.5rem 0 0;

0 commit comments

Comments
 (0)