Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

A package that provides a responsive context to your application, using React Context API.

License

NotificationsYou must be signed in to change notification settings

Farfetch/react-context-responsive

This project is no longer being actively maintained.FARFETCH has decided to archive this project, as widely adopted alternatives, likereact-responsive, have become available.

We won't be accepting pull requests or responding to issues for this project anymore. Thank you for your understanding.

A package that provides a responsive context to your application, using React Context API.

It has the same API ofredux-responsive and they are easily interchangeable.

Installation

$ yarn add @farfetch/react-context-responsive$ npm i @farfetch/react-context-responsive

...and include it in your project

import{ResponsiveProvider,useResponsive}from'@farfetch/react-context-responsive';

Guidelines

Provider use

The app, ideally, should have only one<ResponsiveProvider>, usually atapp.js, wrapping all the components.

You can have as much consumers (useResponsive,useIsMobile,Responsive,withResponsive andwithIsMobile) as you need. When the Provider value changes, all the consumers will update.

Preferred consumers

The hooks (useResponsive anduseIsMobile) are the preferred method of using the context, when possible.

Mobile device detection

When possible, use thewithIsMobile anduseIsMobile for mobile devices detection. In the future we might use it to automatically splitting of mobile-only code.

ResponsiveProvider Props

PropTypeRequiredDefaultDescription
initialMediaType'_initial'
|  'xs'
|  'sm'
|  'md'
|  'lg'
|  'xl'
no'xs'Initial media type before the calculation of the real measures
defaultOrientation'landscape'
|  'portrait'
nonullInitial orientation before the calculation of the real measures
childrennodeyes-React component
breakpoints{ xs: string, sm: string, md: string, lg: string, xl: string }no-Min breakpoints
breakpointsMax{ xs: string, sm: string, md: string, lg: string, xl: string }no-Max breakpoints
mediaQueries{ _initial: string, xs: string, sm: string, md: string, lg: string, xl: string }no-Represents the screen media queries(If this is passed, breakpoints and breakpointsMax props are obsolete)
mobileBreakpoint'_initial'
|  'xs'
|  'sm'
|  'md'
|  'lg'
|  'xl'
no-It's considered mobile until this breakpoint

Object returned by the useResponsive / withResponsive / Responsive:

KeyTypeDescription
mediaType'_initial'
|  'xs'
|  'sm'
|  'md'
|  'lg'
|  'xl'
Current breakpoint name
orientationstringCurrent browser orientation
isCalculatedbooleanFalse on first render. Once true, it means all breakpoints values are based on the window.
is{ _initial: boolean, xs: boolean, sm: boolean, md: boolean, lg: boolean, xl: boolean }Object key breakpoint name and value boolean that shows if width is at a certain breakpoint
lessThan{ _initial: boolean, xs: boolean, sm: boolean, md: boolean, lg: boolean, xl: boolean }Object key breakpoint name and value boolean that shows if width is less than a certain breakpoint
greaterThan{ _initial: boolean, xs: boolean, sm: boolean, md: boolean, lg: boolean, xl: boolean }Object key breakpoint name and value boolean that shows if width is greater than a certain breakpoint

Object returned by the useIsMobile / withIsMobile:

KeyTypeDescription
isMobilebooleanIf it's below the mobile breakpoint defined by mobileBreakpoint
isCalculatedbooleanFalse on first render. Once true, it means all breakpoints values are based on the window.

Usage and examples

To use the package, you must embrace your code with theResponsiveProvider, following the guidelines.

The component has five different exported consumption APIs:

  • useResponsive: A hook which returns the responsive object
  • useIsMobile: A hook which returns an object withisMobile andisCalculated
  • Responsive: A render prop component
  • withResponsive: A HoC which passes the responsive data to theresponsive prop
  • withIsMobile: A HoC which passesisMobile andisCalculated props only

How to setup

There are two possible options to configure your responsive provider withbreakpoints or withmediaQueries

Usingbreakpoints andbreakpointsMax

constbreakpoints={xs:"320px",sm:"576px",md:"960px",lg:"1280px",xl:"1800px"};constbreakpointsMax={xs:"319px",sm:"575px",md:"959px",lg:"1279px",xl:"1799px"};constApp=()=>{return(<ResponsiveProviderbreakpoints={breakpoints}breakpointsMax={breakpointsMax}><Content/></ResponsiveProvider>);};exportdefaultApp;

UsingmediaQueries

constmediaQueries={_initial:"(min-width: 0px) and (max-width: 319px)",xs:"(min-width: 320px) and (max-width: 575px)",sm:"(min-width: 576px) and (max-width: 959px)",md:"(min-width: 960px) and (max-width: 1279px)",lg:"(min-width: 1280px) and (max-width: 1799px)",xl:"(min-width: 1800px)"};constApp=()=>{return(<ResponsiveProvidermediaQueries={mediaQueries}><Content/></ResponsiveProvider>);};exportdefaultApp;

How to consume the package

Rendering components withuseResponsive hook. (Preferred method)

constGreetings=()=>{const{ lessThan}=useResponsive();if(lessThan.sm){return(<p>Hello small screen!</p>);}return(<p>Hello medium/big screen!</p>);};exportdefaultGreetings;

Rendering components withuseIsMobile hook. (Preferred method)

constGreetings=()=>{const{ isMobile}=useIsMobile();if(isMobile){return(<p>Hello mobile!</p>);}return(<p>Hello desktop!</p>);};exportdefaultGreetings;

Rendering components withResponsive render prop component

classGreetingsextendsComponent{render(){return(<ResponsiveProvider><Content><Responsive>{(responsive)=>(<Component1currentBreakpoint={responsive.mediaType}/>)}</Responsive><Responsive>{(responsive)=>(<Component2orientation={responsive.orientation}/>)}</Responsive></Content></ResponsiveProvider>)}}exportdefaultGreetings;

Rendering components withwithResponsive High-Order component

classGreetingsextendsComponent{render(){returnthis.props.responsive.lessThan.sm ?<p>Hello small screen!</p> :<p>Hello big/small screen!</p>}}exportdefaultwithResponsive(Greetings);

Rendering components withwithIsMobile High-Order component

classGreetingsextendsComponent{render(){returnthis.props.isMobile ?<p>Hello mobile!</p> :<p>Hello desktop!</p>}}exportdefaultwithIsMobile(Greetings);

Additional notes

The_initial media type

The gap between window width 0 and the first breakpoint is called_initial media type.

It fixes a problem atredux-responsive in the calculation:

If the first breakpoint starts in a number bigger than 0 (let's call it X), it considers that everything between 0 and X as the first breakpoint when it's not true.

For example, our breakpoints start at 320 (XS),redux-responsive considers 270 as XS, a wrong calculation. We call it_initial.

React compatibility

React >=16.8.0 is required to use this package as theResponsiveProvider is hook-based.

The non-hook APIs just expose theuseResponsive hook with different APIs, for compatibility with class components.

Contributing

Read theContributing guidelines

Disclaimer

By sending us your contributions, you are agreeing that your contribution is made subject to the terms of ourContributor Ownership Statement

Maintainers

License

MIT

About

A package that provides a responsive context to your application, using React Context API.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors10


[8]ページ先頭

©2009-2025 Movatter.jp