next.js/test/e2e/app-dir/optimistic-routing/components/link-accordion.tsx
link-accordion.tsx36 lines830 B
'use client'

import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'

export function LinkAccordion({
  href,
  children,
  prefetch,
}: {
  href: LinkProps['href']
  children: React.ReactNode
  prefetch?: LinkProps['prefetch']
}) {
  // This component controls when prefetching happens by hiding the Link until
  // the checkbox is clicked. Prefetch triggers when the Link enters the viewport.
  const [isVisible, setIsVisible] = useState(false)
  return (
    <>
      <input
        type="checkbox"
        checked={isVisible}
        onChange={() => setIsVisible(!isVisible)}
        data-link-accordion={href}
      />
      {isVisible ? (
        <Link href={href} prefetch={prefetch}>
          {children}
        </Link>
      ) : (
        `${children} (link is hidden)`
      )}
    </>
  )
}
Quest for Codev2.0.0
/
SIGN IN