diff --git a/types/rebass__grid/index.d.ts b/types/rebass__grid/index.d.ts index f49d43a6c6..3f5012851f 100644 --- a/types/rebass__grid/index.d.ts +++ b/types/rebass__grid/index.d.ts @@ -10,7 +10,7 @@ export type Omit = Pick; import { ComponentType } from "react"; -import { StyledComponent, CSSIntrinsicAttributeType } from "styled-components"; +import { StyledComponent, Interpolation } from "styled-components"; export type ResponsiveProp = number | string | Array; @@ -34,10 +34,11 @@ export interface CommonProps { px?: ResponsiveProp; py?: ResponsiveProp; theme?: any; + // this is actually more powerful than the plugin because of some limitations of the transform /** - * NOTE: this is not compatible with the styled-components babel plugin anymore + * This works even without babel-plugin-styled-components. */ - css?: CSSIntrinsicAttributeType; + css?: Interpolation; } export interface BoxProps diff --git a/types/rebass__grid/rebass__grid-tests.tsx b/types/rebass__grid/rebass__grid-tests.tsx index 2cef66a972..147f3ff084 100644 --- a/types/rebass__grid/rebass__grid-tests.tsx +++ b/types/rebass__grid/rebass__grid-tests.tsx @@ -1,8 +1,11 @@ import * as React from "react"; import { Flex, Box } from "@rebass/grid"; +import { css } from "styled-components"; const Layout = () => ( ); + +const cssTest = ; diff --git a/types/styled-components/cssprop.d.ts b/types/styled-components/cssprop.d.ts new file mode 100644 index 0000000000..e19b03dc2c --- /dev/null +++ b/types/styled-components/cssprop.d.ts @@ -0,0 +1,19 @@ +import {} from "react"; +import { CSSProp } from "."; + +declare module "react" { + interface Attributes { + // NOTE: unlike the plain javascript version, it is not possible to get access + // to the element's own attributes inside function interpolations. + // Only theme will be accessible, and only with the DefaultTheme due to the global + // nature of this declaration. + // If you are writing this inline you already have access to all the attributes anyway, + // no need for the extra indirection. + /** + * If present, this React element will be converted by + * `babel-plugin-styled-components` into a styled component + * with the given css as its styles. + */ + css?: CSSProp; + } +} diff --git a/types/styled-components/index.d.ts b/types/styled-components/index.d.ts index 2f3b6b4ec5..761c48a104 100644 --- a/types/styled-components/index.d.ts +++ b/types/styled-components/index.d.ts @@ -436,29 +436,35 @@ export class StyleSheetManager extends React.Component< StyleSheetManagerProps > {} -export type CSSIntrinsicAttributeType = - | string - | CSSObject - | FlattenSimpleInterpolation - // Sad, but because this is global, there is no way to override it with the ThemedStyledComponentsModule - // Only augmenting DefaultTheme will work for inline css prop - | FlattenInterpolation>>; +/** + * The CSS prop is not declared by default in the types as it would cause 'css' to be present + * on the types of anything that uses styled-components indirectly, even if they do not use the + * babel plugin. + * + * You can load a default declaration by using writing this special import from + * a typescript file. This module does not exist in reality, which is why the {} is important: + * + * ```ts + * import {} from 'styled-components/cssprop' + * ``` + * + * Or you can declare your own module augmentation, which allows you to specify the type of Theme: + * + * ```ts + * import { CSSProp } from 'styled-components' + * + * interface MyTheme {} + * + * declare module 'react' { + * interface Attributes { + * css?: CSSProp + * } + * } + * ``` + */ +// ONLY string literals and inline invocations of css`` are supported, anything else crashes the plugin +export type CSSProp> = + string | + FlattenInterpolation>; export default styled; - -declare module "react" { - interface Attributes { - // NOTE: unlike the plain javascript version, it is not possible to get access - // to the element's own attributes inside function interpolations. - // Only theme will be accessible, and only with the DefaultTheme due to the global - // nature of this declaration. - // If you are writing this inline you already have access to all the attributes anyway, - // no need for the extra indirection. - /** - * If present, this React element will be converted by - * `babel-plugin-styled-components` into a styled component - * with the given css as its styles. - */ - css?: CSSIntrinsicAttributeType; - } -} diff --git a/types/styled-components/macro.d.ts b/types/styled-components/macro.d.ts index 08cee96a96..51f6cced26 100644 --- a/types/styled-components/macro.d.ts +++ b/types/styled-components/macro.d.ts @@ -1,2 +1,7 @@ export { default } from '.'; export * from '.'; + +/** + * Recommended: also `import {} from 'styled-components/cssprop'`, + * or augment react's `Attribute` interface with your own version. + */ diff --git a/types/styled-components/test/index.tsx b/types/styled-components/test/index.tsx index c152ffd66e..43f0f68607 100644 --- a/types/styled-components/test/index.tsx +++ b/types/styled-components/test/index.tsx @@ -16,6 +16,7 @@ import styled, { StyledComponent, ThemedStyledComponentsModule } from "styled-components"; +import {} from "styled-components/cssprop"; /** * general usage @@ -782,16 +783,34 @@ function cssProp() { return
; } + const myCss = 'background: blue;'; + return ( <>
-
-
+
+
+
+
"blue"}; @@ -808,7 +827,10 @@ function cssProp() { `} /> - +