flackr / scroll-timeline

A polyfill of ScrollTimeline.
Apache License 2.0
887 stars 82 forks source link

Issue Integrating scroll-timeline with Next.js #240

Open dawidk92 opened 4 months ago

dawidk92 commented 4 months ago

Environment

Description

I'm encountering an issue integrating the scroll-timeline polyfill into my Next.js project, aiming to enable scroll-linked animations using CSS Scroll Timeline. Despite correctly importing the script in my pages/_app.tsx file, the polyfill does not activate, and animations linked to scroll or view timelines do not trigger as expected.

Steps to Reproduce

  1. In the pages/_app.tsx file of a Next.js project, import Script from next/script.
  2. Include the scroll-timeline polyfill using the Script tag as follows:
    <Script src="https://flackr.github.io/scroll-timeline/dist/scroll-timeline.js" />
  3. In the CSS, use the properties animation-timeline: scroll(); and animation-timeline: view(); to define scroll-linked animations.

Expected Result:

Actual Result:

Attempted Solutions

Any assistance or insights into resolving this integration issue would be greatly appreciated. Thank you!

bramus commented 4 months ago

Where do you define your styles? The polyfill – currently - only works with rules present in inlined style blocks.

Don’t work:

The latter is tracked in https://github.com/flackr/scroll-timeline/issues/78

dawidk92 commented 4 months ago

I'm using Next.js with styled-components for styling.

Styles are dynamically generated and injected into the <style> tag within the <head> tag of the document. This setup should, in theory, align with the requirement for inlined style blocks as the styles are not applied via the style attribute directly on elements nor through external style sheets linked via <link rel="stylesheet" href="...">.

Here you can find an example of this kind of setup: https://github.com/vercel/next.js/tree/canary/examples/with-styled-components

gaurangrshah commented 4 months ago

I'm having the same issue can confirm the polyfill has no effect in a brand new next.js project

      <Script async src="https://flackr.github.io/scroll-timeline/dist/scroll-timeline.js" />

Put that into my layout component, and it seems to have no effect.

I've successfully used this previously in an Astro project, with no issues. Would really love to get this working in next.js!!

bramus commented 2 months ago

The polyfill can’t be loaded using async as it needs to catch the animations starting.

flackr commented 2 months ago

I put some ideas on https://github.com/flackr/scroll-timeline/issues/260 for how we could handle loading the polyfill asynchronously and still detect existing or already finished css animations.