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

@anvilapp/react-native-themes

v0.8.1

Published

A lightweight library for managing themes in React Native applications.

Downloads

3

Readme

@anvilapp/react-native-themes

This package provides a set of utility functions for managing and consuming themes in a React Native application.

Installation

npm install @anvilapp/react-native-themes

or

yarn add @anvilapp/react-native-themes

Usage

To start using the package, first define your set of themes and their respective colors. This is done using the CreateThemesType type from the package:

import { CreateThemesType } from '@anvilapp/react-native-themes/types';

type ThemeKeys = 'light' | 'dark';
type ColorKeys = 'textColor' | 'backgroundColor';
type Themes = CreateThemesType<ThemeKeys, ColorKeys>;

const themes: Themes = {
    light: {
        textColor: '#000',
        backgroundColor: '#fff',
    },
    dark: {
        textColor: '#fff',
        backgroundColor: '#000',
    },
};

Once you have defined your set of themes and colors, you can create a set of utility functions for managing and consuming themes using the createThemes function:

import { createThemes } from '@anvilapp/react-native-themes';

const {
    useTheme,
    createStyles,
    useStyles, 
    setTheme,
    getTheme,
    getColors, 
    registerListener,
} = createThemes(themes, 'light');

You can then use these utility functions throughout your application to manage and consume themes. For example, you can create styles that depend on the currently active theme using the createStyles function:

const useStyles = createStyles(({ colors }) => ({
  text: {
    color: colors.textColor,
  },
  background: {
    textAlign: 'auto',
    color: colors.backgroundColor,
  },
}));

const MyComponent = () => {
  const { styles, colors, theme } = useStyles();

  return <Text style={styles.text}>Hello</Text>;
};

The following example demonstrates how you can pass props to a StyleCreator function to create dynamic styles based on component props. This allows you to create highly reusable components that can be styled in a wide variety of ways, depending on the props that are passed in.

In this example, we define a function called createStylesWithProps that takes a size prop and returns a StyleCreator function that creates styles based on the current theme and color scheme. The size prop is used to set the font size of the text style.

In the MyComponent component, we use the useMemo hook to create the StyleCreator function with the current size prop value. We then use the useStyles hook to create a set of styles based on the StyleCreator function. Finally, we use the styles object returned by useStyles to style our View and Text components.

const createStylesWithProps = (size: number) => createStyles(({ colors }) => ({
  text: {
    color: colors.textColor,
    fontSize: size,
  },
  container: {
    textAlign: 'auto',
    color: colors.backgroundColor,
  },
}));

const MyComponent = ({ size }) => {
  const useStyles = useMemo(() => createStylesWithProps(size), [size]);
  const { styles } = useStyles();

  return (
    <View style={styles.container}>
      <Text style={styles.text}>Hello, world!</Text>
    </View>
  );
};

Use the useTheme hook in a component

const MyComponent = () => {
  const { theme, colors } = useTheme();

  return (
    <View style={{ backgroundColor: colors.backgroundColor }}>
      <Text style={{ color: colors.textColor }}>Hello, world!</Text>
    </View>
  );
};

Use the useStyles hook to create a set of styles

const MyComponent = () => {
  const { styles, colors, theme } = useStyles(({ colors }) => ({
      container: {
          backgroundColor: colors.backgroundColor,
          padding: 20,
      },
      text: {
          color: colors.textColor,
          fontSize: 18,
      },
  }));

  return (
    <View style={styles.container}>
      <Text style={styles.text}>Hello, world!</Text>
    </View>
  );
};

Use the setTheme function to change the current theme

setTheme('dark');

Use the getTheme function to get the current theme name

const currentTheme = getTheme();

Use the getColors function to get the color scheme for a specific theme

const lightColors = getColors('light');

se the registerListener function to be notified when the theme changes

const unregisterListener = registerListener((newTheme) => {
  console.log(`Theme changed to ${newTheme}`);
});

// When you're done with the listener, unregister it
unregisterListener();

API

Types

This package exports several TypeScript types:

  • Themes<T>: An object that maps theme names to their respective color schemes.
  • Colors<T>: A type alias for the values of a Themes object.
  • Styles<T>: An object that maps style names to their respective ViewStyle, TextStyle, or ImageStyle.
  • ThemeListener<T>: A function that is called whenever the current theme changes.
  • StyleCreatorProps<T>: An object that contains the current theme and color scheme, as well as a function for selecting specific values from the theme.
  • StyleCreator<T, S>: A function that takes a StyleCreatorProps object and returns a set of styles.
  • SelectorSpecifics<T, V>: An object that specifies specific values to use for a given theme, or a default value to use if no specific value is provided.
  • Selector<T>: A function that takes a SelectorSpecifics<T, V> object and returns the specified value for the current theme, or the default value if no specific value is provided.
  • CreateThemesType<TK, CK>: An object that maps theme names to objects containing color names and their respective values.
  • CreateColorsType<T>: A type alias for the values of a Themes object.

Functions

This package exports several functions:

  • getTheme<T>(): keyof T: Returns the current theme name.
  • setTheme<T>(theme: keyof T): void: Sets the current theme to the specified value.
  • registerListener<T>(listener: ThemeListener<T>): (() => boolean): Registers a function to be called whenever the current theme changes. Returns a function that can be called to unregister the listener.
  • getColors<T>(theme?: keyof T): Readonly<T[keyof T]>: Returns the color scheme for the specified theme, or the current theme if no theme is specified.
  • useTheme<T>(): { theme: keyof T; colors: Readonly<T[keyof T]> }: Returns an object containing the current theme and color scheme.
  • useStyles<T, S extends Styles<any>>(creator: StyleCreator<T, S>): { styles: Readonly<S>; theme: keyof T; colors: Readonly<T[keyof T]> }: Returns a set of styles created using the specified StyleCreator function.
  • createStyles<T, S extends Styles<any>>(creator: StyleCreator<T, S>): () => { styles: Readonly<S>; theme: keyof T; colors: Readonly<T[keyof T]> }: Returns a function that can be called to create a set of styles using the specified StyleCreator function.
  • createThemes<T>(themesConfiguration: T, defaultTheme: keyof T): { useTheme: () => { theme: keyof T; colors: Readonly<T[keyof T]> }; useStyles: <S extends Styles<any>>(creator: StyleCreator<T, S>) => { styles: Readonly<S>; theme: keyof T; colors: Readonly<T[keyof T]> }; createStyles: <S extends Styles<any>>(creator: StyleCreator<T, S>) => () => { styles: Readonly<S>; theme: keyof T; colors: Readonly<T[keyof T]> }; setTheme: (theme: keyof T) => void; getTheme: () => keyof T; getColors: (theme?: keyof T) => Readonly<T[keyof T]>; registerListener: (listener: ThemeListener<T>) => (() => boolean); }
    Initializes the package with the specified themes and default theme, and returns an object that provides several functions for working with themes and styles:
    • useTheme(): A function that returns an object containing the current theme name and color scheme.
    • useStyles<S extends Styles<any>>(creator: StyleCreator<T, S>): A function that returns a set of styles created using the specified StyleCreator function.
    • createStyles<S extends Styles<any>>(creator: StyleCreator<T, S>): A function that returns a function that can be called to create a set of styles using the specified StyleCreator function.
    • setTheme(theme: keyof T): A function that sets the current theme to the specified value.
    • getTheme(): A function that returns the current theme name.
    • getColors(theme?: keyof T): A function that returns the color scheme for the specified theme, or the current theme if no theme is specified.
    • registerListener(listener: ThemeListener<T>): A function that registers a listener to be called whenever the current theme changes. Returns a function that can be called to unregister the listener.