import { Text } from '@hanzo/gui'; import { Children, createElement, type ComponentType, type ReactNode } from 'react'; /** * Give bare text a Text host; pass elements through untouched. * * gui's primitives are react-native primitives, and a react-native View cannot * lay out a raw string. On native it throws; on web it is worse than that, * because it renders — as a zero-height box with no error and no type failure. * That is the whole failure mode: a build stays green and a label is invisible. * * So every component here that takes free-form children runs them through this, * and a string child then behaves the same on web and on native. */ export const ink = ( children: ReactNode, Wrap: ComponentType> = Text as never, props: Record = {}, ): ReactNode => { // Nothing to host: hand the children BACK, the same ones. Not a shortcut — // `Children.map` rebuilds the list and re-keys it, so an only child comes out // as a one-element array. `asChild` resolves its target with // `Children.only`, which then finds a list instead of an element and renders // nothing at all: every row of the account menu vanished this way while the // menu itself opened correctly and no error was logged. let hasText = false; Children.forEach(children, (child) => { if (typeof child === 'string' || typeof child === 'number') hasText = true; }); if (!hasText) return children; return Children.map(children, (child) => ( typeof child === 'string' || typeof child === 'number' ? createElement(Wrap, props, child) : child )); };