'use client' import { XStack } from '@hanzo/gui'; import { debounce } from 'es-toolkit'; import * as React from 'react'; import { ink } from './ink'; import type { InputProps } from './input'; // The adornment widths must be applied before the browser paints, or the field // renders one frame with default padding and the text visibly jumps out from // under the icon. useLayoutEffect does that but has no meaning while rendering // on the server, where it only warns. const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect; export interface InputElementProps extends React.HTMLAttributes { readonly placement?: 'start' | 'end'; } export interface InputGroupProps extends React.HTMLAttributes { readonly startElementProps?: InputElementProps; readonly endElementProps?: InputElementProps; readonly startElement?: React.ReactNode; readonly endElement?: React.ReactNode; readonly children: React.ReactElement; readonly startOffset?: string | number; readonly endOffset?: string | number; } export const InputGroup = React.forwardRef( function InputGroup(props, ref) { const { startElement, startElementProps, endElement, endElementProps, children, startOffset, endOffset, className, ...rest } = props; const startElementRef = React.useRef(null); const endElementRef = React.useRef(null); const groupRef = React.useRef(null); const [ inlinePaddings, setInlinePaddings ] = React.useState<{ start?: number; end?: number }>(); const calculateInlinePaddings = React.useCallback(() => { const startWidth = startElementRef.current?.getBoundingClientRect().width ?? 0; const endWidth = endElementRef.current?.getBoundingClientRect().width ?? 0; setInlinePaddings({ start: startWidth, end: endWidth, }); }, []); React.useEffect(() => { if (!groupRef.current) return; let timeoutId: ReturnType; const intersectionObserver = new IntersectionObserver( (entries) => { const entry = entries[0]; if (entry && entry.isIntersecting) { // Small delay to ensure rendering is complete timeoutId = setTimeout(calculateInlinePaddings, 50); } }, { threshold: 0.01 }, ); intersectionObserver.observe(groupRef.current); return () => { intersectionObserver.disconnect(); if (timeoutId) { clearTimeout(timeoutId); } }; }, [ calculateInlinePaddings ]); useIsomorphicLayoutEffect(() => { calculateInlinePaddings(); const resizeHandler = debounce(calculateInlinePaddings, 300); const resizeObserver = new ResizeObserver(resizeHandler); if (groupRef.current) { resizeObserver.observe(groupRef.current); } return function cleanup() { resizeObserver.disconnect(); }; }, [ calculateInlinePaddings ]); // Combine refs for the wrapper div const combinedRef = React.useCallback((node: HTMLDivElement) => { groupRef.current = node; if (typeof ref === 'function') { ref(node); } else if (ref) { ref.current = node; } }, [ ref ]); const { className: startClassName, ...startRest } = startElementProps ?? {}; const { className: endClassName, ...endRest } = endElementProps ?? {}; return ( { startElement && ( { ink(startElement) } ) } { React.Children.map(children, (child: React.ReactElement) => { if (!React.isValidElement(child)) { return child; } // The adornments are absolutely positioned over the field, so the // control has to be inset by their measured width or the text starts // underneath them. Written as inline padding rather than the `ps`/`pe` // style props this once carried: those are Chakra's, and Input is now // a Tailwind/gui component that spreads unknown props onto the DOM, // so they applied nothing and the icon sat on the placeholder. const start = startElement ? startOffset ?? inlinePaddings?.start : undefined; const end = endElement ? endOffset ?? inlinePaddings?.end : undefined; const px = (v: string | number) => typeof v === 'number' ? `${ v }px` : v; return React.cloneElement(child, { style: { ...child.props.style, ...(start !== undefined && { paddingInlineStart: px(start) }), ...(end !== undefined && { paddingInlineEnd: px(end) }), }, }); }) } { endElement && ( { ink(endElement) } ) } ); }, ); InputGroup.displayName = 'InputGroup';