Skip to content

Type Tweaks #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ By default variant values do not end up propagating to the final DOM element. Th
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.

```tsx
import { CSSComponentPropType } from "@phntms/css-components";
import { styled } from "@phntms/css-components";
import css from "./styles.module.css";

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

```tsx
import { CSSComponentPropType } from "@phntms/css-components";
import { VariantProps } from "@phntms/css-components";
import css from "./styles.module.css";

const Button = styled("button", {
Expand All @@ -252,7 +252,8 @@ const Button = styled("button", {
},
});

type ButtonTypes = CSSComponentPropType<typeof Button, "primary">;
type ButtonVariants = VariantProps<typeof Button>;
type PrimaryType = ButtonVariants["primary"];
```

## CLI Tool (Experimental)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@phntms/css-components",
"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.",
"version": "0.1.3",
"version": "0.2.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/phantomstudios/css-components#readme",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { createElement, forwardRef } from "react";

import {
CSSComponentConfig,
cssType,
CSS,
PolymorphicComponent,
variantsType,
} from "./type";
import { findMatchingCompoundVariants, flattenCss } from "./utils";

export { CSSComponentConfig, CSSComponentPropType } from "./type";
export { CSSComponentConfig, CSS, VariantProps } from "./type";

export const styled = <
V extends variantsType | object,
Expand Down Expand Up @@ -43,7 +43,7 @@ export const styled = <
if (variant && variant.hasOwnProperty(mergedProps[key])) {
const selector = variant[
mergedProps[key] as keyof typeof variant
] as cssType;
] as CSS;
componentStyles.push(flattenCss(selector));
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type PolymorphicComponent<
* The CSS Component Config type.
*/
export interface CSSComponentConfig<V> {
css?: cssType;
css?: CSS;
variants?: V;
compoundVariants?: CompoundVariantType<V>[];
defaultVariants?: {
Expand All @@ -73,24 +73,23 @@ export interface CSSComponentConfig<V> {
/**
* Allows you to extract a type for variant values.
*/
export type CSSComponentPropType<
export type VariantProps<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
P extends keyof React.ComponentProps<C>
> = React.ComponentProps<C>[P];
C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>
> = React.ComponentProps<C>;

/**
* CSS can be passed in as either a string or an array of strings.
*/
export type cssType = string | string[];
export type CSS = string | string[];

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

/**
* An object of variants, and how they map to CSS styles
*/
export type variantsType = Partial<{
[key: string]: { [key: string | number]: cssType };
[key: string]: { [key: string | number]: CSS };
}>;

/**
Expand All @@ -111,5 +110,5 @@ export type VariantOptions<V> = {
* Returns a type object for compound variants.
*/
export type CompoundVariantType<V> = VariantOptions<V> & {
css: cssType;
css: CSS;
};
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cssType, variantValue } from "./type";
import { CSS, variantValue } from "./type";

export const findMatchingCompoundVariants = (
compoundVariants: {
Expand All @@ -14,5 +14,5 @@ export const findMatchingCompoundVariants = (
)
);

export const flattenCss = (css: cssType) =>
export const flattenCss = (css: CSS) =>
Array.isArray(css) ? css.join(" ") : css;
4 changes: 2 additions & 2 deletions test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React from "react";
import { render } from "@testing-library/react";
import { expectTypeOf } from "expect-type";

import { CSSComponentPropType, styled } from "../src";
import { VariantProps, styled } from "../src";

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

type primaryType = CSSComponentPropType<typeof Button, "primary">;
type primaryType = VariantProps<typeof Button>["primary"];

expectTypeOf<primaryType>().toMatchTypeOf<boolean | undefined>();
});
Expand Down