I think we need to agree on a technique for detecting the current media query that is active. This is not the same as #1252178: Add Modernizr to core
I think as we move Drupal to mobile first a lot of Drupal's javascript could be dependent on this. For example:
#1277352: Responsive vertical tabs
#1510532: [META] Implement the new create content page design
#1276908: Administrative tables are too wide for smaller screens
#1524414: Rewrite tabledrag.js to use jQuery UI
#1137920: Fix toolbar on small screen sizes and redesign toolbar for desktop
I think we should probably use this technique - http://www.springload.co.nz/love-the-web/responsive-javascript https://github.com/JoshBarr/js-media-queries
These are also similar:
http://harvesthq.github.com/harvey/
http://xoxco.com/projects/code/breakpoints/
http://rezitech.github.com/syze/
Would love to hear other peoples thoughts.
Comments
Comment #1
gmclelland CreditAttribution: gmclelland commented#1260800: Kill the overlay for widths below 640 pixels - Could possibly be solved by disabling the overlay at a certain breakpoint
Comment #2
effulgentsia CreditAttribution: effulgentsia commented@gmclelland: now that #1260800: Kill the overlay for widths below 640 pixels landed, do you want to submit a patch here for how to improve it with media query detection? Or is there a better use case you'd like us to focus on?
Comment #3
gmclelland CreditAttribution: gmclelland commentedI was just wanting to raise the awareness that as Drupal moves to focus on mobile, there will be lots of places where we will want to test against the screen size or something else in order to enable or disable javascript functionality.
It seems like all the issues above and maybe more in the future are influenced by the screen size.
For instance, in #1260800: Kill the overlay for widths below 640 pixels it is testing against a min width of 640px.
Why not instead, test for a named media query? In http://www.springload.co.nz/love-the-web/responsive-javascript they are giving these media queries names in css by using:
Drupal could call the min-width less than 640px media query the "small" media query or the "phone" media query. Then we could have some standards provided by Drupal in which contrib modules could check against to provide their javascript.
The script in the article not only allows for testing, but also provides callbacks so that your js could destroy some functionality and enable other functionality at different breakpoints.
For example if Drupal provided standard media queries for the following:
phone
tablet
desktop
Then modules like quicktabs could change the way tabs are interacted with on different devices. Maybe on phones you want the tabs to behave like an accordion. Maybe on tablet you want the tabs to be vertical tabs. Maybe on the desktop you want the tabs to behave like horizontal tabs.
In cases similar to the overlay, if someone doesn't like Drupal's 640px media query, simply override Drupal's css file and all the javascript would continue to work. Otherwise you would need to hunt down every instance of each check in Drupal's javascript and change and override the javascript.
I think it could also reduce future js code complexity. What if Drupal decides it wants to check against pixel density(Retina displays) or type length (in ems)? Media queries are perfect for checking these types of things. Instead of writing more and different checks in javascript, use javascript to simply check body:after property.
Sorry, I'm doing a lot of "talking out loud." I could be way off on all of this. I was just hoping for some discussion to see if any one else is noticing this pattern and thought it might be a good lightweight solution.
Comment #4
attiks CreditAttribution: attiks commentedAnother one: https://github.com/paulirish/matchMedia.js used by resp_img 8.x, related core issue #1170478: Responsive Images
Comment #5
gmclelland CreditAttribution: gmclelland commentedHere is another technique I just read about http://viget.com/inspire/managing-javascript-on-responsive-websites using https://github.com/ten1seven/jRespond or https://github.com/sparkbox/mediaCheck
Comment #6
gmclelland CreditAttribution: gmclelland commentedHere is another good looking one. http://wickynilliams.github.com/enquire.js/
Comment #7
gmclelland CreditAttribution: gmclelland commented@attiks - Just thinking out loud... If breakpoints are declared with your http://drupal.org/project/breakpoints module, perhaps there would be some way for this to hook into that? That way if core themes declare breakpoints, then you could also have js that could respond to those breakpoints?
Comment #8
attiks CreditAttribution: attiks commentedI know, it's possible and that's what matchmedia.js is doing. The breakpoints module can pass everything as a JavaScript object so it can be used on the client side. The picturefill.js is using the same principle to select the right image.
We could even allow other modules to define JavaScript for each breakpoint, like we do now for image styles.
Comment #9
gmclelland CreditAttribution: gmclelland commentedClosing in favor of #1815602: Introduce a polyfill for matchMedia
Comment #10
feeela CreditAttribution: feeela commentedI'm voting to re-open this issue. I can't understand why this isue was closed in favour for a
matchMedia
Polyfill.This very issue and
matchMedia
do not have something in common.matchMedia
is usefull to test for media queries on the JS-side. But I have to define and write any media-queries in JS. This issue here is about re-using the existing CSS media queries provided by a theme, instead of re-writing those in JS.There are a lot of techniques to do so, many of them suggested in the comment-links.
Please state if there is a chance that such feature will land in Drupal, if not close this issue. But please do not mark it as duplicate to some other random JS-issue.