Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sundry fixes #687

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions app/components/Article/Contents.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {useRef, useEffect} from 'react'
import useIsMobile from '~/hooks/isMobile'
import {questionUrl} from '~/routesMapper'
import type {Glossary, PageId, GlossaryEntry} from '~/server-utils/stampy'

Expand All @@ -18,7 +19,14 @@ const footnoteHTML = (el: HTMLDivElement, e: HTMLAnchorElement): string | null =
return elem.innerHTML
}

const addPopup = (e: HTMLElement, id: string, contents: string): HTMLElement => {
const scrollToElement = (e: HTMLElement, offset?: number) => {
const elementPosition = e.getBoundingClientRect().top + window.pageYOffset
const offsetPosition = elementPosition - (offset || 0)

window.scrollTo({top: offsetPosition, behavior: 'smooth'})
}

const addPopup = (e: HTMLElement, id: string, contents: string, mobile?: boolean): HTMLElement => {
const preexisting = document.getElementById(id)
if (preexisting) return preexisting

Expand All @@ -29,10 +37,21 @@ const addPopup = (e: HTMLElement, id: string, contents: string): HTMLElement =>

e.insertAdjacentElement('afterend', popup)

e.addEventListener('mouseover', () => popup.classList.add('shown'))
e.addEventListener('mouseout', () => popup.classList.remove('shown'))
popup.addEventListener('mouseover', () => popup.classList.add('shown'))
popup.addEventListener('mouseout', () => popup.classList.remove('shown'))
if (!mobile) {
e.addEventListener('mouseover', () => popup.classList.add('shown'))
e.addEventListener('mouseout', () => popup.classList.remove('shown'))
popup.addEventListener('mouseover', () => popup.classList.add('shown'))
popup.addEventListener('mouseout', () => popup.classList.remove('shown'))
} else {
const togglePopup = (event: Event) => {
event.preventDefault()
popup.classList.toggle('shown')
document.body.classList.toggle('noscroll')
scrollToElement(e, 16)
}
popup.addEventListener('click', togglePopup)
e.addEventListener('click', togglePopup)
}

return popup
}
Expand Down Expand Up @@ -139,6 +158,7 @@ const insertGlossary = (pageid: string, glossary: Glossary) => {

const Contents = ({pageid, html, glossary}: {pageid: PageId; html: string; glossary: Glossary}) => {
const elementRef = useRef<HTMLDivElement>(null)
const mobile = useIsMobile()

useEffect(() => {
const el = elementRef.current
Expand All @@ -161,14 +181,16 @@ const Contents = ({pageid, html, glossary}: {pageid: PageId; html: string; gloss
el.querySelectorAll('.footnote-ref > a').forEach((e) => {
const footnote = footnoteHTML(el, e as HTMLAnchorElement)
const footnoteId = (e.getAttribute('href') || '').replace('#', '')
if (footnote)
if (footnote) {
addPopup(
e as HTMLAnchorElement,
`footnote-${footnoteId}`,
`<div class="footnote">${footnote}</div>`
`<div class="footnote">${footnote}</div>`,
mobile
)
}
})
}, [html, glossary, pageid])
}, [html, glossary, pageid, mobile])

return (
<div
Expand Down
42 changes: 30 additions & 12 deletions app/components/Article/article.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,9 @@ article .glossary-entry {
article .link-popup {
visibility: hidden;
opacity: 0;
position: absolute;
max-width: 400px;
display: inline-block;
font: var(--baseFont);
font-style: normal;
font-weight: 300;
line-height: 170%; /* 30.6px */
z-index: 2;
left: calc(50% - 200px);
transform: translateY(var(--spacing-40));
transition:
visibility 0s 300ms,
opacity cubic-bezier(1, 0, 1, 1) 300ms;
}

article .link-popup .footnote {
padding: var(--spacing-24) var(--spacing-24) 0 var(--spacing-24);
}
Expand Down Expand Up @@ -187,6 +176,20 @@ article .banner h3 .title {
padding-left: 10px;
}

@media (min-width: 780px) {
article .link-popup {
position: absolute;
max-width: 400px;
display: inline-block;
z-index: 2;
left: calc(50% - 200px);
transform: translateY(var(--spacing-40));
transition:
visibility 0s 300ms,
opacity cubic-bezier(1, 0, 1, 1) 300ms;
}
}

@media only screen and (max-width: 780px) {
article {
max-width: 100%;
Expand All @@ -202,4 +205,19 @@ article .banner h3 .title {
article {
margin: 0;
}

article .link-popup {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: #ff000033;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable!

overflow: scroll;
}
article .link-popup .footnote {
margin: auto;
background-color: white;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose these specially for you. i.e. so that you'd get annoyed with it and change them :D

width: 80%;
}
}
2 changes: 1 addition & 1 deletion app/components/Chatbot/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const SplashScreen = ({
<IconStampyLarge />
<div className="col-6 padding-bottom-40 padding-top-40">
<h2 className="teal-500">Hi there, I'm Stampy.</h2>
<h2>I can answer your questions about AI Safety</h2>
<h2>I can answer your questions about AI Safety.</h2>
</div>
<Followups
title="Not sure where to start? Try these:"
Expand Down
4 changes: 4 additions & 0 deletions app/root.css
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ svg {
transition-delay: 0s;
}

.noscroll {
overflow: hidden;
}

/* for troubleshooting */

.pink {
Expand Down
Loading