Created
August 13, 2018 17:09
-
-
Save gtkatakura-bysoft/06792843fa67df2aa3eaedb62cfb40e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import styled from 'styled-components' | |
const Base = ({ value = '' }) => <div>{value}</div> | |
type CSSProperties = { | |
'grid-gap': number | string, | |
} | |
type ObjectFrom<TProperty extends string, TResult> = { | |
[K in TProperty]?: TResult | |
} | |
export const value = <TCSSProperty extends keyof CSSProperties, TProperty extends string>( | |
cssProp: TCSSProperty, | |
componentProp: TProperty | |
) => (props: ObjectFrom<TProperty, CSSProperties[TCSSProperty]>) => { | |
const value = props[componentProp] | |
if (typeof value === 'undefined') return '' | |
return `${cssProp}: ${value};` | |
} | |
const Grid = styled(Base)` | |
${props => props.value} | |
${value('grid-gap', 'gap')} | |
` | |
const App = () => ( | |
<Grid | |
value={"1"} | |
gap={1} // number | string | undefined | |
/> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment