0.4.0 • Published 1 year ago

react-native-slot-machine v0.4.0

Weekly downloads
147
License
MIT
Repository
github
Last release
1 year ago

react-native-slot-machine

Text slot machine for react-native is an easy and fully customizable Slot Machine for React Native app.

Usage

<View>
    <SlotMachine text={1234} />
    <SlotMachine text="hello" range="abcdefghijklmnopqrstuvwxyz" />
</View>

Props

The following props can be used to modify the slot machine's style and/or behaviour:

PropTypeOpt/RequiredDefaultNote
textString|NumberRequired0The text the slot machine animates to.
widthNumberOptional37The width of each slot.
heightNumberOptional50The height of each slot.
paddingNumberOptional4minimum number of slots. Added slots will be filled with 'defaultChar'
durationNumberOptional2000The total time of the animation of all the slots.
delayNumberOptional4Time to wait since componentDidMount until animation begins.
slotIntervalNumberOptional500The added animation time per slot. last slot animation time = 'duration'.
defaultCharNumberOptional0The default character to be used until animation starts & with 'padding'
rangeStringOptional9876543210The range of characters to be used when animating the slot machine.
initialAnimationBooleanOptionaltrueShould initial animation be activated or only subsequent text changes animations
renderTextContentFunctionOptional(char, index, range) => charAllows replacing the inner content of the Text element
renderContentFunctionOptional(char, index, range) => charAllows replacing the entire Text element with your own implementation
stylesObjectOptional{}Allows overriding each of the inner components (container, slotWrapper, slotInner, innerBorder, outerBorder, overlay, text)
useNativeDriverBooleanOptionalfalseEnable use of NativeDriver on Animation. See https://facebook.github.io/react-native/docs/animations.html#using-the-native-driver

Methods

spinTo(text)

Spins the slot machine from current position to the specified text position.

Advanced Example

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {duration: 4000, slot1: 1234, slot2: 'hello', slot3: '2351'};
    }

    componentDidMount() {
        setTimeout(() => this.setState({duration: 1000, slot1: '4321', slot2: 'world', slot3: '1234'}), 5000);
        setTimeout(() => this.setState({duration: 4000, slot1: '1234', slot2: 'hello', slot3: '2351'}), 7000);
        setTimeout(() => this.refs.slot.spinTo('prize'), 12000);
    }
    render() {
        const symbols = ['🍏', '🍎', '🍐', '🍊', '🍋', '🍌']; // can't use emojies in SlotMachine because some of them are comprised of 2 chars
        return (
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <View style={{height: 200, justifyContent: 'space-between', alignItems: 'center'}}>
                    <SlotMachine text={this.state.slot1} duration={this.state.duration} />
                    <SlotMachine
                        text={this.state.slot2}
                        range="abcdefghijklmnopqrstuvwxyz"
                        width={45} duration={this.state.duration}
                        ref="slot"
                    />
                    <SlotMachine text={this.state.slot3} range="012345" renderContent={c => <Text style={{fontSize: 25}}>{symbols[c]}</Text>} duration={this.state.duration} />
                </View>
            </View>
        );
    }
}