Coder Social home page Coder Social logo

verypossible / react-native-responsive-theme-provider Goto Github PK

View Code? Open in Web Editor NEW
2.0 5.0 0.0 119 KB

react-native theme provider for styled-components

TypeScript 80.25% JavaScript 19.75%
react-native styled-components theme-provider responsive

react-native-responsive-theme-provider's Introduction

react-native-responsive-theme-provider

Dimensions aware styled-components theme provider for responsive theming in react-native

Requirements

  • react
  • react-native
  • styled-components

Features

  • Dimensions aware theme for styled-components
  • Theme will update with new screen and window properties when orientation changes
  • Typescript support

Why?

When building react-native applications there are different orientations and different screen sizes that need to be supported. In order to detect the resolution of the screen, we can use the react native Dimensions API. Using styled-components, it would be easy to do something like this:

import { Dimensions, Text } from 'react-native';
import { ThemeProvider } from 'styled-components/native';

const theme = {
  bigColor: 'blue',
  color: 'red',
  screen: Dimensions.get('screen'),
  window: Dimensions.get('window'),
};

const ThemedText = styled(Text)`
  color: ${(props) => props.theme.screen.width > 800 ? props.theme.bigColor : props.theme.color}
`

const App = () => (
  <ThemeProvider theme={theme}>
    <ThemedText>Some text</ThemedText>
  </ThemeProvider>
);

This is great, but what happens when the user goes from portrait to landscape? Using this code, the screen width will not update to the orientation change.

So the primary goal of this library to provide a theme that will update when the screen orientation changes.

How?

All end-developers need to do is install the library

yarn add @very/react-native-theme-provider
import { Dimensions, Text } from 'react-native';
import ThemeProvider from 'react-native-responsive-theme-provider';

const theme = {
  bigColor: 'blue',
  color: 'red',
};

const ThemedText = styled(Text)`
  color: ${(props) => props.theme.screen.width > 800 ? props.theme.bigColor : props.theme.color}
`

const App = () => (
  <ThemeProvider theme={theme}>
    <ThemedText>Some text</ThemedText>
  </ThemeProvider>
);

Under the hood we are listening for Dimensions changes and updating the theme object with new screen and width properties.

Typescript

import { Text } from 'react-native';
import { ThemedStyledComponentsModule } from 'styled-components';
import styledComponents from 'styled-components/native';
import ThemeProvider, { ThemeDimensionProps } from 'react-native-responsive-theme-provider';

interface ThemeInterface {
  bigColor: string;
  color: string;
}

const { default: styled } = styledComponents as ThemedStyledComponentsModule<
  ThemeInterface & ThemeDimensionProps
>;

const theme: ThemeInterface = {
  bigColor: 'blue',
  color: 'red',
};

// now we have typed support for props.theme
const ThemedText = styled(Text)`
  color: ${(props) => props.theme.screen.width > 800 ? props.theme.bigColor : props.theme.color}
`

const App = () => (
  <ThemeProvider theme={theme}>
    <ThemedText>Some text</ThemedText>
  </ThemeProvider>
);

Selectors

getScreen

alias for props.theme.screen

getScreenWidth

alias for props.theme.screen.width

getWindow

alias from props.theme.window

getWindowWidth

alias from props.theme.window.width

import { Dimensions, Text } from 'react-native';
import ThemeProvider, { getScreenWidth } from 'react-native-responsive-theme-provider';

const theme = {
  bigColor: 'blue',
  color: 'red',
};

const ThemedText = styled(Text)`
  color: ${(props) => getScreenWidth(props) > 800 ? props.theme.bigColor : props.theme.color}
`

const App = () => (
  <ThemeProvider theme={theme}>
    <ThemedText>Some text</ThemedText>
  </ThemeProvider>
);

react-native-responsive-theme-provider's People

Contributors

neurosnap avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.