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.
Comment | File | Size | Author |
---|---|---|---|
#1 | 1278504-Modernizr-load-1.patch | 1.81 KB | rupl |
Comments
Comment #1
ruplHere'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!
Comment #2
tamasd CreditAttribution: tamasd commentedGreat 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).
Comment #3
tamasd CreditAttribution: tamasd commentedComment #4
klonosThis looks amazing! Please let us know when there's an actual 7.x-3.x dev available for download and testing. Thanx in advance.
Comment #5
ruplI'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.
Comment #6
klonosI 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).
Comment #7
ruplRight 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!
Comment #8
rupl7.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()
.Comment #9
ruplDev 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.
Comment #10
ruplI 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...
Comment #11
ruplJust FYI: work going on within #1661746: Create Modernizr.load() API is affecting this API.
Comment #12
ruplThis went in along with #1661746-4: Create Modernizr.load() API. Hooray!