Skip to content

Commit

Permalink
addressing some deficiencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Sissons committed Nov 24, 2019
1 parent c2f6f6f commit b7bea71
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
25 changes: 16 additions & 9 deletions packages/native/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import {
ComponentPropsWithoutRef,
ComponentType,
NamedExoticComponent
NamedExoticComponent,
PropsWithChildren
} from 'react'
import * as RN from 'react-native'

Expand Down Expand Up @@ -62,24 +63,28 @@ export type ReactNativeComponentProps<
export type ReactNativeStyleType<Props> = Props extends {
style?: RN.StyleProp<infer StyleType>
}
? StyleType
? StyleType extends ReactNativeStyle ? StyleType : ReactNativeStyle
: ReactNativeStyle

export type ObjectInterpolation<StyleType = ReactNativeStyle> = StyleType
export type ObjectInterpolation<
StyleType extends ReactNativeStyle = ReactNativeStyle
> = StyleType

export interface ArrayInterpolation<MergedProps, StyleType = ReactNativeStyle>
extends Array<Interpolation<MergedProps, StyleType>> {}
export interface ArrayInterpolation<
MergedProps,
StyleType extends ReactNativeStyle = ReactNativeStyle
> extends Array<Interpolation<MergedProps, StyleType>> {}

export interface FunctionInterpolation<
MergedProps,
StyleType = ReactNativeStyle
StyleType extends ReactNativeStyle = ReactNativeStyle
> {
(mergedProps: MergedProps): Interpolation<MergedProps, StyleType>
}

export type Interpolation<
MergedProps = unknown,
StyleType = ReactNativeStyle
StyleType extends ReactNativeStyle = ReactNativeStyle
> =
| null
| undefined
Expand Down Expand Up @@ -120,7 +125,9 @@ export interface StyledWithComponent<ComponentProps extends {}> {
export type StyledComponent<
ComponentProps extends {},
SpecificComponentProps extends {} = {}
> = NamedExoticComponent<ComponentProps & SpecificComponentProps> &
> = NamedExoticComponent<
PropsWithChildren<ComponentProps & SpecificComponentProps>
> &
StyledWithComponent<ComponentProps>

/**
Expand All @@ -132,7 +139,7 @@ export interface CreateStyledComponent<
ComponentProps extends {},
SpecificComponentProps extends {} = {},
StyleProps extends {} = {},
StyleType = ReactNativeStyle
StyleType extends ReactNativeStyle = ReactNativeStyle
> {
/**
* @typeparam AdditionalProps Additional props to add to your styled component
Expand Down
11 changes: 8 additions & 3 deletions packages/native/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ export {
StyledOptions
} from './base'

export function css(
export function css<StyleType extends ReactNativeStyle = ReactNativeStyle>(
template: TemplateStringsArray,
...args: Array<Interpolation>
): ReactNativeStyle
export function css(...args: Array<Interpolation>): ReactNativeStyle
): StyleType
export function css<StyleType extends ReactNativeStyle = ReactNativeStyle>(
...args: Array<StyleType>
): StyleType
export function css<StyleType extends ReactNativeStyle = ReactNativeStyle>(
...args: Array<Interpolation>
): StyleType

export type StyledComponents<Theme extends {} = any> = {
[ComponentName in ReactNativeComponentNames]: CreateStyledComponent<
Expand Down
33 changes: 32 additions & 1 deletion packages/native/types/tests.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as React from 'react'
import { ImageStyle, StyleProp, TextStyle, View } from 'react-native'
import {
FlexStyle,
ImageStyle,
ScrollView,
StyleProp,
TextStyle,
View
} from 'react-native'
import styled, { CreateStyled, css, ReactNativeStyle } from '@emotion/native'

const cssObject = {
Expand Down Expand Up @@ -117,3 +124,27 @@ styled(MyComponent)({ width: 100 })
styled(MyComponent)(({ bar }) => ({ color: bar }))
styled(View)({ width: 100 })
styled(View)<ExtraProps>(({ foo, testID }) => ({ color: foo, testID }))

const styles = {
container: css({ flex: 1 }),
scrollContainer: css`
flex-grow: 1;
align-items: center;
`,
centered: css<FlexStyle>`
justify-content: center;
align-items: center;
`
}

export const scrollElem = (
<ScrollView contentContainerStyle={styles.scrollContainer}>
<View />
</ScrollView>
)
export const Container = styled.View(styles.container)
export const CenterContainer = styled.View(styles.container, styles.centered)
export const ImageFullWidthContained = styled.Image`
${styles.container} width: 100%;
resize-mode: contain;
`
1 change: 1 addition & 0 deletions packages/native/types/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"check-preblock"
],
"no-null-undefined-union": false,
"no-unnecessary-generics": false,
"strict-export-declare-modifiers": false
}
}

0 comments on commit b7bea71

Please sign in to comment.