{"id":28237002,"url":"https://github.com/bytebodger/use-viewport","last_synced_at":"2025-10-25T05:11:37.103Z","repository":{"id":57167243,"uuid":"343283629","full_name":"bytebodger/use-viewport","owner":"bytebodger","description":"A custom Hook for React that reports on the current viewport height and width and size classification.  It also listens for changes to viewport size.","archived":false,"fork":false,"pushed_at":"2021-03-19T03:30:00.000Z","size":168,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-19T02:05:31.228Z","etag":null,"topics":[],"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/bytebodger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-01T03:54:35.000Z","updated_at":"2021-03-19T03:30:02.000Z","dependencies_parsed_at":"2022-08-30T15:21:59.733Z","dependency_job_id":null,"html_url":"https://github.com/bytebodger/use-viewport","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bytebodger/use-viewport","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-viewport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-viewport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-viewport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-viewport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bytebodger","download_url":"https://codeload.github.com/bytebodger/use-viewport/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-viewport/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264842336,"owners_count":23671977,"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":[],"created_at":"2025-05-19T00:17:14.798Z","updated_at":"2025-10-16T10:56:17.319Z","avatar_url":"https://github.com/bytebodger.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-viewport\n\n`useViewport()` is a custom React Hook that reports on the current height, width, and size moniker of the current viewport. It uses a `window` listener to auto-update whenever the size of the viewport changes.\n\n## Usage\n\n```javascript\nconst SomeComponent = () =\u003e {\n   const viewport = useViewport();\n   \n   return \u003c\u003e\n      \u003cdiv\u003eThe viewport is currently {viewport.width} pixels wide.\u003c/div\u003e\n      \u003cdiv style={{display: viewport.size === 'xs' ? 'inherit' : 'none'}}\u003e\n         This div only displays on \"xs\"-sized viewports.\n      \u003c/div\u003e\n      \u003cdiv style={{display: viewport.size === 'xl' ? 'none' : 'inherit'}}\u003e\n         This div disappears once the viewport reaches an \"xl\" size.\n      \u003c/div\u003e\n   \u003c/\u003e;\n}\n```\n\n## Methods\n\n### useViewport()\n\n```javascript\nconst API = {\n   arguments: {\n      initialViewportSizes: {\n         optional,\n         format: 'an array of viewportSize objects',\n         defaultValue: [\n            {\n               name: 'xs',\n               minWidth: Number.MIN_SAFE_INTEGER,\n               maxWidth: 543,\n            },\n            {\n               name: 'sm',\n               minWidth: 544,\n               maxWidth: 767,\n            },\n            {\n               name: 'md',\n               minWidth: 768,\n               maxWidth: 1023,\n            },\n            {\n               name: 'lg',\n               minWidth: 1024,\n               maxWidth: 1279,\n            },\n            {\n               name: 'xl',\n               minWidth: 1280,\n               maxWidth: Number.MAX_SAFE_INTEGER,\n            },\n         ],\n      },\n   },\n   returns: {\n      getViewportSizes: Function,\n      height: Integer,\n      setViewportSizes: Function,\n      size: string,\n      width: Integer,\n   },\n}\n```\n\n**Examples:**\n\n```javascript\nconst SomeComponent = () =\u003e {\n   const viewport = useViewport();\n   \n   return \u003c\u003e\n      \u003cdiv\u003eThe viewport is {viewport.height} pixels high.\u003c/div\u003e\n   \u003c/\u003e\n}\n```\n\nThe Hook automatically sets a listener on the `window` object and the `height`, `width`, and `size` values will update upon any change in viewport size.\n\n### .getViewportSizes()\n\n```javascript\nconst API = {\n   arguments: {\n      // none\n   },\n   returns: [\n      'viewportSize Objects'\n   ],\n}\n```\n\nEvery object in the array will have the following data:\n\n```javascript\nconst viewportSize = {\n   name: string,\n   minWidth: Integer,\n   maxWidth: Integer,\n}\n```\n\n**Examples:**\n\n```javascript\nconst SomeComponent = () =\u003e {\n   const viewport = useViewport();\n   console.log(viewport.getViewportSizes());\n   /*\n      outputs the default viewport sizes:\n      [\n         {\n            name: 'xs',\n            minWidth: Number.MIN_SAFE_INTEGER,\n            maxWidth: 543,\n         },\n         {\n            name: 'sm',\n            minWidth: 544,\n            maxWidth: 767,\n         },\n         {\n            name: 'md',\n            minWidth: 768,\n            maxWidth: 1023,\n         },\n         {\n            name: 'lg',\n            minWidth: 1024,\n            maxWidth: 1279,\n         },\n         {\n            name: 'xl',\n            minWidth: 1280,\n            maxWidth: Number.MAX_SAFE_INTEGER,\n         },\n     ]\n    */\n   \n   return \u003c\u003e\u003c/\u003e\n}\n```\n\n### .setViewportSizes()\n\n```javascript\nconst API = {\n   arguments: {\n      sizes: {\n         required,\n         format: 'Array of viewportSize Objects',\n      },\n   },\n   returns: void,\n}\n```\n\n**Examples:**\n\n```javascript\nconst SomeComponent = () =\u003e {\n   const viewport = useViewport();\n   \n   useEffect(() =\u003e {\n      viewport.setViewportSizes([\n         {\n            name: 'big',\n            minWidth: 1200,\n            maxWidth: Number.MAX_SAFE_INTEGER,\n         },\n         {\n            name: 'small',\n            minWidth: 1,\n            maxWidth: 1199,\n         },\n      ]);\n   }, []);\n   \n   return \u003c\u003e\u003c/\u003e\n}\n```\n\nSince `useViewport()`'s values are stateful, setting viewport size directly in the body of a functional component will lead to endless re-renders. That's why this example is shown inside of `useEffect()`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Fuse-viewport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbytebodger%2Fuse-viewport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Fuse-viewport/lists"}