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-inherit

v0.1.0

Published

Adds inheritable text styles to React Native

Downloads

5

Readme

React Native Inherit

Adds style inheritance to React Native.

Allows you to style text components in groups as you would in regular CSS by applying styles to a higher up component.

Example use cases:

  • Apply a global font family, size, and weight to all <Text> components by wrapping your whole app in <StyleProvider>
  • If your global font color is black, but inside e.g. your <Card> component all text should be gray, you can set the color of the card component to gray and it will cascade to only the <Text> components inside the <Card> component

Installation

npm i react-native-inherit

Usage

The main thing to note is you need to use the exported <Text> component from react-native-inherit instead of the one exported from react-native (the api is exactly the same). This allows us to pull inherited styles from higher up context.

Then to have styles cascade down, use either the <StyleProvider> component, or our re-exported React Native components such as View or TouchableOpacity that contain a special styleProvider prop.

import { Text, StyleProvider, View } from 'react-native-inhert';

// note that Text and View are imported from react-native-inherit

export default function App() {
  return (
    <View>
      <StyleProvider style={{ color: 'green', fontWeight: 'bold' }}>
        <View styleProvider={{ color: 'red', fontSize: 32 }}>
          <Text>Some Red Text</Text>
          <Text style={{ color: 'blue' }}>Some Blue Text</Text>
        </View>
        <Text>Some green text</Text>
      </StyleProvider>
    </View>
  );
}

This produces the following:

Screenshot of the above code output

How it works

Both the <StyleProvider> component and the re-exported React Native components with the styleProvider prop simply wrap a context that accepts the following styles:

export type InheritableTextStyles = Pick<
  TextStyle,
  | 'color'
  | 'fontSize'
  | 'fontFamily'
  | 'fontWeight'
  | 'fontVariant'
  | 'fontStyle'
  | 'lineHeight'
  | 'letterSpacing'
  | 'textAlign'
  | 'textTransform'
>;

Inheritable styles are influenced from the DOM https://web.dev/learn/css/inheritance/#which-properties-are-inheritable

Then in our <Text> component we merge all (including nested) provided styles and forward them onto the base React Native <Text> component.