classname-variants 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/example.tsx +11 -1
- package/lib/react.d.ts +18 -5
- package/lib/react.js +7 -3
- package/package.json +3 -2
- package/src/react.ts +30 -14
package/README.md
CHANGED
|
@@ -138,7 +138,8 @@ const buttonProps = variantProps({
|
|
|
138
138
|
This way a compontents' props (or part of them) can be directly spread into the target element. All variant-related props are used to construct the `className` property while all other props are passed through verbatim:
|
|
139
139
|
|
|
140
140
|
```tsx
|
|
141
|
-
type Props = SX.IntrinsicElements["button"] &
|
|
141
|
+
type Props = SX.IntrinsicElements["button"] &
|
|
142
|
+
VariantPropsOf<typeof buttonProps>;
|
|
142
143
|
|
|
143
144
|
function Button(props: Props) {
|
|
144
145
|
return <button {...buttonProps(props)} />;
|
|
@@ -170,6 +171,8 @@ const Button = styled("button", {
|
|
|
170
171
|
});
|
|
171
172
|
```
|
|
172
173
|
|
|
174
|
+
You can also style other custom React components as long as the accept a `className` prop.
|
|
175
|
+
|
|
173
176
|
# Tailwind IntelliSense
|
|
174
177
|
|
|
175
178
|
In order to get auto-completion for the CSS classes themselves, you can use the [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) plugin for VS Code. In order to make it recognize the strings inside your variants config, you have to somehow mark them and configure the plugin accordingly.
|
package/example.tsx
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import React from "react";
|
|
1
|
+
import React, { createElement } from "react";
|
|
2
2
|
import ReactDOM from "react-dom";
|
|
3
3
|
import { styled } from "./src/react";
|
|
4
4
|
|
|
5
|
+
function CustomComponent(props: { className?: string }) {
|
|
6
|
+
return <div {...props}>I'm a custom component!</div>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const Card = styled(CustomComponent, {
|
|
10
|
+
base: "bg-white p-4 border-2 rounded-lg",
|
|
11
|
+
variants: {},
|
|
12
|
+
});
|
|
13
|
+
|
|
5
14
|
const Button = styled("button", {
|
|
6
15
|
base: "px-5 py-2 text-white disabled:bg-gray-400 disabled:text-gray-300",
|
|
7
16
|
variants: {
|
|
@@ -39,6 +48,7 @@ function App() {
|
|
|
39
48
|
<Button color="accent" disabled>
|
|
40
49
|
Disabled
|
|
41
50
|
</Button>
|
|
51
|
+
<Card />
|
|
42
52
|
</div>
|
|
43
53
|
);
|
|
44
54
|
}
|
package/lib/react.d.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import { ComponentProps, ElementType, ForwardRefExoticComponent, PropsWithoutRef } from "react";
|
|
2
2
|
import { Variants, VariantsConfig, VariantOptions, Simplify } from ".";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Utility type to infer the first argument of a variantProps function.
|
|
5
|
+
*/
|
|
6
|
+
export declare type VariantPropsOf<T> = T extends (props: infer P) => any ? P : never;
|
|
7
|
+
/**
|
|
8
|
+
* Type for the variantProps() argument – consists of the VariantOptions and an optional className for chaining.
|
|
9
|
+
*/
|
|
10
|
+
declare type VariantProps<C extends VariantsConfig<V>, V extends Variants = C["variants"]> = VariantOptions<C> & {
|
|
11
|
+
className?: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function variantProps<C extends VariantsConfig<V>, V extends Variants = C["variants"]>(config: Simplify<C>): <P extends VariantProps<C, C["variants"]>>(props: P) => {
|
|
5
14
|
className: string;
|
|
6
|
-
} & Omit<
|
|
7
|
-
declare type StyledComponent<
|
|
8
|
-
|
|
15
|
+
} & Omit<P, keyof C["variants"]>;
|
|
16
|
+
declare type StyledComponent<T extends ElementType<{
|
|
17
|
+
className: string;
|
|
18
|
+
}>, C extends VariantsConfig<V>, V extends Variants = C["variants"]> = ForwardRefExoticComponent<PropsWithoutRef<ComponentProps<T> & VariantOptions<C>> & React.RefAttributes<T>>;
|
|
19
|
+
export declare function styled<T extends ElementType<{
|
|
20
|
+
className: string;
|
|
21
|
+
}>, C extends VariantsConfig<V>, V extends Variants = C["variants"]>(type: T, config: Simplify<C>): StyledComponent<T, C>;
|
|
9
22
|
export {};
|
package/lib/react.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { createElement, forwardRef, } from "react";
|
|
2
2
|
import { variants, } from ".";
|
|
3
3
|
export function variantProps(config) {
|
|
4
|
-
const
|
|
4
|
+
const variantClassName = variants(config);
|
|
5
5
|
return (props) => {
|
|
6
|
-
const
|
|
7
|
-
|
|
6
|
+
const result = {};
|
|
7
|
+
// Pass-through all unrelated props
|
|
8
8
|
for (let prop in props) {
|
|
9
9
|
if (config.variants && !(prop in config.variants)) {
|
|
10
10
|
result[prop] = props[prop];
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
+
// Add the optionally passed className prop for chaining
|
|
14
|
+
result.className = [props.className, variantClassName(props)]
|
|
15
|
+
.filter(Boolean)
|
|
16
|
+
.join(" ");
|
|
13
17
|
return result;
|
|
14
18
|
};
|
|
15
19
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "classname-variants",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Variant API for plain class names",
|
|
5
5
|
"author": "Felix Gnass <fgnass@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc",
|
|
33
|
-
"start": "npx vite"
|
|
33
|
+
"start": "npx vite",
|
|
34
|
+
"prepublishOnly": "npm run build"
|
|
34
35
|
},
|
|
35
36
|
"keywords": [
|
|
36
37
|
"tailwind",
|
package/src/react.ts
CHANGED
|
@@ -15,43 +15,59 @@ import {
|
|
|
15
15
|
Simplify,
|
|
16
16
|
} from ".";
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Utility type to infer the first argument of a variantProps function.
|
|
20
|
+
*/
|
|
21
|
+
export type VariantPropsOf<T> = T extends (props: infer P) => any ? P : never;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Type for the variantProps() argument – consists of the VariantOptions and an optional className for chaining.
|
|
25
|
+
*/
|
|
26
|
+
type VariantProps<
|
|
27
|
+
C extends VariantsConfig<V>,
|
|
28
|
+
V extends Variants = C["variants"]
|
|
29
|
+
> = VariantOptions<C> & { className?: string };
|
|
21
30
|
|
|
22
31
|
export function variantProps<
|
|
23
32
|
C extends VariantsConfig<V>,
|
|
24
33
|
V extends Variants = C["variants"]
|
|
25
34
|
>(config: Simplify<C>) {
|
|
26
|
-
const
|
|
27
|
-
return <
|
|
28
|
-
const
|
|
29
|
-
|
|
35
|
+
const variantClassName = variants<C>(config);
|
|
36
|
+
return <P extends VariantProps<C>>(props: P) => {
|
|
37
|
+
const result: any = {};
|
|
38
|
+
|
|
39
|
+
// Pass-through all unrelated props
|
|
30
40
|
for (let prop in props) {
|
|
31
41
|
if (config.variants && !(prop in config.variants)) {
|
|
32
42
|
result[prop] = props[prop];
|
|
33
43
|
}
|
|
34
44
|
}
|
|
35
|
-
|
|
45
|
+
|
|
46
|
+
// Add the optionally passed className prop for chaining
|
|
47
|
+
result.className = [props.className, variantClassName(props)]
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.join(" ");
|
|
50
|
+
|
|
51
|
+
return result as { className: string } & Omit<P, keyof C["variants"]>;
|
|
36
52
|
};
|
|
37
53
|
}
|
|
38
54
|
|
|
39
55
|
type StyledComponent<
|
|
40
|
-
|
|
56
|
+
T extends ElementType<{ className: string }>,
|
|
41
57
|
C extends VariantsConfig<V>,
|
|
42
58
|
V extends Variants = C["variants"]
|
|
43
59
|
> = ForwardRefExoticComponent<
|
|
44
|
-
PropsWithoutRef<ComponentProps<
|
|
45
|
-
React.RefAttributes<
|
|
60
|
+
PropsWithoutRef<ComponentProps<T> & VariantOptions<C>> &
|
|
61
|
+
React.RefAttributes<T>
|
|
46
62
|
>;
|
|
47
63
|
|
|
48
64
|
export function styled<
|
|
49
|
-
|
|
65
|
+
T extends ElementType<{ className: string }>,
|
|
50
66
|
C extends VariantsConfig<V>,
|
|
51
67
|
V extends Variants = C["variants"]
|
|
52
|
-
>(type:
|
|
68
|
+
>(type: T, config: Simplify<C>): StyledComponent<T, C> {
|
|
53
69
|
const styledProps = variantProps(config);
|
|
54
|
-
return forwardRef<
|
|
70
|
+
return forwardRef<T, ComponentProps<T> & VariantOptions<C>>((props, ref) =>
|
|
55
71
|
createElement(type, { ...styledProps(props), ref })
|
|
56
72
|
);
|
|
57
73
|
}
|