UNPKG

@deepsquare/turbine

Version:

Zero-dependencies efficient wrapper to build Tailwind-based React components easily.

1 lines 1.98 kB
{"mappings":";AAGA,yBAAyB;KACtB,CAAC,IAAI,MAAM,IAAI,iBAAiB,GAAG,OAAO,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;CAC1G,CAAC;AAEF;;;;;;;;;;GAUG;AACH,gCAAgC,CAAC,SAAS,MAAM,iBAAiB,EAC/D,EAAE,EAAE,CAAC,EACL,SAAS,EAAE,MAAM,EACjB,YAAY,CAAC,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,GACrD,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAe1B","sources":["src/src/turbine.tsx","src/turbine.tsx"],"sourcesContent":[null,"import type { FC } from 'react';\nimport { useMemo } from 'react';\n\ntype ElementsWithClass = {\n [K in keyof JSX.IntrinsicElements]: Extract<JSX.IntrinsicElements[K], { className?: string | undefined }>;\n};\n\n/**\n * Build a turbine component based a the `T` HTML tag.\n * @param {T} As The HTML tag.\n * @param {string} className The class to apply.\n * @param {Omit<JSX.IntrinsicElements[T], \"className\">} defaultProps The default props to pass to the component.\n * @returns {React.FC<JSX.IntrinsicElements[T]>} The \"turbine\" component.\n *\n * @example Bootstrap-like cards:\n * const Card = turbine('div', 'px-6 py-4 rounded bg-grey-200');\n * export default Card\n */\nexport default function turbine<T extends keyof ElementsWithClass>(\n As: T,\n className: string,\n defaultProps?: Omit<ElementsWithClass[T], 'className'>,\n): FC<ElementsWithClass[T]> {\n const inner: FC<ElementsWithClass[T]> = ({ className: classNameProp, ...props }) => {\n // Merge the two class definitions.\n // We chose to not use the famous `classnames` package here since the merge is fairly simple.\n const mergedClassNames = useMemo(() => {\n return `${className} ${classNameProp ?? ''}`.trim().replace(/ +/, ' ');\n }, [classNameProp]);\n\n // @ts-ignore Too complex for TypeScript, but it's good, trust us!\n return <As {...defaultProps} {...props} className={mergedClassNames} />;\n };\n\n inner.displayName = `pure_${As}`;\n\n return inner;\n}\n"],"names":[],"version":3,"file":"turbine.d.ts.map"}