Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add global variable to USWDS package for the polling to recognize the… #3867

Merged
merged 30 commits into from
Dec 12, 2020

Conversation

sarnikbixal
Copy link

@sarnikbixal sarnikbixal commented Dec 3, 2020

This PR adds a small (under 1 KB minified) initialization script (uswds-init.min.js) to the package. When included in the <head> — either by reference or inline — this script prevents the banner from flashing its content on page load and shifting content (#3092).

Note: This only prevents flashing when you use CSS generated from USWDS 2.10.0 or later.

What it does

Simply:

  • It adds a .usa-js-loading class to the <html> element as the USWDS JS library is loading.

In more detail:

  • If JavaScript is present, it adds a .usa-js-loading class to the <html> element before the body renders.
  • If the USWDS JS library loads, it removes the .usa-js-loading class.
  • If the USWDS JS library does not load in a timeout period, it removes the .usa-js-loading class.

Since USWDS components are meant to make their content available even if our JS does not load, their visibility of any toggle-able content defaults to visible and the JS is responsible for hiding it. This resulted in a flash of "hidden" content between when the page renders and the JS loads.

This initialization script allows us to visually hide "hidden" content at page render by hooking those styles to the .usa-js-loading class. When the USWDS JS loads, the JavaScript is responsible for hiding/showing interactive content as usual and the class hook is removed. If the USWDS JS does not load, uswds-init removes the .usa-ls-loading class and the content will revert to visible. If there is no JS present at all, uswds-init will not load and the content will be visible at page render.

Using the initializer

  1. Copy uswds-init-min.js out of the USWDS package an into your project directory.
  2. Reference the script in the document <head> or include the entire script inline.

Include by reference

   <!DOCTYPE html>
   <html>
     <head>
       <meta charset="utf-8" />
       <meta http-equiv="X-UA-Compatible" content="IE=edge" />
       <title>My Example Project</title>
       <script src="assets/uswds/js/uswds-init.min.js"></script>
       <link rel="stylesheet" href="assets/uswds/css/uswds.min.css" />
     </head>
     <body>
        ...
       <script src="assets/uswds/js/uswds.min.js"></script>
     </body>
   </html>

Include inline

   <!DOCTYPE html>
   <html>
     <head>
       <meta charset="utf-8" />
       <meta http-equiv="X-UA-Compatible" content="IE=edge" />
       <title>My Example Project</title>
       <script>
         const docElem=window.document.documentElement,loadingClass="usa-js-loading";function addLoadingClass(){docElem.className+=` ${loadingClass}`}function revertClass(){docElem.className=docElem.className.replace(loadingClass,"")}if("querySelector"in window.document&&"addEventListener"in window){addLoadingClass();const e=setTimeout(revertClass,8e3);let s=100;const n=()=>{setTimeout(()=>{s+=1,"undefined"!=typeof uswdsPresent?(clearTimeout(e),setTimeout(revertClass,100)):s>0&&n()},100)};n()}
       </script>
       <link rel="stylesheet" href="assets/uswds/css/uswds.min.css" />
     </head>
     <body>
        ...
       <script src="assets/uswds/js/uswds.min.js"></script>
     </body>
   </html>

Other changes

  • Install snyk as a dependency for more reliable local tests
  • Minifies javascript in development environment
  • Builds USWDS on every npm start
  • Adds documentation

This initialization script is based on techniques found in https://www.filamentgroup.com/lab/enhancing-optimistically.html

@thisisdano thisisdano linked an issue Dec 11, 2020 that may be closed by this pull request
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Banner open on page load is distracting
2 participants