diff --git a/test/glamorous.test.tsx b/test/glamorous.test.tsx index d800bad4..c9811296 100644 --- a/test/glamorous.test.tsx +++ b/test/glamorous.test.tsx @@ -12,34 +12,38 @@ const Static = glamorous.div({ }); // dynamic styles -const Title = glamorous.h1( +const Title = glamorous.h1<{ color: string }>( { "fontSize": "10px", "zIndex": "auto", }, - (props: { color: string; }) => ({ + (props) => ({ "color": props.color, }), ); +const UseTitle = () => ( + +) + // theme styles -const Divider = glamorous.span( +const Divider = glamorous.span<{}, { main: { color: string; } }>( { "fontSize": "10px", "zIndex": "auto" }, - (props, theme: { main: { color: string; } }) => ({ - "color": theme.main.color, + (props, theme) => ({ + "color": theme && theme.main.color, }), ); // n-number of styles -const SpanDivider = glamorous.span( +const SpanDivider = glamorous.span<{}, { awesome: string, main: string }>( { "fontSize": "10px", }, - (props, theme: { awesome: { color: string } }) => ({ - "color": theme.awesome.color, + (props, theme) => ({ + "color": theme && theme.awesome, }), { "fontWeight": 500, @@ -48,8 +52,8 @@ const SpanDivider = glamorous.span( "fontFamily": "Roboto", "fontWeight": 500, }, - (props, theme: { main: { color: string; } }) => ({ - "color": theme.main.color, + (props, theme) => ({ + "color": theme && theme.main, }), ); @@ -116,8 +120,6 @@ class ClassToWrap extends React.Component<object, object> { } } -const ThemedClass = withTheme(ClassToWrap) - const WrappedClass = glamorous(ClassToWrap)({}) // React Stateless Wrapped Component @@ -130,3 +132,47 @@ const WrappedStateless = glamorous(StatelessToWrap)({}) // Exported Component (for testing declaration generation) export const ExportTest = glamorous.div({}) + +// Theme Provider + +interface ExampleTheme { + color: string +} + +const exampleTheme: ExampleTheme = { + color: "red", +} + +const ThemedComponent = glamorous.h1< + {}, + ExampleTheme +>({ + fontSize: '10px' +}, (props, theme) => ({ + color: theme ? theme.color : 'blue' +})) + +export const ThemeProviderAndThemedComponent = () => ( + <ThemeProvider theme={exampleTheme}> + <ThemedComponent /> + </ThemeProvider> +); + +// Extended component with theme prop + +interface ExampleTheme { + color: string +} + +const ComponentWithTheme: React.SFC<{ + theme: ExampleTheme + title: string +}> = ({title, theme: {color}}) => ( + <h3 style={{color}}>{title}</h3> +) + +const NonGlamorousThemedComponent = withTheme<ExampleTheme>(ComponentWithTheme) + +const UseNonGlamorousThemedComponent = ( + <NonGlamorousThemedComponent color={'red'} /> +) diff --git a/typings/glamorous.d.ts b/typings/glamorous.d.ts index 3f55bf17..0bb920b9 100644 --- a/typings/glamorous.d.ts +++ b/typings/glamorous.d.ts @@ -47,15 +47,11 @@ interface ThemeProps { export class ThemeProvider extends React.Component<ThemeProps, any> { } - -export declare function withTheme<P>(component: Component<P & ThemeProps>): GlamorousComponent<P> - - -export declare function withTheme - <P>( - component: - | React.ComponentClass<P> - | React.StatelessComponent<P>): GlamorousComponent<P> +export function withTheme<Theme>(component: Component<{ theme: Theme }>): GlamorousComponent<Theme> +export function withTheme<Theme>(component: + | React.ComponentClass<{ theme: Theme }> + | React.StatelessComponent<{ theme: Theme }> +): GlamorousComponent<Theme> declare const glamorous: GlamorousInterface diff --git a/typings/styled-function.d.ts b/typings/styled-function.d.ts index dbe68511..db9b41f4 100644 --- a/typings/styled-function.d.ts +++ b/typings/styled-function.d.ts @@ -21,12 +21,12 @@ export type StaticStyles<Properties> = Partial<Properties> * * @see {@link https://github.com/paypal/glamorous/blob/master/src/create-glamorous.js#L28-L131} */ -export type DynamicStyledFunction<Properties, CustomProps> = ( +export type DynamicStyledFunction<Properties, CustomProps, ThemeProps> = ( props: CustomProps, - theme?: object + theme?: ThemeProps ) => Partial<Properties> -type Styles<Properties, CustomProps> = Array<DynamicStyledFunction<Properties, CustomProps> | StaticStyles<Properties>> +type Styles<Properties, CustomProps, ThemeProps> = Array<DynamicStyledFunction<Properties, CustomProps, ThemeProps> | StaticStyles<Properties>> // TODO: Using a union for a parameter kills autocomplete so we // use overloading to give autocomplete on the first two styles @@ -53,120 +53,120 @@ export interface ExtraGlamorousProps { } export interface StyledFunction<Props, Properties> { - <CustomProps>( + <CustomProps, ThemeProps = {}>( style1: StaticStyles<Properties>, - ...styles: Styles<Properties, CustomProps> + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps>; } export interface StyledFunction<Props, Properties> { - <CustomProps>( + <CustomProps, ThemeProps = {}>( style1: StaticStyles<Properties>, style2: StaticStyles<Properties>, - ...styles: Styles<Properties, CustomProps> + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( + <CustomProps, ThemeProps = {}>( style1: StaticStyles<Properties>, - style2: DynamicStyledFunction<Properties, CustomProps>, - ...styles: Styles<Properties, CustomProps> + style2: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( + <CustomProps, ThemeProps = {}>( style1: StaticStyles<Properties>, - style2: DynamicStyledFunction<Properties, CustomProps>, + style2: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, style3: StaticStyles<Properties>, - ...styles: Styles<Properties, CustomProps> + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( + <CustomProps, ThemeProps = {}>( style1: StaticStyles<Properties>, - style2: DynamicStyledFunction<Properties, CustomProps>, - style3: DynamicStyledFunction<Properties, CustomProps>, - ...styles: Styles<Properties, CustomProps> + style2: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + style3: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( + <CustomProps, ThemeProps = {}>( style1: StaticStyles<Properties>, style2: StaticStyles<Properties>, - style3: DynamicStyledFunction<Properties, CustomProps>, - ...styles: Styles<Properties, CustomProps> + style3: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( + <CustomProps, ThemeProps = {}>( style1: StaticStyles<Properties>, style2: StaticStyles<Properties>, style3: StaticStyles<Properties>, - ...styles: Styles<Properties, CustomProps> + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( - style1: DynamicStyledFunction<Properties, CustomProps>, - ...styles: Styles<Properties, CustomProps> + <CustomProps, ThemeProps = {}>( + style1: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( - style1: DynamicStyledFunction<Properties, CustomProps>, + <CustomProps, ThemeProps = {}>( + style1: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, style2: StaticStyles<Properties>, - ...styles: Styles<Properties, CustomProps> + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( - style1: DynamicStyledFunction<Properties, CustomProps>, - style2: DynamicStyledFunction<Properties, CustomProps>, - ...styles: Styles<Properties, CustomProps> + <CustomProps, ThemeProps = {}>( + style1: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + style2: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( - style1: DynamicStyledFunction<Properties, CustomProps>, + <CustomProps, ThemeProps = {}>( + style1: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, style2: StaticStyles<Properties>, - style3: DynamicStyledFunction<Properties, CustomProps>, - ...styles: Styles<Properties, CustomProps> + style3: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( - style1: DynamicStyledFunction<Properties, CustomProps>, + <CustomProps, ThemeProps = {}>( + style1: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, style2: StaticStyles<Properties>, style3: StaticStyles<Properties>, - ...styles: Styles<Properties, CustomProps> + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( - style1: DynamicStyledFunction<Properties, CustomProps>, - style2: DynamicStyledFunction<Properties, CustomProps>, + <CustomProps, ThemeProps = {}>( + style1: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + style2: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, style3: StaticStyles<Properties>, - ...styles: Styles<Properties, CustomProps> + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> } export interface StyledFunction<Props, Properties> { - <CustomProps>( - style1: DynamicStyledFunction<Properties, CustomProps>, - style2: DynamicStyledFunction<Properties, CustomProps>, - style3: DynamicStyledFunction<Properties, CustomProps>, - ...styles: Styles<Properties, CustomProps> + <CustomProps, ThemeProps = {}>( + style1: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + style2: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + style3: DynamicStyledFunction<Properties, CustomProps, ThemeProps>, + ...styles: Styles<Properties, CustomProps, ThemeProps> ): GlamorousComponent<Props & CustomProps> }