Skip to content

Commit 12ea4f6

Browse files
author
John Richard Chipps-Harding
authored
Type Tweaks (#19)
* Type Tweaks * typo * another typo
1 parent 12a8a25 commit 12ea4f6

File tree

7 files changed

+21
-21
lines changed

7 files changed

+21
-21
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ By default variant values do not end up propagating to the final DOM element. Th
223223
In the following example, `readOnly` is an intrinsic HTML attribute that we both want to style, but also continue to pass through to the DOM element.
224224

225225
```tsx
226-
import { CSSComponentPropType } from "@phntms/css-components";
226+
import { styled } from "@phntms/css-components";
227227
import css from "./styles.module.css";
228228

229229
const Input = styled("input", {
@@ -242,7 +242,7 @@ const Input = styled("input", {
242242
We have included a helper that allows you to access the types of the variants you have defined.
243243

244244
```tsx
245-
import { CSSComponentPropType } from "@phntms/css-components";
245+
import { VariantProps } from "@phntms/css-components";
246246
import css from "./styles.module.css";
247247

248248
const Button = styled("button", {
@@ -252,7 +252,8 @@ const Button = styled("button", {
252252
},
253253
});
254254

255-
type ButtonTypes = CSSComponentPropType<typeof Button, "primary">;
255+
type ButtonVariants = VariantProps<typeof Button>;
256+
type PrimaryType = ButtonVariants["primary"];
256257
```
257258

258259
## CLI Tool (Experimental)

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@phntms/css-components",
33
"description": "At its core, css-components is a simple wrapper around standard CSS. It allows you to write your CSS how you wish then compose them into a component ready to be used in React.",
4-
"version": "0.1.3",
4+
"version": "0.2.0",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"homepage": "https://github.com/phantomstudios/css-components#readme",

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { createElement, forwardRef } from "react";
22

33
import {
44
CSSComponentConfig,
5-
cssType,
5+
CSS,
66
PolymorphicComponent,
77
variantsType,
88
} from "./type";
99
import { findMatchingCompoundVariants, flattenCss } from "./utils";
1010

11-
export { CSSComponentConfig, CSSComponentPropType } from "./type";
11+
export { CSSComponentConfig, CSS, VariantProps } from "./type";
1212

1313
export const styled = <
1414
V extends variantsType | object,
@@ -43,7 +43,7 @@ export const styled = <
4343
if (variant && variant.hasOwnProperty(mergedProps[key])) {
4444
const selector = variant[
4545
mergedProps[key] as keyof typeof variant
46-
] as cssType;
46+
] as CSS;
4747
componentStyles.push(flattenCss(selector));
4848
}
4949
}

src/type.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type PolymorphicComponent<
6161
* The CSS Component Config type.
6262
*/
6363
export interface CSSComponentConfig<V> {
64-
css?: cssType;
64+
css?: CSS;
6565
variants?: V;
6666
compoundVariants?: CompoundVariantType<V>[];
6767
defaultVariants?: {
@@ -73,24 +73,23 @@ export interface CSSComponentConfig<V> {
7373
/**
7474
* Allows you to extract a type for variant values.
7575
*/
76-
export type CSSComponentPropType<
76+
export type VariantProps<
7777
// eslint-disable-next-line @typescript-eslint/no-explicit-any
78-
C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
79-
P extends keyof React.ComponentProps<C>
80-
> = React.ComponentProps<C>[P];
78+
C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>
79+
> = React.ComponentProps<C>;
8180

8281
/**
8382
* CSS can be passed in as either a string or an array of strings.
8483
*/
85-
export type cssType = string | string[];
84+
export type CSS = string | string[];
8685

8786
export type variantValue = string | number | boolean | string[];
8887

8988
/**
9089
* An object of variants, and how they map to CSS styles
9190
*/
9291
export type variantsType = Partial<{
93-
[key: string]: { [key: string | number]: cssType };
92+
[key: string]: { [key: string | number]: CSS };
9493
}>;
9594

9695
/**
@@ -111,5 +110,5 @@ export type VariantOptions<V> = {
111110
* Returns a type object for compound variants.
112111
*/
113112
export type CompoundVariantType<V> = VariantOptions<V> & {
114-
css: cssType;
113+
css: CSS;
115114
};

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cssType, variantValue } from "./type";
1+
import { CSS, variantValue } from "./type";
22

33
export const findMatchingCompoundVariants = (
44
compoundVariants: {
@@ -14,5 +14,5 @@ export const findMatchingCompoundVariants = (
1414
)
1515
);
1616

17-
export const flattenCss = (css: cssType) =>
17+
export const flattenCss = (css: CSS) =>
1818
Array.isArray(css) ? css.join(" ") : css;

test/index.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import React from "react";
77
import { render } from "@testing-library/react";
88
import { expectTypeOf } from "expect-type";
99

10-
import { CSSComponentPropType, styled } from "../src";
10+
import { VariantProps, styled } from "../src";
1111

1212
describe("Basic functionality", () => {
1313
it("should return the correct type of DOM node", async () => {
@@ -278,7 +278,7 @@ describe("supports more exotic setups", () => {
278278
variants: { primary: { true: "primary" } },
279279
});
280280

281-
type primaryType = CSSComponentPropType<typeof Button, "primary">;
281+
type primaryType = VariantProps<typeof Button>["primary"];
282282

283283
expectTypeOf<primaryType>().toMatchTypeOf<boolean | undefined>();
284284
});

0 commit comments

Comments
 (0)