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

@carimus/react-native-theme-provider

v1.1.0

Published

A wrapper around @callstack/react-theme-provider with some RN-specific goodies.

Downloads

22

Readme

@carimus/react-native-theme-provider

A React Native package that wraps @callstack/react-theme-provider to add in some React Native-specific goodies.

See the react-theme-provider's docs for usage details.

Additions

withStyleSheet

When you call createTheming, the returned object will also contain a withStyleSheet property which creates a HOC when called with styles that injects a styleSheet into the component props.

In short this allows you to declare a component's stylesheet as a function of the theme, analogous to Material UI's withStyles.

Usage

withStyleSheet(styles, [options]) => ((YourComponent) => ThemedComponent)

Calling withStyleSheet returns a higher order component which you call with your component in order to inject a stylesheet based on the theme into your component.

Arguments

  1. styles ((function(theme: object, props?: object): object) | object): If an object, styles is passed through StyleSheet.create() and given straight to the wrapped component as styleSheet. If it's a function, it will be called with the theme (and possibly the props as the second arg if options.withProps is set to true, not the default) and it should return an object which will be passed through StyleSheet.create() and given to the wrapped component as styleSheet.
  2. options (object optional):
    • options.name (string; optional): Used when generating the display name of the wrapper component, otherwise YourComponent.displayName || YourComponent.name is used.
    • options.propName (string; optional, defaults to 'styleSheet'): The name of the prop to pass the generated and compiled stylesheet as to YourComponent
    • options.forwardTheme (boolean; optional, defaults to true): If true (the default), the theme will be passed into YourComponent on the theme prop.
    • options.withProps (boolean; optional, defaults to false): This has no effect if styles is an object. If true, the incoming props will be passed to your styles function as the second argument. Important note: this disables caching of the compiled stylesheet. The performance hit is marginal but if performance is crucial to this component, you should instead prefer to conditionally apply styles in your styleSheet in the render function of your component based on the props of interest, and, of course, appropriately implement shouldComponentUpdate or similar.
    • options.propagate (boolean; optional, defaults to true): If true (the default), will also wrap YourComponent in the ThemeProvider so that the final theme (i.e. including any merged in styles) will propagate into the subtree. This isn't strictly necessary if the theme is never modified and you have the ThemeProvider at the root of your app, but if you manually update the theme by providing a theme prop which will be merged into the contextual theme per the behaviour of withTheme, then you need to leave this as true in order for themed components in the subtree to use the same modified theme. If you don't want this (i.e. if you want components in the subtree to use the original theme), set this to false.

Notes

  • The returned function is a HOC which you use to wrap your component. All props will be forwarded to your component with the addition of the styleSheet and theme props (both are configurable via options).
  • theme is the original theme object that was passed to createTheming
  • styleSheet is the result of StyleSheet.create() called on the styles object (or the result of calling styles if it's a function given the theme and optionally the props).
  • If styles is a function and options.withProps is false, the result of calling styles with a given theme is cached. styles won't be called again to generate the styleSheet on future renders unless the theme changes.
  • withStyleSheet implies withTheme so all behaviour that withTheme exhibits applies to withStyleSheet as well. For example, the component can be called with a theme prop and that theme will be merged into the contextual theme.

Example

theme.js

import { createTheming } from '@carimus/react-native-theme-provider';

const theme = {
    palette: {
        background: '#000000',
        foreground: '#FFFFFF',
    },
};

export const {
    ThemeProvider,
    withTheme,
    useTheme,
    withStyleSheet,
} = createTheming(theme);

FooComponent.js

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

import { withStyleSheet } from './theme';

const styles = (theme) => ({
    root: {
        backgroundColor: theme.palette.background,
    },
    text: {
        color: theme.palette.foreground,
    },
});

function FooComponent({ styleSheet, children }) {
    return (
        <View style={styleSheet.root}>
            <Text style={styleSheet.text}>{children}</Text>
        </View>
    );
}

export default withStyleSheet(styles)(FooComponent);

TODO

  • [ ] [withStyleSheet] Advanced caching in order to cache compiled styleSheets even when withProps is true.
  • [ ] Some generic HOC that isn't dependent on a theme (i.e. isn't a result of createTheming) that can still somehow accept a theme dynamically from context and apply/merge it in to a default theme. For generic components to except a theme somehow but have sane defaults.