Feat: Add Background to Current Section Title

Added functionality to allow the background color of the link
that links to the current section to be highlighted.
This commit is contained in:
Stevan Freeborn
2023-04-28 08:33:11 -05:00
parent 43b4f774b2
commit ac386b5717
2 changed files with 32 additions and 10 deletions
+12 -5
View File
@@ -3,7 +3,7 @@
flex-direction: column; flex-direction: column;
height: 100%; height: 100%;
border-right: 1px solid #848586; border-right: 1px solid #848586;
padding: 0 1rem; padding: 0 1rem 0 0.5rem;
overflow: auto; overflow: auto;
background-color: inherit; background-color: inherit;
width: 300px; width: 300px;
@@ -64,19 +64,26 @@
width: 100%; width: 100%;
} }
.linkGroup {
padding: 0 0.5rem;
}
.link, .link,
.activeLink { .activeLink {
padding: 0 1rem 0 0.5rem;
text-decoration: none; text-decoration: none;
} }
.activeLink {
background-color: #8485866b;
border-top-right-radius: 999px;
border-bottom-right-radius: 999px;
}
.link:hover { .link:hover {
color: #64adac; color: #64adac;
} }
.activeLink {
background-color: #1e1e1e;
}
.expandable { .expandable {
width: 100%; width: 100%;
display: flex; display: flex;
+20 -5
View File
@@ -2,6 +2,7 @@
import Link from 'next/link.js'; import Link from 'next/link.js';
import { useState } from 'react'; import { useState } from 'react';
import { useSectionContext } from '../context/section';
import { useThemeContext } from '../context/theme'; import { useThemeContext } from '../context/theme';
import { Doc, DocsStructure } from '../types/types'; import { Doc, DocsStructure } from '../types/types';
import { toKebabCase } from '../utils/stringUtils'; import { toKebabCase } from '../utils/stringUtils';
@@ -11,6 +12,16 @@ function TreeNode({ doc }: { doc: Doc }) {
const [isExpanded, setIsExpanded] = const [isExpanded, setIsExpanded] =
useState<boolean>(true); useState<boolean>(true);
const { section, setSection } = useSectionContext();
const docTitleId = toKebabCase(doc.title);
const isActiveSection = docTitleId === section;
const linkStyle = isActiveSection
? styles.activeLink
: styles.link;
const hasChildren = const hasChildren =
doc.children && doc.children?.length > 0; doc.children && doc.children?.length > 0;
@@ -20,13 +31,16 @@ function TreeNode({ doc }: { doc: Doc }) {
<div className={styles.expandable}> <div className={styles.expandable}>
{doc.copy ? ( {doc.copy ? (
<a <a
href={`#${toKebabCase(doc.title)}`} onClick={() => setSection(docTitleId)}
className={styles.link} href={`#${docTitleId}`}
className={linkStyle}
> >
{doc.title} {doc.title}
</a> </a>
) : ( ) : (
<div>{doc.title}</div> <div className={styles.linkGroup}>
{doc.title}
</div>
)} )}
<div <div
className={styles.chevronContainer} className={styles.chevronContainer}
@@ -65,8 +79,9 @@ function TreeNode({ doc }: { doc: Doc }) {
</div> </div>
) : doc.copy ? ( ) : doc.copy ? (
<a <a
href={`#${toKebabCase(doc.title)}`} onClick={() => setSection(docTitleId)}
className={styles.link} href={`#${docTitleId}`}
className={linkStyle}
> >
{doc.title} {doc.title}
</a> </a>