Skip to content

Commit 8bc9b66

Browse files
committed
fix(Build): Load modernizr early and non-asynchronously.
Include the modernizr bundle by injecting a script tag. This ensures modernizr is loaded synchronously and executing early and sets it's feature detection classes before the layout is done by the browser. This reverts the breaking change from the previous Patternslib 9.8.0-beta.2 release. The separate modernizr.min.js build file is still kept, but modernizr is included by the main Patternslib bundle. There is no need to add another script tag to include modernizr. You can disable loading of modernizr by setting the following before the Patternslib bundle.min.js is included: <script>window.__patternslib_disable_modernizr = true;</script> Also, the "js" class is set on the HTML root tag when a "no-js" class was present regardless of the "__patternslib_disable_modernizr" setting. Since Patternslib 9.0.0-alpha.0 where we introduced webpack module federation for our bundles, Modernizr is loaded asynchronously and applying it's CSS classes a tick too late. For example, the change from the "no-js" to the "js" class was done while the tiles have already been drawn and visible on the screen, resulting in screen flickering. There are a number of projects which depend on Modernizr being applied early.
1 parent 836c912 commit 8bc9b66

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

index.html

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<title>Patterns demo pages</title>
55
<meta charset="utf-8">
66
<link rel="stylesheet" href="/style/common.css" />
7-
<script src="/modernizr.min.js"></script>
87
<script src="/bundle.min.js"></script>
98
</head>
109
<body class="component-index">

src/core/feature-detection.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(function () {
2+
// Add JavaScript feature as class to the <html> element, just like
3+
// Modernizr does. This is needed for accessibility reasons, to support
4+
// browsers and situations where JavaScript is not available or disabled.
5+
// The HTML root tag needs to have the `no-js` class set which is then
6+
// replaced by `js`.
7+
const html = document.getElementsByTagName("html")[0];
8+
if (html.classList.contains("no-js")) {
9+
html.classList.remove("no-js");
10+
html.classList.add("js");
11+
}
12+
13+
// Do not load modernizr if disabled. It's enabled by default.
14+
// You might want to disable it for your project by setting:
15+
// window.__patternslib_disable_modernizr = true;
16+
if (window.__patternslib_disable_modernizr) {
17+
return;
18+
}
19+
20+
// Get the current script tag's URL.
21+
// See: https://stackoverflow.com/a/984656/1337474
22+
const scripts = document.getElementsByTagName("script");
23+
const script = scripts[scripts.length - 1];
24+
let script_url = script.src;
25+
// Get the base URL of the current script tag's URL.
26+
script_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/";
27+
28+
// Inject a new one with the modernizr bundle.
29+
const script_tag = document.createElement("script");
30+
script_tag.src = script_url + "modernizr.min.js";
31+
document.getElementsByTagName("head")[0].appendChild(script_tag);
32+
})();

src/core/feature-detection.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Feature detection
2+
3+
This module adds the `js` class to the HTML root node and loads Modernizr for more feature detection.
4+
5+
---
6+
** Note **
7+
8+
If you create own bundles based on Patternslib, you would need to import the `src/core/feature-detection` module for these features to be available.
9+
10+
---
11+
12+
13+
## Adding the "js" class for accessibility styles
14+
15+
There are situations where web browsers do not have JavaScript available or when it is disabled.
16+
In situations where no JavaScript is available you might want to show certain elements where in JavaScript situations you might want to hide them until a JavaScript functionality is showing them.
17+
18+
In the Media Query Level 5 specification there is a CSS media feature to detect JavaScript via a [`scripting` at-rule](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/scripting).
19+
But at time of this writing this is [not available in any of the major browsers](https://caniuse.com/mdn-css_at-rules_media_scripting).
20+
21+
Therefore Patternslib adds a `js` class to the HTML root node, if the HTML root node already has a `no-js` class.
22+
The `js` class is set very early before any browser layout is done if you include the Patternslib script in the HEAD of your site.
23+
24+
Markup:
25+
26+
```
27+
<html class="no-js">
28+
<head>
29+
<script src="https://cdn.jsdelivr.net/npm/@patternslib/[email protected]/dist/bundle.min.js"></script>
30+
</head>
31+
<body>
32+
</body>
33+
</html>
34+
```
35+
36+
When the JavaScript is loaded, the `no-js` class is removed and the `js` class is set:
37+
38+
```
39+
<html class="js">
40+
<head>
41+
<script src="https://cdn.jsdelivr.net/npm/@patternslib/[email protected]/dist/bundle.min.js"></script>
42+
</head>
43+
<body>
44+
</body>
45+
</html>
46+
```
47+
48+
49+
## Loading Modernizr
50+
51+
Modernizr is loaded for more feature detection.
52+
53+
To disable Modernizr you can set `window.__patternslib_disable_modernizr = true;` just before you load the Patternslib bundle:
54+
55+
```
56+
<html class="no-js">
57+
<head>
58+
<script>window.__patternslib_disable_modernizr = true;</script>
59+
<script src="https://cdn.jsdelivr.net/npm/@patternslib/[email protected]/dist/bundle.min.js"></script>
60+
</head>
61+
<body>
62+
</body>
63+
</html>
64+
```
65+
66+
---
67+
** Note **
68+
69+
The Modernizr feature detection is being phased out and might be removed in a future version of Patternslib.
70+
71+
---

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// Load modernizr and the `html.js` feature class.
2+
import "./core/feature-detection";
3+
14
// Webpack entry point for module federation.
25
import "@patternslib/dev/webpack/module_federation";
6+
37
// The next import needs to be kept with parentheses, otherwise we get this error:
48
// "Shared module is not available for eager consumption."
59
import("./patterns");

src/pat/clone-code/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<title>pat-clone demo page</title>
55
<meta charset="utf-8">
66
<link rel="stylesheet" href="/style/common.css" />
7+
<script>window.__patternslib_disable_modernizr = true;</script>
78
<script src="/bundle.min.js"></script>
89
</head>
910
<body>

0 commit comments

Comments
 (0)