1- import React from 'react'
1+ import React , { useReducer , useRef , useEffect , forwardRef , Ref } from 'react'
22import { ThemeProvider } from 'theme-ui'
33import { withA11y } from '@storybook/addon-a11y'
44import { withKnobs , text , color , boolean } from '@storybook/addon-knobs'
5- import { Checkbox , useCheckboxState } from 'reakit'
65
76import Button from '.'
87
@@ -46,8 +45,55 @@ export function TheSXProp() {
4645 )
4746}
4847
49- export function TheAsProp ( ) {
50- const checkbox = useCheckboxState ( )
48+ export function TheAsPropWithIntrinsicElement ( ) {
49+ const ref = useRef < HTMLAnchorElement > ( null )
50+ const [ checked , toggle ] = useReducer ( ( value ) => ! value , false )
51+
52+ useEffect ( ( ) => {
53+ console . warn (
54+ `TheAsPropWithIntrinsicElement: Detects properly type of \`ref\` from \`as\`` ,
55+ ref . current
56+ )
57+ } , [ ] )
58+
59+ return (
60+ < ThemeProvider
61+ theme = { {
62+ colors : {
63+ background : '#FFFFFF' ,
64+ primary : '#2F323A' ,
65+ secondary : '#4F5D75' ,
66+ } ,
67+ } }
68+ >
69+ < Button ref = { ref } as = "a" target = "_blank" onClick = { toggle } >
70+ { checked ? '😄 Happy' : '😞 Sad' }
71+ </ Button >
72+ </ ThemeProvider >
73+ )
74+ }
75+
76+ const Link = forwardRef ( function Link (
77+ { children, ...props } : JSX . IntrinsicElements [ 'a' ] ,
78+ ref : Ref < HTMLAnchorElement >
79+ ) {
80+ return (
81+ < a ref = { ref } { ...props } >
82+ { children }
83+ </ a >
84+ )
85+ } )
86+
87+ export function TheAsPropWithCustomElement ( ) {
88+ const ref = useRef < HTMLAnchorElement > ( null )
89+ const [ checked , toggle ] = useReducer ( ( value ) => ! value , false )
90+
91+ useEffect ( ( ) => {
92+ console . warn (
93+ `TheAsPropWithCustomElement: Detects properly type of \`ref\` from \`as\`` ,
94+ ref . current
95+ )
96+ } , [ ] )
5197
5298 return (
5399 < ThemeProvider
@@ -59,9 +105,9 @@ export function TheAsProp() {
59105 } ,
60106 } }
61107 >
62- < Checkbox { ... checkbox } as = { Button } >
63- { checkbox . state ? '😄 Happy' : '😞 Sad' }
64- </ Checkbox >
108+ < Button ref = { ref } as = { Link } target = "_blank" onClick = { toggle } >
109+ { checked ? '😄 Happy' : '😞 Sad' }
110+ </ Button >
65111 </ ThemeProvider >
66112 )
67113}
0 commit comments