Skip to content

Commit b278deb

Browse files
author
doong
committed
use-stylex/lite without Tailwind syntax
1 parent ab0cf75 commit b278deb

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

lite.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dist/lite';

lite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/lite');

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-stylex",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "",
55
"keywords": [],
66
"author": "",
@@ -11,7 +11,9 @@
1111
"files": [
1212
"dist",
1313
"tailwind.js",
14-
"tailwind.d.ts"
14+
"tailwind.d.ts",
15+
"lite.js",
16+
"lite.d.ts"
1517
],
1618
"publishConfig": {
1719
"access": "public"

src/lite.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as stylex from '@stylexjs/stylex';
2+
3+
type UseStyleXParams = {
4+
isWithAttrs?: boolean;
5+
};
6+
7+
export type Classes<T> = {
8+
[K in keyof T]: T[K] extends (...args: any[]) => any ? ((...args: Parameters<T[K]>) => {
9+
[key: string]: string
10+
}) : string;
11+
} & {
12+
getClass: (...args: (keyof T | T[keyof T])[]) => string;
13+
};
14+
15+
export const useStyleX = <T extends Record<string, any>>(xStyles: T, params: UseStyleXParams = {}): {
16+
classes: Classes<T>
17+
} => {
18+
const { isWithAttrs = false } = params;
19+
20+
const classKey = isWithAttrs ? 'class' : 'className';
21+
const funcKey = isWithAttrs ? 'attrs' : 'props';
22+
23+
const classes = {} as Partial<Classes<T>>;
24+
25+
Object.keys(xStyles).forEach((key) => {
26+
const value = xStyles[key];
27+
28+
if (typeof value === 'function') {
29+
(classes as any)[key as keyof T] = (...args: any[]) => {
30+
return (stylex as any)[funcKey](value(...args));
31+
};
32+
} else {
33+
const props = (stylex as any)[funcKey](value);
34+
classes[key as keyof T] = props[classKey];
35+
}
36+
});
37+
38+
classes.getClass = ((...args: (keyof T | T[keyof T])[]): string => {
39+
const styles = args.map((arg) => {
40+
if (typeof arg === 'string') {
41+
return xStyles[arg];
42+
}
43+
44+
return arg;
45+
});
46+
47+
const props: any = (stylex as any)[funcKey](...styles);
48+
return props[classKey];
49+
}) as Classes<T>['getClass'];
50+
51+
return {
52+
classes: classes as Classes<T>
53+
}
54+
};
55+
56+
export default useStyleX

0 commit comments

Comments
 (0)