Skip to content

Commit 3f0e9f5

Browse files
committed
Support inference extra props from as props
1 parent 1a2f813 commit 3f0e9f5

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"react-dom": "^16.13.1",
5252
"react-scripts": "^3.4.1",
5353
"reakit": "^1.1.2",
54+
"reakit-utils": "^0.13.1",
5455
"theme-ui": "^0.3.1",
5556
"ts-loader": "^8.0.1",
5657
"tsdx": "^0.13.2",

src/Button.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export function TheAsProp() {
5959
},
6060
}}
6161
>
62-
<Checkbox {...checkbox} as={Button}>
62+
<Button as={Checkbox} state={checkbox.state} setState={checkbox.setState}>
6363
{checkbox.state ? '😄 Happy' : '😞 Sad'}
64-
</Checkbox>
64+
</Button>
6565
</ThemeProvider>
6666
)
6767
}

src/index.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import React, { forwardRef, Ref } from 'react'
1+
import React, { forwardRef, Ref, PropsWithoutRef } from 'react'
22
import { Button as A11yButton, ButtonProps as A11yProps } from 'reakit'
3+
import { PropsWithAs, As } from 'reakit-utils/types'
34
import {
45
Button as ThemeAwareButton,
56
ButtonProps as ThemeAwareProps,
67
} from 'theme-ui'
78

8-
type Props = A11yProps & ThemeAwareProps
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 }
916

10-
function Button({ as, ...props }: Props, ref: Ref<HTMLButtonElement>) {
11-
return <A11yButton {...props} ref={ref} as={as ?? ThemeAwareButton} />
17+
const Button = (
18+
{ as = ThemeAwareButton, ...props }: ButtonProps,
19+
ref: Ref<As>
20+
) => {
21+
return <A11yButton ref={ref} as={as} {...props} />
1222
}
1323

14-
export { A11yProps, ThemeAwareProps }
15-
export default forwardRef(Button)
24+
export { ButtonProps, A11yProps, ThemeAwareProps }
25+
26+
export default forwardRef(Button) as Component<
27+
typeof ThemeAwareButton,
28+
A11yProps
29+
>

0 commit comments

Comments
 (0)