{"name":"react-responsive","description":"Media queries in react for responsive design","version":"10.0.1","homepage":"http://github.com/yocontra/react-responsive","repository":{"type":"git","url":"git://github.com/yocontra/react-responsive.git"},"author":{"name":"Contra","email":"yo@contra.io","url":"https://contra.io"},"license":"MIT","main":"./dist/cjs/index.js","module":"./dist/esm/index.js","types":"./dist/types/index.d.ts","sideEffects":false,"keywords":["css","react-component","viewport","react","mobile","media queries","respond","media query","matchMedia","responsive","component"],"dependencies":{"hyphenate-style-name":"^1.0.0","matchmediaquery":"^0.4.2","prop-types":"^15.6.1","shallow-equal":"^3.1.0"},"peerDependencies":{"react":">=16.8.0"},"devDependencies":{"@rollup/plugin-typescript":"^12.0.0","@types/chai":"^5.0.0","@types/hyphenate-style-name":"^1.0.0","@types/jsdom":"^21.0.0","@types/match-media-mock":"^0.1.5","@types/matchmediaquery":"^0.3.0","@types/mocha":"^10.0.0","@types/react":"^18.0.0","@types/react-dom":"^18.0.0","@types/sinon":"^17.0.0","@typescript-eslint/eslint-plugin":"^8.0.0","@typescript-eslint/parser":"^8.0.0","chai":"^5.0.0","cross-env":"^7.0.0","eslint":"^9.0.0","eslint-plugin-compat":"^6.0.0","gh-pages":"^6.0.0","jsdom":"^26.0.0","match-media-mock":"^0.1.1","mocha":"^11.0.0","prettier":"^3.0.0","react":"^18.0.0","react-dom":"^18.0.0","rimraf":"^6.0.1","rollup":"^4.0.0","rollup-plugin-node-externals":"^8.0.0","should":"^13.2.1","sinon":"^19.0.0","tslib":"^2.6.2","tsx":"^4.7.1","typedoc":"^0.27.9","typescript":"^5.4.2"},"scripts":{"preversion":"npm run clean && npm run build","postpublish":"npm run tag && npm run docs","prebuild":"npm run clean","build:types":"tsc --outDir ./dist/types --declaration --emitDeclarationOnly","build:lib":"rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript","build":"npm run build:lib && npm run build:types","build:watch":"npm run build -- --watch","clean":"rimraf dist","tag":"git tag -a \"v$npm_package_version\" -m \"tag version $npm_package_version\" && git push origin master --tags","lint":"eslint --ext=ts,tsx . src test --fix && prettier . src test --write","test":"npx mocha -R spec ./test/setup.js test/*_test.{ts,tsx}","docs":"typedoc src/index.ts && gh-pages -d docs"},"engines":{"node":">=14"},"_id":"react-responsive@10.0.1","gitHead":"ff23a19f49576a3f06c8334affbef5996f147e12","bugs":{"url":"https://github.com/yocontra/react-responsive/issues"},"_nodeVersion":"20.18.1","_npmVersion":"11.1.0","dist":{"integrity":"sha512-OM5/cRvbtUWEX8le8RCT8scA8y2OPtb0Q/IViEyCEM5FBN8lRrkUOZnu87I88A6njxDldvxG+rLBxWiA7/UM9g==","shasum":"293d4d2562da93409861216f0110d146c5676eb3","tarball":"http://registry-npm.ywwl.com/react-responsive/download/react-responsive-10.0.1.tgz","fileCount":21,"unpackedSize":56596,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCMYCWF8jxsewBzTIVW+MWjbBYxIiz3duvJRNPXuCDEQgIgUE0W/9lvGcEzx7hzUQmYKfw8meD/m9EN0c8uWtLPPDk="}],"size":11769,"noattachment":false,"key":"/react-responsive/-/react-responsive-10.0.1.tgz"},"_npmUser":{"name":"yocontra","email":"yo@contra.io"},"directories":{},"maintainers":[{"name":"fractal","email":"contact@wearefractal.com"},{"name":"yocontra","email":"yo@contra.io"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/react-responsive_10.0.1_1740864466989_0.5221951013272115"},"_hasShrinkwrap":false,"readme":"# react-responsive [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url]\n\n## Information\n\n<table>\n<tr>\n<td>Package</td><td>react-responsive</td>\n</tr>\n<tr>\n<td>Description</td>\n<td>Media queries in react for responsive design</td>\n</tr>\n<tr>\n<td>Browser Version</td>\n<td>>= IE6*</td>\n</tr>\n<tr>\n<td colspan='2'><a href='http://contra.io/react-responsive/'>Demo</a></td>\n</tr>\n</table>\n\nThe best supported, easiest to use react media query module.\n\n## Install\n\n```console\n$ npm install react-responsive --save\n```\n\n## Example Usage\n\n### With Hooks\n\nHooks is a new feature available in 8.0.0!\n\n```jsx\nimport React from 'react'\nimport { useMediaQuery } from 'react-responsive'\n\nconst Example = () => {\n  const isDesktopOrLaptop = useMediaQuery({\n    query: '(min-width: 1224px)'\n  })\n  const isBigScreen = useMediaQuery({ query: '(min-width: 1824px)' })\n  const isTabletOrMobile = useMediaQuery({ query: '(max-width: 1224px)' })\n  const isPortrait = useMediaQuery({ query: '(orientation: portrait)' })\n  const isRetina = useMediaQuery({ query: '(min-resolution: 2dppx)' })\n\n  return (\n    <div>\n      <h1>Device Test!</h1>\n      {isDesktopOrLaptop && <p>You are a desktop or laptop</p>}\n      {isBigScreen && <p>You have a huge screen</p>}\n      {isTabletOrMobile && <p>You are a tablet or mobile phone</p>}\n      <p>Your are in {isPortrait ? 'portrait' : 'landscape'} orientation</p>\n      {isRetina && <p>You are retina</p>}\n    </div>\n  )\n}\n```\n\n### With Components\n\n```jsx\nimport MediaQuery from 'react-responsive'\n\nconst Example = () => (\n  <div>\n    <h1>Device Test!</h1>\n    <MediaQuery minWidth={1224}>\n      <p>You are a desktop or laptop</p>\n      <MediaQuery minWidth={1824}>\n        <p>You also have a huge screen</p>\n      </MediaQuery>\n    </MediaQuery>\n    <MediaQuery minResolution=\"2dppx\">\n      {/* You can also use a function (render prop) as a child */}\n      {(matches) =>\n        matches ? <p>You are retina</p> : <p>You are not retina</p>\n      }\n    </MediaQuery>\n  </div>\n)\n```\n\n## API\n\n### Using Properties\n\nTo make things more idiomatic to react, you can use camel-cased shorthands to construct media queries.\n\nFor a list of all possible shorthands and value types see https://github.com/yocontra/react-responsive/blob/master/src/mediaQuery.ts#L9.\n\nAny numbers given as shorthand will be expanded to px (`1234` will become `'1234px'`).\n\nThe CSS media queries in the example above could be constructed like this:\n\n```jsx\nimport React from 'react'\nimport { useMediaQuery } from 'react-responsive'\n\nconst Example = () => {\n  const isDesktopOrLaptop = useMediaQuery({ minWidth: 1224 })\n  const isBigScreen = useMediaQuery({ minWidth: 1824 })\n  const isTabletOrMobile = useMediaQuery({ maxWidth: 1224 })\n  const isPortrait = useMediaQuery({ orientation: 'portrait' })\n  const isRetina = useMediaQuery({ minResolution: '2dppx' })\n\n  return <div>...</div>\n}\n```\n\n### Forcing a device with the `device` prop\n\nAt times you may need to render components with different device settings than what gets automatically detected. This is especially useful in a Node environment where these settings can't be detected (SSR) or for testing.\n\n#### Possible Keys\n\n`orientation`, `scan`, `aspectRatio`, `deviceAspectRatio`,\n`height`, `deviceHeight`, `width`, `deviceWidth`, `color`, `colorIndex`, `monochrome`,\n`resolution` and `type`\n\n##### Possible Types\n\n`type` can be one of: `all`, `grid`, `aural`, `braille`, `handheld`, `print`, `projection`,\n`screen`, `tty`, `tv` or `embossed`\n\nNote: The `device` property always applies, even when it can be detected (where window.matchMedia exists).\n\n```jsx\nimport { useMediaQuery } from 'react-responsive'\n\nconst Example = () => {\n  const isDesktopOrLaptop = useMediaQuery(\n    { minDeviceWidth: 1224 },\n    { deviceWidth: 1600 } // `device` prop\n  )\n\n  return (\n    <div>\n      {isDesktopOrLaptop && (\n        <p>\n          this will always get rendered even if device is shorter than 1224px,\n          that's because we overrode device settings with 'deviceWidth: 1600'.\n        </p>\n      )}\n    </div>\n  )\n}\n```\n\n#### Supplying through Context\n\nYou can also pass `device` to every `useMediaQuery` hook in the components tree through a React [Context](https://reactjs.org/docs/context.html).\nThis should ease up server-side-rendering and testing in a Node environment, e.g:\n\n##### Server-Side Rendering\n\n```jsx\nimport { Context as ResponsiveContext } from 'react-responsive'\nimport { renderToString } from 'react-dom/server'\nimport App from './App'\n\n...\n  // Context is just a regular React Context component, it accepts a `value` prop to be passed to consuming components\n  const mobileApp = renderToString(\n    <ResponsiveContext.Provider value={{ width: 500 }}>\n      <App />\n    </ResponsiveContext.Provider>\n  )\n...\n```\n\nIf you use next.js, structure your import like this to disable server-side rendering for components that use this library:\n\n```js\nimport dynamic from 'next/dynamic'\nconst MediaQuery = dynamic(() => import('react-responsive'), {\n  ssr: false\n})\n```\n\n##### Testing\n\n```jsx\nimport { Context as ResponsiveContext } from 'react-responsive'\nimport { render } from '@testing-library/react'\nimport ProductsListing from './ProductsListing'\n\ndescribe('ProductsListing', () => {\n  test('matches the snapshot', () => {\n    const { container: mobile } = render(\n      <ResponsiveContext.Provider value={{ width: 300 }}>\n        <ProductsListing />\n      </ResponsiveContext.Provider>\n    )\n    expect(mobile).toMatchSnapshot()\n\n    const { container: desktop } = render(\n      <ResponsiveContext.Provider value={{ width: 1000 }}>\n        <ProductsListing />\n      </ResponsiveContext.Provider>\n    )\n    expect(desktop).toMatchSnapshot()\n  })\n})\n```\n\nNote that if anything has a `device` prop passed in it will take precedence over the one from context.\n\n### `onChange`\n\nYou can use the `onChange` callback to specify a change handler that will be called when the media query's value changes.\n\n```jsx\nimport React from 'react'\nimport { useMediaQuery } from 'react-responsive'\n\nconst Example = () => {\n  const handleMediaQueryChange = (matches) => {\n    // matches will be true or false based on the value for the media query\n  }\n  const isDesktopOrLaptop = useMediaQuery(\n    { minWidth: 1224 },\n    undefined,\n    handleMediaQueryChange\n  )\n\n  return <div>...</div>\n}\n```\n\n```jsx\nimport React from 'react'\nimport MediaQuery from 'react-responsive'\n\nconst Example = () => {\n  const handleMediaQueryChange = (matches) => {\n    // matches will be true or false based on the value for the media query\n  }\n\n  return (\n    <MediaQuery minWidth={1224} onChange={handleMediaQueryChange}>\n      ...\n    </MediaQuery>\n  )\n}\n```\n\n## Easy Mode\n\nThat's it! Now you can create your application specific breakpoints and reuse them easily. Here is an example:\n\n```jsx\nimport { useMediaQuery } from 'react-responsive'\n\nconst Desktop = ({ children }) => {\n  const isDesktop = useMediaQuery({ minWidth: 992 })\n  return isDesktop ? children : null\n}\nconst Tablet = ({ children }) => {\n  const isTablet = useMediaQuery({ minWidth: 768, maxWidth: 991 })\n  return isTablet ? children : null\n}\nconst Mobile = ({ children }) => {\n  const isMobile = useMediaQuery({ maxWidth: 767 })\n  return isMobile ? children : null\n}\nconst Default = ({ children }) => {\n  const isNotMobile = useMediaQuery({ minWidth: 768 })\n  return isNotMobile ? children : null\n}\n\nconst Example = () => (\n  <div>\n    <Desktop>Desktop or laptop</Desktop>\n    <Tablet>Tablet</Tablet>\n    <Mobile>Mobile</Mobile>\n    <Default>Not mobile (desktop or laptop or tablet)</Default>\n  </div>\n)\n\nexport default Example\n```\n\nAnd if you want a combo (the DRY way):\n\n```js\nimport { useMediaQuery } from 'react-responsive'\n\nconst useDesktopMediaQuery = () =>\n  useMediaQuery({ query: '(min-width: 1280px)' })\n\nconst useTabletAndBelowMediaQuery = () =>\n  useMediaQuery({ query: '(max-width: 1279px)' })\n\nconst Desktop = ({ children }) => {\n  const isDesktop = useDesktopMediaQuery()\n\n  return isDesktop ? children : null\n}\n\nconst TabletAndBelow = ({ children }) => {\n  const isTabletAndBelow = useTabletAndBelowMediaQuery()\n\n  return isTabletAndBelow ? children : null\n}\n```\n\n## Browser Support\n\n### Out of the box\n\n<table>\n<tr>\n<td>Chrome</td>\n<td>9</td>\n</tr>\n<tr>\n<td>Firefox (Gecko)</td>\n<td>6</td>\n</tr>\n<tr>\n<td>MS Edge</td>\n<td>All</td>\n</tr>\n<tr>\n<td>Internet Explorer</td>\n<td>10</td>\n</tr>\n<tr>\n<td>Opera</td>\n<td>12.1</td>\n</tr>\n<tr>\n<td>Safari</td>\n<td>5.1</td>\n</tr>\n</table>\n\n### With Polyfills\n\nPretty much everything. Check out these polyfills:\n\n- [matchMedia.js by Paul Irish](https://github.com/paulirish/matchMedia.js/)\n- [media-match (faster, but larger and lacking some features)](https://github.com/weblinc/media-match)\n\n[downloads-image]: http://img.shields.io/npm/dm/react-responsive.svg\n[npm-url]: https://npmjs.org/package/react-responsive\n[npm-image]: http://img.shields.io/npm/v/react-responsive.svg\n","publish_time":1740864467177,"_cnpm_publish_time":1740864467177,"dist-tags":{"beta":"10.0.0-beta.1","latest":"10.0.1"}}