New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dokuhero/react-native-theme

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dokuhero/react-native-theme

Theme and stylesheet manager for ReactNative.

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React Native Theme

Theme and stylesheet manager for ReactNative.

Installation

Using npm:

npm i -S @dokuhero/react-native-theme

or yarn:

yarn add @dokuhero/react-native-theme

Usage

Create your themes

You'll use createTheme function to create new theme. Call this function without any parameter will produce default theme values.

import { createTheme } from '@dokuhero/react-native-theme'

export const dark = createTheme({
  id: 'dark',
  name: 'dark',
  color: {
    primary: '#3878c7',
    secondary: '#44d4fc'
  }
  // just follow intellisense to override default theme values...
  // you may also create new key value here.
})

You may also override your existing theme as if needed

import { createTheme } from '@dokuhero/react-native-theme'

export const dark = createTheme(light, {
  id: 'light',
  name: 'light',
  color: {
    primary: '#ffff'
  }
  // ....
}

Theme Provider

Put ThemeProvider (a React context provider for theme) on your top View of application.

import { Theme, ThemeProvider } from '@dokuhero/react-native-theme'

export class MainScreen extends React.Component<Partial<StateProps>> {
  render() {
    return (
      <ThemeProvider value={light}>
        <View style={{ flex: 1 }}>{/* rest of code.. */}</View>
      </ThemeProvider>
    )
  }
}

Theme Consumer

To consume theme, you can use ThemeConsumer (a React context consumer), or use withTheme HOC function or also withThemeClass class decorator.

ThemeConsumer
const TheComponent: React.SFC = (({children})) => (
  <ThemeConsumer>
    {({ color, space, radius }) => (
      <View style={{
          backgroundColor: color.dark,
          padding: space.medium,
          borderRadius: radius.small
        }}>
        {children}
      </View>
    )}
  </ThemeConsumer>
)
withTheme HOC
interface TheComponentProps {
  children?: React.ReactNode
}

const TheComponent: withTheme<TheComponentProps>(({children, color, space, radius}) => (
  <View style={{
      backgroundColor: color.dark,
      padding: space.medium,
      borderRadius: radius.small
    }}>
    {children}
  </View>
))
withThemeClass class decorator
import { withThemeClass, WithThemeProps } from '@dokuhero/react-native-theme'

interface TheComponentProps extends WithThemeProps {
  children?: React.ReactNode;
}

@withThemeClass()
export class TheComponent extends React.Component<TheComponentProps> {
  render() {
    const {
      theme: { children, color, space, radius }
    } = this.props

    return (
      <View
        style={{
          backgroundColor: color.dark,
          padding: space.medium,
          borderRadius: radius.small
        }}
      >
        {children}
      </View>
    )
  }
}

Themed Stylesheet

We are using react-native-extended-stylesheet to build our Stylesheet module.

createStyleSheet

This is to define your style sheet.

// styles.ts

import {
  ColorKeys,
  createStyleSheet,
  RadiusKeys,
  SpaceKeys
} from '@dokuhero/react-native-theme'

export const styles = createStyleSheet({
  container: {
    flex: 1
  },
  form: {
    backgroundColor: ColorKeys.primary,
    paddingBottom: SpaceKeys.medium,
    borderRadius: RadiusKeys.small,
    alignContent: 'center'
  },
  formContainer: { width: '100%', padding: SpaceKeys.medium }
})

Using it as usually

import { styles } from './styles'

export class MyForm extends React.Component {
  render() {
    const { children } = this.props
    return <View style={styles.form}>{children}</View>
  }
}
buildStyleSheet

To make it work, you need to call this function in the very beginning of your app.

buildStyleSheet(light)

And you may call this function again to change theme.

License

MIT

Keywords

FAQs

Package last updated on 21 Sep 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc