aboutsummaryrefslogtreecommitdiff
path: root/packages/ui/src/utils.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/ui/src/utils.tsx')
-rw-r--r--packages/ui/src/utils.tsx28
1 files changed, 28 insertions, 0 deletions
diff --git a/packages/ui/src/utils.tsx b/packages/ui/src/utils.tsx
new file mode 100644
index 0000000..5ccb488
--- /dev/null
+++ b/packages/ui/src/utils.tsx
@@ -0,0 +1,28 @@
+import clsx from 'clsx';
+import React from 'react';
+
+const twFactory =
+ (element: any) =>
+ ([newClassNames, ..._]: TemplateStringsArray) =>
+ React.forwardRef(({ className, ...props }: any, ref) =>
+ React.createElement(element, {
+ ...props,
+ className: clsx(newClassNames, className),
+ ref
+ })
+ );
+
+type ClassnameFactory<T> = (s: TemplateStringsArray) => T;
+
+type TailwindFactory = {
+ [K in keyof JSX.IntrinsicElements]: ClassnameFactory<
+ React.ForwardRefExoticComponent<JSX.IntrinsicElements[K]>
+ >;
+} & {
+ <T>(c: T): ClassnameFactory<T>;
+};
+
+export const tw = new Proxy((() => {}) as unknown as TailwindFactory, {
+ get: (_, property: string) => twFactory(property),
+ apply: (_, __, [el]: [React.ReactElement]) => twFactory(el)
+});