From 113efaa770dd5614e621c8a23bb4d93be8feb9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Fri, 22 Sep 2023 20:36:40 +0200 Subject: [PATCH] chore(Portal): add View Transition --- .../gatsby-browser.tsx | 10 ++--- .../src/shared/menu/StickyMenuBar.js | 2 + .../src/shared/parts/PortalStyle.scss | 6 +++ .../src/shared/tags/Anchor.tsx | 11 ++++- .../src/shared/tags/Intro.tsx | 11 +++-- .../src/shared/tags/Tabbar.tsx | 42 +++++++++---------- .../src/shared/tags/Transition.ts | 8 ++++ 7 files changed, 55 insertions(+), 35 deletions(-) create mode 100644 packages/dnb-design-system-portal/src/shared/tags/Transition.ts diff --git a/packages/dnb-design-system-portal/gatsby-browser.tsx b/packages/dnb-design-system-portal/gatsby-browser.tsx index 2da16bfb98e..207da388f15 100644 --- a/packages/dnb-design-system-portal/gatsby-browser.tsx +++ b/packages/dnb-design-system-portal/gatsby-browser.tsx @@ -56,12 +56,8 @@ export const onRouteUpdate = ({ location, prevLocation }) => { } } -export const onPreRouteUpdate = ({ prevLocation }) => { - if (prevLocation && !globalThis.IS_TEST) { - // Omit scrolling on page changes - document.documentElement.style.scrollBehavior = 'auto' - setTimeout(() => { - document.documentElement.style.scrollBehavior = '' - }, 800) +declare global { + interface Document { + startViewTransition(): void } } diff --git a/packages/dnb-design-system-portal/src/shared/menu/StickyMenuBar.js b/packages/dnb-design-system-portal/src/shared/menu/StickyMenuBar.js index e2c3a89e819..6811b5f0704 100644 --- a/packages/dnb-design-system-portal/src/shared/menu/StickyMenuBar.js +++ b/packages/dnb-design-system-portal/src/shared/menu/StickyMenuBar.js @@ -25,6 +25,7 @@ import { portalHeaderWrapperStyle, hideSidebarToggleButtonStyle, } from './StickyMenuBar.module.scss' +import { Link } from '../tags/Anchor' export default function StickyMenuBar({ hideSidebarToggleButton, @@ -66,6 +67,7 @@ export default function StickyMenuBar({ text="Home" title="Eufemia main sections" href="/" + element={Link} icon="chevron_left" icon_position="left" /> diff --git a/packages/dnb-design-system-portal/src/shared/parts/PortalStyle.scss b/packages/dnb-design-system-portal/src/shared/parts/PortalStyle.scss index 57f2a35d32a..2525a40b8c0 100644 --- a/packages/dnb-design-system-portal/src/shared/parts/PortalStyle.scss +++ b/packages/dnb-design-system-portal/src/shared/parts/PortalStyle.scss @@ -104,3 +104,9 @@ html[show-dev-grid] .dev-grid { .lh-32 { line-height: calc(var(--line-height-basis) + 0.5rem); /* 2rem */ } + +// Make the home => pages animation +.home-background ul, +#portal-sidebar-menu { + view-transition-name: home; +} diff --git a/packages/dnb-design-system-portal/src/shared/tags/Anchor.tsx b/packages/dnb-design-system-portal/src/shared/tags/Anchor.tsx index 713c01a1f83..5b798fa90c7 100644 --- a/packages/dnb-design-system-portal/src/shared/tags/Anchor.tsx +++ b/packages/dnb-design-system-portal/src/shared/tags/Anchor.tsx @@ -11,6 +11,7 @@ import { } from '@dnb/eufemia/src/components/Anchor' import { GatsbyLinkProps, Link as GatsbyLink } from 'gatsby' import { ElementIsType } from '@dnb/eufemia/src/elements/Element' +import { startPageTransition } from './Transition' export type AnchorProps = Props & Omit< @@ -22,7 +23,7 @@ export type AnchorProps = Props & > const PortalLink = React.forwardRef(function Link( - { href, ...props }: AnchorProps, + { href, onClick = null, ...props }: AnchorProps, ref, ) { return ( @@ -30,8 +31,16 @@ const PortalLink = React.forwardRef(function Link( to={href} ref={ref} {...(props as Omit, 'ref' | 'onClick'>)} + onClick={clickHandler} /> ) + + function clickHandler(event: React.MouseEvent) { + startPageTransition() + if (onClick) { + onClick(event) + } + } }) export { PortalLink as Link } diff --git a/packages/dnb-design-system-portal/src/shared/tags/Intro.tsx b/packages/dnb-design-system-portal/src/shared/tags/Intro.tsx index c27c58f4740..f1d8c7e1d21 100644 --- a/packages/dnb-design-system-portal/src/shared/tags/Intro.tsx +++ b/packages/dnb-design-system-portal/src/shared/tags/Intro.tsx @@ -7,8 +7,10 @@ import React from 'react' import { Global, css } from '@emotion/react' import PropTypes from 'prop-types' import { navigate } from 'gatsby' -import { Link, Button, Space } from '@dnb/eufemia/src' +import { Anchor, Button, Space } from '@dnb/eufemia/src' import { wrapperStyle, innerStyle, footerStyle } from './Intro.module.scss' +import { startPageTransition } from './Transition' +import { Link } from './Anchor' const ref = React.createRef() const Intro = ({ children }) => { @@ -17,6 +19,7 @@ const Intro = ({ children }) => { if (/textarea|input/i.test(document.activeElement.tagName)) { return } + startPageTransition() try { if (e.key === 'ArrowRight' && ref && ref.current) { const elem = ref.current.querySelector('a[href*="/intro"]') @@ -61,13 +64,14 @@ export const IntroFooter = ({ href, text }) => ( } `} /> -