Skip to content

Commit

Permalink
Export CSSPseudos, CSSProperties, StrictCSSProperties types for a s…
Browse files Browse the repository at this point in the history
…trict `XCSSProp` implementation. (#1600)

* Export `CSSPseudos, CSSProperties, StrictCSSProperties` types for a strict `XCSSProp` implementation.

To avoid the complexity of requiring `ReturnType<typeof XCSSProp<…>>` when creating a `createStrictAPI().XCSSProp` and `props.xcss` implementation, being able to use these types in generics are helpful.

The example usage is:
```tsx
const { XCSSProp, css, cssMap, cx } = createStrictAPI<…>();

export type StrictXCSSProp<
  TAllowedProperties extends keyof StrictCSSProperties,
  TAllowedPseudos extends CSSPseudos,
  TRequiredProperties extends {
    requiredProperties: TAllowedProperties;
    requiredPseudos: TAllowedPseudos;
  } = never,
> = ReturnType<
  typeof XCSSProp<TAllowedProperties, TAllowedPseudos, TRequiredProperties>
>;
```

Then this becomes a bit easier of a type to understand:
```diff
-export const Component = ({ xcss }: { xcss?: ReturnType<typeof XCSSProp<…>> }) =>
+export const Component = ({ xcss }: { xcss?: StrictXCSSProp<…> }) =>
  <div className={xcss} />
```

Particularly when contrasted against the non-createStrictAPI `XCSSProp` type, which is different.

* Change `XCSSAllProperties` (which we document for usage) to be our `StrictCSSProperties` as it removes some edge-cases.
  • Loading branch information
kylorhall-atlassian authored Dec 22, 2023
1 parent c252398 commit 5dca5c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-melons-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compiled/react': patch
---

Export `CSSPseudos, CSSProperties, StrictCSSProperties` types for a strict `XCSSProp` implementation.
17 changes: 11 additions & 6 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { createElement } from 'react';

import type { CompiledJSX } from './jsx/jsx-local-namespace';
import type { CssFunction, CSSProps, CssType } from './types';

export type { CSSProps, CssFunction, CssType };

export { keyframes } from './keyframes';
export { styled } from './styled';
export { ClassNames } from './class-names';
export { createStrictAPI } from './create-strict-api';
export { default as css } from './css';
export { default as cssMap } from './css-map';
export { createStrictAPI } from './create-strict-api';
export { keyframes } from './keyframes';
export { styled } from './styled';
export type {
CSSProperties,
CSSProps,
CSSPseudos,
CssFunction,
CssType,
StrictCSSProperties,
} from './types';
export { type XCSSAllProperties, type XCSSAllPseudos, type XCSSProp, cx } from './xcss-prop';

// Pass through the (classic) jsx runtime.
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/xcss-prop/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as CSS from 'csstype';

import { ac } from '../runtime';
import type { CSSPseudos, CSSProperties } from '../types';
import type { CSSPseudos, CSSProperties, StrictCSSProperties } from '../types';

type MarkAsRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };

Expand Down Expand Up @@ -64,7 +64,7 @@ export type CompiledStyles<TObject> = {
* Use in conjunction with {@link XCSSProp} to allow all properties to be given to
* your component.
*/
export type XCSSAllProperties = keyof CSSProperties;
export type XCSSAllProperties = keyof StrictCSSProperties;

/**
* Please think twice before using this type, you're better off declaring explicitly
Expand Down

0 comments on commit 5dca5c5

Please sign in to comment.