All Projects → intergalacticspacehighway → react-native-responsive-query

intergalacticspacehighway / react-native-responsive-query

Licence: MIT License
Responsive style hook for react native.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-native-responsive-query

Match Media
Universal polyfill for match media API using Expo APIs on mobile
Stars: ✭ 95 (+120.93%)
Mutual labels:  responsive, react-native-web
awrora-starter
Landing page template built with one of most popular javascript library Vue.JS, Vuetify (Material Design) and Nuxt.JS with SSR.
Stars: ✭ 38 (-11.63%)
Mutual labels:  responsive
hope-ui-design-system
Hope UI - Open Source Bootstrap 5 Design System
Stars: ✭ 37 (-13.95%)
Mutual labels:  responsive
react-responsive-spritesheet
React component which helps you to easily apply responsive spritesheet animations on your project.
Stars: ✭ 82 (+90.7%)
Mutual labels:  responsive
motley
CSS Framework based on ITCSS
Stars: ✭ 24 (-44.19%)
Mutual labels:  responsive
devhub
TweetDeck for GitHub - Filter Issues, Activities & Notifications - Web, Mobile & Desktop with 99% code sharing between them
Stars: ✭ 8,064 (+18653.49%)
Mutual labels:  react-native-web
40-lines-of-Sass
Full featured flexbox grid in 40 lines of Sass
Stars: ✭ 20 (-53.49%)
Mutual labels:  responsive
vue-resize-text
A vue directive which automatically resize font size based on element width.
Stars: ✭ 65 (+51.16%)
Mutual labels:  responsive
i-Cut
🔨 个人技术栈
Stars: ✭ 18 (-58.14%)
Mutual labels:  responsive
react-quiz-app
A Simple React Quiz App 💎
Stars: ✭ 37 (-13.95%)
Mutual labels:  responsive
nglp-angular-material-landing-page
NGLP is an Angular Material Landing Page.
Stars: ✭ 32 (-25.58%)
Mutual labels:  responsive
fuse-core
The 'Fuse Core' Jekyll theme.
Stars: ✭ 29 (-32.56%)
Mutual labels:  responsive
breakpoints-js
Awesome Breakpoints in JavaScript. (bootstrap supported)
Stars: ✭ 70 (+62.79%)
Mutual labels:  responsive
pass-culture-app-native
Mobile and web application for pass Culture
Stars: ✭ 18 (-58.14%)
Mutual labels:  react-native-web
GOSH-FHIRworks2020-React-Dashboard
🩺 Fully Responsive FHIR Dashboard written using @reactjs for NHS and GOSH hackathon
Stars: ✭ 21 (-51.16%)
Mutual labels:  responsive
react-native-dimension
A React Native Dimension for Responsive Layout
Stars: ✭ 31 (-27.91%)
Mutual labels:  responsive
gridsome-starter-liebling
Gridsome starter based on the Liebling theme for Ghost. Content is added via markdown, while Tailwind CSS is used for the layout/styling.
Stars: ✭ 21 (-51.16%)
Mutual labels:  responsive
ml-stack-nav
Customizable, responsive, accessible, easy-to-use multi-level stack navigation menu with slide effect.
Stars: ✭ 20 (-53.49%)
Mutual labels:  responsive
react-multiversal
React components that works everywhere (iOS, Android, Web, Node)
Stars: ✭ 43 (+0%)
Mutual labels:  react-native-web
mobx-react-matchmedia
A React HOC with mediaqueries for responsive layout
Stars: ✭ 32 (-25.58%)
Mutual labels:  responsive

React native responsive query

Responsive style hook for React Native apps.

Why?

  • This hook aims to provide an API that can be useful for universal design systems like dripsy, NativeBase and React Native apps that uses responsive styling.
  • It transforms styles to CSS media-query on react native web that can be useful for responsive SSR react native web apps.
  • Read disableCSSMediaQueries section.

Installation

yarn add react-native-responsive-query

// or

npm install react-native-responsive-query

Usage

import { View } from 'react-native';
import { useResponsiveQuery } from 'react-native-responsive-query';

export default function App() {
  const { dataSet, styles } = useResponsiveQuery({
    initial: {
      backgroundColor: 'yellow',
      height: 200,
      width: 200,
    },
    query: [
      {
        minWidth: 400,
        style: {
          backgroundColor: 'pink',
        },
      },
      {
        minWidth: 1200,
        style: {
          backgroundColor: 'black',
        },
      },
      {
        minWidth: 1600,
        style: {
          backgroundColor: 'purple',
        },
      },
    ],
  });

  return <View dataSet={dataSet} style={styles} />;
}
  • Here, the View will have background color yellow as default, pink when width >= 400, black when width >= 1200, purple when width >= 1600. It'll have the height and width equals to 200.

API

useResponsiveQuery(params?: UseResponsiveQueryParams)

UseResponsiveQueryParams
  • initial (optional): ReactNativeStyle
  • query: Array<{minWidth: number, maxWidth: number, style: ReactNativeStyle}>
  • disableCSSMediaQueries (optional): boolean

getResponsiveStyles(params?: UseResponsiveQueryParams)

  • useResponsiveQuery also returns a function named getResponsiveStyles for cases where a hook might be inconvenient.
import { View } from 'react-native';
import { useResponsiveQuery } from 'react-native-responsive-query';

export default function App() {
  const { getResponsiveStyles } = useResponsiveQuery();

  const { dataSet, styles } = getResponsiveStyles({
    initial: {
      height: 200,
      width: 200,
    },
    query: [
      {
        minWidth: 400,
        style: {
          backgroundColor: 'pink',
        },
      },
    ],
  });

  return <View dataSet={dataSet} style={styles} />;
}

Range query

const { styles, dataSet } = useResponsiveQuery({
  query: [
    {
      minWidth: 400,
      maxWidth: 800,
      style: {
        backgroundColor: 'pink',
      },
    },
  ],
});

return <View dataSet={dataSet} style={styles} />;
  • Here, background color pink will be applied only when screen width is >= 400 and <= 800.

Disable CSS media queries

  • You can disable CSS media queries by using disableCSSMediaQueries boolean at the hook level or the Provider level.

Using a Provider

  • The below snippet will disable CSS media queries for all the hooks.
import { ResponsiveQueryProvider } from 'react-native-responsive-query';

function App() {
  return (
    <ResponsiveQueryProvider disableCSSMediaQueries>
      {/* Your app goes here */}
    </ResponsiveQueryProvider>
  );
}

Using hook

  • The below hook will disable CSS media queries for this particular hook.
const { styles } = useResponsiveQuery({
  disableCSSMediaQueries: true,
  query: [
    {
      minWidth: 400,
      style: {
        backgroundColor: 'pink',
      },
    },
  ],
});

return <View style={styles} />;

How it works?

  • It uses dataSet prop to add responsive styling.
  • We're relying on internal/undocumented RNW functions for injecting + generating styles. (most of these functions are pure (and memoises) but the current injecting solution might not be the cleanest). Checkout source

Examples

Credits

  • Thanks to Fernando for the hook idea and helping to shape the hook API :)

NOTE

Media query is currently a web-only feature. RNW recommends on using Dimension listener (which uses window resize event) for responsive layouts which is better IMO but can be limiting if one needs device dimension based styling for SSR apps. You can use disableCSSMediaQueries option if you are not using SSR responsive styling or if it's a client side rendered app. Read more here - #1688 and RNW talk


Contributing

See the contributing guide to learn how to contribute to the repository and the development

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].