UNPKG

@crossed/primitive

Version:

A universal & performant styling library for React Native, Next.js & React

50 lines (49 loc) 1.44 kB
import { jsx } from "react/jsx-runtime"; import { useRef, forwardRef, isValidElement, Children, cloneElement, useState } from "react"; import { InputProvider, useInputContext } from "./context"; import { composeEventHandlers } from "@crossed/core"; const createInputGroup = (Styled) => forwardRef((props, ref) => { const [states, setStates] = useState({ isActive: false, isFocus: false, isHover: false }); const inputRef = useRef(null); return /* @__PURE__ */ jsx( InputProvider, { states, setStates: (style) => setStates((old) => ({ ...old, ...style })), inputRef, children: /* @__PURE__ */ jsx(Slot, { ...props, children: /* @__PURE__ */ jsx(Styled, { ...props, ref }) }) } ); }); const Slot = ({ children, ...props }) => { const { setStates, states, inputRef } = useInputContext(); return isValidElement(children) && Children.count(children) === 1 ? cloneElement(children, { ...props, states, onPointerEnter: composeEventHandlers(props.onPointerEnter, () => { setStates({ isHover: true }); }), onPointerLeave: composeEventHandlers(props.onPointerLeave, () => { setStates({ isHover: false }); }), onPress: composeEventHandlers(() => { var _a; (_a = inputRef.current) == null ? void 0 : _a.focus(); }, props.onPress) }) : children; }; export { createInputGroup }; //# sourceMappingURL=InputGroup.js.map