Skip to content

Commit c64a62a

Browse files
committed
Utility types Props and Components to reuse in another packages
1 parent 33a327c commit c64a62a

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/index.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
import React, { forwardRef, Ref, PropsWithoutRef } from 'react'
1+
import React, { forwardRef, Ref } from 'react'
22
import { Button as A11yButton, ButtonProps as A11yProps } from 'reakit'
3-
import { PropsWithAs, As } from 'reakit-utils/types'
43
import {
54
Button as ThemeAwareButton,
65
ButtonProps as ThemeAwareProps,
76
} from 'theme-ui'
7+
import { As, Props, Component } from '@vtex-components/types'
88

9-
type Component<DefaultAs extends As, DefaultProps> = {
10-
<T extends As = DefaultAs>(
11-
props: PropsWithAs<PropsWithoutRef<DefaultProps>, T>
12-
): JSX.Element
13-
}
14-
15-
type ButtonProps = A11yProps & { as?: As }
9+
type ButtonProps<T extends As = typeof ThemeAwareButton> = Props<T, A11yProps>
1610

1711
const Button = (
18-
{ as = ThemeAwareButton, ...props }: ButtonProps,
12+
{ as = ThemeAwareButton, ...props }: ButtonProps<As>,
1913
ref: Ref<As>
2014
) => {
2115
return <A11yButton ref={ref} as={as} {...props} />
2216
}
2317

2418
export { ButtonProps, A11yProps, ThemeAwareProps }
2519

26-
export default forwardRef(Button) as Component<
27-
typeof ThemeAwareButton,
28-
A11yProps
29-
>
20+
export default forwardRef(Button) as Component<ButtonProps>

src/typings.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,27 @@ declare module '*.svg' {
1515
export default svgUrl
1616
export { svgComponent as ReactComponent }
1717
}
18+
19+
declare module '@vtex-components/types' {
20+
import { PropsWithoutRef } from 'react'
21+
import { As, PropsWithAs } from 'reakit-utils/types'
22+
23+
export { As }
24+
25+
export type Props<T extends As, BaseProps> = PropsWithAs<
26+
PropsWithoutRef<BaseProps>,
27+
T
28+
>
29+
30+
export type AsOf<T> = T extends Props<infer TAs, any> ? TAs : never
31+
32+
export type BasePropsOf<T> = T extends Props<any, infer TProps>
33+
? TProps
34+
: never
35+
36+
export type Component<DefaultProps extends Props<any, any>> = {
37+
<T extends As = AsOf<DefaultProps>>(
38+
props: Props<T, BasePropsOf<DefaultProps>>
39+
): JSX.Element
40+
}
41+
}

0 commit comments

Comments
 (0)