import type { ReactNode } from 'react' import type { ResolvedLink } from '../../links/resolveNav.js' import type { LinkData } from '../data.js' import type { LinkComponent } from './Anchor.js' import { Anchor } from './Anchor.js' import { buttonClass as defaultButtonClass } from './Button.js' import { cx } from './cx.js' export type ResolveLink = (link: LinkData, locale: string) => null | Promise | ResolvedLink export type CMSLinkProps = { /** For a link that wraps something (a card, an image): the content instead of the label — shown without a link when the link leads nowhere. */ children?: ReactNode className?: string link: LinkData locale: string } /** The plugin's link: `resolve` (lib/content `resolveLink`), `buttonClass` and `Link` (next/link) are bound by createBlockRegistry. */ export async function CMSLink({ buttonClass = defaultButtonClass, children, className, link, Link, locale, resolve, }: { buttonClass?: (appearance?: null | string) => string; Link?: LinkComponent; resolve: ResolveLink } & CMSLinkProps) { const l = link ? await resolve(link, locale) : null // A link that wraps something (a logo, a card) and leads nowhere — an empty link group, a deleted page — // keeps its content as it is; a plain text link without a target renders nothing. if (!l) {return children ?? null} const content = children ?? l.label if (!content) {return null} const cls = cx(buttonClass(l.appearance), className) || undefined return ( {content} ) }