import { FileType, GeneratedFolder, ProjectUIDL } from '@teleporthq/teleport-types' import { ScrollSceneRuntime } from '@teleporthq/teleport-shared' import uidlSample from '../../../../examples/uidl-samples/tests.json' import { createHTMLProjectGenerator, pluginCloneGlobals, pluginHomeReplace, ProjectPluginCloneGlobals, } from '../../src' import { pluginMotionRuntime } from '../../src/plugin-motion-runtime' import HTMLTemplate from '../../src/project-template' import { SCENE_BACKDROP_DECLARATIONS, SCENE_BACKDROP_MEDIA_DECLARATIONS, SCENE_BACKDROP_MEDIA_SELECTOR, SCENE_BACKDROP_SELECTOR, SCENE_CHAPTERS_DECLARATIONS, SCENE_CHAPTERS_DEFAULT_DECLARATIONS, SCENE_CHAPTERS_SELECTOR, SCENE_COUNT_CSS, SCENE_HIDDEN_CSS, sceneCss, } from '../../src/motion/runtime-css' import { normalizeSceneLength } from '../../src/motion/contract' import { generateScrollSceneComponentCode } from '../../../teleport-project-generator-next/src/widgets/scroll-scene-component' const s = (content: unknown) => ({ type: 'static', content }) const LANES = '[{"prop":"opacity","at":[0,0.3],"values":[0,1]},{"prop":"x","at":[0,1],"values":[0,-70],"unit":"%"}]' const chapter = (id: string, bind: string) => ({ type: 'element', content: { elementType: 'container', attrs: { id: s(id), 'data-scroll-bind': s(bind) }, children: [s(id)], }, }) const SCENE = { type: 'element', content: { elementType: 'scroll-scene-node', name: 'story', attrs: { id: s('story'), sceneLength: s('400vh'), pin: s(true), scrub: s(0.4), reducedMotion: s('static'), exposeProgress: s(true), chapterSnap: s('gentle'), layout: s('chapters'), }, style: { width: s('100%') }, children: [chapter('chapter-one', LANES), chapter('chapter-two', 'depth-2')], }, } const UNPINNED_SCENE = { type: 'element', content: { elementType: 'scroll-scene-node', name: 'drift', attrs: { id: s('drift'), pin: s(false) }, children: [chapter('drift-item', 'depth-1')], }, } const MOTION = { type: 'element', content: { elementType: 'motion-node', name: 'reveal', attrs: { id: s('reveal'), preset: s('slide-up'), trigger: s('in-view'), inViewOnce: s(false), inViewAmount: s(0.4), repeatType: s('mirror'), scrollOffset: s('enter'), from: s({ opacity: 0, y: 80 }), to: s({ opacity: 1, y: 0 }), }, children: [s('Revealed')], }, } const video = (attrs: Record) => ({ type: 'element', content: { elementType: 'scroll-video-node', name: 'clip', attrs, children: [] as unknown[] }, }) const projectWith = (...nodes: unknown[]): ProjectUIDL => { const uidl = JSON.parse(JSON.stringify(uidlSample)) uidl.root.node.content.children[0].content.node.content.children.push(...nodes) return uidl as ProjectUIDL } const generate = async (uidl: ProjectUIDL): Promise => { const generator = createHTMLProjectGenerator() generator.addPlugin(pluginHomeReplace) generator.addPlugin(pluginCloneGlobals) generator.addPlugin(pluginMotionRuntime) return generator.generateProject(uidl, HTMLTemplate) } // The editor's "strict white spacing for HTML" setting: every page is formatted // with Prettier's strict whitespace. Behind a head that ends with an inline // script (custom head code), the head's closing tag is written ``. const generateStrict = async (uidl: ProjectUIDL): Promise => { uidl.globals.customCode = { head: '' } const generator = createHTMLProjectGenerator() generator.addPlugin(pluginHomeReplace) generator.addPlugin(new ProjectPluginCloneGlobals({ strictHtmlWhitespaceSensitivity: true })) generator.addPlugin(pluginMotionRuntime) return generator.generateProject(uidl, HTMLTemplate, {}, true) } const fileOf = (folder: GeneratedFolder, name: string, fileType: string) => folder.files.find((file) => file.name === name && file.fileType === fileType)?.content ?? '' const pagesOf = (folder: GeneratedFolder) => folder.files.filter((file) => file.fileType === FileType.HTML) describe('Motion in the static HTML export', () => { it('adds nothing to a project that has no motion', async () => { const folder = await generate(projectWith()) expect(folder.files.some((file) => file.name === 'tq-motion')).toBe(false) folder.files.forEach((file) => { expect(file.content).not.toContain('data-tq-motion-on') expect(file.content).not.toContain('data-scene-stage') }) }) it('a pinned scene becomes a track holding a sticky stage, with its chapters inside', async () => { const folder = await generate(projectWith(SCENE)) const page = fileOf(folder, 'index', FileType.HTML) expect(page).not.toContain('scroll-scene-node') expect(page).toMatch( /]*data-scene-track="true"[^>]*>\s*]*data-scene-stage="true"/ ) const track = /]*data-scene-track="true"[^>]*)>/.exec(page)?.[1] ?? '' expect(track).toContain('id="story"') expect(track).toContain('data-scene-length="400vh"') expect(track).toContain('data-scene-pin="true"') expect(track).toContain('data-scene-scrub="0.4"') expect(track).toContain('data-scene-reduced-motion="static"') expect(track).toContain('data-scene-expose-progress="true"') expect(track).toContain('data-scene-chapter-snap="gentle"') // the authored class still styles the track, and no camelCase prop survives expect(track).toContain('class="') expect(track).not.toMatch(/scenelength|chaptersnap|reducedmotion|layout=/i) expect(page).toContain('data-scene-layout="chapters"') const stage = page.slice(page.indexOf('data-scene-stage')) expect(stage.indexOf('chapter-one')).toBeGreaterThan(-1) expect(stage.indexOf('chapter-two')).toBeGreaterThan(stage.indexOf('chapter-one')) // the lanes reach the page as JSON the runtime can parse const bind = /data-scroll-bind='([^']*)'/.exec(page)?.[1] ?? '' expect(JSON.parse(bind)).toEqual(JSON.parse(LANES)) }) it('a scene that does not pin keeps its children as they are: no stage, no length rule', async () => { const folder = await generate(projectWith(UNPINNED_SCENE)) const page = fileOf(folder, 'index', FileType.HTML) const css = fileOf(folder, 'style', FileType.CSS) expect(page).toContain('data-scene-pin="false"') expect(page).not.toContain('data-scene-stage=') expect(css).not.toContain('min-height: 300vh') }) it('lays the scene out in the stylesheet, only on a page that said it will be animated', async () => { const folder = await generate(projectWith(SCENE)) const css = fileOf(folder, 'style', FileType.CSS) expect(css).toContain( ':where(html[data-tq-motion-on]) [data-scene-track][data-scene-pin="true"][data-scene-length="400vh"] {\n min-height: 400vh !important;' ) expect(css).toContain('position: sticky !important;') expect(css).toContain(SCENE_HIDDEN_CSS) expect(css).toContain(SCENE_COUNT_CSS) // every structural rule waits for the flag, so a page without JavaScript reads as plain content css .split('}') .filter((rule) => /data-scene-(stage|track)/.test(rule)) .forEach((rule) => expect(rule).toContain(':where(html[data-tq-motion-on])')) }) it('a motion element becomes a div that carries its whole configuration', async () => { const folder = await generate(projectWith(MOTION)) const page = fileOf(folder, 'index', FileType.HTML) const tag = /]*data-tq-motion="true"[^>]*)>/.exec(page)?.[1] ?? '' expect(page).not.toContain('motion-node') expect(tag).toContain('id="reveal"') expect(tag).toContain('data-motion-preset="slide-up"') expect(tag).toContain('data-motion-trigger="in-view"') expect(tag).toContain('data-motion-in-view-once="false"') expect(tag).toContain('data-motion-in-view-amount="0.4"') expect(tag).toContain('data-motion-repeat-type="mirror"') expect(tag).toContain('data-motion-scroll-offset="enter"') expect(JSON.parse(/data-motion-from='([^']*)'/.exec(tag)?.[1] ?? '')).toEqual({ opacity: 0, y: 80, }) expect(JSON.parse(/data-motion-to='([^']*)'/.exec(tag)?.[1] ?? '')).toEqual({ opacity: 1, y: 0, }) const css = fileOf(folder, 'style', FileType.CSS) expect(css).toContain(':not([data-motion-ready]) {\n visibility: hidden;') expect(css).toContain('@media (prefers-reduced-motion: reduce)') }) it('a scroll video becomes a host holding its clip; with a phone variant the runtime picks the source', async () => { const single = fileOf( await generate(projectWith(video({ src: s('/clip.mp4'), poster: s('/poster.jpg') }))), 'index', FileType.HTML ) expect(single).toMatch(/]*data-scroll-video="true"[^>]*>\s*