Skip to content

Commit

Permalink
Added better footnoteref animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Heilemann committed Jan 10, 2020
1 parent df17a2c commit f80cb34
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 75 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- [x] add custom slug to all pages
- [x] test `postcss-easing-gradients` with Scss loaded in `/src`
- [ ] add md table of contents
- [ ] new start page: https://labs.semplice.com/s5-vertical-grid
- [x] new start page: https://labs.semplice.com/s5-vertical-grid
- [x] test purgecss with classes from Markdown files
- [ ] add NOW deployment
- [ ] Setup Nameserver
Expand Down
11 changes: 11 additions & 0 deletions content/projects/__demo__/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ ModuleNotFoundError: No module named 'flask'
> cleaner yet i see a bird i stare at it i meow at it i do a <b>wiggle come
> here, eat a plant, kill</b>
<div class="container container--small">
<blockquote>
<p>
Sleep in the bathroom sink allways wanting food. Rub whiskers on bare skin act innocent intently sniff hand intrigued by the shower.
</p>
<footer>
Marvin Heilemann
</footer>
</blockquote>
</div>

## Images

![Caption could be here](img-01.jpg)
Expand Down
64 changes: 49 additions & 15 deletions src/layouts/Article.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'

import { scrollTo } from '../utils/animate'
import { updateLocationHash, getDocumentHeight } from '../utils/helper'
import { prefersReducedMotion } from '../utils/accessibility'
import Lightbox from '../scripts/lightbox'

Expand All @@ -10,30 +11,64 @@ class Article extends React.Component {
componentDidMount() {
Lightbox.init()

this.scrollElements = document.querySelectorAll(
'.footnote-backref, .footnote-ref, .toc a'
)
this.scrollElements.forEach(ref => {
ref.addEventListener('click', this.handleClick)
})
// TODO: put article blocks into separate components
if (!prefersReducedMotion()) {
this.scrollElements = document.querySelectorAll(
'.footnote-backref, .footnote-ref, .toc a'
)
this.scrollElements.forEach((ref) => {
ref.addEventListener('click', this.handleClick)
})
}
}

componentWillUnmount() {
Lightbox.destroy()

this.scrollElements.forEach(ref => {
ref.removeEventListener('click', this.handleClick)
})
if (this.scrollElements.length > 0) {
this.scrollElements.forEach((ref) => {
ref.removeEventListener('click', this.handleClick)
})
}
}

handleClick(event) {
if (prefersReducedMotion()) {
return
}
handleClick = (event) => {
event.preventDefault()
const target = document.getElementById(event.target.hash.substr(1))
if (target) {
scrollTo(target)
const offsetPosition = scrollTo(target)

// highlight target element when reached
function onScrollEvent() {
if (
window.scrollY === offsetPosition || // reached element
window.scrollY === getDocumentHeight() - window.innerHeight || // reached document end
window.scrollY === 0 // reached document start
) {
removeListeners()
updateLocationHash(target.id)

target.classList.add('highlight')
setTimeout(() => {
target.classList.remove('highlight')
}, 2400)
}
}

// remove event listeners when user interrupts `scrollTo` function
function detectScrollInterrupt() {
removeListeners()
}

function removeListeners() {
window.removeEventListener('scroll', onScrollEvent)
window.removeEventListener('wheel', detectScrollInterrupt)
window.removeEventListener('keyup', detectScrollInterrupt)
}

window.addEventListener('scroll', onScrollEvent)
window.addEventListener('wheel', detectScrollInterrupt)
window.addEventListener('keyup', detectScrollInterrupt)
}
}

Expand All @@ -48,7 +83,6 @@ class Article extends React.Component {
<>
<article
id="article"
// className="container container--small"
dangerouslySetInnerHTML={{
__html: this.props.html || fallbackContent,
}}
Expand Down
28 changes: 28 additions & 0 deletions src/styles/_animations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Pulsation
@keyframes pulse {
0% {
transform: scale(0.3);
}
80%,
100% {
opacity: 0;
}
}

// // Pop-out effect
// @keyframes seeMe {
// 0% {
// transform: scale(1);
// }
// 70% {
// transform: scale(2);
// }
// 100% {
// transform: scale(1);
// }
// }

// .seeMe {
// // transition: transform 300ms ease-in-out;
// animation: seeMe 600ms cubic-bezier(0, 0.63, 0.49, 1.7);
// }
39 changes: 38 additions & 1 deletion src/styles/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ article {
form,
figure,
table,
blockquote,
fieldset,
p,
ul,
ol,
Expand Down Expand Up @@ -126,4 +126,41 @@ article {
padding: var(--spacing-md);
}
}

blockquote {
position: relative;
margin: 0 0 var(--content-margin) 0;
padding: var(--spacing-xxl) var(--spacing-xxl)
calc(var(--spacing-xxl) - 5px);
background: var(--color-10);
font-family: var(--font-serif);
z-index: 0;
text-align: center;

&::before {
content: '"';
position: absolute;
top: 0px;
left: 20px;
font-size: 200px;
font-weight: var(--font-black);
font-family: var(--font-serif);
line-height: 1;
z-index: -1;
opacity: 0.1;
}

p {
margin-bottom: 0;
}

footer {
margin-top: var(--spacing-lg);
text-transform: uppercase;
letter-spacing: 1px;
font-size: var(--text-sm);
font-weight: var(--font-semi-bold);
font-family: var(--font-sans);
}
}
}
2 changes: 1 addition & 1 deletion src/styles/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@import 'mixins';
@import 'reset';
@import 'base';
@import 'animations';

// ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
// Themes
Expand Down Expand Up @@ -36,7 +37,6 @@
// ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
// Components

@import 'components/animation';
@import 'components/backdrop';
@import 'components/button';
@import 'components/code';
Expand Down
28 changes: 0 additions & 28 deletions src/styles/components/_animation.scss

This file was deleted.

11 changes: 11 additions & 0 deletions src/styles/components/_footnote.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ article {
}

.footnotes {
@extend .container;
@extend .container--small;

a {
text-decoration: none;
}

li {
transition: background 240ms linear;
}

.highlight {
background: var(--color-10);
}
}
15 changes: 1 addition & 14 deletions src/styles/components/_list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ article {
justify-content: center;
opacity: 0.6;
}

// &:nth-child(1n)::before {
// content: '♡';
// }
// &:nth-child(2n)::before {
// content: '♢';
// }
// &:nth-child(3n)::before {
// content: '♤';
// }
// &:nth-child(4n)::before {
// content: '♧';
// }
}
}

Expand All @@ -66,7 +53,7 @@ article {
&::before {
content: counter(counter) '.';
left: 0;
top: 8px;
top: 7px;
width: 13px;
height: 13px;
font-size: var(--text-sm);
Expand Down
24 changes: 9 additions & 15 deletions src/utils/animate.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import { updateLocationHash } from './helper'

/**
* Scroll to a HTML element by target.
*
* @param {HTMLElement} target HTML element to scroll to
*
* @returns {number} Y position of target element
*/
export function scrollTo(target) {
const offset = 50

target.classList.remove('seeMe')
const scrollY = window.scrollY || window.pageYOffset
const headerHeight = document.getElementById('header').offsetHeight
const elementPosition = target.getBoundingClientRect().top + scrollY
const offsetPosition = Math.round(elementPosition - headerHeight - offset)

window.addEventListener('scroll', function onScrollEvent() {
if (
window.scrollY === offsetPosition || // reached element
window.scrollY === document.body.scrollHeight - window.innerHeight || // reached document end
window.scrollY === 0 // reached document start
) {
window.removeEventListener('scroll', onScrollEvent)
target.classList.add('seeMe')
updateLocationHash(target.id)
}
})

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

return offsetPosition
}
18 changes: 18 additions & 0 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ export function getRandomDate(
return dayjs(+start + Math.random() * (end - start))
}

/**
* Get documents real height.
*/
export function getDocumentHeight() {
const body = document.body
const html = document.documentElement

let height = Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
)

return height
}

/**
* Returns a random number between min (inclusive) and max (exclusive)
*
Expand Down

0 comments on commit f80cb34

Please sign in to comment.