|
| 1 | +import React from 'react'; |
| 2 | +import styled, { flush, ThemedReactEmotionInterface } from '../'; |
| 3 | + |
| 4 | +let Component; |
| 5 | +let mount; |
| 6 | + |
| 7 | +/* |
| 8 | + * Inference HTML Tag Props |
| 9 | + */ |
| 10 | +Component = styled.div({ color: 'red' }); |
| 11 | +mount = <Component onClick={event => event} />; |
| 12 | + |
| 13 | +Component = styled('div')({ color: 'red' }); |
| 14 | +mount = <Component onClick={event => event} />; |
| 15 | + |
| 16 | +Component = styled.div`color: red;`; |
| 17 | +mount = <Component onClick={(e) => e} />; |
| 18 | + |
| 19 | +Component = styled('div')`color: red;`; |
| 20 | +mount = <Component onClick={(e) => e} />; |
| 21 | + |
| 22 | +Component = styled.a({ color: 'red' }); |
| 23 | +mount = <Component href="#" />; |
| 24 | + |
| 25 | +Component = styled('a')({ color: 'red' }); |
| 26 | +mount = <Component href="#" />; |
| 27 | + |
| 28 | +/* |
| 29 | + * Passing custom props |
| 30 | + */ |
| 31 | +type CustomProps = { lookColor: string }; |
| 32 | + |
| 33 | +Component = styled.div<CustomProps>( |
| 34 | + { color: 'blue' }, |
| 35 | + props => ({ |
| 36 | + background: props.lookColor, |
| 37 | + }), |
| 38 | + props => ({ |
| 39 | + border: `1px solid ${props.lookColor}`, |
| 40 | + }), |
| 41 | +); |
| 42 | +mount = <Component lookColor="red" />; |
| 43 | + |
| 44 | +Component = styled<CustomProps, 'div'>('div')( |
| 45 | + { color: 'blue' }, |
| 46 | + props => ({ |
| 47 | + background: props.lookColor, |
| 48 | + }), |
| 49 | +); |
| 50 | +mount = <Component lookColor="red" />; |
| 51 | + |
| 52 | +const anotherColor = 'blue'; |
| 53 | +Component = styled<CustomProps, 'div'>('div')` |
| 54 | + background: ${props => props.lookColor}; |
| 55 | + color: ${anotherColor}; |
| 56 | +` |
| 57 | +mount = <Component lookColor="red" />; |
| 58 | + |
| 59 | +/* |
| 60 | + * With other components |
| 61 | + */ |
| 62 | +type CustomProps2 = { customProp: string }; |
| 63 | +type SFCComponentProps = { className?: string, foo: string }; |
| 64 | + |
| 65 | +const SFCComponent: React.StatelessComponent<SFCComponentProps> = props => ( |
| 66 | + <div className={props.className}>{props.children} {props.foo}</div> |
| 67 | +); |
| 68 | + |
| 69 | +// infer SFCComponentProps |
| 70 | +Component = styled(SFCComponent)({ color: 'red' }); |
| 71 | +mount = <Component foo="bar" />; |
| 72 | + |
| 73 | +// infer SFCComponentProps |
| 74 | +Component = styled(SFCComponent)`color: red`; |
| 75 | +mount = <Component foo="bar" />; |
| 76 | + |
| 77 | +// do not infer SFCComponentProps with pass CustomProps, need to pass both |
| 78 | +Component = styled<CustomProps2 & SFCComponentProps>(SFCComponent)({ |
| 79 | + color: 'red', |
| 80 | +}, props => ({ |
| 81 | + background: props.customProp, |
| 82 | +})); |
| 83 | +mount = <Component customProp="red" foo="bar" />; |
| 84 | + |
| 85 | +// do not infer SFCComponentProps with pass CustomProps, need to pass both |
| 86 | +Component = styled<CustomProps2 & SFCComponentProps>(SFCComponent)` |
| 87 | + color: red; |
| 88 | + background: ${props => props.customProp}; |
| 89 | +`; |
| 90 | +mount = <Component customProp="red" foo="bar" />; |
| 91 | + |
| 92 | + |
| 93 | +/* |
| 94 | + * With explicit theme |
| 95 | + */ |
| 96 | + |
| 97 | +type Theme = { |
| 98 | + color: { |
| 99 | + primary: string, |
| 100 | + secondary: string, |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +const _styled = styled as ThemedReactEmotionInterface<Theme>; |
| 105 | + |
| 106 | +Component = _styled.div` |
| 107 | + color: ${props => props.theme.color.primary} |
| 108 | +`; |
| 109 | +mount = <Component onClick={event => event} />; |
| 110 | + |
| 111 | +/* |
| 112 | + * withComponent |
| 113 | + */ |
| 114 | + |
| 115 | +type CustomProps3 = { |
| 116 | + bgColor: string, |
| 117 | +}; |
| 118 | + |
| 119 | +Component = styled.div<CustomProps3>(props => ({ |
| 120 | + bgColor: props.bgColor, |
| 121 | +})); |
| 122 | + |
| 123 | +let Link = Component.withComponent('a'); |
| 124 | +mount = <Link href="#" bgColor="red" />; |
| 125 | + |
| 126 | +let Button = Component.withComponent('button'); |
| 127 | +mount = <Button type="submit" bgColor="red" />; |
| 128 | + |
| 129 | +/* |
| 130 | + * Can use emotion helpers importing from react-emotion |
| 131 | + */ |
| 132 | + |
| 133 | +flush(); |
0 commit comments