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

gmclelland’s picture

#1260800: Kill the overlay for widths below 640 pixels - Could possibly be solved by disabling the overlay at a certain breakpoint

effulgentsia’s picture

@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?

gmclelland’s picture

I 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:

@media screen and (min-width: 56em) { 
    body:after { 
        content: 'wide-screen' 
    } 
} 

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.

attiks’s picture

gmclelland’s picture

gmclelland’s picture

Issue tags: +JavaScript

Here is another good looking one. http://wickynilliams.github.com/enquire.js/

gmclelland’s picture

@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?

attiks’s picture

I 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.

gmclelland’s picture

Status: Active » Closed (duplicate)
feeela’s picture

Issue summary: View changes
Status: Closed (duplicate) » Active

I'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.

Version: 8.0.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.