-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e01f8b2
commit 52c0636
Showing
6 changed files
with
160 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const defaultAlias = { | ||
bg: 'backgroundColor', | ||
p: 'padding', | ||
pt: 'paddingTop', | ||
pr: 'paddingRight', | ||
pb: 'paddingBottom', | ||
pl: 'paddingLeft', | ||
ps: 'paddingStart', | ||
pe: 'paddingEnd', | ||
px: 'paddingHorizontal', | ||
py: 'paddingVertical', | ||
m: 'margin', | ||
mt: 'marginTop', | ||
mr: 'marginRight', | ||
mb: 'marginBottom', | ||
ml: 'marginLeft', | ||
ms: 'marginStart', | ||
me: 'marginEnd', | ||
mx: 'marginHorizontal', | ||
my: 'marginVertical', | ||
w: 'width', | ||
h: 'height', | ||
minW: 'minWidth', | ||
minH: 'minHeight', | ||
maxW: 'maxWidth', | ||
maxH: 'maxHeight', | ||
justify: 'justifyContent', | ||
items: 'alignItems', | ||
self: 'alignSelf', | ||
direction: 'flexDirection', | ||
wrap: 'flexWrap', | ||
basis: 'flexBasis', | ||
grow: 'flexGrow', | ||
shrink: 'flexShrink', | ||
} as const; | ||
|
||
// size: 'fontSize', | ||
// weight: 'fontWeight', | ||
// align: 'textAlign', |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { styled } from './styled'; | ||
export { defaultAlias } from './aliases'; |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ComponentProps, createElement } from 'react'; | ||
import { allowedProps } from './allowedProps'; | ||
import { StyleProp, Text, View } from 'react-native'; | ||
|
||
type Options<T extends React.ComponentType<any>> = { | ||
alias?: Record<string, keyof AliasMap<T>>; | ||
defaultStyles?: AliasMap<T>; | ||
}; | ||
|
||
type AliasMap<T extends React.ComponentType<any>> = ComponentProps<T> extends { | ||
style?: infer S; | ||
} | ||
? S extends StyleProp<infer P> | ||
? Exclude<P, false | null | undefined> | ||
: {} | ||
: {}; | ||
|
||
/** | ||
* Creates a styled component, allowing you to pass style props directly to the component | ||
* instead of using the `style` prop or `StyleSheet.create`. Prop names can be aliased, and | ||
* default styles can be provided. | ||
*/ | ||
export function styled< | ||
T extends React.ComponentType<any>, | ||
U extends Options<T> = {} | ||
>(component: T, options: U = {} as U) { | ||
type BaseProps = ComponentProps<T>; | ||
type PropsWithAliasMap = BaseProps & | ||
// @ts-ignore | ||
Omit<AliasMap<T>, U['alias'][keyof U['alias']]> & { | ||
// @ts-ignore | ||
[k in keyof U['alias']]?: AliasMap<T>[U['alias'][k]]; | ||
}; | ||
|
||
return function StyledComponent(props: PropsWithAliasMap) { | ||
return createElement(component, { | ||
...props, | ||
style: { | ||
...(options?.defaultStyles || {}), | ||
...props.style, | ||
...mapStyleProps(props, options?.alias), | ||
}, | ||
}); | ||
}; | ||
} | ||
|
||
function mapStyleProps( | ||
props: Record<string, any>, | ||
aliasMap?: Record<string, any> | ||
) { | ||
const styleProps: Record<string, any> = {}; | ||
if (props) { | ||
Object.keys(props).forEach((key) => { | ||
const keyOrAlias = aliasMap?.[key] || key; | ||
if (allowedProps.includes(keyOrAlias)) { | ||
styleProps[keyOrAlias] = props[key]; | ||
} | ||
}); | ||
} | ||
return styleProps; | ||
} | ||
|
||
styled.Text = styled(Text); | ||
styled.View = styled(View); |