{"id":13630440,"url":"https://github.com/flexdinesh/react-socks","last_synced_at":"2025-05-16T03:04:31.770Z","repository":{"id":46672753,"uuid":"156363343","full_name":"flexdinesh/react-socks","owner":"flexdinesh","description":"🎉 React library to render components only on specific viewports 🔥","archived":false,"fork":false,"pushed_at":"2024-04-07T16:47:19.000Z","size":1618,"stargazers_count":426,"open_issues_count":22,"forks_count":28,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-08T13:11:11.447Z","etag":null,"topics":["breakpoints","library","media-queries","react","viewport"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flexdinesh.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-06T10:03:06.000Z","updated_at":"2024-08-07T17:55:56.000Z","dependencies_parsed_at":"2024-06-18T13:37:53.849Z","dependency_job_id":"53ccb15b-9a91-4cea-a6b2-9dc40fdcae39","html_url":"https://github.com/flexdinesh/react-socks","commit_stats":{"total_commits":91,"total_committers":14,"mean_commits":6.5,"dds":0.3846153846153846,"last_synced_commit":"1b91c31e7d2aed6834089e5202abff1b327b3780"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Freact-socks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Freact-socks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Freact-socks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Freact-socks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flexdinesh","download_url":"https://codeload.github.com/flexdinesh/react-socks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253839036,"owners_count":21972254,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["breakpoints","library","media-queries","react","viewport"],"created_at":"2024-08-01T22:01:42.858Z","updated_at":"2025-05-16T03:04:26.761Z","avatar_url":"https://github.com/flexdinesh.png","language":"JavaScript","readme":"# React Socks\r\n\r\n[![Build Status](https://travis-ci.org/flexdinesh/react-socks.svg?branch=master)](https://travis-ci.org/flexdinesh/react-socks)\r\n[![npm version](https://badge.fury.io/js/react-socks.svg)](https://www.npmjs.com/package/react-socks)\r\n[![dependencies Status](https://david-dm.org/flexdinesh/react-socks/status.svg)](https://david-dm.org/flexdinesh/react-socks)\r\n[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\r\n\r\nWrap your components with **React Socks** to prevent unnecessary render in different viewports.\r\n\r\n```jsx\r\n\u003cBreakpoint small down\u003e\r\n  \u003cMyAwesomeMobileMenu\u003e\r\n    This component will render only in mobile devices\r\n  \u003c/MyAwesomeMobileMenu\u003e\r\n\u003c/Breakpoint\u003e\r\n```\r\n\r\n## Why? [![start with why](https://img.shields.io/badge/start%20with-why%3F-brightgreen.svg?style=flat)](http://www.ted.com/talks/simon_sinek_how_great_leaders_inspire_action)\r\n\r\nConventionally we have been writing _css media queries_ for different viewports to hide and show elements that are always present in the DOM. With React taking over the world, everything is about rendering components into the DOM. **React Socks** helps you conditionally render elements based on viewports.\r\n\r\n1. Render viewport specific components without hassle\r\n\r\n2. You can define your own breakpoints (Eg. xs, ipad, bigmonitors) and use them\r\n\r\n3. You can improve your app performance if you lazy load your viewport specific components\r\n\r\n4. Simpler and **sweeter** syntax for ease of use\r\n\r\n## Install\r\n\r\n```sh\r\n$ npm install --save react-socks\r\n```\r\n\r\n## Usage\r\n\r\nJust wrap your top level component with `BreakpointProvider` and use the `Breakpoint` component anywhere you need.\r\n\r\n_Note: `BreakpointProvider` was introduced only in `v1.0.0`. It's not available in previous alpha versions._\r\n\r\n```jsx\r\nimport  { Breakpoint, BreakpointProvider } from 'react-socks';\r\n\r\n// entry file (usually App.js or index.js)\r\nconst App = () =\u003e (\r\n  \u003cBreakpointProvider\u003e\r\n    \u003cExample /\u003e\r\n  \u003c/BreakpointProvider\u003e\r\n);\r\n```\r\n\r\n```jsx\r\n// Example.js\r\nconst Example = () =\u003e {\r\n  return (\r\n    \u003cdiv\u003e\r\n      \u003cBreakpoint small down\u003e\r\n        \u003cdiv\u003eI will render only in mobile devices\u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n\r\n      \u003cBreakpoint medium only\u003e\r\n        \u003cdiv\u003eI will render only in tablets (iPad, etc...)\u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n\r\n      \u003cBreakpoint medium down\u003e\r\n        \u003cdiv\u003eI will render in tablets (iPad, etc...) and everything below (mobile devices)\u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n\r\n      \u003cBreakpoint medium up\u003e\r\n        \u003cdiv\u003eI will render in tablets (iPad, etc...) and everything above (laptops, desktops)\u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n\r\n      \u003cBreakpoint large up\u003e\r\n        \u003cdiv\u003eI will render in laptops, desktops and everything above\u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n\r\n      {/* Add breakpoints on the fly using custom queries */}\r\n\r\n      \u003cBreakpoint customQuery=\"(min-width: 500px)\"\u003e\r\n        \u003cdiv style={{backgroundColor: 'red' }}\u003e\r\n          Custom breakpoint: (min-width : 500px)\r\n        \u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n      \r\n      \u003cBreakpoint customQuery=\"(max-width: 1000px)\"\u003e\r\n        \u003cdiv style={{backgroundColor: 'yellow' }}\u003e\r\n          Custom breakpoint: (max-width : 1000px)\r\n        \u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n      \r\n      \u003cBreakpoint customQuery=\"(min-width: 500px) and (max-width: 700px)\"\u003e\r\n        \u003cdiv style={{backgroundColor: 'lightblue' }}\u003e\r\n          Custom breakpoint: (min-width : 500px) \u0026\u0026 (max-width : 700px)\r\n        \u003c/div\u003e\r\n      \u003c/Breakpoint\u003e\r\n    \u003c/div\u003e\r\n  );\r\n};\r\n```\r\n\r\n## API\r\n\r\n- [setDefaultBreakpoints](#set-default-breakpoints)\r\n- [setDefaultWidth](#set-default-width)\r\n- [Breakpoint](#breakpoint)\r\n- [useCurrentWidth](#use-current-width--breakpoint-name) / [useCurrentBreakpointName](#use-current-width--breakpoint-name)\r\n\r\n### Set Default Breakpoints\r\n\r\nYou can define your own breakpoints.\r\n\r\n- Pass an array of objects with the **breakpoint name** and **width** in _px_ to `setDefaultBreakpoints` **once** in your `App.js` or your React entry file.\r\n\r\n**Note: You only need to set default breakpoints once in your app**\r\n\r\n```jsx\r\nimport { setDefaultBreakpoints } from 'react-socks';\r\n\r\nsetDefaultBreakpoints([\r\n  { xs: 0 },\r\n  { s: 376 },\r\n  { m: 426 },\r\n  { l: 769 },\r\n  { xl: 1025 }\r\n]);\r\n\r\n\u003cBreakpoint m only\u003e\r\n    I will render only in m devices\r\n\u003c/Breakpoint\u003e\r\n\r\n```\r\n\r\n- You can use any breakpoint name (Eg. cats, puppies, dinosaurs, etc) and width.\r\n\r\n```jsx\r\nsetDefaultBreakpoints([\r\n  { cats: 0 },\r\n  { dinosaurs: 900 }\r\n]);\r\n\r\n\u003cBreakpoint cats only\u003e\r\n    Only cats can render me\r\n\u003c/Breakpoint\u003e\r\n```\r\n\r\n- If you don't set a default breakpoint, the library will fallback to **Bootstrap 4 default breakpoints** as described below.\r\n\r\n```jsx\r\nsetDefaultBreakpoints([\r\n  { xsmall: 0 }, // all mobile devices\r\n  { small: 576 }, // mobile devices (not sure which one's this big)\r\n  { medium: 768 }, // ipad, ipad pro, ipad mini, etc\r\n  { large: 992 }, // smaller laptops\r\n  { xlarge: 1200 } // laptops and desktops\r\n]);\r\n```\r\n\r\n### Set Default Width\r\n\r\nYou can define your own default width. This will help when you want to render a particular default width from the server. Usually in the server, there are no breakpoints and the lib defaults to 0 and renders mobile views. Use this API to change that.\r\n\r\n- Pass **width** in _px_ to `setDefaultWidth` **once** in your `App.js` or your React entry file.\r\n\r\n**Note: You only need to set default width once in your app**\r\n\r\n```jsx\r\nimport { setDefaultWidth } from 'react-socks';\r\n\r\nsetDefaultWidth(992); // render desktop components in the server\r\n\r\n```\r\n\r\n### Breakpoint\r\n\r\nImport the `Breakpoint` component anywhere in the your code and start using it with your **breakpoint** and **modifier** props.\r\n\r\n```jsx\r\n// small is breakpoint\r\n// down is modifier\r\n\u003cBreakpoint small down\u003e\r\n  \u003cMyAwesomeMobileMenu\u003e\r\n    This component will render only in mobile devices\r\n  \u003c/MyAwesomeMobileMenu\u003e\r\n\u003c/Breakpoint\u003e\r\n```\r\n\r\nYou have **three** modifiers\r\n\r\n- **only** - will render the component **only** in the specified breakpoint.\r\n\r\n- **up** - will render the component **in** the specified breakpoint and all the breakpoints **above** it (greater than the width).\r\n\r\n- **down** - will render the component **in** the specified breakpoint and all the breakpoints **below** it (less than the width).\r\n\r\n\r\n\r\n### Custom Breakpoints 🆕\r\n\r\nNow, you can add a breakpoint of any width by using this prop: `customQuery`.\r\nSimply write your media query as a _string_ and pass it to `customQuery` \r\n\r\n```jsx\r\n  \u003cBreakpoint customQuery=\"(min-width: 500px)\"\u003e\r\n    \u003cdiv style={{backgroundColor: 'red' }}\u003e\r\n      Custom breakpoint: (min-width : 500px)\r\n    \u003c/div\u003e\r\n  \u003c/Breakpoint\u003e\r\n  \r\n  \u003cBreakpoint customQuery=\"(max-width: 1000px)\"\u003e\r\n    \u003cdiv style={{backgroundColor: 'yellow' }}\u003e\r\n      Custom breakpoint: (max-width : 1000px)\r\n    \u003c/div\u003e\r\n  \u003c/Breakpoint\u003e\r\n  \r\n  \u003cBreakpoint customQuery=\"(min-width: 500px) and (max-width: 700px)\"\u003e\r\n    \u003cdiv style={{backgroundColor: 'lightblue' }}\u003e\r\n      Custom breakpoint: (min-width : 500px) \u0026\u0026 (max-width : 700px)\r\n    \u003c/div\u003e\r\n  \u003c/Breakpoint\u003e\r\n```\r\n\r\n**Note: `customQuery` will be ignored if you have mentioned any modifiers like `up`, `down` \u0026 `only`**\r\n\r\nUse `customQuery` only if you want to make use of arbitary breakpoints.\r\n\r\n\r\n\r\n### Use Current Width / Breakpoint Name\r\n\r\nIf you call `useCurrentWidth` in the render function, you can access the current width directly:\r\n\r\n```jsx\r\nimport { useCurrentWidth } from 'react-socks'\r\n\r\nconst CustomComponent = () =\u003e {\r\n  const width = useCurrentWidth()\r\n  if (width \u003c 500) {\r\n    return \u003ch1\u003eHello!\u003c/h1\u003e\r\n  } else {\r\n    return \u003ch2\u003eHello!\u003c/h2\u003e\r\n  }\r\n}\r\n```\r\n\r\nYou can also use the current breakpoint name with `useCurrentBreakpointName`:\r\n\r\n```jsx\r\nimport { useCurrentBreakpointName } from 'react-socks'\r\n\r\nconst CustomComponent = () =\u003e {\r\n  const breakpoint = useCurrentBreakpointName()\r\n  if (breakpoint == 'small') {\r\n    return \u003ch1\u003eHello!\u003c/h1\u003e\r\n  } else {\r\n    return \u003ch2\u003eHello!\u003c/h2\u003e\r\n  }\r\n}\r\n```\r\n\r\n## Contributors\r\n\r\nThanks goes to these amazing people 🎉\r\n\r\n| [\u003cimg src=\"https://avatars3.githubusercontent.com/u/5777880?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDinesh Pandiyan\u003c/b\u003e\u003c/sub\u003e](https://github.com/flexdinesh)\u003cbr /\u003e | [\u003cimg src=\"https://avatars1.githubusercontent.com/u/3236388?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eCapelo\u003c/b\u003e\u003c/sub\u003e](https://github.com/antoniocapelo)\u003cbr /\u003e | [\u003cimg src=\"https://avatars0.githubusercontent.com/u/8450195?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAdarsh\u003c/b\u003e\u003c/sub\u003e](https://github.com/sadarshannaiynar)\u003cbr /\u003e | [\u003cimg src=\"https://avatars1.githubusercontent.com/u/22812131?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003ePatryk\u003c/b\u003e\u003c/sub\u003e](https://github.com/PatrykRudzinski)\u003cbr /\u003e | [\u003cimg src=\"https://avatars1.githubusercontent.com/u/9076205?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eWRNGFRNK\u003c/b\u003e\u003c/sub\u003e](https://github.com/wrngfrnk)\u003cbr /\u003e | [\u003cimg src=\"https://avatars3.githubusercontent.com/u/9896958?\u0026v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eFarhad Yasir\u003c/b\u003e\u003c/sub\u003e](https://github.com/nutboltu)\u003cbr /\u003e\r\n| :---: | :---: | :---: | :---: | :---: | :---: |\r\n| [\u003cimg src=\"https://avatars1.githubusercontent.com/u/13507950?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eEntkenntnis\u003c/b\u003e\u003c/sub\u003e](https://github.com/Entkenntnis)\u003cbr /\u003e | [\u003cimg src=\"https://avatars1.githubusercontent.com/u/1263310?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDouglas Moore\u003c/b\u003e\u003c/sub\u003e](https://github.com/dbryantm)\u003cbr /\u003e | [\u003cimg src=\"https://avatars1.githubusercontent.com/u/49582824?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAbdul rehman\u003c/b\u003e\u003c/sub\u003e](https://github.com/rehman-00001)\u003cbr /\u003e | [\u003cimg src=\"https://avatars.githubusercontent.com/u/2353850?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eNawaz Khan\u003c/b\u003e\u003c/sub\u003e](https://github.com/nawazkhan)\u003cbr /\u003e | [\u003cimg src=\"https://avatars.githubusercontent.com/u/27327?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003ehems.io\u003c/b\u003e\u003c/sub\u003e](https://github.com/hems)\u003cbr /\u003e\r\n\r\n## License\r\n\r\nMIT © Dinesh Pandiyan\r\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflexdinesh%2Freact-socks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflexdinesh%2Freact-socks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflexdinesh%2Freact-socks/lists"}