Skip to content

Commit 3ece488

Browse files
committed
#3464 Go to top floating button added
1 parent 97f0df9 commit 3ece488

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

web/app/[locale]/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import DraftModeToast from '@/sections/DraftMode/DraftModeToast'
1616
import { routing } from '../../i18n/routing'
1717
import { GoogleTagManagerHead } from './GTMHead'
1818
import { SiteImprove } from './SiteImprove'
19+
import GoToTopButton from '@/sections/GoToTopButton'
1920

2021
const equinorRegular = localFont({
2122
src: '../fonts/equinor/Equinor-Regular.woff',
@@ -69,6 +70,7 @@ export default async function LocaleLayout({
6970
<body>
7071
<Toaster />
7172
{isDraftMode && <DraftModeToast />}
73+
<GoToTopButton/>
7274
{/* <SanityLive onError={handleError} /> */}
7375
<NextIntlClientProvider>
7476
<PageProvider

web/icons/ArrowUp.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { arrow_up } from '@equinor/eds-icons'
2+
import { forwardRef, type Ref, type SVGProps } from 'react'
3+
import { TransformableIcon } from './TransformableIcon'
4+
5+
export type ArrowUpProps = {
6+
/** Size, use if you need large icon resolutions
7+
* @default 24
8+
*/
9+
size?: 16 | 18 | 24 | 32 | 40 | 48
10+
/** @ignore */
11+
ref?: Ref<SVGSVGElement>
12+
} & SVGProps<SVGSVGElement>
13+
14+
export const ArrowUp = forwardRef<SVGSVGElement, ArrowUpProps>(
15+
function ArrowUp({ ...rest }, ref) {
16+
return <TransformableIcon iconData={arrow_up} {...rest} ref={ref} />
17+
},
18+
)
19+
export default ArrowUp

web/icons/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { default as Linkedin } from './Linkedin'
44
export { default as Twitter } from './Twitter'
55
export { default as Youtube } from './Youtube'
66
export { default as ArrowRight } from './ArrowRight'
7+
export { default as ArrowUp } from './ArrowUp'
78
export { default as Play } from './Play'
89
export { default as Pause } from './Pause'
910
export { default as QuoteSymbol } from './QuoteSymbol'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use client'
2+
3+
import { useEffect, useState } from 'react'
4+
import ArrowUp from '@/icons/ArrowUp'
5+
6+
export default function GoToTopButton() {
7+
const [isVisible, setIsVisible] = useState(false)
8+
9+
// Show button when page is scrolled down
10+
const toggleVisibility = () => {
11+
if (typeof window !== 'undefined') {
12+
setIsVisible(window.scrollY > window.innerHeight)
13+
}
14+
}
15+
16+
// Scroll to top smoothly
17+
const scrollToTop = () => {
18+
if (typeof window !== 'undefined') {
19+
window.scrollTo({
20+
top: 0,
21+
behavior: 'smooth',
22+
})
23+
}
24+
}
25+
26+
useEffect(() => {
27+
window.addEventListener('scroll', toggleVisibility)
28+
return () => {
29+
window.removeEventListener('scroll', toggleVisibility)
30+
}
31+
}, [])
32+
33+
return (
34+
<>
35+
{isVisible && (
36+
<button
37+
onClick={scrollToTop}
38+
aria-label="Go to top"
39+
className="fixed bottom-8 right-8 z-40 p-3 rounded-full bg-slate-blue-95 text-white shadow-lg hover:shadow-xl transition-all duration-300 cursor-pointer"
40+
>
41+
<ArrowUp size={24} />
42+
</button>
43+
)}
44+
</>
45+
)
46+
}

0 commit comments

Comments
 (0)