-
Notifications
You must be signed in to change notification settings - Fork 136
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
e95eb73
commit 31ddfeb
Showing
5 changed files
with
201 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, {ComponentPropsWithoutRef} from 'react'; | ||
import {Text, TouchableOpacity} from 'react-native'; | ||
|
||
import useRestyle from '../hooks/useRestyle'; | ||
import {position, PositionProps} from '../restyleFunctions'; | ||
import createVariant, {VariantProps} from '../createVariant'; | ||
import composeRestyleFunctions from '../composeRestyleFunctions'; | ||
|
||
const theme = { | ||
colors: {}, | ||
spacing: {}, | ||
buttonVariants: { | ||
defaults: {}, | ||
}, | ||
breakpoints: { | ||
phone: 0, | ||
tablet: 376, | ||
}, | ||
zIndices: { | ||
phone: 5, | ||
}, | ||
}; | ||
type Theme = typeof theme; | ||
|
||
type Props = VariantProps<Theme, 'buttonVariants'> & | ||
PositionProps<Theme> & | ||
ComponentPropsWithoutRef<typeof TouchableOpacity>; | ||
|
||
const restyleFunctions = [ | ||
position, | ||
createVariant<Theme>({themeKey: 'buttonVariants'}), | ||
]; | ||
|
||
const composedRestyleFunction = composeRestyleFunctions<Theme, Props>( | ||
restyleFunctions, | ||
); | ||
|
||
export function Button({title, ...rest}: Props & {title: string}) { | ||
const props = useRestyle(composedRestyleFunction, rest); | ||
return ( | ||
<TouchableOpacity {...props}> | ||
<Text>{title}</Text> | ||
</TouchableOpacity> | ||
); | ||
} |
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,68 @@ | ||
import React from 'react'; | ||
import {View, ViewProps} from 'react-native'; | ||
|
||
import useRestyle from '../hooks/useRestyle'; | ||
import createVariant, {VariantProps} from '../createVariant'; | ||
import composeRestyleFunctions from '../composeRestyleFunctions'; | ||
import { | ||
backgroundColor, | ||
BackgroundColorProps, | ||
BorderProps, | ||
spacing, | ||
SpacingProps, | ||
} from '../restyleFunctions'; | ||
|
||
const palette = { | ||
purple: '#5A31F4', | ||
green: '#099C77', | ||
black: '#101010', | ||
white: '#FFF', | ||
}; | ||
|
||
export const theme = { | ||
colors: { | ||
background: palette.purple, | ||
}, | ||
spacing: { | ||
none: 0, | ||
m: 8, | ||
}, | ||
breakpoints: { | ||
phone: 320, | ||
tablet: 768, | ||
}, | ||
spacingVariant: { | ||
defaults: {}, | ||
spacingParent: { | ||
padding: { | ||
phone: 'none', | ||
tablet: 'none', | ||
}, | ||
}, | ||
spacingNested: { | ||
padding: { | ||
phone: 'm', | ||
tablet: 'm', | ||
}, | ||
}, | ||
}, | ||
}; | ||
type Theme = typeof theme; | ||
|
||
type RestyleProps = SpacingProps<Theme> & | ||
BorderProps<Theme> & | ||
BackgroundColorProps<Theme> & | ||
VariantProps<Theme, 'spacingVariant'>; | ||
|
||
const restyleFunctions = composeRestyleFunctions<Theme, RestyleProps>([ | ||
spacing, | ||
backgroundColor, | ||
createVariant({themeKey: 'spacingVariant'}), | ||
]); | ||
|
||
type Props = RestyleProps & ViewProps; | ||
|
||
export const Container = ({children, ...rest}: Props) => { | ||
const props = useRestyle(restyleFunctions, rest); | ||
return <View {...props}>{children}</View>; | ||
}; |
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 |
---|---|---|
@@ -1,56 +1,63 @@ | ||
import React, {ComponentPropsWithoutRef} from 'react'; | ||
import {Text, TouchableOpacity} from 'react-native'; | ||
|
||
import useRestyle from '../hooks/useRestyle'; | ||
import {position, PositionProps} from '../restyleFunctions'; | ||
import createVariant, {VariantProps} from '../createVariant'; | ||
import composeRestyleFunctions from '../composeRestyleFunctions'; | ||
|
||
const theme = { | ||
colors: {}, | ||
spacing: {}, | ||
buttonVariants: { | ||
defaults: {}, | ||
}, | ||
breakpoints: { | ||
phone: 0, | ||
tablet: 376, | ||
}, | ||
zIndices: { | ||
phone: 5, | ||
}, | ||
}; | ||
type Theme = typeof theme; | ||
|
||
type Props = VariantProps<Theme, 'buttonVariants'> & | ||
PositionProps<Theme> & | ||
ComponentPropsWithoutRef<typeof TouchableOpacity>; | ||
|
||
const restyleFunctions = [ | ||
position, | ||
createVariant<Theme>({themeKey: 'buttonVariants'}), | ||
]; | ||
|
||
const composedRestyleFunction = composeRestyleFunctions<Theme, Props>( | ||
restyleFunctions, | ||
); | ||
|
||
function Button({title, ...rest}: Props & {title: string}) { | ||
const props = useRestyle(composedRestyleFunction, rest); | ||
return ( | ||
<TouchableOpacity {...props}> | ||
<Text>{title}</Text> | ||
</TouchableOpacity> | ||
); | ||
} | ||
|
||
function Screen() { | ||
return <Button title="test" position="absolute" />; | ||
} | ||
import React from 'react'; | ||
import {View} from 'react-native'; | ||
import {create as render} from 'react-test-renderer'; | ||
|
||
import {ThemeProvider} from '../context'; | ||
|
||
import {Button} from './TestButton'; | ||
import {Container, theme} from './TestContainer'; | ||
|
||
describe('Use restyle', () => { | ||
it('creates a button', () => { | ||
const button = Screen(); | ||
const button = <Button title="test" position="absolute" />; | ||
expect(button.props.title).toBe('test'); | ||
}); | ||
|
||
it('uses theme props', () => { | ||
const {root} = render( | ||
<ThemeProvider theme={theme}> | ||
<Container backgroundColor="background" /> | ||
</ThemeProvider>, | ||
); | ||
|
||
expect(root.findByType(View).props.style).toStrictEqual([ | ||
{backgroundColor: '#5A31F4'}, | ||
]); | ||
}); | ||
|
||
it('uses theme props with variant', () => { | ||
const {root} = render( | ||
<ThemeProvider theme={theme}> | ||
<Container variant="spacingParent" /> | ||
</ThemeProvider>, | ||
); | ||
|
||
expect(root.findByType(View).props.style).toStrictEqual([{padding: 0}]); | ||
}); | ||
|
||
it('parent styles match theme', () => { | ||
const {root} = render( | ||
<ThemeProvider theme={theme}> | ||
<Container variant="spacingParent"> | ||
<Container variant="spacingNested" /> | ||
</Container> | ||
</ThemeProvider>, | ||
); | ||
|
||
expect(root.findByType(View).props.style).toStrictEqual([{padding: 0}]); | ||
}); | ||
|
||
it('child styles match theme', () => { | ||
const {root} = render( | ||
<ThemeProvider theme={theme}> | ||
<Container variant="spacingParent"> | ||
<Container variant="spacingNested" /> | ||
</Container> | ||
</ThemeProvider>, | ||
); | ||
|
||
expect(root.findAllByType(View)[1].props.style).toStrictEqual([ | ||
{padding: 8}, | ||
]); | ||
}); | ||
}); |