From 69cc90730c4119cb995a805dfd9ed1c8b1669d97 Mon Sep 17 00:00:00 2001
From: Alex <8125011+alex-kinokon@users.noreply.github.com>
Date: Sat, 29 Jun 2024 12:28:04 -0400
Subject: [PATCH] Support data object
---
package.json | 2 +-
src/babel-tailwind.ts | 8 +++++++
src/classed.tsx | 49 +++++++++++++++++++++++++++++++++----------
src/index.test.ts | 10 +++++++--
src/index.ts | 7 ++++++-
5 files changed, 61 insertions(+), 15 deletions(-)
diff --git a/package.json b/package.json
index 9a2937f..e2336aa 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@aet/tailwind",
- "version": "0.0.1-beta.14",
+ "version": "0.0.1-beta.15",
"main": "dist/index.js",
"license": "MIT",
"scripts": {
diff --git a/src/babel-tailwind.ts b/src/babel-tailwind.ts
index 0085402..2f031cd 100644
--- a/src/babel-tailwind.ts
+++ b/src/babel-tailwind.ts
@@ -86,6 +86,14 @@ export function babelTailwind(
if (isPlainObject(value)) {
return Object.entries(value).flatMap(([modifier, classes]) => {
+ if (modifier === "data" && isPlainObject(classes)) {
+ return Object.entries(classes as object).flatMap(([key, cls]) =>
+ trim(cls)
+ .split(" ")
+ .map(value => `${modifier}-[${key}]:${value}`)
+ );
+ }
+
if (typeof classes !== "string") {
throw new Error(`Value for "${modifier}" should be a string`);
}
diff --git a/src/classed.tsx b/src/classed.tsx
index 3f8c724..97099b6 100644
--- a/src/classed.tsx
+++ b/src/classed.tsx
@@ -5,27 +5,54 @@ interface WithClassName
extends FunctionComponent
{
className: string;
}
+type InputProps = React.DetailedHTMLProps<
+ React.InputHTMLAttributes,
+ HTMLInputElement
+>;
+
export const classed: {
(
type: "input",
- className: string | string[]
- ): WithClassName<
- React.DetailedHTMLProps, HTMLInputElement>
- >;
+ className: string | string[],
+ defaultProps?: Partial
+ ): InputProps;
(
type: K,
- className: string | string[]
- ): WithClassName;
+ className: string | string[],
+ defaultProps?: Partial
+ ): JSX.IntrinsicElements[K];
(
type: string,
- className: string | string[]
+ className: string | string[],
+ defaultProps?: Record
): WithClassName & React.DOMAttributes>;
- (type: FunctionComponent
, className: string | string[]): WithClassName
;
-
(type: React.ComponentClass
, className: string | string[]): WithClassName
;
-} = (Component: any, classNameInput: string | (string | false)[]) => {
+
(
+ type: FunctionComponent
,
+ className: string | string[],
+ defaultProps?: Partial
+ ): WithClassName
;
+
(
+ type: React.ComponentClass
,
+ className: string | string[],
+ defaultProps?: Partial
+ ): WithClassName
;
+} = (
+ Component: any,
+ classNameInput: string | string[],
+ defaultProps?: Record
+) => {
const className = cx(classNameInput);
const component: any = forwardRef(({ className: cls, ...props }, ref) => (
-
+ cx(className, cls(...args))
+ : cx(className, cls)
+ }
+ />
));
component.className = className;
return component;
diff --git a/src/index.test.ts b/src/index.test.ts
index ed48e3f..2daa6e0 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -167,12 +167,15 @@ describe("babel-tailwind", () => {
export default tw("text-sm", \`flex\`, {
"group-hover": "text-center",
"[&>div]": \`font-semibold\`,
+ data: {
+ "name='hello'": "text-right",
+ },
})
`,
});
const clsName = getClassName(
- "text-sm flex group-hover:text-center [&>div]:font-semibold"
+ "text-sm flex group-hover:text-center [&>div]:font-semibold data-[name='hello']:text-right"
);
expect(files.js.text).toContain(`= "${clsName}"`);
expect(files.css.text).toMatch(
@@ -185,6 +188,9 @@ describe("babel-tailwind", () => {
`.group:hover .${clsName} {`,
" text-align: center;",
"}",
+ `.${clsName}[data-name=hello] {`,
+ " text-align: right;",
+ "}",
`.${clsName} > div {`,
" font-weight: 600;",
"}",
@@ -192,7 +198,7 @@ describe("babel-tailwind", () => {
);
});
- it("supports grouped array css jsx attribute like tw function", async () => {
+ it("supports grouped array css attribute", async () => {
const { files } = await compileESBuild({
clsx: "emotion",
expectFiles: 2,
diff --git a/src/index.ts b/src/index.ts
index f240d23..3c06b9b 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -19,7 +19,12 @@ type GetClassName = (className: string) => string;
*/
export interface TailwindFunction {
(strings: TemplateStringsArray): string;
- (...args: (string | { [modifier: string]: string })[]): string;
+ (
+ ...args: (
+ | string
+ | ({ data?: { [key: string]: string } } & { [modifier: string]: string })
+ )[]
+ ): string;
}
export interface TailwindPluginOptions {