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

fix: refactor styled-base Flow types to work again #1570

Merged
merged 8 commits into from Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/red-chefs-camp.md
@@ -0,0 +1,5 @@
---
'@emotion/styled-base': patch
FezVrasta marked this conversation as resolved.
Show resolved Hide resolved
---

Fixed package's Flow types
1 change: 1 addition & 0 deletions .flowconfig
Expand Up @@ -23,4 +23,5 @@

[options]
suppress_comment=.*\\$FlowFixMe
suppress_comment=.*\\$FlowExpectError
sharedmemory.hash_table_pow=21
1 change: 1 addition & 0 deletions .flowconfig-ci
Expand Up @@ -24,5 +24,6 @@

[options]
suppress_comment=.*\\$FlowFixMe
suppress_comment=.*\\$FlowExpectError
server.max_workers=1
sharedmemory.hash_table_pow=21
16 changes: 6 additions & 10 deletions packages/styled-base/src/index.js
Expand Up @@ -4,7 +4,9 @@ import type { ElementType } from 'react'
import {
getDefaultShouldForwardProp,
type StyledOptions,
type CreateStyled
type CreateStyled,
type CreateStyledComponent,
type StyledComponent
} from './utils'
import { withEmotionCache, ThemeContext } from '@emotion/core'
import { getRegisteredStyles, insertStyles } from '@emotion/utils'
Expand All @@ -17,12 +19,6 @@ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_liter

let isBrowser = typeof document !== 'undefined'

type StyledComponent = (
props: *
) => React.Node & {
withComponent(nextTag: ElementType, nextOptions?: StyledOptions): *
}

let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
if (process.env.NODE_ENV !== 'production') {
if (tag === undefined) {
Expand Down Expand Up @@ -55,7 +51,7 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
shouldForwardProp || getDefaultShouldForwardProp(baseTag)
const shouldUseAs = !defaultShouldForwardProp('as')

return function(): StyledComponent {
return (function() {
let args = arguments
let styles =
isReal && tag.__emotion_styles !== undefined
Expand All @@ -82,7 +78,7 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
}
}

const Styled: any = withEmotionCache((props, context, ref) => {
const Styled: StyledComponent = withEmotionCache((props, context, ref) => {
return (
<ThemeContext.Consumer>
{theme => {
Expand Down Expand Up @@ -221,7 +217,7 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
)(...styles)
}
return Styled
}
}: CreateStyledComponent)
FezVrasta marked this conversation as resolved.
Show resolved Hide resolved
}

export default createStyled
38 changes: 25 additions & 13 deletions packages/styled-base/src/utils.js
@@ -1,9 +1,31 @@
// @flow
import * as React from 'react'
import type { ElementType } from 'react'
import isPropValid from '@emotion/is-prop-valid'

export type Interpolations = Array<any>

export type StyledOptions = {
label?: string,
shouldForwardProp?: string => boolean,
target?: string
}

export type StyledComponent = {
Andarist marked this conversation as resolved.
Show resolved Hide resolved
(Interpolations): React$Node,
withComponent: (
nextTag: ElementType,
nextOptions?: StyledOptions
) => StyledComponent,
displayName: string,
defaultProps: any,
__emotion_real: StyledComponent,
Andarist marked this conversation as resolved.
Show resolved Hide resolved
__emotion_base: any,
__emotion_styles: any,
__emotion_forwardProp: any,
toString: () => string
}

const testOmitPropsOnStringTag = isPropValid
const testOmitPropsOnComponent = (key: string) =>
key !== 'theme' && key !== 'innerRef'
Expand All @@ -17,19 +39,9 @@ export const getDefaultShouldForwardProp = (tag: React.ElementType) =>
? testOmitPropsOnStringTag
: testOmitPropsOnComponent

export type StyledOptions = {
label?: string,
shouldForwardProp?: string => boolean,
target?: string
}

type CreateStyledComponent = (...args: Interpolations) => *

type BaseCreateStyled = (
tag: React.ElementType,
options?: StyledOptions
) => CreateStyledComponent
export type CreateStyledComponent = (...args: Interpolations) => StyledComponent

export type CreateStyled = BaseCreateStyled & {
export type CreateStyled = {
(tag: React.ElementType, options?: StyledOptions): CreateStyledComponent,
[key: string]: CreateStyledComponent
}
8 changes: 8 additions & 0 deletions packages/styled-base/tests/flow.js
@@ -0,0 +1,8 @@
// @flow
Andarist marked this conversation as resolved.
Show resolved Hide resolved
FezVrasta marked this conversation as resolved.
Show resolved Hide resolved
import createStyled from '../src'
import type { CreateStyledComponent } from '../src/utils'

export const valid: CreateStyledComponent = createStyled('div')

// $FlowExpectError: we can't cast a StyledComponent to string
export const invalid: string = createStyled('div')