{"id":22220538,"url":"https://github.com/drawbotics/use-screen-size","last_synced_at":"2025-08-23T19:10:59.172Z","repository":{"id":44070833,"uuid":"209523942","full_name":"Drawbotics/use-screen-size","owner":"Drawbotics","description":"Small utility hook to get the size of the screen matching media queries","archived":false,"fork":false,"pushed_at":"2023-01-04T10:47:59.000Z","size":436,"stargazers_count":10,"open_issues_count":13,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T04:11:10.214Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Drawbotics.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-19T10:18:53.000Z","updated_at":"2020-05-19T03:26:47.000Z","dependencies_parsed_at":"2023-02-02T09:10:13.105Z","dependency_job_id":null,"html_url":"https://github.com/Drawbotics/use-screen-size","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/Drawbotics/use-screen-size","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fuse-screen-size","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fuse-screen-size/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fuse-screen-size/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fuse-screen-size/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Drawbotics","download_url":"https://codeload.github.com/Drawbotics/use-screen-size/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fuse-screen-size/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266312135,"owners_count":23909744,"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","status":"online","status_checked_at":"2025-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2024-12-02T23:09:01.799Z","updated_at":"2025-07-21T13:34:23.408Z","avatar_url":"https://github.com/Drawbotics.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Get Screen Size Hook\n\nThis hook will use the breakpoints defined in `@drawbotics/drylus-style-vars` to identify screen size where the app is running.\n\n## Installation\n\n```bash\n$ npm install @drawbotics/use-screen-size @drawbotics/drylus-style-vars\n```\n\n## Usage\n\n```js\nimport { useScreenSize } from '@drawbotics/use-screen-size';\n\n\nconst App = ({ children }) =\u003e {\n  const { screenSize, ScreenSizes } = useScreenSize();\n  return (\n    \u003cPage\u003e\n      {do {\n        if (screenSize \u003e= ScreenSizes.M) {\n          \u003cHeader /\u003e\n        }\n      }}\n      {do {\n        if (screenSize \u003c ScreenSizes.S) {\n          \u003cSidebar /\u003e\n        }\n      }}\n      \u003cContent\u003e\n        {children}\n      \u003c/Content\u003e\n    \u003c/Page\u003e\n  )\n};\n\n\nexport default App;\n```\n\n## Api\n\nThe hook returns two properties `screenSize` and `ScreenSizes`:\n- `screenSize` is equivalent to the current size of the screen following the current media query matching the size\n- `ScreenSizes` is a constant with the screen size value matching its size definition:\n```\nconst ScreenSizes = {\n  XS: 1,\n  S: 2,\n  M: 3,\n  L: 4,\n  XL: 5,\n};\n```\n\nThis allows you to mimic the CSS way of writing queries, but in a more verbose way in JavaScript. You can use the comparative operators `\u003c,=,\u003e` to determine what should be rendered on the screen.\n\nIf you want to render something when the screen is smaller or equal to a small size:\n```\n  if (screenSize \u003c= ScreenSizes.S) {\n    // Content for small screens and lower\n  }\n  else {\n    // Content for screens larger than small\n  }\n```\n\nYou can also target specific sizes with the `===` operator:\n```\n  if (screenSize === ScreenSizes.L) {\n    // Only render this content when the screen is L (smaller than XL and larger than S)\n  }\n```\n\nFor anything larger than XL you should use the following condition:\n```\n  if (screnSize \u003e ScreenSizes.XL) {\n    // Content for big screens\n  }\n```\n\n### Order of conditions\nAs with CSS `@media` queries, you have to check the screen size in the same order, i.e. from largest to smallest, otherwise the first one will always apply. This is because a small screen still falls within a large one, but not vice versa.\n\n---\n\nThere's also another utility function called `getScreenSize` that returns exactly the contents of the value returned by the hook (properties of `screenSize`) (it's actually used internally in the hook) but that won't update the value based on the resize events.\n\nExample:\n\n```js\nimport { getScreenSize } from '@drawbotics/use-screen-size';\n\nconsole.log(getScreenSize());\n\n// on medium screen prints\n3\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Fuse-screen-size","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrawbotics%2Fuse-screen-size","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Fuse-screen-size/lists"}