Created
April 7, 2022 23:54
-
-
Save sapegin/991704a876057393efe3a3f74d4c8c47 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 styled from 'styled-components/native'; | |
import { space, layout, position, flexbox, SpaceProps, LayoutProps, PositionProps, FlexboxProps } from 'styled-system'; | |
import { composeNative } from './styledSystem'; | |
export type BoxProps = SpaceProps & LayoutProps & PositionProps & FlexboxProps; | |
/** | |
* A generic container with responsive props to control whitespace, layout, positioning and colors. | |
*/ | |
export const Box = styled.View<BoxProps>(composeNative(space, layout, position, flexbox)); |
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 styled from 'styled-components/native'; | |
import { space, layout, position, flexbox, SpaceProps, LayoutProps, PositionProps, FlexboxProps } from 'styled-system'; | |
import { composeNative } from './styledSystem'; | |
export type BoxProps = SpaceProps & LayoutProps & PositionProps & FlexboxProps; | |
/** | |
* A generic container with responsive props to control whitespace, layout, positioning and colors. | |
*/ | |
export const Box = styled.div<BoxProps>( | |
{ | |
boxSizing: 'border-box', | |
minWidth: 0, | |
}, | |
compose( | |
space, | |
layout, | |
position, | |
flexbox, | |
), | |
); |
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 { createParser, ResponsiveValue, styleFn, TLengthStyledSystem } from 'styled-system'; | |
type StyleProps = { [prop: string]: ResponsiveValue<TLengthStyledSystem> }; | |
const removeResponsiveProps = (props: StyleProps): StyleProps => { | |
const mobileProps: StyleProps = {}; | |
Object.entries(props).forEach(([prop, value]) => { | |
if (Array.isArray(value)) { | |
const [mobileValue] = value; | |
if (mobileValue) { | |
mobileProps[prop] = mobileValue; | |
} | |
} else { | |
mobileProps[prop] = value; | |
} | |
}); | |
return mobileProps; | |
}; | |
const wrap = (func: styleFn) => (props: StyleProps): styleFn => { | |
return func(removeResponsiveProps(props)); | |
}; | |
export const composeNative = (...parsers: styleFn[]): styleFn => { | |
const config = {}; | |
parsers.forEach(parser => { | |
if (!parser || !parser.config) { | |
return; | |
} | |
Object.assign(config, parser.config); | |
}); | |
const parser = createParser(config); | |
return wrap(parser); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment