-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better detection of device orientation based on browser feature availability #5172
Conversation
- Prefer to use screen.orientation API first - Then check for window.orientation (e.g. if using Safari browser on a mobile device) - Finally, fall back to using window.innerWidth/window.innerHeight
- Prefer to use hasOwnProperty() over truthy check
- Provides Agent instance to Notebook component - Determine device type and orientation using Agent methods instead of checking against body.classList
Codecov Report
@@ Coverage Diff @@
## master #5172 +/- ##
==========================================
- Coverage 50.15% 49.90% -0.26%
==========================================
Files 548 534 -14
Lines 20101 19555 -546
Branches 1859 1767 -92
==========================================
- Hits 10082 9759 -323
+ Misses 9538 9350 -188
+ Partials 481 446 -35
Continue to review full report at Codecov.
|
@@ -52,7 +52,6 @@ export default (agent, document) => { | |||
if (agent.isMobile()) { | |||
const mediaQuery = window.matchMedia("(orientation: landscape)"); | |||
function eventHandler(event) { | |||
console.log("changed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yikes, good catch.
// address in https://github.com/nasa/openmct/issues/4875 | ||
// eslint-disable-next-line compat/compat | ||
const isPortrait = window.screen.orientation.type.includes('portrait'); | ||
const isPhone = this.agent.isPhone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Awesome work with this, thanks @ozyx ! |
Closes #4875
Describe your changes:
Adds multiple methods of detecting device orientation (Portrait vs. Landscape) based on browser feature availability. Prefer to use
screen.orientation
if available, falling back to using the deprecatedwindow.orientation
(in case of using Safari mobile browser), and finally falling back to comparingwindow.innerHeight
andwindow.innerWidth
.Injects an instance of the
Agent
utility class into theNotebook
Vue component in order to directly call these device and orientation classification methods. Previously, theNotebook
would check for specific classes within thedocument.body.classList
on re-render in order to determine device type and orientation. However, this leads to a race-condition when changing orientation on a phone, since the Vue component re-renders before theDeviceClassifier
can add/remove the proper classes to thedocument.body
.It seems to me that the
Agent
utility class might be better as a collection of static utility methods, since it does not necessarily need to maintain state. However, this has broader implications on testing methods and overall architecture, so for this PR I went with the strategy of directly injecting an instance ofAgent
as we do in the initialization of theDeviceClassifier
plugin. I suppose this helps to decoupleDeviceClassifier
andNotebook
, so maybe a good strategy? Open to any opinions on this.All Submissions:
Author Checklist
Reviewer Checklist