import { brand } from "@dbx-tools/shared-core"; import { createContext, type ImgHTMLAttributes, type PropsWithChildren, useContext, useEffect, useMemo, } from "react"; import { applyBrandContext, type BrandAssetResolver, resolveBrandAsset } from "../browser.ts"; interface BrandState { context: brand.BrandContext; resolveAsset: BrandAssetResolver; } const BrandReactContext = createContext({ context: brand.defaultBrandContext, resolveAsset: resolveBrandAsset, }); export interface BrandProviderProps extends PropsWithChildren { context?: brand.BrandContextInput; resolveAsset?: BrandAssetResolver; applyToDocument?: boolean; } export function BrandProvider({ children, context, resolveAsset = resolveBrandAsset, applyToDocument = false, }: BrandProviderProps) { const parsed = useMemo(() => brand.parseBrandContext(context), [context]); const value = useMemo(() => ({ context: parsed, resolveAsset }), [parsed, resolveAsset]); useEffect(() => { if (applyToDocument) applyBrandContext(parsed, { resolveAsset }); }, [applyToDocument, parsed, resolveAsset]); return {children}; } export function useBrand(): BrandState { return useContext(BrandReactContext); } export interface BrandImageProps extends Omit, "src" | "alt"> { alt?: string; mode?: "auto" | "light" | "dark"; } function BrandImage({ asset, alt, mode = "auto", ...props }: BrandImageProps & { asset: brand.BrandAssetSet }) { const { resolveAsset } = useBrand(); const light = resolveAsset(asset.light); const dark = resolveAsset(asset.dark ?? asset.light); if (mode !== "auto") { return {alt; } return ( {alt ); } export function BrandIcon(props: BrandImageProps) { const { context } = useBrand(); const { alt, ...imageProps } = props; return ( ); } export function BrandLogo(props: BrandImageProps) { const { context } = useBrand(); const { alt, ...imageProps } = props; return ; }