I was thinking about the way that conditional stylesheets are loaded into the theme, and figured we could implement the same system within Modernizr. Instead of wrapping the assets in conditional comments, we wrap them in Modernizr.load() calls, allowing asynchronous loading from within any theme.

Here are some calls I came up with:

; Modernizr.load() asset calls for Drupal
; inspired by drupal.org/project/conditional_styles

; basic Drupal calls - same as always
stylesheets[all][] = styles.css
scripts[] = scripts.js

; basic Modernizr.load()
modernizr[Modernizr.mq('(min-width: 300px)')][yep][] = mobile.css
modernizr[Modernizr.mq('(min-width: 300px)')][nope][] = unsupported.css
modernizr[Modernizr.mq('(min-width: 300px)')][both][] = regardless.css

modernizr[Modernizr.mq('(min-width: 640px)')][yep][] = medium.css
modernizr[Modernizr.mq('(min-width: 960px)')][yep][] = large.css
modernizr[Modernizr.mq('(min-width: 1450px)')][yep][] = massive.css

; polyfill loading
modernizr[Modernizr.mq('(min-width:0px)')][nope][] = respond.js

; inline loading using document.write (recommended for some polyfills)
modernizr-inline[Modernizr.mq('(min-width:0px)')][nope][] = respond.js

Which would produce something like this:

<link rel="stylesheet" href="styles.css">
<script src="scripts.js"></script>

Modernizr.load({
  test: Modernizr.mq('(min-width: 300px)'),
  yep: 'mobile.css',
  nope: 'unsupported.css',
  both: 'regardless.css',
});
Modernizr.load({
  test: Modernizr.mq('(min-width: 640px)'),
  yep: 'medium.css',
});
Modernizr.load({
  test: Modernizr.mq('(min-width: 960px)'),
  yep: 'large.css',
});
Modernizr.load({
  test: Modernizr.mq('(min-width: 1450px)'),
  yep: 'massive.css',
});
Modernizr.load({
  test: Modernizr.mq('(min-width:0px)'),
  nope: 'respond.js',
});
if ( ! Modernizr.mq('(min-width:0px)') ) {
  document.write('<script src="respond.js"></s'+'cript>');
}

Although this example uses very similar media query tests, the .info file calls need the full syntax with the "Modernizr." prefix because this could easily apply to other tests from within Modernizr or another lib, or even custom conditionals.

CommentFileSizeAuthor
#1 1278504-Modernizr-load-1.patch1.81 KBrupl
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rupl’s picture

Status: Active » Needs work
FileSize
1.81 KB

Here's a first pass at implementing Modernizr.load() calls from within theme .info files. This is a proof of concept, and simply prints the requests via drupal_set_message(). I didn't implement the modernizr-inline version yet. Another obvious improvement would be chaining the requests together inside one Modernizr.load() call.

Thoughts, suggestions, or criticism welcome!

tamasd’s picture

Assigned: Unassigned » tamasd

Great idea!

This feature will be the next big milestone (3.x). I'll open a branch for it when the 2.x branch goes to beta (it will happen soon).

tamasd’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev
klonos’s picture

This looks amazing! Please let us know when there's an actual 7.x-3.x dev available for download and testing. Thanx in advance.

rupl’s picture

Assigned: tamasd » rupl

I'm going to wait for 7.x-2.x to get a real release before branching too far off into 3.x, but I promise to keep this issue updated with my progress.

klonos’s picture

I just downloaded the fresh 3.x dev and tried it. It still lacks:

- libraries API support (so that the library can be placed under ../sites/all/libraries/)
- the filename regex (so that we cover various variations of the filename - now it *requires* the library file to be modernizr.min.js)
- version detection (so that the installed version can be displayed in the site's status report).

rupl’s picture

Right now the 7.x-3.x is branched from 6.x codebase. I've requested from Yorirou that it be re-created from the current 7.x-2.x. But yes -- for now, all of these issues exist. Thanks for confirming!

rupl’s picture

7.x-3.x is now updated with history intact. My patch in #1 is included in the dev branch, except the drupal_set_message() call is commented out by default to maintain the module's usability. To play with this, you will have to uncomment that line and add Modernizr calls to your current theme .info file.

Note: it normally takes two refreshes to see your Modernizr calls once you uncomment drupal_set_message().

rupl’s picture

Dev branch now has real ability to output Modernizr.load() calls. It stores the results in a variable called modernizr_load, and prints the calls to $modernizr, accessible within html.tpl.php.

rupl’s picture

I have removed the need for a $modernizr variable in html.tpl.php, which was a silly solution that I never should have committed. The module is now using drupal_add_js() to output the Modernizr.load() calls inline :)

http://drupalcode.org/project/modernizr.git/commitdiff/4f31057fba73cae6d...

rupl’s picture

Just FYI: work going on within #1661746: Create Modernizr.load() API is affecting this API.

rupl’s picture

Status: Needs work » Fixed

This went in along with #1661746-4: Create Modernizr.load() API. Hooray!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.