-
Notifications
You must be signed in to change notification settings - Fork 43
feat(web): add scroll-to-top button #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5313dc1
feat(web): add scroll-to-top button
Hard-system 2b0d5df
fix(web): separate scroll-to-top from accessibility widget, rAF-throt…
StanislavBG d27f9cf
fix(web): respect prefers-reduced-motion on scroll-to-top
StanislavBG 5e5be83
fix(web): run prettier on scroll-to-top component
StanislavBG de85a1f
test(web): add coverage and address reviewer round on scroll-to-top
StanislavBG 21450d3
Merge branch 'main' into feat/scroll-to-top
todorkolev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,111 @@ | ||
| // @vitest-environment jsdom | ||
| import { act } from 'react'; | ||
| import { createRoot, type Root } from 'react-dom/client'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { ScrollToTop } from './ScrollToTop'; | ||
|
|
||
| function mockMatchMedia(prefersReducedMotion: boolean) { | ||
| window.matchMedia = vi.fn().mockImplementation((query: string) => ({ | ||
| matches: query === '(prefers-reduced-motion: reduce)' && prefersReducedMotion, | ||
| media: query, | ||
| onchange: null, | ||
| addListener: vi.fn(), | ||
| removeListener: vi.fn(), | ||
| addEventListener: vi.fn(), | ||
| removeEventListener: vi.fn(), | ||
| dispatchEvent: vi.fn(), | ||
| })); | ||
| } | ||
|
|
||
| function scrollTo(y: number) { | ||
| Object.defineProperty(window, 'scrollY', { value: y, configurable: true, writable: true }); | ||
| window.dispatchEvent(new Event('scroll')); | ||
| } | ||
|
|
||
| describe('ScrollToTop', () => { | ||
| let container: HTMLDivElement; | ||
| let root: Root; | ||
|
|
||
| beforeEach(() => { | ||
| container = document.createElement('div'); | ||
| document.body.appendChild(container); | ||
| root = createRoot(container); | ||
| mockMatchMedia(false); | ||
| window.scrollTo = vi.fn(); | ||
| window.requestAnimationFrame = (cb: FrameRequestCallback) => { | ||
| cb(0); | ||
| return 0; | ||
| }; | ||
| Object.defineProperty(window, 'scrollY', { value: 0, configurable: true, writable: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| act(() => { | ||
| root.unmount(); | ||
| }); | ||
| container.remove(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| function getButton() { | ||
| return container.querySelector('button.scroll-to-top') as HTMLButtonElement; | ||
| } | ||
|
|
||
| it('is hidden below the SHOW_AFTER_PX threshold', () => { | ||
| act(() => { | ||
| root.render(<ScrollToTop />); | ||
| }); | ||
| act(() => { | ||
| scrollTo(399); | ||
| }); | ||
| expect(getButton().className).not.toContain('is-visible'); | ||
| expect(getButton().getAttribute('aria-hidden')).toBe('true'); | ||
| }); | ||
|
|
||
| it('becomes visible above the SHOW_AFTER_PX threshold', () => { | ||
| act(() => { | ||
| root.render(<ScrollToTop />); | ||
| }); | ||
| act(() => { | ||
| scrollTo(401); | ||
| }); | ||
| expect(getButton().className).toContain('is-visible'); | ||
| expect(getButton().getAttribute('aria-hidden')).toBe('false'); | ||
| }); | ||
|
|
||
| it('hides again when scrolling back under the threshold', () => { | ||
| act(() => { | ||
| root.render(<ScrollToTop />); | ||
| }); | ||
| act(() => { | ||
| scrollTo(500); | ||
| }); | ||
| expect(getButton().className).toContain('is-visible'); | ||
| act(() => { | ||
| scrollTo(100); | ||
| }); | ||
| expect(getButton().className).not.toContain('is-visible'); | ||
| }); | ||
|
|
||
| it('calls window.scrollTo with smooth behavior on click by default', () => { | ||
| act(() => { | ||
| root.render(<ScrollToTop />); | ||
| }); | ||
| act(() => { | ||
| getButton().click(); | ||
| }); | ||
| expect(window.scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' }); | ||
| }); | ||
|
|
||
| it('calls window.scrollTo with auto behavior when reduced motion is preferred', () => { | ||
| mockMatchMedia(true); | ||
| act(() => { | ||
| root.render(<ScrollToTop />); | ||
| }); | ||
| act(() => { | ||
| getButton().click(); | ||
| }); | ||
| expect(window.scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'auto' }); | ||
| }); | ||
| }); |
This file contains hidden or 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,71 @@ | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| const SHOW_AFTER_PX = 400; | ||
|
|
||
| export function ScrollToTop() { | ||
| const [isVisible, setIsVisible] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| // Assumes document/window-level scrolling (confirmed: the app's inner-scrolling | ||
| // regions — the mobile nav drawer, the search suggestions popover — are both | ||
| // self-contained overlays, not the main content area). If a page ever scrolls via | ||
| // an inner container instead, this threshold check needs to target that container. | ||
| const toggleVisibility = () => { | ||
|
StanislavBG marked this conversation as resolved.
|
||
| if (window.scrollY > SHOW_AFTER_PX) { | ||
| setIsVisible(true); | ||
| } else { | ||
| setIsVisible(false); | ||
| } | ||
| }; | ||
|
|
||
| let ticking = false; | ||
| const onScroll = () => { | ||
| if (ticking) return; | ||
| ticking = true; | ||
| window.requestAnimationFrame(() => { | ||
| toggleVisibility(); | ||
| ticking = false; | ||
| }); | ||
| }; | ||
|
|
||
| window.addEventListener('scroll', onScroll, { passive: true }); | ||
| // Initial check in case the page is loaded already scrolled down | ||
| toggleVisibility(); | ||
|
|
||
| return () => window.removeEventListener('scroll', onScroll); | ||
| }, []); | ||
|
|
||
| const scrollToTop = () => { | ||
| const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; | ||
|
|
||
| window.scrollTo({ | ||
| top: 0, | ||
| behavior: prefersReducedMotion ? 'auto' : 'smooth', | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <button | ||
| type="button" | ||
| className={`scroll-to-top ${isVisible ? 'is-visible' : ''}`} | ||
| onClick={scrollToTop} | ||
| aria-label="Към началото" | ||
| aria-hidden={!isVisible} | ||
| tabIndex={isVisible ? 0 : -1} | ||
| > | ||
| <svg | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| strokeWidth="2" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| aria-hidden="true" | ||
| > | ||
| <line x1="12" y1="19" x2="12" y2="5" /> | ||
| <polyline points="5 12 12 5 19 12" /> | ||
| </svg> | ||
| </button> | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.