Skip to content

Commit

Permalink
types are working
Browse files Browse the repository at this point in the history
  • Loading branch information
roginfarrer committed Nov 26, 2020
1 parent b8949ce commit bcb6c03
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 67 deletions.
20 changes: 2 additions & 18 deletions examples/styled-components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,9 @@ import {
shouldForwardProp,
} from 'system-props';
import styled, { ThemeProvider } from 'styled-components';
import { theme } from './theme';
import * as CSS from 'csstype';

const theme = {
breakpoints: ['42em', '52em', '60em'],
space: ['0px', '4px', '8px', '12px', '16px', '20px', '24px', '28px', '32px'],
colors: {
gray: {
10: '#333',
20: '#666',
30: '#999',
},
blue: {
10: 'skyblue',
20: 'teal',
30: 'blue',
},
},
} as const;

const system = createSystem();

interface BoxProps extends SystemProps, PseudoProps<SystemProps> {
Expand Down Expand Up @@ -65,7 +49,7 @@ const App = () => {
<ThemeProvider theme={theme}>
<Box
color="$blue.10"
bg={(t: typeof theme) => t.colors.gray[10]}
bg={(theme) => theme.colors.yellow}
padding="$2 $8"
border="1px solid rgba(0, 0, 0, 0.1)"
mb="$6"
Expand Down
49 changes: 49 additions & 0 deletions examples/styled-components/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Theme, StrictTheme } from 'system-props';

interface AppTheme extends StrictTheme {
colors: {
gray10: string;
gray20: string;
gray30: string;
blue10: string;
blue20: string;
blue30: string;
};
space: {
$0: string;
$1: string;
$3: string;
$4: string;
};
breakpoints: {
small: string;
medium: string;
large: string;
};
}

declare module 'system-props' {
export interface Theme extends AppTheme {}
}

export const theme: Theme = {
colors: {
gray10: '#333',
gray20: '#666',
gray30: '#999',
blue10: 'skyblue',
blue20: 'teal',
blue30: 'blue',
},
space: {
$0: '0px',
$1: '4px',
$3: '8px',
$4: '12px',
},
breakpoints: {
small: '42em',
medium: '52em',
large: '60em',
},
};
1 change: 1 addition & 0 deletions src/core/createSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const createParser = (
const parse: Parser = (props: Props) => {
let styles: Record<string, any> = {};
let shouldSort = false;
// @ts-ignore
const isCacheDisabled = Boolean(props.theme?.disableSystemPropsCache);

const parseEntry = (obj: SomeObject, key: string) => {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export { betterGet as get } from './core/get';
export { createSystem } from './core/createSystem';
export { ResponsiveProp } from '@/types';

export * from './props';
export * from './pseudos';
export * from './propNames';
export * from './shouldForwardProp';
export {
ResponsiveProp,
Theme,
LooseTheme,
StrictTheme,
ColorProps,
} from './types';
89 changes: 62 additions & 27 deletions src/more-types.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,82 @@
import { Theme, ResponsiveProp } from './types';
import { ColorProps } from './props/color';
import { SpaceProps } from './props/space';
import { LayoutProps } from './props/layout';

interface LooseTheme {
[x: string]: any;
}

// interface LocalTheme extends LooseTheme {
// colors: {
// blue100: string;
// blue200: string;
// blue300: string;
// blue400: string;
// blue500: string;
// blue600: string;
// blue700: string;
// blue800: string;
// blue900: string;
// reds: [string, string, string];
// };
// space: [number, number, number, number];
// }

interface LocalTheme extends Theme {
colors: {
blue100: 'blue';
blue200: 'blue';
blue300: 'blue';
blue400: 'blue';
blue500: 'blue';
blue600: 'blue';
blue700: 'blue';
blue800: 'blue';
blue100: string;
blue200: string;
blue300: string;
blue400: string;
blue500: string;
blue600: string;
blue700: string;
blue800: string;
blue900: string;
red100: string;
red200: string;
};
space: {
250: '2px';
500: '4px';
1000: '8px';
};
breakpoints: {
320: '320px';
640: '640px';
0: string;
1: string;
2: string;
};
}

// const theme: LocalTheme = {
// colors: {
// blue100: 'blue',
// blue200: 'blue',
// blue300: 'blue',
// blue400: 'blue',
// blue500: 'blue',
// blue600: 'blue',
// blue700: 'blue',
// blue800: 'blue',
// blue900: 'blue',
// reds: ['one', 'two', 'three'],
// },
// space: [0, 4, 8, 12],
// };

type NewColorProps = {
[K in keyof ColorProps]?:
| ResponsiveProp<keyof LocalTheme['colors']>
| ColorProps[K];
[K in keyof ColorProps]?: LocalTheme extends Theme
? ResponsiveProp<keyof LocalTheme['colors']>
: ColorProps[K];
};

type NewSpaceProps = {
[K in keyof SpaceProps]?: SpaceProps[K] | keyof LocalTheme['space'];
[K in keyof SpaceProps]?: LocalTheme extends Theme
? ResponsiveProp<keyof LocalTheme['space']>
: SpaceProps[K];
};

interface Props extends NewSpaceProps, NewColorProps {}

const foo: Props = {
px: (theme) => theme.colors.blue100,
backgroundColor: {
320: 'blue100',
640: 'blue200',
},
bg: ['blue100', 'blue200', 'blue900'],
mx: 1000,
const bar: Props = {
bg: 'red100',
color: 'red100',
fill: 'blue100',
margin: 1,
};
36 changes: 27 additions & 9 deletions src/props/color/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { PropConfigCollection, ResponsiveProp } from '@/types';
import {
PropConfigCollection,
ResponsiveProp,
Theme,
ResponsiveObjectValue,
ResponsiveArrayValue,
} from '@/types';
import * as CSS from 'csstype';

const config: PropConfigCollection = {
Expand All @@ -22,13 +28,25 @@ const config: PropConfigCollection = {
};
config.bg = config.backgroundColor;

export interface ColorProps {
color?: ResponsiveProp<CSS.Property.Color>;
backgroundColor?: ResponsiveProp<CSS.Property.Color>;
bg?: ResponsiveProp<CSS.Property.Color>;
fill?: ResponsiveProp<CSS.Property.Color>;
stroke?: ResponsiveProp<CSS.Property.Color>;
opacity?: ResponsiveProp<CSS.Property.Opacity>;
}
type Colors<T extends Theme> = keyof T['colors'];

export type BaseColorProps = {
color?: CSS.Property.Color;
backgroundColor?: CSS.Property.BackgroundColor;
bg?: CSS.Property.BackgroundColor;
fill?: CSS.Property.Fill;
stroke?: CSS.Property.Stroke;
};

// export type BaseColorProps<T extends Theme> = {
// [K in keyof One]?: ResponsiveProp<keyof T['colors']>;
// // [K in keyof CoreColorProps]?: Theme extends StrictTheme
// // ? keyof Theme['colors']
// // : CoreColorProps[K];
// } & { opacity?: ResponsiveProp<CSS.Property.Opacity> };

// export interface BaseColorProps {
// color?: keyof Theme['colors'];
// }

export const color = config;
2 changes: 0 additions & 2 deletions src/props/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BackgroundProps } from './background';
import { BorderProps } from './border';
import { ColorProps } from './color';
import { FlexboxProps } from './flexbox';
import { GridProps } from './grid';
import { LayoutProps } from './layout';
Expand All @@ -24,7 +23,6 @@ export { layout as styledSystemLayout } from './styled-system-layout';
export interface SystemProps
extends BackgroundProps,
BorderProps,
ColorProps,
FlexboxProps,
GridProps,
LayoutProps,
Expand Down
23 changes: 21 additions & 2 deletions src/props/space/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { betterGet } from '@/core/get';
import { PropConfigCollection, Transform, ResponsiveProp } from '@/types';
import {
PropConfigCollection,
Transform,
ResponsiveProp,
Theme,
StrictTheme,
} from '@/types';
import { positiveOrNegative } from '../positiveOrNegative';
import { Property } from 'csstype';

Expand Down Expand Up @@ -217,4 +223,17 @@ export interface MarginProps {
my?: ResponsiveProp<Property.MarginTop>;
}

export interface SpaceProps extends MarginProps, PaddingProps {}
// export interface SpaceProps extends MarginProps, PaddingProps {}

type MorePadding = {
[K in keyof PaddingProps]?: Theme extends StrictTheme
? ResponsiveProp<keyof Theme['space']>
: PaddingProps[K];
};
type MoreMargin = {
[K in keyof MarginProps]?: Theme extends StrictTheme
? ResponsiveProp<keyof Theme['space']>
: MarginProps[K];
};

export type SpaceProps = MorePadding & MoreMargin;
Loading

0 comments on commit bcb6c03

Please sign in to comment.