Skip to content

Commit

Permalink
refactor(components): get rid of @ts-expect-error in favoir of __inte…
Browse files Browse the repository at this point in the history
…rnalProps function
  • Loading branch information
hasparus committed Mar 18, 2022
1 parent 94fc994 commit 8327c49
Show file tree
Hide file tree
Showing 33 changed files with 360 additions and 291 deletions.
26 changes: 14 additions & 12 deletions packages/components/src/Alert.tsx
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import { Box, BoxProps } from './Box'
import type { ForwardRef } from './types'
import { __internalProps } from './util'

export type AlertProps = BoxProps

Expand All @@ -18,18 +19,19 @@ export const Alert: ForwardRef<HTMLDivElement, AlertProps> = React.forwardRef(
ref={ref}
variant="primary"
{...props}
// @ts-expect-error
__themeKey="alerts"
__css={{
display: 'flex',
alignItems: 'center',
px: 3,
py: 2,
fontWeight: 'bold',
color: 'white',
bg: 'primary',
borderRadius: 4,
}}
{...__internalProps({
__themeKey: 'alerts',
__css: {
display: 'flex',
alignItems: 'center',
px: 3,
py: 2,
fontWeight: 'bold',
color: 'white',
bg: 'primary',
borderRadius: 4,
},
})}
/>
)
}
Expand Down
10 changes: 6 additions & 4 deletions packages/components/src/AspectImage.tsx
Expand Up @@ -3,6 +3,7 @@ import React from 'react'
import { AspectRatio } from './AspectRatio'
import { Image, ImageProps } from './Image'
import type { ForwardRef } from './types'
import { __internalProps } from './util'

export interface AspectImageProps extends ImageProps {
ratio?: number
Expand All @@ -18,10 +19,11 @@ export const AspectImage: ForwardRef<HTMLImageElement, AspectImageProps> =
<Image
ref={ref}
{...props}
// @ts-expect-error
__css={{
objectFit: 'cover',
}}
{...__internalProps({
__css: {
objectFit: 'cover',
},
})}
/>
</AspectRatio>
)
Expand Down
22 changes: 13 additions & 9 deletions packages/components/src/AspectRatio.tsx
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import { Box, BoxProps } from './Box'
import type { ForwardRef } from './types'
import { __internalProps } from './util'

export interface AspectRatioProps extends BoxProps {
ratio?: number
Expand All @@ -22,7 +23,8 @@ export const AspectRatio: ForwardRef<HTMLDivElement, AspectRatioProps> =
sx={{
position: 'relative',
overflow: 'hidden',
}}>
}}
>
<Box
sx={{
width: '100%',
Expand All @@ -32,14 +34,16 @@ export const AspectRatio: ForwardRef<HTMLDivElement, AspectRatioProps> =
/>
<Box
{...props}
// @ts-expect-error
__css={{
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
}}>
{...__internalProps({
__css: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
},
})}
>
{children}
</Box>
</Box>
Expand Down
10 changes: 6 additions & 4 deletions packages/components/src/Avatar.tsx
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import { Image, ImageProps } from './Image'
import type { ForwardRef } from './types'
import { __internalProps } from './util'

export interface AvatarProps extends ImageProps {
size?: number | string
Expand All @@ -16,10 +17,11 @@ export const Avatar: ForwardRef<HTMLImageElement, AvatarProps> =
height={size}
variant="avatar"
{...props}
// @ts-expect-error
__css={{
borderRadius: 9999,
}}
{...__internalProps({
__css: {
borderRadius: 9999,
},
})}
/>
)
})
28 changes: 15 additions & 13 deletions packages/components/src/Badge.tsx
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import { Box, BoxProps } from './Box'
import type { ForwardRef } from './types'
import { __internalProps } from './util'

export type BadgeProps = BoxProps

Expand All @@ -12,19 +13,20 @@ export const Badge: ForwardRef<HTMLDivElement, BadgeProps> = React.forwardRef(
ref={ref}
variant="primary"
{...props}
// @ts-expect-error
__themeKey="badges"
__css={{
display: 'inline-block',
verticalAlign: 'baseline',
fontSize: 0,
fontWeight: 'bold',
whiteSpace: 'nowrap',
px: 1,
borderRadius: 2,
color: 'white',
bg: 'primary',
}}
{...__internalProps({
__themeKey: 'badges',
__css: {
display: 'inline-block',
verticalAlign: 'baseline',
fontSize: 0,
fontWeight: 'bold',
whiteSpace: 'nowrap',
px: 1,
borderRadius: 2,
color: 'white',
bg: 'primary',
},
})}
/>
)
}
Expand Down
16 changes: 7 additions & 9 deletions packages/components/src/Box.tsx
Expand Up @@ -10,11 +10,11 @@ import React, { forwardRef } from 'react'
import {
css,
get,
ThemeUICSSObject,
ThemeUICSSProperties,
ThemeUIStyleObject,
} from '@theme-ui/css'
import type { Assign } from './types'
import type { __ThemeUIComponentsInternalProps } from './util'

const boxSystemProps = [
// space scale props (inherited from @styled-system/space)
Expand Down Expand Up @@ -77,7 +77,10 @@ export const __isBoxStyledSystemProp = (prop: string) =>

const pickSystemProps = (props: BoxOwnProps) => {
const res: Partial<Pick<BoxOwnProps, typeof boxSystemProps[number]>> = {}
for (const key of boxSystemProps) res[key] = props[key]
for (const key of boxSystemProps) {
// ts2590: union is too large
;(res as any)[key] = props[key]
}

return res
}
Expand All @@ -86,14 +89,9 @@ const pickSystemProps = (props: BoxOwnProps) => {
* Use the Box component as a layout primitive to add margin, padding, and colors to content.
* @see https://theme-ui.com/components/box
*/
export const Box = forwardRef<HTMLElement, BoxProps>(function Box(props, ref) {
export const Box = forwardRef<any, BoxProps>(function Box(props, ref) {
const theme = useTheme()

interface __BoxInternalProps {
__css: ThemeUICSSObject
__themeKey?: string
}

const {
__themeKey = 'variants',
__css,
Expand All @@ -102,7 +100,7 @@ export const Box = forwardRef<HTMLElement, BoxProps>(function Box(props, ref) {
sx,
as: Component = 'div',
...rest
} = props as BoxProps & __BoxInternalProps
} = props as BoxProps & __ThemeUIComponentsInternalProps

const baseStyles: CSSObject = {
boxSizing: 'border-box',
Expand Down
44 changes: 25 additions & 19 deletions packages/components/src/Button.tsx
@@ -1,7 +1,12 @@
import React from 'react'

import { Box, BoxOwnProps } from './Box'
import { Box as _Box, BoxOwnProps, BoxProps } from './Box'
import type { Assign, ForwardRef } from './types'
import { __internalProps } from './util'

const Box = _Box as React.ForwardRefExoticComponent<
BoxProps & React.RefAttributes<HTMLButtonElement>
>

export interface ButtonProps
extends Assign<React.ComponentPropsWithRef<'button'>, BoxOwnProps> {}
Expand All @@ -13,26 +18,27 @@ export const Button: ForwardRef<HTMLButtonElement, ButtonProps> =
React.forwardRef(function Button(props, ref) {
return (
<Box
ref={ref}
ref={ref as React.Ref<HTMLButtonElement>}
as="button"
variant="primary"
{...props}
// @ts-expect-error
__themeKey="buttons"
__css={{
appearance: 'none',
display: props.hidden ? undefined : 'inline-block',
textAlign: 'center',
lineHeight: 'inherit',
textDecoration: 'none',
fontSize: 'inherit',
px: 3,
py: 2,
color: 'white',
bg: 'primary',
border: 0,
borderRadius: 4,
}}
{...(props as BoxProps)}
{...__internalProps({
__themeKey: 'buttons',
__css: {
appearance: 'none',
display: props.hidden ? undefined : 'inline-block',
textAlign: 'center',
lineHeight: 'inherit',
textDecoration: 'none',
fontSize: 'inherit',
px: 3,
py: 2,
color: 'white',
bg: 'primary',
border: 0,
borderRadius: 4,
},
})}
/>
)
})
4 changes: 2 additions & 2 deletions packages/components/src/Card.tsx
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import { Box, BoxProps } from './Box'
import type { ForwardRef } from './types'
import { __internalProps } from './util'

export type CardProps = BoxProps
/**
Expand All @@ -16,8 +17,7 @@ export const Card: ForwardRef<HTMLDivElement, CardProps> = React.forwardRef(
ref={ref}
variant="primary"
{...props}
// @ts-expect-error
__themeKey="cards"
{...__internalProps({ __themeKey: 'cards' })}
/>
)
}
Expand Down
56 changes: 30 additions & 26 deletions packages/components/src/Checkbox.tsx
Expand Up @@ -3,6 +3,7 @@ import React from 'react'
import { Box, BoxOwnProps } from './Box'
import { SVG, SVGProps } from './SVG'
import { Assign, ForwardRef } from './types'
import { __internalProps } from './util'

const CheckboxChecked = (props: SVGProps) => (
<SVG {...props}>
Expand All @@ -20,23 +21,25 @@ const CheckboxIcon = (props: SVGProps) => (
<React.Fragment>
<CheckboxChecked
{...props}
// @ts-expect-error internal prop
__css={{
display: 'none',
'input:checked ~ &': {
display: 'block',
{...__internalProps({
__css: {
display: 'none',
'input:checked ~ &': {
display: 'block',
},
},
}}
})}
/>
<CheckboxUnchecked
{...props}
// @ts-expect-error internal prop
__css={{
display: 'block',
'input:checked ~ &': {
display: 'none',
{...__internalProps({
__css: {
display: 'block',
'input:checked ~ &': {
display: 'none',
},
},
}}
})}
/>
</React.Fragment>
)
Expand Down Expand Up @@ -78,21 +81,22 @@ export const Checkbox: ForwardRef<HTMLInputElement, CheckboxProps> =
variant={variant}
className={className}
sx={sx}
// @ts-expect-error internal prop
__themeKey="forms"
__css={{
mr: 2,
borderRadius: 4,
color: 'gray',
flexShrink: 0,
'input:checked ~ &': {
color: 'primary',
},
'input:focus ~ &': {
color: 'primary',
bg: 'highlight',
{...__internalProps({
__themeKey: 'forms',
__css: {
mr: 2,
borderRadius: 4,
color: 'gray',
flexShrink: 0,
'input:checked ~ &': {
color: 'primary',
},
'input:focus ~ &': {
color: 'primary',
bg: 'highlight',
},
},
}}
})}
/>
{children}
</Box>
Expand Down

0 comments on commit 8327c49

Please sign in to comment.