Skip to content
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

Add TypeScript support of using other styled components in selectors #882

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. If a contri
- Added a test for `withComponent` followed by `attrs`, thanks to [@btmills](https://github.com/btmills). (see [#851](https://github.com/styled-components/styled-components/pull/851))
- Fix Flow type signatures for compatibility with Flow v0.47.0 (see [#840](https://github.com/styled-components/styled-components/pull/840))
- Upgraded stylis to v3.0. (see [#829](https://github.com/styled-components/styled-components/pull/829) and [#876](https://github.com/styled-components/styled-components/pull/876))
- Added missing v2.0 APIs to TypeScript typings, thanks to [@patrick91](https://github.com/patrick91), [@igorbek](https://github.com/igorbek) (see [#837](https://github.com/styled-components/styled-components/pull/837), [#882](https://github.com/styled-components/styled-components/pull/882))

## [v2.0.0] - 2017-05-25

Expand Down
6 changes: 3 additions & 3 deletions typings/styled-components.d.ts
Expand Up @@ -20,7 +20,7 @@ export type OuterStyledProps<P> = ThemedOuterStyledProps<P, any>;

export type Interpolation<P> = FlattenInterpolation<P> | ReadonlyArray<FlattenInterpolation<P> | ReadonlyArray<FlattenInterpolation<P>>>;
export type FlattenInterpolation<P> = InterpolationValue | InterpolationFunction<P>;
export type InterpolationValue = string | number;
export type InterpolationValue = string | number | StyledComponentClass<any, any>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure an interpolation can be a StyledComponentClass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can. So StyledComponentClass produced by a styled function:

const Link: StyledComponentClass<any, any> = styled.a` color: blue; `;

then it can be used in both functional and constant interpolations:

const Section = styled.section`
  color: black;
  ${Link} {
    color: red;
  }
  ${p => p.theme.linkClass || Link} {
    color: red;
  }
`;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, that's new from v2

export type SimpleInterpolation = InterpolationValue | ReadonlyArray<InterpolationValue | ReadonlyArray<InterpolationValue>>;
export interface InterpolationFunction<P> {
(props: P): Interpolation<P>;
Expand Down Expand Up @@ -105,13 +105,13 @@ interface StylesheetComponentProps {
sheet: ServerStyleSheet;
}

export class StyleSheetManager extends React.Component<StylesheetComponentProps, any> { }
export class StyleSheetManager extends React.Component<StylesheetComponentProps, {}> { }

export class ServerStyleSheet {
collectStyles(tree: React.ReactNode): ReactElement<StylesheetComponentProps>;

getStyleTags(): string;
getStyleElement(): ReactElement<any>[];
getStyleElement(): ReactElement<{}>[];
}

export default styled;
30 changes: 30 additions & 0 deletions typings/tests/nested-test.tsx
@@ -0,0 +1,30 @@
import * as React from "react";

import styled, { css } from "../..";

const Link = styled.a`
color: red;
`;

const AlternativeLink = styled.a`
color: blue;
`;

const freeStyles = css`
background-color: black;
color: white;
${Link} {
color: blue;
}
`;

const Article = styled.section`
color: red;
${freeStyles}
& > ${Link} {
color: green;
}
${p => p.theme.useAlternativeLink ? AlternativeLink : Link} {
color: black
}
`;