I\'m working on an app that uses triangles that overlay other containers/divs. Had that solved with CS
The best way to do this is to create an <Image>
component and absolutely position it, similar to how you would a pure CSS triangle. If the triangle has a flat color, as opposed to a gradient or other pattern, you can set its color using the tintColor
style property.
Example:
<Image
source={require('image!triangle')}
style={{tintColor: '#008080'}}
/>
render() {
return (
<View style={[styles.triangle,styles.arrowUp]}/>
);
}
And styles
const styles = {
triangle: {
width: 0,
height: 0,
backgroundColor: 'transparent',
borderStyle: 'solid',
},
arrowUp: {
borderTopWidth: 0,
borderRightWidth: 30,
borderBottomWidth: 30,
borderLeftWidth: 30,
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: "tomato",
borderLeftColor: 'transparent',
},
arrowRight: {
borderTopWidth: 30,
borderRightWidth: 0,
borderBottomWidth: 30,
borderLeftWidth: "tomato",
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: "tomato",
},
arrowDown: {
borderTopWidth: 30,
borderRightWidth: 30,
borderBottomWidth: 0,
borderLeftWidth: 30,
borderTopColor: "tomato",
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowLeft: {
borderTopWidth: 30,
borderRightWidth: "tomato",
borderBottomWidth: 30,
borderLeftWidth: 0,
borderTopColor: 'transparent',
borderRightColor: "tomato",
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowUpLeft: {
borderTopWidth: 30,
borderRightWidth: "tomato",
borderBottomWidth: 0,
borderLeftWidth: 0,
borderTopColor: "tomato",
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowUpRight: {
borderTopWidth: 0,
borderRightWidth: "tomato",
borderBottomWidth: 30,
borderLeftWidth: 0,
borderTopColor: 'transparent',
borderRightColor: "tomato",
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowDownLeft: {
borderTopWidth: 30,
borderRightWidth: 0,
borderBottomWidth: 0,
borderLeftWidth: "tomato",
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: "tomato",
},
arrowDownRight: {
borderTopWidth: 0,
borderRightWidth: 0,
borderBottomWidth: 30,
borderLeftWidth: "tomato",
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: "tomato",
borderLeftColor: 'transparent',
},
}
Source : https://github.com/Jpoliachik/react-native-triangle
It is still possible to draw triangles in React Native using the CSS trick. I wrote a class to encapsulate this: https://github.com/Jpoliachik/react-native-triangle
If you'd like to write it yourself, I used this tool: http://apps.eky.hk/css-triangle-generator/ to generate the triangle I wanted and modify the styles to React Native syntax.
For example, an Isosceles triangle 90x90 pointing up in CSS reads:
width: 0;
height: 0;
border-style: solid;
border-width: 0 45px 90px 45px;
border-color: transparent transparent #007bff transparent;
But in React Native the styles would be:
triangle: {
width: 0,
height: 0,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderTopWidth: 0,
borderRightWidth: 45,
borderBottomWidth: 90,
borderLeftWidth: 45,
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: 'red',
borderLeftColor: 'transparent',
},
Why not use a dedicated library? https://github.com/react-native-community/react-native-svg