New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-native-inherit

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-inherit

Adds inheritable text styles to React Native

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
9
350%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

react native

FAQs

Package last updated on 05 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts