npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-global-props

v1.1.5

Published

A simple javascript implementation to add custom, default props to react-native components.

Downloads

21,340

Readme

react-native-global-props

A simple javascript implementation to add custom, default props to react-native components.

Ever wish you could set global styles that would apply to react-native components like in CSS?

body {
    background-color: 'teal'
}
p {
    font-size: 12,
    font-family: 'Helvetica Neue'
}

or get rid of that ugly line at the bottom of every TextInput in Android?

BLEGH! Now you can!

Installation

npm i react-native-global-props --save

How to use

The example source code can be found under example/.

Once you've installed react-native-global-props, go ahead choose which components you want to add custom props to. In my example, I want to customize the View, TextInput, Text, Image, and TouchableOpacity.

Go to your highest order component that contains your whole application. In my case that is example/Main.js.

First I import the set functions that will allow me to create my custom components.

import {
  setCustomView,
  setCustomTextInput,
  setCustomText,
  setCustomImage,
  setCustomTouchableOpacity
} from 'react-native-global-props';

Then I create the custom props I want to add to each of these components.

// Setting a default background color for all View components.
const customViewProps = {
  style: {
    backgroundColor: '#d3d3d3' // light gray
  }
};

// Getting rid of that ugly line on Android and adding some custom style to all TextInput components.
const customTextInputProps = {
  underlineColorAndroid: 'rgba(0,0,0,0)',
  style: {
    borderWidth: 1,
    borderColor: 'gray',
    paddingVertical: 5,
    paddingHorizontal: 10,
    backgroundColor: 'white'
  }
};

// Setting default styles for all Text components.
const customTextProps = {
  style: {
    fontSize: 16,
    fontFamily: Platform.OS === 'ios' ? 'HelveticaNeue' : 'Roboto',
    color: 'black'
  }
};

// Makes every image resize mode cover by default.
const customImageProps = {
  resizeMode: 'cover'
};

// Adds a bigger hit box for all TouchableOpacity's.
const customTouchableOpacityProps = {
  hitSlop: { top: 15, right: 15, left: 15, bottom: 15 }
};

You can pass in any valid props as seen on react-native documentation. All of these props can easily be overridden.

Now plug your custom props into the set functions like so

setCustomView(customViewProps);
setCustomTextInput(customTextInputProps);
setCustomText(customTextProps);
setCustomImage(customImageProps);
setCustomTouchableOpacity(customTouchableOpacityProps);

And Voila, your react-native components will have these certain props wherever you use them.

Here is an example of me using the components and overriding props. The below example source code can be found in example/src/App.js

import React from 'react';
import {
  View,
  TextInput,
  Text,
  Image,
  TouchableOpacity
} from 'react-native';

const images = {
  whosThatCoolCat: require('./img/MyNormPic.jpeg')
};

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputValue: '',
      hiddenText: true
    }
  }
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text style={{ marginBottom: 20 }}>react-native-global-props</Text>
        <Image
          source={images.whosThatCoolCat}
          style={{ width: 80, height: 80, borderRadius: 40 }}
        />
        <View style={{ marginVertical: 20 }}>
          <TextInput
            style={{ width: 200, height: 30 }}
            placeholder="Enter text"
            onChangeText={(text) => this.setState({ inputValue: text })}
            value={this.state.inputValue}
          />
        </View>
        <TouchableOpacity onPress={() => this.setState({ hiddenText: !this.state.hiddenText })}>
          <View style={{ backgroundColor: 'orange' }}>
            <Text style={{ color: 'white' }}>Press me to show or hide the input text</Text>
          </View>
        </TouchableOpacity>
        <Text style={{ color: 'red' }}>{this.state.inputValue}</Text>
      </View>
    );
  }
}

export default App;

And this is the result

react-native-global-props demo

This module is useful for

1.) Creating an amazing modular experience with your react-native components

2.) Adding customization to react-native components without having to create completely new ones

3.) Setting global styles like fontFamily and color

And much more!

Contributing

Please contribute! All contributions are greatly appreciated no matter how small or large the contribution is. Whether it's a small grammar fix in the README, a huge bug fix, or just an issue report, you will be recognized as a 'Contributor' to this project.

Some great contributions would be to add customization for the components that currently aren't implemented. The following components have been implemented.

  • Image
  • ListView
  • Modal
  • Picker
  • Refresh Control
  • ScrollView
  • Slider
  • StatusBar
  • Switch
  • Text
  • TextInput
  • TouchableWithoutFeedback
  • TouchableOpacity
  • TouchableHighlight
  • TouchableNativeFeedback
  • View
  • WebView
  • KeyboardAvoidingView
  • ActivityIndicator

If you have any questions that can't be answered in the Issues forum under this repository, then feel free to email me at [email protected].