classname-variants 1.2.0 → 1.2.1

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/example.tsx CHANGED
@@ -12,7 +12,9 @@ function CustomComponent({
12
12
  return <div {...props}>{title}</div>;
13
13
  }
14
14
 
15
- const Card = styled(CustomComponent, "bg-white p-4 border-2 rounded-lg");
15
+ const Card = styled("div", "bg-white p-4 border-2 rounded-lg");
16
+
17
+ const TitleCard = styled(CustomComponent, "bg-white p-4 border-2 rounded-lg");
16
18
 
17
19
  const Button = styled("button", {
18
20
  base: "px-5 py-2 text-white disabled:bg-gray-400 disabled:text-gray-300",
@@ -51,7 +53,11 @@ function App() {
51
53
  <Button color="accent" disabled>
52
54
  Disabled
53
55
  </Button>
54
- <Card title="Hello" />
56
+ <TitleCard title="Hello" />
57
+ <Card>
58
+ <h1>Hello</h1>
59
+ <p>world</p>
60
+ </Card>
55
61
  </div>
56
62
  );
57
63
  }
@@ -0,0 +1 @@
1
+ export {};
package/lib/example.js ADDED
@@ -0,0 +1,45 @@
1
+ import React from "react";
2
+ import ReactDOM from "react-dom";
3
+ import { styled } from "./src/react";
4
+ function CustomComponent({ title, ...props }) {
5
+ return React.createElement("div", { ...props }, title);
6
+ }
7
+ const Card = styled("div", "bg-white p-4 border-2 rounded-lg");
8
+ const TitleCard = styled(CustomComponent, "bg-white p-4 border-2 rounded-lg");
9
+ const Button = styled("button", {
10
+ base: "px-5 py-2 text-white disabled:bg-gray-400 disabled:text-gray-300",
11
+ variants: {
12
+ color: {
13
+ neutral: "bg-slate-500 hover:bg-slate-400",
14
+ accent: "bg-teal-500 hover:bg-teal-400",
15
+ },
16
+ outlined: {
17
+ true: "border-2",
18
+ },
19
+ rounded: {
20
+ true: "rounded-full",
21
+ false: "rounded-sm",
22
+ },
23
+ },
24
+ compoundVariants: [
25
+ {
26
+ variants: { color: "accent", outlined: true },
27
+ className: "border-teal-600",
28
+ },
29
+ ],
30
+ defaultVariants: {
31
+ color: "neutral",
32
+ },
33
+ });
34
+ function App() {
35
+ return (React.createElement("div", { className: "flex justify-center items-center pt-8 gap-4 flex-wrap" },
36
+ React.createElement(Button, { onClick: console.log }, "Accent"),
37
+ React.createElement(Button, { rounded: true }, "Neutral + Rounded"),
38
+ React.createElement(Button, { color: "accent", outlined: true }, "Accent + Outlined"),
39
+ React.createElement(Button, { color: "accent", disabled: true }, "Disabled"),
40
+ React.createElement(TitleCard, { title: "Hello" }),
41
+ React.createElement(Card, null,
42
+ React.createElement("h1", null, "Hello"),
43
+ React.createElement("p", null, "world"))));
44
+ }
45
+ ReactDOM.render(React.createElement(App, null), document.getElementById("root"));
package/lib/react.d.ts CHANGED
@@ -14,5 +14,6 @@ export declare function variantProps<C extends VariantsConfig<V>, V extends Vari
14
14
  className: string;
15
15
  } & Omit<P, keyof C["variants"]>;
16
16
  declare type StyledComponent<T extends ElementType, C extends VariantsConfig<V>, V extends Variants = C["variants"]> = ForwardRefExoticComponent<PropsWithoutRef<ComponentProps<T> & VariantOptions<C>> & React.RefAttributes<T>>;
17
- export declare function styled<T extends ElementType, C extends VariantsConfig<V>, V extends Variants = C["variants"]>(type: T, config: string | Simplify<C>): StyledComponent<T, C>;
17
+ declare type VariantsOf<T> = T extends VariantsConfig<infer V> ? V : {};
18
+ export declare function styled<T extends ElementType, C extends VariantsConfig<V>, V extends Variants = VariantsOf<C>>(type: T, config: string | Simplify<C>): StyledComponent<T, C>;
18
19
  export {};
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Definition of the available variants and their options.
3
+ * @example
4
+ * {
5
+ * color: {
6
+ * white: "bg-white"
7
+ * green: "bg-green-500",
8
+ * },
9
+ * size: {
10
+ * small: "text-xs",
11
+ * large: "text-lg"
12
+ * }
13
+ * }
14
+ */
15
+ export declare type Variants = Record<string, Record<string, string>>;
16
+ /**
17
+ * Configuration including defaults and compound variants.
18
+ */
19
+ export interface VariantsConfig<V extends Variants> {
20
+ base?: string;
21
+ variants: V;
22
+ compoundVariants?: CompoundVariant<V>[];
23
+ defaultVariants?: Partial<OptionsOf<V>>;
24
+ }
25
+ /**
26
+ * Rules for class names that are applied for certain variant combinations.
27
+ */
28
+ export interface CompoundVariant<V extends Variants> {
29
+ variants: Partial<OptionsOf<V>>;
30
+ className: string;
31
+ }
32
+ /**
33
+ * Only the boolean variants, i.e. ones that have "true" or "false" as options.
34
+ */
35
+ declare type BooleanVariants<V extends Variants> = {
36
+ [K in keyof V as V[K] extends {
37
+ true: any;
38
+ } | {
39
+ false: any;
40
+ } ? K : never]: V[K];
41
+ };
42
+ /**
43
+ * Only the variants for which a default options is set.
44
+ */
45
+ declare type DefaultVariants<C extends VariantsConfig<V>, V extends Variants = C["variants"]> = {
46
+ [K in keyof V as K extends keyof C["defaultVariants"] ? K : never]: C["variants"][K];
47
+ };
48
+ /**
49
+ * Names of all optional variants, i.e. booleans or ones with default options.
50
+ */
51
+ declare type OptionalVariantNames<C extends VariantsConfig<V>, V extends Variants = C["variants"]> = keyof BooleanVariants<V> | keyof DefaultVariants<C>;
52
+ /**
53
+ * Possible options for all the optional variants.
54
+ *
55
+ * @example
56
+ * {
57
+ * color?: "white" | "green",
58
+ * rounded?: boolean | undefined
59
+ * }
60
+ */
61
+ declare type OptionalOptions<C extends VariantsConfig<V>, V extends Variants = C["variants"]> = {
62
+ [K in keyof V as K extends OptionalVariantNames<C> ? K : never]?: OptionsOf<V>[K];
63
+ };
64
+ /**
65
+ * Possible options for all required variants.
66
+ *
67
+ * @example {
68
+ * size: "small" | "large"
69
+ * }
70
+ */
71
+ declare type RequiredOptions<C extends VariantsConfig<V>, V extends Variants = C["variants"]> = {
72
+ [K in keyof V as K extends OptionalVariantNames<C> ? never : K]: OptionsOf<V>[K];
73
+ };
74
+ /**
75
+ * Utility type to extract the possible options.
76
+ * Converts "true" | "false" options into booleans.
77
+ *
78
+ * @example
79
+ * OptionsOf<{
80
+ * size: { small: "text-xs"; large: "text-lg" };
81
+ * rounded: { true: "rounded-full" }
82
+ * }>
83
+ * ==>
84
+ * {
85
+ * size: "text-xs" | "text-lg";
86
+ * rounded: boolean;
87
+ * }
88
+ */
89
+ declare type OptionsOf<V extends Variants> = {
90
+ [K in keyof V]: keyof V[K] extends "true" | "false" ? boolean : keyof V[K];
91
+ };
92
+ /**
93
+ * Extracts the possible options.
94
+ */
95
+ export declare type VariantOptions<C extends VariantsConfig<V>, V extends Variants = C["variants"]> = RequiredOptions<C> & OptionalOptions<C>;
96
+ /**
97
+ * Without this conversion step, defaultVariants and compoundVariants will
98
+ * allow extra keys, i.e. non-existent variants.
99
+ * See https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts
100
+ */
101
+ export declare type Simplify<T> = {
102
+ [K in keyof T]: T[K];
103
+ };
104
+ export declare function variants<C extends VariantsConfig<V>, V extends Variants = C["variants"]>(config: Simplify<C>): (props: VariantOptions<C>) => string;
105
+ export {};
@@ -0,0 +1,27 @@
1
+ export function variants(config) {
2
+ const { base, variants, compoundVariants, defaultVariants } = config;
3
+ const isBooleanVariant = (name) => {
4
+ const v = variants === null || variants === void 0 ? void 0 : variants[name];
5
+ return v && ("false" in v || "true" in v);
6
+ };
7
+ return (props) => {
8
+ var _a;
9
+ const res = [base];
10
+ const getSelected = (name) => {
11
+ var _a, _b;
12
+ return (_b = (_a = props[name]) !== null && _a !== void 0 ? _a : defaultVariants === null || defaultVariants === void 0 ? void 0 : defaultVariants[name]) !== null && _b !== void 0 ? _b : (isBooleanVariant(name) ? false : undefined);
13
+ };
14
+ for (let name in variants) {
15
+ const selected = getSelected(name);
16
+ if (selected !== undefined)
17
+ res.push((_a = variants[name]) === null || _a === void 0 ? void 0 : _a[selected]);
18
+ }
19
+ for (let { variants, className } of compoundVariants !== null && compoundVariants !== void 0 ? compoundVariants : []) {
20
+ const isSelected = (name) => getSelected(name) === variants[name];
21
+ if (Object.keys(variants).every(isSelected)) {
22
+ res.push(className);
23
+ }
24
+ }
25
+ return res.filter(Boolean).join(" ");
26
+ };
27
+ }
@@ -0,0 +1,19 @@
1
+ import { ComponentProps, ElementType, ForwardRefExoticComponent, PropsWithoutRef } from "react";
2
+ import { Variants, VariantsConfig, VariantOptions, Simplify } from "./index.js";
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) => {
14
+ className: string;
15
+ } & Omit<P, keyof C["variants"]>;
16
+ declare type StyledComponent<T extends ElementType, C extends VariantsConfig<V>, V extends Variants = C["variants"]> = ForwardRefExoticComponent<PropsWithoutRef<ComponentProps<T> & VariantOptions<C>> & React.RefAttributes<T>>;
17
+ declare type Vars<T> = T extends VariantsConfig<infer V> ? V : {};
18
+ export declare function styled<T extends ElementType, C extends VariantsConfig<V>, V extends Variants = Vars<C>>(type: T, config: string | Simplify<C>): StyledComponent<T, C>;
19
+ export {};
@@ -0,0 +1,25 @@
1
+ import { createElement, forwardRef, } from "react";
2
+ import { variants, } from "./index.js";
3
+ export function variantProps(config) {
4
+ const variantClassName = variants(config);
5
+ return (props) => {
6
+ const result = {};
7
+ // Pass-through all unrelated props
8
+ for (let prop in props) {
9
+ if (config.variants && !(prop in config.variants)) {
10
+ result[prop] = props[prop];
11
+ }
12
+ }
13
+ // Add the optionally passed className prop for chaining
14
+ result.className = [props.className, variantClassName(props)]
15
+ .filter(Boolean)
16
+ .join(" ");
17
+ return result;
18
+ };
19
+ }
20
+ export function styled(type, config) {
21
+ const styledProps = typeof config === "string"
22
+ ? variantProps({ base: config, variants: {} })
23
+ : variantProps(config);
24
+ return forwardRef((props, ref) => createElement(type, { ...styledProps(props), ref }));
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "classname-variants",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Variant API for plain class names",
5
5
  "author": "Felix Gnass <fgnass@gmail.com>",
6
6
  "license": "MIT",
@@ -44,6 +44,6 @@
44
44
  "@types/react": "^17.0.39",
45
45
  "react": "^17.0.2",
46
46
  "react-dom": "^17.0.2",
47
- "typescript": "^4.5.5"
47
+ "typescript": "^4.6.4"
48
48
  }
49
49
  }
package/src/react.ts CHANGED
@@ -61,10 +61,12 @@ type StyledComponent<
61
61
  React.RefAttributes<T>
62
62
  >;
63
63
 
64
+ type VariantsOf<T> = T extends VariantsConfig<infer V> ? V : {};
65
+
64
66
  export function styled<
65
67
  T extends ElementType,
66
68
  C extends VariantsConfig<V>,
67
- V extends Variants = C["variants"]
69
+ V extends Variants = VariantsOf<C>
68
70
  >(type: T, config: string | Simplify<C>): StyledComponent<T, C> {
69
71
  const styledProps =
70
72
  typeof config === "string"