classname-variants 1.1.1 → 1.2.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 CHANGED
@@ -4,7 +4,7 @@ Stitches-like [variant API](https://stitches.dev/docs/variants) for plain class
4
4
 
5
5
  The library is framework agnostic and can be used with any kind of CSS flavor.
6
6
 
7
- It is especially useful though if used with [Tailwind](https://tailwindcss.com/) and React, as it provides some [dedicated helpers](#React) and even allows for a _styled-components_ like API, but with [class names instead of styles](#bonus-styled-components-but-with-class-names-)!
7
+ It is especially useful though if used with [Tailwind](https://tailwindcss.com/) or [CSS Modules](https://github.com/css-modules/css-modules) in cobmination with React, as it provides some [dedicated helpers](#React) and even allows for a _styled-components_ like API, but with [class names instead of styles](#bonus-styled-components-but-with-class-names-)!
8
8
 
9
9
  [![Edit classname-variants/react](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/classname-variants-react-3bzjl?fontsize=14&hidenavigation=1&theme=dark)
10
10
 
@@ -171,6 +171,22 @@ const Button = styled("button", {
171
171
  });
172
172
  ```
173
173
 
174
+ The same can be done with CSS modules:
175
+
176
+ ```tsx
177
+ import { styled } from "classname-variants/react";
178
+ import styles from "./styles.module.css";
179
+
180
+ const Button = styled("button", {
181
+ variants: {
182
+ size: {
183
+ small: styles.small,
184
+ large: styles.large,
185
+ },
186
+ },
187
+ });
188
+ ```
189
+
174
190
  You can also style other custom React components as long as the accept a `className` prop.
175
191
 
176
192
  # Tailwind IntelliSense
package/example.tsx CHANGED
@@ -12,10 +12,7 @@ function CustomComponent({
12
12
  return <div {...props}>{title}</div>;
13
13
  }
14
14
 
15
- const Card = styled(CustomComponent, {
16
- base: "bg-white p-4 border-2 rounded-lg",
17
- variants: {},
18
- });
15
+ const Card = styled(CustomComponent, "bg-white p-4 border-2 rounded-lg");
19
16
 
20
17
  const Button = styled("button", {
21
18
  base: "px-5 py-2 text-white disabled:bg-gray-400 disabled:text-gray-300",
package/lib/react.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ComponentProps, ElementType, ForwardRefExoticComponent, PropsWithoutRef } from "react";
2
- import { Variants, VariantsConfig, VariantOptions, Simplify } from ".";
2
+ import { Variants, VariantsConfig, VariantOptions, Simplify } from "./index.js";
3
3
  /**
4
4
  * Utility type to infer the first argument of a variantProps function.
5
5
  */
@@ -14,5 +14,5 @@ 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: Simplify<C>): StyledComponent<T, C>;
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>;
18
18
  export {};
package/lib/react.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createElement, forwardRef, } from "react";
2
- import { variants, } from ".";
2
+ import { variants, } from "./index.js";
3
3
  export function variantProps(config) {
4
4
  const variantClassName = variants(config);
5
5
  return (props) => {
@@ -18,6 +18,8 @@ export function variantProps(config) {
18
18
  };
19
19
  }
20
20
  export function styled(type, config) {
21
- const styledProps = variantProps(config);
21
+ const styledProps = typeof config === "string"
22
+ ? variantProps({ base: config, variants: {} })
23
+ : variantProps(config);
22
24
  return forwardRef((props, ref) => createElement(type, { ...styledProps(props), ref }));
23
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "classname-variants",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Variant API for plain class names",
5
5
  "author": "Felix Gnass <fgnass@gmail.com>",
6
6
  "license": "MIT",
package/src/react.ts CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  VariantsConfig,
14
14
  VariantOptions,
15
15
  Simplify,
16
- } from ".";
16
+ } from "./index.js";
17
17
 
18
18
  /**
19
19
  * Utility type to infer the first argument of a variantProps function.
@@ -65,8 +65,11 @@ export function styled<
65
65
  T extends ElementType,
66
66
  C extends VariantsConfig<V>,
67
67
  V extends Variants = C["variants"]
68
- >(type: T, config: Simplify<C>): StyledComponent<T, C> {
69
- const styledProps = variantProps(config);
68
+ >(type: T, config: string | Simplify<C>): StyledComponent<T, C> {
69
+ const styledProps =
70
+ typeof config === "string"
71
+ ? variantProps({ base: config, variants: {} })
72
+ : variantProps(config);
70
73
  return forwardRef<T, ComponentProps<T> & VariantOptions<C>>((props, ref) =>
71
74
  createElement(type, { ...styledProps(props), ref })
72
75
  );