Skip to content

Commit

Permalink
tighten up cssMap type
Browse files Browse the repository at this point in the history
  • Loading branch information
liamqma committed Sep 1, 2023
1 parent 5b8006d commit 8dc34f0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-yaks-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compiled/react': patch
---

Update `cssMap` type
6 changes: 5 additions & 1 deletion examples/parcel/src/ui/css-map.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { css, cssMap } from '@compiled/react';

const base = css({
backgroundColor: 'blue',
});

const styles = cssMap({
danger: {
color: 'red',
Expand All @@ -9,4 +13,4 @@ const styles = cssMap({
},
});

export default ({ variant, children }) => <div css={css(styles[variant])}>{children}</div>;
export default ({ variant, children }) => <div css={[base, styles[variant]]}>{children}</div>;
6 changes: 5 additions & 1 deletion examples/webpack/src/ui/css-map.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { css, cssMap } from '@compiled/react';

const base = css({
backgroundColor: 'blue',
});

const styles = cssMap({
danger: {
color: 'red',
Expand All @@ -9,4 +13,4 @@ const styles = cssMap({
},
});

export default ({ variant, children }) => <div css={css(styles[variant])}>{children}</div>;
export default ({ variant, children }) => <div css={[base, styles[variant]]}>{children}</div>;
39 changes: 34 additions & 5 deletions packages/react/src/css-map/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,37 @@
* Flowgen v1.21.0
* @flow
*/
import type { CSSProps, CssObject } from '../types';
import * as CSS from 'csstype';
/**
* These are all the CSS props that will exist.
* Only 'string' and 'number' are valid CSS values.
* @example ```
* const style: CssProps = {
* color: 'red',
* margin: 10,
* };
* ```
*/
declare type CssProps = $ReadOnly<CSS.Properties<string, number>>;
/**
* Recursively typed CSS object because nested objects are allowed.
* @example ```
* const style: CssObject = {
* "@media screen and (min-width: 480px)": {
* ":hover": {
* color: 'red'
* }
* }
* }
* ```
*/
declare type CssObject = $ReadOnly<
| {
[key: string]: CssObject,
}
| CssProps
>;
declare type returnType<T: string> = { [key: T]: CssProps };
/**
* ## cssMap
*
Expand All @@ -20,7 +50,6 @@ import type { CSSProps, CssObject } from '../types';
* <Component borderStyle="solid" />
* ```
*/
declare type returnType<T: string, P> = { [key: T]: CSSProps<P> };
declare export default function cssMap<T: string, TProps>(_styles: {
[key: T]: CssObject<TProps>,
}): $ReadOnly<returnType<T, TProps>>;
declare export default function cssMap<T: string>(_styles: { [key: T]: CssObject }): $ReadOnly<
returnType<T>
>;
47 changes: 42 additions & 5 deletions packages/react/src/css-map/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import type { CSSProps, CssObject } from '../types';
import type * as CSS from 'csstype';

import { createSetupError } from '../utils/error';

/**
* These are all the CSS props that will exist.
* Only 'string' and 'number' are valid CSS values.
*
* @example
* ```
* const style: CssProps = {
* color: 'red',
* margin: 10,
* };
* ```
*/
type CssProps = Readonly<CSS.Properties<string, number>>;

/**
* Recursively typed CSS object because nested objects are allowed.
*
* @example
* ```
* const style: CssObject = {
* "@media screen and (min-width: 480px)": {
* ":hover": {
* color: 'red'
* }
* }
* }
* ```
*/
type CssObject = Readonly<
| {
[key: string]: CssObject;
}
| CssProps
>;

type returnType<T extends string> = Record<T, CssProps>;

/**
* ## cssMap
*
Expand All @@ -18,10 +56,9 @@ import { createSetupError } from '../utils/error';
* <Component borderStyle="solid" />
* ```
*/
type returnType<T extends string, P> = Record<T, CSSProps<P>>;

export default function cssMap<T extends string, TProps = unknown>(
_styles: Record<T, CssObject<TProps>>
): Readonly<returnType<T, TProps>> {
export default function cssMap<T extends string>(
_styles: Record<T, CssObject>
): Readonly<returnType<T>> {
throw createSetupError();
}

0 comments on commit 8dc34f0

Please sign in to comment.