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-themed-stylesheet

v0.3.3

Published

React Native StyleSheets with Theming Support

Downloads

12

Readme

codecov Version NPM Downloads License

react-native-themed-stylesheet

A package that allows you to create React Native StyleSheets with support for Dark/Light/Auto Themes.

  • Simple API
  • Fully typed
  • Builds on top of StyleSheets and Hooks
  • Storybook addon to change Theme Mode

Installation

Using Yarn

yarn add react-native-themed-stylesheet

Using NPM

npm install --save react-native-themed-stylesheet

Usage

Create a type declaration file to merge BaseTheme declaration:

// react-native-themed-stylesheet.d.ts
import 'react-native-themed-stylesheet'

declare module 'react-native-themed-stylesheet' {
  export interface BaseTheme {
    theme: {
      colors: {
        primary: string
      }
    },
    constants: {  // This is optional. If declared/defined, it will be merge with the current theme.
      colors: {
        accent: string
      }
    }
  }
}

Using the theme:

// App.tsx
import React from 'react'
import { View, Text, Button } from 'react-native'
import { ThemeProvider, useCreateStyles, useMode, useTheme, useThemes } from 'react-native-themed-stylesheet'

const DefaultComponent: React.FC = () => {
  const { colors } = useTheme()
  const [mode, setMode] = useMode()
  const [themes, setThemes] = useThemes()
  console.log('Current Mode:', mode)
  console.log('Themes:', themes)
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text style={{ color: colors.primary, backgroundColor: colors.accent }}>Hello World</Text>
      <Button title='Dark Mode' onPress={() => setMode('dark')}/>
      <Button title='Light Mode' onPress={() => setMode('light')}/>
      <Button title='Auto Mode' onPress={() => setMode('auto')}/>
      <Button title='Change Themes' onPress={
          () => setThemes({
            light: {
              colors: { primary: '#c1c1c1' }
            },
            dark: {
              colors: { primary: '#c2c2c2' }
            },
            constants: {
              colors: { accent: '#c3c3c3' }
            }
          })
        }
      />
    </View>
  )
}

const ComponentWithUseCreateStyles: React.FC = () => {
  const [styles, theme] = useCreateStyles(({ colors }) => {
    text: {
      color: colors.primary,
      backgroundColor: colors.accent
    }
  }
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text style={styles.text}>Hello World</Text>
      <Text style={{ color: theme.colors.accent }}>Hello World 2</Text>
    </View>
  )
}

const themes = {
  dark: {
    colors: {
      primary: '#000'
    }
  },
  light: {
    colors: {
      primary: '#fff'
    }
  },
  constants: {
    colors: {
      accent: '#c0c0c0'
    }
  }
}

const App: React.FC = () => (
  <ThemeProvider themes={themes}>
    <DefaultComponent />
    <ComponentWithUseCreateStyles />
  </ThemeProvider>
)

export default App

Storybook Addon

Installation:

// storybook.js
import {
  getStorybookUI,
  configure
} from '@storybook/react-native'
import 'react-native-themed-stylesheet/storybook/register'

configure(() => {
  require('path/to/some/story')
}, module)

const StorybookUIRoot = getStorybookUI()

export default StorybookUIRoot // Make sure to use this component within ThemeProvider.

API

React Component: ThemeProvider

Component to provide ThemeContext.

Props

  • themes: An object of type Themes:
type Themes = {
  dark: BaseTheme['theme']
  light: BaseTheme['theme']
  constants?: BaseTheme['constants']
}
  • mode: An optional string of type ThemeMode: (Default: 'auto')
type ThemeMode = 'auto' | 'dark' | 'light'

Function: useCreateStyle(createStyles)

Hook to create themed stylesheets.

Parameters

  • createStyles: A function that receives the current theme and returns an object of type Styles.
type Styles  = {
  [prop: string]: ViewStyle | TextStyle | ImageStyle
}
type Theme = (BaseTheme['constants'] & BaseTheme['theme']) | BaseTheme['theme']

Returns

[styles, theme]: [StyleSheet.NamedStyles<Styles>, Theme]

Function: useMode()

Hook to get access to current mode.

Returns

[mode, setMode]: [ThemeMode, (newMode: ThemeMode) => void]

Function: useTheme()

Hook to get access to current theme.

Returns

theme: Theme

Function: useThemes()

Hook to get access to themes.

Returns

[themes, setThemes]: [Themes, (newThemes: Themes) => void]