Skip to content

Commit

Permalink
Properly feature detect for orientation APIs (#4875)
Browse files Browse the repository at this point in the history
- Prefer to use hasOwnProperty() over truthy check
  • Loading branch information
ozyx committed May 8, 2022
1 parent 0095d08 commit 5074e05
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/utils/agent/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ export default class Agent {
* @returns {boolean} true in portrait mode
*/
isPortrait() {
const { screen, orientation } = this.window;
const { screen } = this.window;
const hasScreenOrientation = screen && Object.prototype.hasOwnProperty.call(screen, 'orientation');
const hasWindowOrientation = Object.prototype.hasOwnProperty.call(this.window, 'orientation');

if (screen && screen.orientation) {
if (hasScreenOrientation) {
return screen.orientation.type.includes('portrait');
} else if (orientation) {
} else if (hasWindowOrientation) {
// Use window.orientation API if available (e.g. Safari mobile)
// which returns [-90, 0, 90, 180] based on device orientation.
const { orientation } = this.window;

return Math.abs(orientation / 90) % 2 === 0;
} else {
return this.window.innerWidth < this.window.innerHeight;
Expand Down

0 comments on commit 5074e05

Please sign in to comment.