import type { Element, ElementContent } from "hast"; import Image from "next/image.js"; import Link from "next/link.js"; import type { ComponentPropsWithoutRef } from "react"; import { Children, cloneElement, isValidElement, useId } from "react"; import { twJoin, twMerge } from "tailwind-merge"; import invariant from "tiny-invariant"; import pageLinks from "@/build-pages/links.ts"; import type { PageIndexDetail, TableOfContents } from "@/build-pages/types.ts"; import { ArrowRight } from "@/icons/arrow-right.tsx"; import { Hashtag } from "@/icons/hashtag.tsx"; import { NewWindow } from "@/icons/new-window.tsx"; import { isValidHref } from "@/lib/is-valid-href.ts"; import { AuthDisabled, AuthEnabled, AuthLoading, NotSubscribed, Subscribed, } from "./auth.tsx"; import { Command } from "./command.tsx"; import { InlineLink } from "./inline-link.tsx"; import { PageCards } from "./page-cards.tsx"; import { PageHeroProvider } from "./page-context.tsx"; import { PageExample } from "./page-example.tsx"; import { PageHovercardAnchor } from "./page-hovercard.tsx"; import { PageSidebar } from "./page-sidebar.tsx"; import { PageTag, PageTagList } from "./page-tag.tsx"; export interface PageHeadingProps extends ComponentPropsWithoutRef<"h1"> { node?: Element; level: number; } export function PageHeading({ node, level, ...props }: PageHeadingProps) { const className = twJoin( // base styles "text-black text-pretty dark:text-white tracking-[-0.035em] dark:tracking-[-0.015em]", "[&_code]:font-monospace [&_code]:rounded [&_code]:px-[0.2em] [&_code]:py-[0.15em]", "[&_code]:bg-black/[7.5%] dark:[&_code]:bg-white/[7.5%] max-w-[--size-content-box]", // sticky styles level < 4 && [ "max-md:flex items-center z-20 top-[--header-height]", "max-md:sticky max-md:min-h-[48px] max-md:pb-2 max-md:-mb-2 max-md:pr-12", "max-md:-mx-[--page-padding] max-md:w-[calc(100%+var(--page-padding)*2)] max-md:px-[--page-padding]", "max-md:bg-gray-50 max-md:dark:bg-gray-800", "[[data-dialog]_&]:static [[data-dialog]_&]:mb-0 [[data-dialog]_&]:pb-0 [[data-dialog]_&]:bg-inherit", ], level === 1 && "text-2xl font-extrabold dark:font-bold sm:text-3xl md:text-5xl [[data-dialog]_&]:text-2xl", level === 2 && "text-xl font-semibold dark:font-medium sm:text-2xl md:text-3xl [&_code]:font-medium [[data-dialog]_&]:text-xl", level === 3 && "text-lg font-semibold dark:font-medium sm:text-xl [[data-dialog]_&]:text-lg", level === 4 && "text-sm uppercase tracking-wider opacity-70", props.className, ); if (level === 1) { return (

{props.children}

); } if (level === 4) { return

; } const { id, ...rest } = props; const Element = level === 2 ? "h2" : "h3"; return ( {props.children} ); } export interface PageParagraphProps extends ComponentPropsWithoutRef<"p"> { node?: Element; } export function PageParagraph({ node, ...props }: PageParagraphProps) { const className = twJoin( "dark:text-white/[85%] leading-7", "max-w-[--size-content] text-pretty", "[p&_code]:rounded [p&_code]:text-[0.9375em] text-pretty", "[p&_code]:px-[0.25em] [p&_code]:py-[0.2em]", "[p&_code]:bg-black/[7.5%] dark:[p&_code]:bg-white/[7.5%]", "[p&_code]:font-monospace", props.className, ); const paragraph = (

); const children = Children.toArray(props.children); if (children.length > 1) return paragraph; const [child] = children; if (!child) return paragraph; if (!isValidElement(child)) return paragraph; if (!child.props) return paragraph; if ("data-large" in child.props) return child; if (!("data-playground" in child.props)) return paragraph; if (!child.props.href) return paragraph; return <>{props.children}; } export interface PageAsideProps extends ComponentPropsWithoutRef<"div"> { node?: Element; } export function PageAside({ node, title, ...props }: PageAsideProps) { const id = useId(); const className = twJoin( "flex flex-col items-start justify-center w-full gap-4 p-4 pl-5 sm:p-8", "rounded-lg sm:rounded-xl !rounded-l relative overflow-hidden", "max-w-[--size-lg] *:w-full", "before:absolute before:top-0 before:left-0 before:bottom-0 before:w-1", "data-[type=danger]:bg-red-100/70", "data-[type=danger]:dark:bg-red-700/20", "data-[type=danger]:before:bg-red-600", "data-[type=warn]:bg-amber-100", "data-[type=warn]:dark:bg-amber-800/20", "data-[type=warn]:before:bg-amber-500", "data-[type=warn]:before:dark:bg-yellow-600", "data-[type=note]:bg-blue-50/70", "data-[type=note]:dark:bg-blue-900/20", "data-[type=note]:before:bg-blue-600", props.className, ); if (!title) { return
; } return ( ); } export interface PageDescriptionProps extends ComponentPropsWithoutRef<"p"> { node?: Element; } export function PageDescription({ node, children, ...props }: PageDescriptionProps) { const [p, paragraph = p] = Children.toArray(children); invariant( isValidElement>(paragraph), "Expected paragraph", ); return cloneElement(paragraph, { ...props, children: ( {paragraph.props.children} ), className: twJoin( "-translate-y-2 !max-w-[--size-content-box] [:has([data-call-to-action])_&]:md:grid grid-cols-[1fr_260px] w-full gap-4 md:gap-8 text-lg sm:text-xl sm:leading-8 text-black/70 dark:!text-white/60", paragraph.props.className, props.className, ), }); } export interface PageDividerProps extends ComponentPropsWithoutRef<"hr"> { node?: Element; } export function PageDivider({ node, ...props }: PageDividerProps) { const className = twJoin( "w-full max-w-[--size-content] border-t border-black/10 dark:border-white/10", "[[data-dialog]_section:last-of-type_&]:hidden", props.className, ); return
; } export interface PageFigureProps extends ComponentPropsWithoutRef<"figure"> { node?: Element; } export function PageFigure({ node, ...props }: PageFigureProps) { const className = twJoin( "group gap-2 flex-col grid-cols-1 sm:grid-cols-2 overflow-hidden rounded-lg md:rounded-xl", "[&>img]:!rounded-none", "data-[wide]:max-w-[--size-xl] data-[wide]:md:rounded-2xl", "data-[media]:grid", "data-[quote]:flex data-[quote]:max-w-[--size-quote]", "data-[bigquote]:flex data-[bigquote]:!w-auto data-[bigquote]:p-4", props.className, ); return
; } export interface PageBlockquoteProps extends ComponentPropsWithoutRef<"blockquote"> { node?: Element; } export function PageBlockquote({ node, ...props }: PageBlockquoteProps) { const className = twJoin( "flex flex-col gap-4 px-4 max-w-[--size-quote] border-l-4 border-black/25 dark:border-white/25", "group-data-[bigquote]:border-0", "group-data-[bigquote]:italic", "group-data-[bigquote]:p-0", "group-data-[bigquote]:text-lg", "group-data-[bigquote]:sm:text-xl", "group-data-[bigquote]:md:text-2xl", "group-data-[bigquote]:opacity-75", props.className, ); return
; } export interface PageListProps extends ComponentPropsWithoutRef<"ol"> { node?: Element; ordered?: boolean; } export function PageList({ node, ordered, ...props }: PageListProps) { const className = twJoin( "flex flex-col gap-4 pl-8 list-none w-full max-w-[--size-content]", props.className, ); const Element = ordered ? "ol" : "ul"; return (
); } export interface PageListItemProps extends ComponentPropsWithoutRef<"li"> { node?: Element; ordered?: boolean; index?: number; multiline?: boolean; } export function PageListItem({ node, index, ordered, multiline, ...props }: PageListItemProps) { const isMultiline = multiline ?? Children.toArray(props.children).at(0) === "\n"; const className = twJoin( "relative flex flex-col", ordered ? "gap-4" : "gap-2", ordered && isMultiline && "before:absolute before:top-9 before:-left-5 before:w-px before:h-[calc(100%-theme(spacing.7))] before:bg-black/10 dark:before:bg-white/10", props.className, ); return (
  • {ordered ? ( {(index || 0) + 1} ) : ( )} {isMultiline ? ( props.children ) : ( {props.children} )}
  • ); } export interface PageSectionProps extends ComponentPropsWithoutRef<"section"> { level?: number; plus?: boolean; media?: PageIndexDetail["media"]; tableOfContents?: TableOfContents; } export function PageSection({ level, plus, media, tableOfContents, ...props }: PageSectionProps) { level = level ?? ("data-level" in props ? Number.parseInt(props["data-level"] as string, 10) : undefined); const section = (
    ); if (plus) { if (level && level > 1 && props.id !== "related-examples") { return ( {section} ); } if (level === 1) { tableOfContents = tableOfContents?.filter( (item, index) => index !== 0 && !["Components", "Related examples"].includes(item.text), ); return ( {section} {!!tableOfContents?.length && (
    Learn more about this example This is a new example exclusive to Ariakit Plus subscribers. In addition to gaining access to the complete source code above, you will also have the opportunity to delve deeper into this example through the following topics: {tableOfContents.map((item, index) => ( {item.text} ))}
    } > Unlock Ariakit Plus
    {/* {media?.slice(0, 3).map((item) => { if (item.type === "image") { return ( ); } return null; })} */}
    )}
    ); } } if (level === 1) { return {section}; } return section; } export interface PageDivProps extends ComponentPropsWithoutRef<"div"> { node?: Element; tags?: string[]; media?: PageIndexDetail["media"]; category?: string; page?: string; title?: string; tableOfContents?: TableOfContents; } export function PageDiv({ node, category, page, title, tags = [], media = [], tableOfContents, ...props }: PageDivProps) { if (node?.properties?.dataLevel) { return ( ); } if (node?.properties?.dataSections != null && props.children) { const sidebarPlaceholder =
    ; const sidebar = tableOfContents && ( ); return (
    {tags.includes("Plus") ? ( <> {sidebarPlaceholder} {sidebarPlaceholder} {tableOfContents ? sidebar : sidebarPlaceholder} {sidebarPlaceholder} ) : ( sidebar )}
    {props.children}
    ); } if (node?.properties?.dataDescription != null) { return ; } if (node?.properties?.dataTags != null) { if (!tags.length) return null; return ( {tags.map((tag) => ( ))} ); } if (node?.properties?.dataCards != null) { const type = node.properties.dataCards.toString(); return ( ); } return
    ; } export interface PageKbdProps extends ComponentPropsWithoutRef<"kbd"> { node?: Element; } export function PageKbd({ node, ...props }: PageKbdProps) { const className = twJoin( "font-[inherit] px-[0.2667em] p-[0.1334em]", "rounded-[0.2667em] rounded-b-[0.3334em] border-b-[0.1334em] border-t-[0.0667em] border-b-black/[7.5%] border-t-white", "dark:border-b-[0.2em] dark:border-t-0 dark:border-b-black/40 dark:rounded-b-[0.4em]", "bg-gradient-to-b from-black/[15%] to-black/5 dark:from-white/10 dark:to-white/[15%]", "[box-shadow:0_0_0_max(1px,0.033333em)_rgba(0,0,0,0.25)]", "dark:[box-shadow:0_min(-1px,(-0.0666em))_rgba(255,255,255,0.1),0_0_0_max(1px,0.0666em)_rgba(255,255,255,0.15)]", props.className, ); return ; } export interface PageStrongProps extends ComponentPropsWithoutRef<"strong"> { node?: Element; } export function PageStrong({ node, ...props }: PageStrongProps) { const isBreaking = Children.toArray(props.children).at(0) === "BREAKING"; const className = twJoin( "font-semibold dark:text-white", isBreaking && "px-[0.2667em] p-[0.1334em] text-white bg-red-700 rounded-[0.2667em] text-sm mx-[0.1334em]", props.className, ); return ; } export interface PageImageProps extends ComponentPropsWithoutRef<"img"> { node?: Element; } export function PageImage({ node, src, alt, width, height, ...props }: PageImageProps) { const className = twJoin( "overflow-hidden rounded-lg max-w-[--size-md] data-[large]:max-w-[--size-lg] data-[wide]:max-w-[--size-xl] md:rounded-xl data-[wide]:md:rounded-2xl", props.className, ); return ( {alt!} ); } function getNodeText(node: Element | ElementContent): string { if ("children" in node) return node.children.map(getNodeText).join(""); if ("value" in node) return node.value; return ""; } export interface PageAProps extends ComponentPropsWithoutRef<"a"> { category?: string; page?: string; file?: string; node?: Element; hovercards?: Set>>; tags?: string[]; } export function PageA({ node, file, href, hovercards, tags, category: currentCategory, page: currentPage, ...props }: PageAProps) { if ("data-playground" in props && href) { return ( ); } href = href?.replace(/^https:\/\/(www\.)?ariakit\.(com|org)/, ""); if (href?.startsWith("http")) { return ( {props.children} ⁠ ); } if (href?.startsWith("/apis")) { return {props.children}; } if (href?.startsWith("#")) { return ( ⁠ {props.children} ); } if (href) { if (!isValidHref(href, pageLinks)) { throw new Error(`Invalid link: ${href}`); } const url = new URL(href, "https://ariakit.com"); const [, category, page] = url.pathname.split("/"); if (category === "reference" && page && node) { if ( currentCategory && currentPage && url.hash && ((!page.endsWith("provider") && !page.endsWith("store")) || currentPage.endsWith("provider") || currentPage.endsWith("store")) && isValidHref(`/${currentCategory}/${currentPage}${url.hash}`, pageLinks) ) { href = `/${currentCategory}/${currentPage}${url.hash}`; } const hash = url.hash.replace("#", ""); const text = getNodeText(node); const isComponent = /^[A-Z]/.test(text); const isHook = page.startsWith("use-"); const className = twMerge( "decoration-dotted hover:decoration-solid", hash ? isHook ? "text-[#000f80] dark:text-[#9cdcfe]" : "text-[#ce0000] dark:text-[#9cdcfe]" : isComponent ? "text-[#227289] dark:text-[#4ec9b0]" : "text-[#795e26] dark:text-[#dcdcaa]", props.className, ); hovercards?.add(Promise.resolve(href)); return ( } />} /> ); } return } />; } return ; }