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

↔️ Hoc to easily map window sizes to props.

NotificationsYou must be signed in to change notification settings

renatorib/react-sizes

Repository files navigation

react-sizes

npmnpmGitHub issuesGitHub starsTwitter

Install

yarn add react-sizes
npm install react-sizes

What and why

React Sizes is a higher-order component with strong performance that transforms window sizes (width and height) into props.
You can check inside your component, for example, if user's window is less than 480 pixels of width, and add a customcontent.

It can be very powerful for when you need to display different content for mobile and desktop.But it's not limited to this case. Just use that at your needs.

Usage

With class component.

importReact,{Component}from'react'importwithSizesfrom'react-sizes'classMyComponentextendsComponent{render(){return<div>{this.props.isMobile ?'Is Mobile' :'Is Not Mobile'}</div>}}constmapSizesToProps=({ width})=>({isMobile:width<480,})exportdefaultwithSizes(mapSizesToProps)(MyComponent)

You can play with this examplehere.

As decorator.

importReactfrom'react'importwithSizesfrom'react-sizes'@withSizes(({ width})=>({isMobile:width<480}))classMyComponentextendsComponent{render(){return<div>{this.props.isMobile ?'Is Mobile' :'Is Not Mobile'}</div>}}exportdefaultMyComponent

Interoperate with other libraries.

importReactfrom'react'importwithSizesfrom'react-sizes'import{withState,compose}from'recompose'constenhancer=compose(withState('counter','setCounter',0),withSizes(({ width})=>({isMobile:width<480})))constMyComponent=enhancer(({ isMobile, counter, setCounter})=>(<div><div>      Count:{counter}{' '}<buttononClick={()=>setCounter(n=>n+1)}>Increment</button></div><div>{isMobile ?'Is Mobile' :'Is Not Mobile'}</div></div>))exportdefaultMyComponent

With functional component.

importReactfrom'react'importwithSizesfrom'react-sizes'constMyComponent=({ isMobile})=>(<div>{isMobile ?'Is Mobile' :'Is Not Mobile'}</div>)constmapSizesToProps=({ width})=>({isMobile:width<480,})exportdefaultwithSizes(mapSizesToProps)(MyComponent)

Mess with props.

(Added in 0.1.0)

importReactfrom'react'importwithSizesfrom'react-sizes'constMyComponent=({ isMobile})=>(<div>{isMobile ?'Is Mobile' :'Is Not Mobile'}</div>)constmapSizesToProps=({ width},{ mobileBreakpoint})=>({isMobile:width<mobileBreakpoint,})exportdefaultwithSizes(mapSizesToProps)(MyComponent)

then:

<MyComponentmobileBreakpoint={480}/><MyComponentmobileBreakpoint={400}/><MyComponentmobileBreakpoint={600}/>

With presets selectors.

- const mapSizesToProps = ({ width }) => ({-   isMobile: width < 480,- });+ const mapSizesToProps = sizes => ({+  isMobile: withSizes.isMobile(sizes),+ });

Presets Selectors

You can check allour presets selectors at our main codesrc/withSizes.js.

withSizes.isMobile=({ width})=>width<480withSizes.isTablet=({ width})=>width>=480&&width<1024withSizes.isDesktop=({ width})=>width>=1024withSizes.isGtMobile=sizes=>!withSizes.isMobile(sizes)withSizes.isGtTablet=sizes=>withSizes.isDesktop(sizes)withSizes.isStTablet=sizes=>withSizes.isMobile(sizes)withSizes.isStDesktop=sizes=>!withSizes.isStDesktop(sizes)withSizes.isTabletAndGreater=sizes=>!withSizes.isMobile(sizes)withSizes.isTabletAndSmaller=sizes=>!withSizes.isStDesktop(sizes)

If it don't fit to your needs, you can create your own selectors.

// utils/sizes/selectors.jsexportconstisntDesktop=({ width})=>width<1024exportconstbackgroundColor=({ width})=>(width<480 ?'red' :'green')// your componentimport{isntDesktop,backgroundColor}from'utils/sizes/selectors'constmapSizesToProps=sizes=>({canDisplayMobileFeature:isntDesktop(sizes),backgroundColor:backgroundColor(sizes),})

sizes argument is an object withwidth andheight properties and represents DOM window width and height.

Guide

mapSizesToProps(sizes)

sizes argument is an object withwidth andheight of DOM window.

constmapSizesToProps=sizes=>{console.log(sizes)// { width: 1200, height: 720 } (example)}

In pratice, it is a callback that return props that will injected into your Component.

constmapSizesToProps=function(sizes){constprops={backgroundColor:sizes.width<700 ?'red' :'green',}returnprops}

But you can simplify this to stay practical and elegant.

constmapSizesToProps=({ width})=>({backgroundColor:width<700 ?'red' :'green',})

Server Side Rendering

Since React Sizes rely on window to computate sizes, we can't computate the values in server enviroment. To try to get around this we canguess user viewport based on your user-agent, and pass values by a Context Provider.
But be careful,user-agent based detection is not a reliable solution. It's a workaround.

// Config can be created based on user-agent. See belowconstconfig={fallbackWidth:360,fallbackHeight:640}return(<SizesProviderconfig={config}><App/></SizesProvider>)

Example:

importMobileDetectfrom'mobile-detect'importExpressfrom'express'import{SizesProvider}from'react-sizes'// All other importsconstgetSizesFallback=userAgent=>{constmd=newMobileDetect(userAgent)if(!!md.mobile()){return{fallbackWidth:360,fallbackHeight:640,}}elseif(!!md.tablet()){return{fallbackWidth:768,fallbackHeight:1024,}}return{fallbackWidth:1280,fallbackHeight:700,}}// Note: you don't need to use express, this is just an exampleconstapp=newExpress()app.use((req,res)=>{// ...constsizesConfig=getSizesFallback(req.headers['user-agent'])constApp=(<AnotherProvider><Routerlocation={req.url}><SizesProviderconfig={sizesConfig}><Root/></SizesProvider></Router></AnotherProvider>)res.status(200)res.send(`<!doctype html>\n${ReactDOM.renderToString(<App/>)}`)res.end()})app.listen(/* ... */)

Performance Notes

Shallow Compare

React Sizes do a shallow compare in props generated frommapSizesToProps (calledpropsToPass), so it will only rerender when they really change. If you create a deep data sctructure, this can generate false positives. In these cases, we recommend using immutable for a more reliable shallow compare result. Or just don't use deep data structures, if possible.

Contribute

You can help improving this project sending PRs and helping with issues.
Also you ping me atTwitter

About

↔️ Hoc to easily map window sizes to props.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors10


[8]ページ先頭

©2009-2025 Movatter.jp