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.

1.7 KB
Raw Blame
import React, { useId } from 'react';
import { cn } from '@/lib/utils';
import { SurfaceTitleContext } from './surfaceTitle';

export interface SurfaceBoxProps extends Omit<React.ComponentProps<'section'>, 'title' | 'content'> {
  children?: React.ReactNode;
  title?: React.ReactNode;
  subheader?: React.ReactNode;
  secondary?: React.ReactNode;
  content?: boolean;
  contentClassName?: string;
  className?: string;
  noPadding?: boolean;
  headingLevel?: 'h2' | 'h3';
}

// the one container for grouped content; without a title it is a plain panel
export default function SurfaceBox({
  children,
  title,
  subheader,
  secondary,
  content = true,
  contentClassName = '',
  className = '',
  noPadding = false,
  headingLevel: Heading = 'h2',
  ...props
}: SurfaceBoxProps) {
  const titleId = useId();
  return (
    <SurfaceTitleContext.Provider value={title ? titleId : undefined}>
      <section
        className={cn('flex flex-col rounded-card border border-line bg-surface-raised text-ink', className)}
        {...props}
      >
        {title && (
          <header className="flex items-start justify-between gap-4 border-b border-divider px-5 py-4">
            <div className="min-w-0">
              <Heading id={titleId} className="font-heading text-section font-semibold">{title}</Heading>
              {subheader && <div className="mt-0.5 max-w-prose text-small text-ink-muted">{subheader}</div>}
            </div>
            {secondary && <div className="shrink-0">{secondary}</div>}
          </header>
        )}
        {content ? (
          <div className={cn('px-5 py-4', noPadding && 'p-0', contentClassName)}>{children}</div>
        ) : (
          children
        )}
      </section>
    </SurfaceTitleContext.Provider>
  );
}