Skip to documentation

Design system / Sources & standards

Current repository source

useContentShowcase.ts

src/components/showcase/useContentShowcase.ts

This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.

"use client";

import { useCallback, useRef, useState } from "react";
import type { CarouselApi } from "@/components/ui/carousel";

function pauseVideos(root: HTMLElement | null) {
  root?.querySelectorAll("video").forEach((video) => video.pause());
}

/** Collection-owned dialog coordination. Embla and Radix retain their own lifecycles. */
export function useContentShowcase() {
  const [open, setOpen] = useState(false);
  const [startIndex, setStartIndex] = useState(0);
  const inlineRef = useRef<HTMLDivElement>(null);
  const dialogRef = useRef<HTMLDivElement>(null);
  const triggerRef = useRef<HTMLButtonElement | null>(null);
  const inlineApi = useRef<CarouselApi>(undefined);
  const expandedApi = useRef<CarouselApi>(undefined);
  const setInlineApi = useCallback((api: CarouselApi) => { inlineApi.current = api; }, []);
  const setExpandedApi = useCallback((api: CarouselApi) => { expandedApi.current = api; }, []);
  const expand = useCallback((trigger: HTMLButtonElement, index?: number) => {
    triggerRef.current = trigger;
    pauseVideos(inlineRef.current);
    expandedApi.current = undefined;
    setStartIndex(index ?? inlineApi.current?.selectedScrollSnap() ?? 0);
    setOpen(true);
  }, []);
  const onOpenChange = useCallback((nextOpen: boolean) => {
    if (!nextOpen) {
      pauseVideos(dialogRef.current);
      inlineApi.current?.scrollTo(expandedApi.current?.selectedScrollSnap() ?? startIndex, true);
    }
    setOpen(nextOpen);
  }, [startIndex]);
  const restoreFocus = useCallback((event: Event) => {
    event.preventDefault();
    triggerRef.current?.focus();
  }, []);
  const focusCarousel = useCallback((event: Event) => {
    const carousel = dialogRef.current?.querySelector<HTMLElement>('[data-slot="carousel"]');
    if (carousel) { event.preventDefault(); carousel.focus(); }
  }, []);

  return { open, startIndex, inlineRef, dialogRef, setInlineApi, setExpandedApi, expand, onOpenChange, restoreFocus, focusCarousel };
}
All sources and standards
Current components and theme tokens. Fictional example data. Source views refresh on deployment; authors maintain contract notes.