-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #974
- Loading branch information
Erik van Velzen
committed
Jun 12, 2024
1 parent
a62564d
commit d53feeb
Showing
8 changed files
with
200 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
.Step:hover .StepNumber { | ||
color: white; | ||
@apply bg-holon-blue-500; | ||
} | ||
|
||
.active { | ||
@apply bg-holon-slated-blue-900; | ||
color: white; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {FunctionComponent, useEffect, useState} from "react"; | ||
import styles from "./Step.module.css"; | ||
|
||
export type StepProps = { | ||
id: string, | ||
num: number, | ||
title: string, | ||
} | ||
|
||
export const Step: FunctionComponent<StepProps> = ({id, num, title}) => { | ||
const [active, setActive] = useState(false); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
const target = document.getElementById(id) | ||
if (!target) { | ||
console.error(`Target with id ${id} not in DOM`) | ||
return | ||
} | ||
const rect = target.parentElement.getBoundingClientRect() | ||
const visibleTop = Math.max(0, rect.top); | ||
const visibleBottom = Math.min(window.visualViewport.height, rect.bottom); | ||
const visibleHeight = Math.max(0, visibleBottom - visibleTop); | ||
|
||
if (visibleHeight > (window.visualViewport.height / 2)) { | ||
setActive(true) | ||
} else { | ||
setActive(false) | ||
} | ||
} | ||
window.addEventListener('scroll', handleScroll); | ||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<a href={`#${id}`} style={{ | ||
textAlign: "center", | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "center", | ||
}} className={styles.Step}> | ||
<div | ||
className={`${styles.StepNumber} ${active && styles.active} bg-white border-holon-blue-900 border-2 rounded`} | ||
style={{ | ||
padding: "0.2rem 2rem", | ||
fontSize: "1.2rem", | ||
fontWeight: "bold", | ||
}}> | ||
{num} | ||
</div> | ||
<div>{title}</div> | ||
</a> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
.Steps { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
gap: 1rem; | ||
|
||
min-width: 4rem; | ||
position: sticky; | ||
|
||
top: 5.75rem; | ||
height: calc(100vh - 5.75rem); | ||
|
||
padding: 1rem; | ||
z-index: 20; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {FunctionComponent, useEffect, useState} from "react"; | ||
import {Storyline} from "@/containers/StorylinePage/StorylinePage"; | ||
import {Step} from "@/components/Storyline/Steps/Step"; | ||
import styles from "./Steps.module.css"; | ||
import {SectionVariant} from "@/containers/types"; | ||
|
||
type StepData = { | ||
id: string, | ||
title: string, | ||
index: number, | ||
} | ||
|
||
export const Steps: FunctionComponent<{storyline: Storyline[], className?: string}> = ({storyline, className = ""}) => { | ||
// useState + useEffect is to force client side rendering | ||
// because it uses DOMParser to grep for a title | ||
// which is not available in Node.js. | ||
const [steps, setSteps] = useState<StepData[]>([]) | ||
useEffect(() => { | ||
setSteps(stepsFromStoryLine(storyline)) | ||
}, [storyline]) | ||
|
||
return ( | ||
<div className={`${styles.Steps} ${className}`}> | ||
{steps.map((step) => { | ||
return ( | ||
<Step | ||
key={step.id} | ||
id={step.id} | ||
num={step.index + 1} | ||
title={step.title} | ||
/> | ||
); | ||
})} | ||
</div> | ||
); | ||
} | ||
|
||
const stepsFromStoryLine = (storyline: Storyline[]) => { | ||
return storyline | ||
.filter((section) => section.type === "section") | ||
.map((section, index) => { | ||
const title = searchTitle(section) || `Stap ${index + 1}` | ||
|
||
return { | ||
id: section.id, | ||
title: title, | ||
index: index, | ||
} | ||
}) | ||
} | ||
|
||
const searchTitle = (section: SectionVariant): string|null|undefined => { | ||
const contentItems = section.value.content | ||
const textItem = contentItems.find((contentItem) => contentItem.type === "text") | ||
|
||
if (!textItem) { | ||
return null | ||
} | ||
|
||
if (typeof DOMParser === "undefined") { | ||
return null | ||
} | ||
const parser = new DOMParser(); | ||
const htmlDoc = parser.parseFromString(textItem.value, "text/html"); | ||
|
||
return htmlDoc.querySelector("h1, h2, h3, h4, h5, h6")?.textContent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
.StorylinePage { | ||
display: flex; | ||
} | ||
|
||
.Steps { | ||
max-width: 16rem; | ||
@media (width <= 1250px) { | ||
display: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters