diff --git a/examples/magento-graphcms/components/Layout/LayoutNavigation.tsx b/examples/magento-graphcms/components/Layout/LayoutNavigation.tsx index 4c1a69a9c02..47dcaf024f7 100644 --- a/examples/magento-graphcms/components/Layout/LayoutNavigation.tsx +++ b/examples/magento-graphcms/components/Layout/LayoutNavigation.tsx @@ -21,11 +21,13 @@ import { NavigationOverlay, useNavigationSelection, useMemoDeep, + LazyHydrate, } from '@graphcommerce/next-ui' import { i18n } from '@lingui/core' import { Trans } from '@lingui/react' import { Divider, Fab } from '@mui/material' import { useRouter } from 'next/router' +import { useState } from 'react' import { Footer } from './Footer' import { LayoutQuery } from './Layout.gql' import { Logo } from './Logo' @@ -39,9 +41,12 @@ export function LayoutNavigation(props: LayoutNavigationProps) { const selection = useNavigationSelection() const router = useRouter() + const [hold, setHold] = useState(true) + const i = () => selection.set([menu?.items?.[0]?.uid || '']) return ( <> [ @@ -126,7 +131,12 @@ export function LayoutNavigation(props: LayoutNavigationProps) { ))} - selection.set([menu?.items?.[0]?.uid || ''])}> + { + setHold(false) + setTimeout(() => selection.set([menu?.items?.[0]?.uid || '']), 100) + }} + > {menu?.items?.[0]?.name} diff --git a/packages/next-ui/LazyHydrate/LazyHydrate.tsx b/packages/next-ui/LazyHydrate/LazyHydrate.tsx index 70b3634df27..7b9e1b969d1 100644 --- a/packages/next-ui/LazyHydrate/LazyHydrate.tsx +++ b/packages/next-ui/LazyHydrate/LazyHydrate.tsx @@ -11,6 +11,7 @@ type WithHydrationOnDemandOptions = { wrapperProps?: React.ComponentProps<'div'> forceHydration?: boolean hold?: boolean + afterHydrate?: () => void } function withHydrationOnDemandServerSide

(options: WithHydrationOnDemandOptions) { @@ -26,8 +27,10 @@ function withHydrationOnDemandClientSide

(incoming: WithHydrati const { wrapperProps, forceHydration = false } = incoming return (Component: React.ComponentType

) => { - function ComponentWithHydration(props: P & { forceHydration?: boolean; hold?: boolean }) { - const { hold } = props + function ComponentWithHydration( + props: P & { forceHydration?: boolean; hold?: boolean; afterHydrate?: () => void }, + ) { + const { hold, afterHydrate } = props const rootRef = useRef(null) const isInputPending = () => { @@ -44,8 +47,11 @@ function withHydrationOnDemandClientSide

(incoming: WithHydrati const [isHydrated, setIsHydrated] = useState(getDefaultHydrationState()) const hydrate = useCallback(() => { - startTransition(() => setIsHydrated(true)) - }, []) + startTransition(() => { + setIsHydrated(true) + if (afterHydrate) afterHydrate() + }) + }, [afterHydrate]) useLayoutEffect(() => { if (hold) return @@ -116,12 +122,21 @@ export type LazyHydrateProps = { * When eager is set to true, it disables all functionality and render the component regularly */ eager?: boolean - + /** + * When hold is set to true, render nothing + */ hold?: boolean + + afterHydrate?: () => void } -export function LazyHydrate(props: { children: React.ReactNode; eager?: boolean; hold?: boolean }) { - const { children, eager = false, hold = false } = props +export function LazyHydrate(props: { + children: React.ReactNode + eager?: boolean + hold?: boolean + afterHydrate?: () => void +}) { + const { children, eager = false, hold = false, afterHydrate } = props const LazyComponent = lazyHydrate(() => children) - return eager ? children : + return eager ? children : } diff --git a/packages/next-ui/Navigation/components/NavigationProvider.tsx b/packages/next-ui/Navigation/components/NavigationProvider.tsx index db547fd5e3c..e6133343d49 100644 --- a/packages/next-ui/Navigation/components/NavigationProvider.tsx +++ b/packages/next-ui/Navigation/components/NavigationProvider.tsx @@ -11,7 +11,7 @@ import { NavigationNodeComponent, } from '../hooks/useNavigation' -export type NavigationProviderProps = { +export type NavigationProviderBaseProps = { items: (NavigationNode | React.ReactElement)[] hideRootOnNavigate?: boolean closeAfterNavigate?: boolean @@ -21,9 +21,10 @@ export type NavigationProviderProps = { serverRenderDepth?: number } +export type NavigationProviderProps = NavigationProviderBaseProps & { hold?: boolean } const nonNullable = (value: T): value is NonNullable => value !== null && value !== undefined -export const NavigationProvider = React.memo((props) => { +const NavigationProviderBase = React.memo((props) => { const { items, hideRootOnNavigate = true, @@ -37,6 +38,8 @@ export const NavigationProvider = React.memo((props) => const animating = useMotionValue(false) const closing = useMotionValue(false) + console.log('joe') + const value = useMemo( () => ({ hideRootOnNavigate, @@ -69,10 +72,17 @@ export const NavigationProvider = React.memo((props) => ) return ( - - - {children} - - + + {children} + ) }) + +export function NavigationProvider(props: NavigationProviderProps) { + const { hold = false } = props + return ( + + + + ) +}