From d698f058b51597a419910cb70b72495f8328c817 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 5 Sep 2020 14:34:43 +0300 Subject: Make scrolling less hacky --- web/index.js | 97 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 37 deletions(-) (limited to 'web/index.js') diff --git a/web/index.js b/web/index.js index bc478af..dbd5e5c 100644 --- a/web/index.js +++ b/web/index.js @@ -23,23 +23,10 @@ class App extends Component { loading: true, error: null, } - this.observer = null + this.imageObserver = null this.packListRef = null } - observeIntersection = intersections => { - for (const entry of intersections) { - const img = entry.target.children.item(0) - if (entry.isIntersecting) { - img.setAttribute("src", img.getAttribute("data-src")) - img.classList.add("visible") - } else { - img.removeAttribute("src") - img.classList.remove("visible") - } - } - } - componentDidMount() { fetch(`${PACKS_BASE_URL}/index.json`).then(async indexRes => { if (indexRes.status >= 400) { @@ -61,20 +48,50 @@ class App extends Component { }) } }, error => this.setState({ loading: false, error })) - this.observer = new IntersectionObserver(this.observeIntersection, { + + this.imageObserver = new IntersectionObserver(this.observeImageIntersection, { rootMargin: "100px", - threshold: 0, }) + this.sectionObserver = new IntersectionObserver(this.observeSectionIntersection, {}) + } + + observeImageIntersection = intersections => { + for (const entry of intersections) { + const img = entry.target.children.item(0) + if (entry.isIntersecting) { + img.setAttribute("src", img.getAttribute("data-src")) + img.classList.add("visible") + } else { + img.removeAttribute("src") + img.classList.remove("visible") + } + } + } + + observeSectionIntersection = intersections => { + for (const entry of intersections) { + const packID = entry.target.getAttribute("data-pack-id") + const navElement = document.getElementById(`nav-${packID}`) + if (entry.isIntersecting) { + navElement.classList.add("visible") + } else { + navElement.classList.remove("visible") + } + } } componentDidUpdate() { for (const elem of this.packListRef.getElementsByClassName("sticker")) { - this.observer.observe(elem) + this.imageObserver.observe(elem) + } + for (const elem of this.packListRef.children) { + this.sectionObserver.observe(elem) } } componentWillUnmount() { - this.observer.disconnect() + this.imageObserver.disconnect() + this.sectionObserver.disconnect() } render() { @@ -86,9 +103,9 @@ class App extends Component {

${this.state.error}

` } else if (this.state.packs.length === 0) { - return html`

No packs found :(

` + return html`

No packs found 😿

` } - return html`
+ return html`
@@ -99,24 +116,30 @@ class App extends Component { } } -const NavBarItem = ({ pack }) => html` -
- ${pack.stickers[0].body} -
-
` +const NavBarItem = ({ pack }) => html` + +
+ ${pack.stickers[0].body} +
+
+` -const Pack = ({ pack }) => html`
-

${pack.title}

-
- ${pack.stickers.map(sticker => html` - <${Sticker} key=${sticker["net.maunium.telegram.sticker"].id} content=${sticker}/> - `)} -
-
` +const Pack = ({ pack }) => html` +
+

${pack.title}

+
+ ${pack.stickers.map(sticker => html` + <${Sticker} key=${sticker["net.maunium.telegram.sticker"].id} content=${sticker}/> + `)} +
+
+` -const Sticker = ({ content }) => html`
sendSticker(content)}> - ${content.body} -
` +const Sticker = ({ content }) => html` +
sendSticker(content)}> + ${content.body} +
+` render(html`<${App} />`, document.body) -- cgit v1.2.3