Skip to content

Commit

Permalink
SUL23-638 | configure related links; memo-ize headings; add link styles
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahongsf committed Oct 8, 2024
1 parent 9ccb4fb commit 8ac2f59
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const NodePage = async ({params, previewMode}: PageProps) => {
<NodePageDisplay node={entity} />
</div>

{entity.__typename === "NodeStanfordPage" && <OnThePageLink content={entity.suPageComponents} />}
{entity.__typename === "NodeStanfordPage" && <OnThePageLink node={entity} />}
<SecondaryMenu menuItems={menuItems} currentPath={entity.path} />
</div>
)}
Expand Down
92 changes: 56 additions & 36 deletions src/components/patterns/on-the-page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use client"

import React, {useState, useEffect} from "react"
import React, {useState, useEffect, useMemo} from "react"
import parse from "html-react-parser"
import {NodeStanfordPage} from "@/lib/gql/__generated__/drupal.d"
import {twMerge} from "tailwind-merge"

interface Heading {
text: string
Expand All @@ -11,28 +12,31 @@ interface Heading {

const OnThePageLink = ({node}: {node: NodeStanfordPage}) => {
const [activeHeading, setActiveHeading] = useState<string>("")
const headings: Heading[] = []
const relLinkHeading = node.sulRelLinksHeading
const relLinks = node.sulRelLinks

const content =
node.suPageComponents &&
node.suPageComponents
.filter(item => item.__typename === "ParagraphStanfordWysiwyg")
.map(item => item.suWysiwygText.processed)
.join(" ")
const headings = useMemo(() => {
const content =
node?.suPageComponents &&
node?.suPageComponents
.filter(item => item.__typename === "ParagraphStanfordWysiwyg")
.map(item => item.suWysiwygText.processed)
.join(" ")

console.log("CONTENT", content)
const parsedContent = parse(content)
const headingsArr: Heading[] = []

const parsedContent = parse(content)
React.Children.forEach(parsedContent, child => {
if (React.isValidElement(child) && child.type === "h2") {
const text = child.props.children
const id = text.toLowerCase().replace(/\s+/g, "-")
headingsArr.push({text, id})
}
})

React.Children.forEach(parsedContent, child => {
if (React.isValidElement(child) && child.type === "h2") {
const text = child.props.children
const id = text.toLowerCase().replace(/\s+/g, "-")
headings.push({text, id})
}
})
return headingsArr
}, [node])

// Set up Intersection Observer
useEffect(() => {
const options: IntersectionObserverInit = {
root: null,
Expand Down Expand Up @@ -61,24 +65,40 @@ const OnThePageLink = ({node}: {node: NodeStanfordPage}) => {
}, [headings])

return (
<nav>
<h2>Jump to:</h2>
<ul>
{headings.map(heading => (
<li key={heading.id}>
<a
href={`#${heading.id}`}
style={{
fontWeight: activeHeading === heading.id ? "bold" : "normal",
color: activeHeading === heading.id ? "blue" : "black",
}}
>
{heading.text}
</a>
</li>
))}
</ul>
</nav>
<div className="sticky top-0 h-fit bg-fog-light px-24 pb-40 pt-16">
<nav>
<h3 className="font-sans font-semibold">On this page</h3>
<ul className="list-none p-0">
{headings.map(heading => (
<li key={heading.id}>
<a
href={`#${heading.id}`}
className={twMerge(
"type-1 border-l-4 pl-16 font-sans font-normal leading-[36px] text-black no-underline",
activeHeading === heading.id ? "border-cardinal-red" : "border-transparent"
)}
>
{heading.text}
</a>
</li>
))}
</ul>
</nav>
{relLinks && (
<div className="mt-40">
<h3 className="font-sans font-semibold">{relLinkHeading || "Related content"}</h3>
<ul className="list-none p-0">
{relLinks.map((link, index) => (
<li key={index}>
<a href={link.url} className={twMerge("type-1 font-sans font-normal leading-[36px]")}>
{link.title}
</a>
</li>
))}
</ul>
</div>
)}
</div>
)
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/format-html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ const options: HTMLReactParserOptions = {
return <NodeName {...nodeProps}>{domToReact(domNode.children as DOMNode[], options)}</NodeName>

case "h2":
console.log("DATA:", domNode.children[0])
const text = domNode.children[0].data
const id = text.toLowerCase().replace(/\s+/g, "-")
return (
<NodeName {...nodeProps} id={id}>
<NodeName {...nodeProps} id={id} className={twMerge("scroll-smooth", nodeProps.className)}>
{domToReact(domNode.children as DOMNode[], options)}
</NodeName>
)
Expand Down

0 comments on commit 8ac2f59

Please sign in to comment.