generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add scroll option support for React SPA adapter (#911)
- Loading branch information
Showing
18 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { testScroll } from 'e2e-shared/specs/scroll.cy' | ||
|
||
testScroll({ | ||
path: '/app/scroll', | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testScroll({ | ||
path: '/pages/scroll', | ||
nextJsRouter: 'pages' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Scroll } from 'e2e-shared/specs/scroll' | ||
import { Suspense } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<Scroll /> | ||
</Suspense> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Scroll } from 'e2e-shared/specs/scroll' | ||
|
||
export default Scroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testScroll } from 'e2e-shared/specs/scroll.cy' | ||
|
||
testScroll({ path: '/scroll' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Scroll } from 'e2e-shared/specs/scroll' | ||
|
||
export default Scroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Scroll } from 'e2e-shared/specs/scroll' | ||
|
||
export default Scroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testScroll } from 'e2e-shared/specs/scroll.cy' | ||
|
||
testScroll({ path: '/scroll' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testScroll } from 'e2e-shared/specs/scroll.cy' | ||
|
||
testScroll({ path: '/scroll' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Scroll } from 'e2e-shared/specs/scroll' | ||
|
||
export default Scroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Scroll } from 'e2e-shared/specs/scroll' | ||
|
||
export default Scroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testScroll } from 'e2e-shared/specs/scroll.cy' | ||
|
||
testScroll({ path: '/scroll' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createTest } from '../create-test' | ||
|
||
export const testScroll = createTest('scroll', ({ path }) => { | ||
it('does not scroll to the top of the page by default (scroll: false)', () => { | ||
cy.visit(path + '?scroll=false') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#not-at-the-top').should('be.visible') | ||
cy.get('button').click() | ||
cy.get('#not-at-the-top').should('be.visible') | ||
}) | ||
it('scrolls to the top of the page when setting scroll: true', () => { | ||
cy.visit(path + '?scroll=true') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#not-at-the-top').should('be.visible') | ||
cy.get('button').click() | ||
cy.get('#at-the-top').should('be.visible') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use client' | ||
|
||
import { parseAsBoolean, useQueryState } from 'nuqs' | ||
import { useEffect, useState } from 'react' | ||
|
||
export function Scroll() { | ||
return ( | ||
<> | ||
<ScrollDetector /> | ||
<div style={{ height: '200vh' }} /> | ||
<ScrollAction /> | ||
</> | ||
) | ||
} | ||
|
||
function ScrollDetector() { | ||
const [atTheTop, setAtTheTop] = useState(false) | ||
|
||
useEffect(() => { | ||
const controller = new AbortController() | ||
window.addEventListener('scroll', () => setAtTheTop(window.scrollY === 0), { | ||
signal: controller.signal | ||
}) | ||
return () => controller.abort() | ||
}, []) | ||
|
||
return ( | ||
<span | ||
id={atTheTop ? 'at-the-top' : 'not-at-the-top'} | ||
style={{ position: 'fixed', top: 8 }} | ||
> | ||
{atTheTop ? null : 'not '}at the top | ||
</span> | ||
) | ||
} | ||
|
||
function ScrollAction() { | ||
const [scroll] = useQueryState('scroll', parseAsBoolean.withDefault(false)) | ||
const [, setState] = useQueryState('test', { | ||
scroll | ||
}) | ||
|
||
useEffect(() => { | ||
document.getElementById('scroll-to-me')?.scrollIntoView() | ||
}, []) | ||
|
||
return ( | ||
<button | ||
id="scroll-to-me" | ||
onClick={() => setState('pass')} | ||
style={{ marginInline: 'auto', display: 'block' }} | ||
> | ||
Test | ||
</button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters