Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

2.2 KB
Raw Blame
import React, { useEffect, useRef } from 'react';
import { Link } from 'react-router-dom';
import { ArrowLeft } from 'lucide-react';
import { cn } from '@/lib/utils';
import { takeHeadingFocus } from '@/layout/routeFocus';

export interface PageHeaderBack {
  to: string;
  label: React.ReactNode;
}

export interface PageHeaderProps {
  title: React.ReactNode;
  description?: React.ReactNode;
  eyebrow?: React.ReactNode;
  actions?: React.ReactNode;
  back?: PageHeaderBack;
  media?: React.ReactNode;
  size?: 'default' | 'display';
  className?: string;
}

// Page title block. One per page; actions sit on the right and wrap under the title on narrow screens.
export default function PageHeader({
  title,
  description,
  eyebrow,
  actions,
  back,
  media,
  size = 'default',
  className,
}: PageHeaderProps) {
  const headingRef = useRef<HTMLHeadingElement>(null);
  useEffect(() => { takeHeadingFocus(headingRef.current); }, []);
  return (
    <header className={cn('flex flex-col gap-4', className)}>
      {back && (
        <Link
          to={back.to}
          className="inline-flex w-fit items-center gap-1.5 rounded-row text-small font-medium text-ink-muted transition-colors duration-(--duration-fast) hover:text-ink"
        >
          <ArrowLeft className="size-4 stroke-[1.75]" />
          {back.label}
        </Link>
      )}
      <div className="flex flex-wrap items-end justify-between gap-x-6 gap-y-4">
        <div className="flex min-w-0 items-center gap-4">
          {media}
          <div className="min-w-0">
            {eyebrow && <p className="eyebrow mb-2 text-brand-text">{eyebrow}</p>}
            <h1
              ref={headingRef}
              tabIndex={-1}
              className={cn(
                'font-heading font-semibold text-ink focus-visible:outline-offset-4',
                size === 'display' ? 'text-display' : 'text-page-title',
              )}
            >
              {title}
            </h1>
            {description && <div className="mt-1.5 max-w-prose text-body text-ink-muted">{description}</div>}
          </div>
        </div>
        {actions && <div className="flex flex-wrap items-center gap-2">{actions}</div>}
      </div>
    </header>
  );
}