Skip to content

Commit 00e9b9f

Browse files
John Richard Chipps-Hardingnattog
andauthored
Ability to inspect type (#9)
* Ability to inspect type * remove pants * Better docs * Update README.md Co-authored-by: Guy Purssell <[email protected]> * linting Co-authored-by: Guy Purssell <[email protected]>
1 parent 4e58c0a commit 00e9b9f

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ const StyledButton = styled(
149149

150150
## Other
151151

152+
### Array of Classes
153+
152154
Wherever you specify a css selector, you can also pass in an array of classes to help composing and reusing styles.
153155

154156
```tsx
@@ -163,6 +165,21 @@ const Link = styled("a", [shared.link, shared.fontNormal, css.root], {
163165
});
164166
```
165167

168+
### Type Helper
169+
170+
We have included a helper that allows you to access the types of the variants you have defined.
171+
172+
```tsx
173+
import { CSSComponentPropType } from "@phntms/css-components";
174+
import css from "./styles.module.css";
175+
176+
const Button = styled("button", css.baseButton, {
177+
primary: { true: css.primary },
178+
});
179+
180+
type ButtonTypes = CSSComponentPropType<typeof Button, "primary">;
181+
```
182+
166183
[npm-image]: https://img.shields.io/npm/v/@phntms/css-components.svg?style=flat-square&logo=react
167184
[npm-url]: https://npmjs.org/package/@phntms/css-components
168185
[npm-downloads-image]: https://img.shields.io/npm/dm/@phntms/css-components.svg

package-lock.json

Lines changed: 15 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: 2 additions & 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.0.4",
4+
"version": "0.0.5",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"homepage": "https://github.com/phantomstudios/css-components#readme",
@@ -55,6 +55,7 @@
5555
"eslint-plugin-prettier": "^4.2.1",
5656
"eslint-plugin-react": "^7.31.10",
5757
"eslint-plugin-react-hooks": "^4.6.0",
58+
"expect-type": "^0.15.0",
5859
"jest": "^29.3.1",
5960
"jest-environment-jsdom": "^29.3.1",
6061
"npm-run-all": "^4.1.5",

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { createElement, forwardRef } from "react";
1+
import { createElement, forwardRef, JSXElementConstructor } from "react";
2+
3+
export type CSSComponentPropType<
4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5+
C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
6+
P extends keyof React.ComponentProps<C>
7+
> = React.ComponentProps<C>[P];
28

39
type variantValue = string | number | boolean | string[];
410

test/index.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import React from "react";
66

77
import { render } from "@testing-library/react";
8+
import { expectTypeOf } from "expect-type";
89

9-
import { styled } from "../src";
10+
import { CSSComponentPropType, styled } from "../src";
1011

1112
describe("Basic functionality", () => {
1213
it("should return the correct type of DOM node", async () => {
@@ -187,4 +188,12 @@ describe("supports more exotic setups", () => {
187188
expect(container.firstChild?.nodeName).toEqual("BUTTON");
188189
expect(ref).toBeCalled();
189190
});
191+
192+
it("Should be able to inspect the variants", async () => {
193+
const Button = styled("button", "test", { primary: { true: "primary" } });
194+
195+
type bla = CSSComponentPropType<typeof Button, "primary">;
196+
197+
expectTypeOf<bla>().toMatchTypeOf<boolean | undefined>();
198+
});
190199
});

0 commit comments

Comments
 (0)