{"id":22160386,"url":"https://github.com/rozek/svelte-viewport-info","last_synced_at":"2025-07-24T14:33:21.624Z","repository":{"id":57375400,"uuid":"374566203","full_name":"rozek/svelte-viewport-info","owner":"rozek","description":"informs about viewport size and orientation (not only for Svelte)","archived":false,"fork":false,"pushed_at":"2024-09-16T05:46:58.000Z","size":73,"stargazers_count":10,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-11T07:17:08.928Z","etag":null,"topics":["device-orientation","viewport-size"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/rozek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2021-06-07T06:58:49.000Z","updated_at":"2025-04-01T23:40:35.000Z","dependencies_parsed_at":"2024-02-01T06:27:54.227Z","dependency_job_id":"2555e1d4-d952-40df-bd7d-037a8f5f707b","html_url":"https://github.com/rozek/svelte-viewport-info","commit_stats":{"total_commits":78,"total_committers":1,"mean_commits":78.0,"dds":0.0,"last_synced_commit":"566cccbae1c497ec0bb6a977bcd06d8707984250"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/rozek/svelte-viewport-info","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rozek%2Fsvelte-viewport-info","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rozek%2Fsvelte-viewport-info/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rozek%2Fsvelte-viewport-info/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rozek%2Fsvelte-viewport-info/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rozek","download_url":"https://codeload.github.com/rozek/svelte-viewport-info/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rozek%2Fsvelte-viewport-info/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266855798,"owners_count":23995553,"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-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["device-orientation","viewport-size"],"created_at":"2024-12-02T04:07:39.830Z","updated_at":"2025-07-24T14:33:21.602Z","avatar_url":"https://github.com/rozek.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# svelte-viewport-info #\n\ninforms about viewport size and orientation (not only in Svelte)\n\n**NPM users**: please consider the [Github README](https://github.com/rozek/svelte-viewport-info/blob/main/README.md) for the latest description of this package (as updating the docs would otherwise always require a new NPM package version)\n\n\u003e Just a small note: if you like this module and plan to use it, consider \"starring\" this repository (you will find the \"Star\" button on the top right of this page), so that I know which of my repositories to take most care of.\n\n## Installation ##\n\n`svelte-viewport-info` may be used as an ECMAScript module (ESM), a CommonJS or AMD module or from a global variable.\n\nYou may either install the package into your build environment using [NPM](https://docs.npmjs.com/) with the command\n\n```\nnpm install svelte-viewport-info\n```\n\nor load the plain script file directly\n\n```html\n\u003cscript src=\"https://unpkg.com/svelte-viewport-info\"\u003e\u003c/script\u003e\n```\n\n## Access ##\n\nHow to access the package depends on the type of module you prefer\n\n* ESM (or Svelte): `import Viewport from 'svelte-viewport-info'`\n* CommonJS: `const Viewport = require('svelte-viewport-info')`\n* AMD: `require(['svelte-viewport-info'], (Viewport) =\u003e {...})`\n\nAlternatively, you may access the global variable `Viewport` directly.\n\nNote for ECMAScript module users: all module functions and values are exported individually, thus allowing your bundler to perform some \"tree-shaking\" in order to include actually used functions or values (together with their dependencies) only.\n\n## Usage within Svelte ##\n\nFor Svelte it is recommended to import the package within a module context:\n\n```html\n\u003cscript context=\"module\"\u003e\n  import Viewport from 'svelte-viewport-info'\n\u003c/script\u003e\n\n\u003cscript\u003e\n  console.log('Viewport Width x Height:     ',Viewport.Width+'x'+Viewport.Height)\n  console.log('standard Screen Orientation: ',Viewport.Orientation)\n  console.log('detailled Screen Orientation:',Viewport.detailledOrientation)\n\u003c/script\u003e\n\n\u003csvelte:body\n  on:viewportchanged={() =\u003e {\n    console.log('Viewport Size changed to: ',Viewport.Width+'x'+Viewport.Height)\n  }}\n  \n  on:orientationchangeend={() =\u003e { console.log(\n    'Screen Orientation changed to: ', Viewport.Orientation + (\n      Viewport.detailledOrientation == null\n      ? ''\n      : '(' + Viewport.detailledOrientation + ')'\n    )\n  ) }}\n/\u003e\n```\n\n## Usage as ECMAscript, CommonJS or AMD Module (or as a global Variable) ##\n\nLet's assume that you already \"required\" or \"imported\" (or simply loaded) the module according to your local environment. In that case, you may use it as follows:\n\n```javascript\nconsole.log('Viewport Width x Height:     ',Viewport.Width+'x'+Viewport.Height)\nconsole.log('standard Screen Orientation: ',Viewport.Orientation)\nconsole.log('detailled Screen Orientation:',Viewport.detailledOrientation)\n\nfunction observeViewportAndOrientation () {\n  document.body.addEventListener('viewportchanged', () =\u003e {\n    console.log('Viewport Size changed to: ',Viewport.Width+'x'+Viewport.Height)\n  }}\n  \n  document.body.addEventListener('orientationchangeend', () =\u003e { console.log(\n    'Screen Orientation changed to: ', Viewport.Orientation + (\n      Viewport.detailledOrientation == null\n      ? ''\n      : '(' + Viewport.detailledOrientation + ')'\n    )\n  ) }}\n}\n  \nif (\n  (document.readyState === 'interactive') ||\n  (document.readyState === 'complete')\n) {\n  observeViewportAndOrientation()\n} else {\n  window.addEventListener('DOMContentLoaded', observeViewportAndOrientation)\n}\n```\n\n## Example ##\n\nAn example is available on the Svelte REPL - feel free to play with it!\n\n* [svelte-viewport-info](https://svelte.dev/repl/84ad979c06e84c5e8a98933554ab49c6)\n\n## Background Information ##\n\nThis package determines the current viewport size and device (or viewport) orientation. In addition, it listens to the events sent when these values change and informs the application about such changes. The idea behind this approach is to normalize the behaviour of various platforms and browsers.\n\nThe package's finding may either be retrieved using JavaScript or by styling a few CSS classes which are added to or removed from the document body depending on the current viewport `Orientation` or `detailledOrientation`.\n\n### JavaScript API ###\n\nThis package offers a JavaScript `default` export, which may be imported (or `required`) as shown in the \"Access\" section above.\n\nWith such an import, the JavaScript API can be used as follows:\n\n* **`Viewport.Width`** - retrieves the current viewport width in pixels. If the actual viewport size can not be determined, the inner width of the current browser window is used instead\n* **`Viewport.Height`** - retrieves the current viewport height in pixels. If the actual viewport size can not be determined, the inner height of the current browser window is used instead\u003cbr\u003e\u0026nbsp;\u003cbr\u003e\n* **`Viewport.Orientation`** - retrieves the current device orientation. At first, a \"media query\" is attempted - if that does not work, the orientation is determined from the relation between viewport width and height. Possible results are:\n  * **`\"portrait\"`** - the device is in a \"Portrait\" orientation\n  * **`\"landscape\"`** - the device is in a \"Landscape\" orientation\n* **`Viewport.detailledOrientation`** - informs about which (of the two possible) \"Portrait\" or \"Landscape\" orientations is actually active (if available). Possible results are:\n  * **`\"portrait-primary\"`** - the device is in its \"primary\" \"Portrait\" orientation (usually upright)\n  * **`\"portrait-secondary\"`** - the device is in its \"secondary\" \"Portrait\" orientation (usually upside down)\n  * **`\"landscape-primary\"`** - the device is in its \"primary\" \"Landscape\" orientation (usually turned right from primary Portrait orientation)\n  * **`\"landscape-secondary\"`** - the device is in its \"secondary\" \"Landscape\" orientation (usually turned left from primary Portrait orientation)\n  * **`undefined`** - no detail available\n\n### Events ###\n\nWhenever viewport size and/or device orientation change, this package emits the following events:\n\n* **`viewportchanged`** - viewport size has changed\n* **`orientationchangeend`** - device orientation has changed\n\nThese events may easily be caught as follows\n\n```html\n\u003csvelte:body\n  on:viewportchanged={...}\n  on:orientationchangeend={...}\n/\u003e\n```\n\nor\n\n```javascript\ndocument.body.addEventListener('viewportchanged',...)\ndocument.body.addEventListener('orientationchangeend',...)\n```\n\n## CSS Classes ##\n\nIn addition, the package also adds or removes the following CSS classes to and from the document body depending on the current device orientation:\n\n* **`Portrait`** - indicates that the device is currently in any \"Portrait\" orientation\n* **`Landscape`** - indicates that the device is currently in any \"Landscape\" orientation\n* **`Portrait-primary`** - indicates that the device is currently in its primary \"Portrait\" orientation\n* **`Portrait-secondary`** - indicates that the device is currently in its secondary \"Portrait\" orientation\n* **`Landscape-primary`** - indicates that the device is currently in its primary \"Landscape\" orientation\n* **`Landscape-secondary`** - indicates that the device is currently in its secondary \"Landscape\" orientation\n\nAny of the listed CSS classes is set only if the package actually recognizes the corresponding orientation - otherwise, it is missing.\n\n## Build Instructions ##\n\nYou may easily build this package yourself.\n\nJust install [NPM](https://docs.npmjs.com/) according to the instructions for your platform and follow these steps:\n\n1. either clone this repository using [git](https://git-scm.com/) or [download a ZIP archive](https://github.com/rozek/svelte-viewport-info/archive/refs/heads/main.zip) with its contents to your disk and unpack it there \n2. open a shell and navigate to the root directory of this repository\n3. run `npm install` in order to install the complete build environment\n4. execute `npm run build` to create a new build\n\nYou may also look into the author's [build-configuration-study](https://github.com/rozek/build-configuration-study) for a general description of his build environment.\n\n## License ##\n\n[MIT License](LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frozek%2Fsvelte-viewport-info","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frozek%2Fsvelte-viewport-info","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frozek%2Fsvelte-viewport-info/lists"}