|
| 1 | +import * as React from "react"; |
| 2 | +import { StyleSheet, View, Text } from "react-native"; |
| 3 | +import { Children, cloneElement, isValidElement } from "react"; |
| 4 | + |
| 5 | +function TextStroke(props) { |
| 6 | + const {color, stroke, children} = props; |
| 7 | + const strokeW = stroke; |
| 8 | + const top = createClones(0, -strokeW * 1.2, color); |
| 9 | + const topLeft = createClones(-strokeW, -strokeW, color); |
| 10 | + const topRight = createClones(strokeW, -strokeW, color); |
| 11 | + const right = createClones(strokeW, 0, color); |
| 12 | + const bottom = createClones(0, strokeW, color); |
| 13 | + const bottomLeft = createClones(-strokeW, strokeW, color); |
| 14 | + const bottomRight = createClones(strokeW, strokeW, color); |
| 15 | + const left = createClones(-strokeW * 1.2, 0, color); |
| 16 | + |
| 17 | + function createClones(w, h, color) { |
| 18 | + return Children.map(children, child => { |
| 19 | + if (isValidElement(child)) { |
| 20 | + const currentProps = child.props; |
| 21 | + const currentStyle = currentProps ? (currentProps.style || {}) : {}; |
| 22 | + |
| 23 | + const newProps = { |
| 24 | + ...currentProps, |
| 25 | + style: { |
| 26 | + ...currentStyle, |
| 27 | + textShadowOffset: { |
| 28 | + width: w, |
| 29 | + height: h |
| 30 | + }, |
| 31 | + textShadowColor: color, |
| 32 | + textShadowRadius: 0.1 |
| 33 | + } |
| 34 | + } |
| 35 | + return cloneElement(child, newProps) |
| 36 | + } |
| 37 | + return child; |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + return( |
| 42 | + <View> |
| 43 | + <View style={ styles.outline }>{ left }</View> |
| 44 | + <View style={ styles.outline }>{ right }</View> |
| 45 | + <View style={ styles.outline }>{ bottom }</View> |
| 46 | + <View style={ styles.outline }>{ top }</View> |
| 47 | + <View style={ styles.outline }>{ topLeft }</View> |
| 48 | + <View style={ styles.outline }>{ topRight }</View> |
| 49 | + <View style={ styles.outline }>{ bottomLeft }</View> |
| 50 | + { bottomRight } |
| 51 | + </View> |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +const styles = StyleSheet.create({ |
| 56 | + outline: { |
| 57 | + position: 'absolute', |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +export { TextStroke }; |
0 commit comments