react-native-tachyons
Installation
npm i --save @shipt/react-native-tachyons
yarn add @shipt/react-native-tachyons
Configuration
You can compose your own styles and pass them into the build process as desired.
import { build } from '@shipt/react-native-tachyons';
import { StyleSheet } from 'react-native';
const rem = 16;
const colors = {
white: '#ffffff',
black: '#000000'
};
const styles = {
'gutter-h': { paddingLeft: '1rem', paddingRight: '1rem' },
'gutter-v': { paddingTop: '1rem', paddingBottom: '1rem' },
'some-style': { height: 100, width: '100%' }
};
build({
rem,
colors,
styles,
StyleSheet
});
And then...
import T from '@shipt/react-native-tachyons';
function MyComponent() {
return <View style={T('flx-i gutter-h bg-white')} />;
}
export default MyComponent;
Composing with custom styles dynamically
If you find yourself wanting to override some styles with something custom in some places in your app. Just throw them in the function call with your string of shorthands.
import T from '@shipt/react-native-tachyons'
const customStyle = StyleSheet.create({
MyComponent: {
width: 234
}
})
function MyComponent() {
return <View style={T('flx-i gutter-h bg-white', customStyle.MyComponent)} />
}
function MyCleanerComponent(props) {
const style = props.active
? T('flx-i gutter-h bg-white', customStyle.MyComponent)
: T('flx-i gutter-h bg-black', customStyle.MyComponent)
return <View style={style} />
}
export default MyComponent
export default MyCleanerComponent
Styled Components Syntax
import { View, Text } from 'react-native';
import { styled } from '@shipt/react-native-tachyons';
const staticStyles = StyleSheet.create({
Button: {
width: 253
}
});
const ButtonContainer = styled(View, staticStyles.Button)`flx-i gutter ${props => (props.primary ? 'green' : 'white')}`;
const ButtonContent = styled(Text)`white`;
export function Button(props) {
const dynamicStyle = {
height: props.active ? 51 : 47
};
return (
<ButtonContainer primary={props.primary} style={dynamicStyle}>
<ButtonContent>{props.text}</ButtonContent>
</ButtonContainer>
);
}
Styled Components Babel Plugin
Checkout our babel-plugin
Gotchas
Usage with Animated.createAnimatedComponent
We haven't tracked down the exact issue nor can we reliably replicate, BUT, occasionally RN will throw a nasty error
if you use Animated.createAnimatedComponent
with a styled component. The solution style your component prior to using
Animated.createAnimatedComponent
.
const Component = styled(View)`absolute`;
const AnimatedComponent = React.createAnimatedComponent(Component);
const AnimatedView = React.createAnimatedComponent(View);
const AnimatedComponent = styled(AnimatedView)`absolute`;