forked from emotion-js/emotion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make StyledComponent polymorphic (emotion-js#1588)
* chore: tell Prettier we are using Flow this gets rid of a lot of warnings and wrong auto formatting * fix: make CreateStyled callable function polymorphic This makes it possible to type styled functional components and enables a workaround for lack of tagged templates support of Flow * chore: update type annotation to use new style * fix: wrong formatting * style: fix prettier issues * chore: changeset * fix: just import the @emotion/sheet type * style: make prettier happy * docs: added Flow types documentation page * Update flow.mdx * refactor: rename P to Props # Conflicts: # packages/styled-base/src/utils.js
- Loading branch information
Showing
14 changed files
with
172 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@emotion/styled-base': patch | ||
'@emotion/styled': patch | ||
--- | ||
|
||
StyledComponent Flow type is now polymorphic, that means you can now define the component prop types to get better type safety. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ overrides: | |
- files: "docs/*.md" | ||
options: | ||
printWidth: 60 | ||
- files: "*.js" | ||
options: | ||
parser: flow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
title: 'Flow' | ||
--- | ||
|
||
Emotion is built with Flow, so it exports type definitions for most of its packages, | ||
including `@emotion/styled`. | ||
|
||
## @emotion/styled | ||
|
||
The styled package can be used to define styled components in two ways, by calling `styled()`, | ||
or by using the `styled.*` shortcuts. | ||
|
||
Unfortunately, Flow doesn't currently support generic types on tagged templates, this means if | ||
you'd like to explictly type a styled component props, you will have to use one of the following | ||
alternatives: | ||
|
||
```jsx | ||
import styled from '@emotion/styled' | ||
|
||
// Option A | ||
const A = styled<Props>('div')` | ||
color: red; | ||
` | ||
|
||
// Option B | ||
const B = styled.div<Props>({ | ||
color: 'red', | ||
}) | ||
``` | ||
|
||
Styled components are annotated the same way normal React components are: | ||
|
||
```jsx | ||
import styled from '@emotion/styled' | ||
|
||
type Props = { a: string } | ||
const Link = styled<Props>('a')` | ||
color: red; | ||
` | ||
|
||
const App = () => <Link href="#">Click me</Link> | ||
``` | ||
|
||
Just like for normal React components, you don't need to provide type annotations | ||
for your styled components if you don't plan to export them from your module: | ||
|
||
```jsx | ||
import styled from '@emotion/styled' | ||
|
||
const Internal = styled.div` | ||
color: red; | ||
` | ||
``` | ||
|
||
Be aware, Flow infers the return type of your components by referencing their return type, | ||
this means you will need to annotate the properties of the root component in the case below: | ||
|
||
```jsx | ||
|
||
const Container = styled.div` | ||
^^^^^^^^^^^ Missing type annotation for P. P is a type parameter declared in function type [1] and was implicitly instantiated at | ||
encaps tag [2]. | ||
color: red; | ||
` | ||
|
||
export const App = () => <Container /> | ||
``` | ||
|
||
You can use `React$ElementConfig` to obtain the props type of a HTML tag, or of | ||
any existing React component: | ||
|
||
```jsx | ||
import type { ElementConfig } from 'react' | ||
|
||
type Props = ElementConfig<'div'> | ||
const Container = styled<Props>('div')` | ||
color: red; | ||
` | ||
|
||
export const App = () => <Container /> | ||
``` | ||
|
||
|
||
```jsx | ||
import type { ElementConfig } from 'react' | ||
import styled from '@emotion/styled' | ||
|
||
const Container = styled<ElementConfig<'div'>>('div')` | ||
background-color: yellow; | ||
` | ||
|
||
const App = () => ( | ||
<Container>{() => 10}</Container> | ||
^^^^^^^^^^ Cannot create Container element because in property children: | ||
• Either inexact function [1] is incompatible with exact React.Element [2]. | ||
• Or function [1] is incompatible with React.Portal [3]. | ||
• Or property @@iterator is missing in function [1] but exists in $Iterable [4]. | ||
) | ||
``` | ||
|
||
Alternatively, you can define the return type of your component, so that | ||
Flow doesn't need to infer it reading the props type of the internal component: | ||
|
||
```jsx | ||
import type { Node } from 'react' | ||
|
||
const Container = styled.div` | ||
color: red; | ||
` | ||
|
||
export const App = (): Node => <Container /> | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,4 @@ const NotFoundPage = () => { | |
</Layout> | ||
) | ||
} | ||
|
||
export default NotFoundPage |