Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.2 KB
Raw Blame
import { type ReactNode } from 'react'
import { Button, cn, FloatingPlate } from 'ui'

export interface SkipToContentProps {
  /** Hash href to the main content landmark, e.g. `#main`. */
  href: string
  children?: ReactNode
  className?: string
}

/**
 * Keyboard-accessible skip link. Hidden until focused via Tab, then slides into view.
 *
 * Callers must provide a matching landmark target with `id`, `tabIndex={-1}`,
 * `outline-hidden`, and `scroll-mt` when a sticky header is present. The
 * landmark itself should not show a visible focus ring.
 */
function SkipToContent({ href, children = 'Skip to content', className }: SkipToContentProps) {
  return (
    <FloatingPlate
      className={cn(
        'fixed top-0 left-[10px] z-[100] w-fit',
        // Extra offset clears the raised Button drop shadow (0 1px 3px), which
        // otherwise peeks into the viewport when parked at exactly -100%.
        '-translate-y-[calc(100%+8px)] focus-within:translate-y-[10px]',
        'transition-transform duration-200 ease-out',
        className
      )}
    >
      <Button size="tiny" asChild>
        <a href={href}>{children}</a>
      </Button>
    </FloatingPlate>
  )
}

export { SkipToContent }