IBM / css-gridish

Automatically build your grid design’s CSS Grid code, CSS Flexbox fallback code, Sketch artboards, and Chrome extension.
https://ibm.github.io/css-gridish/
Other
2.25k stars 113 forks source link

feat(js): optional javascript file to adjust grid to scrollbar width #39

Closed seejamescode closed 6 years ago

seejamescode commented 6 years ago

Comes after #38

Adds an optional JavaScript file included at js/yourGrid-grid.js. This will adjust the grid to not go behind browser scrollbars since different browsers handle the vw unit differently. It applies to any browser that supports CSS Variables and MutationObserver.

Thoughts, @joshblack?

seejamescode commented 6 years ago

Without JS: screen shot 2018-04-30 at 1 12 10 pm

With JS: screen shot 2018-04-30 at 1 12 27 pm

I have a question for you: Do you think the JavaScript is appropriate how it is or if it should be included in a module or function?

joshblack commented 6 years ago

Hmm, good question. Definitely would be nice to have it as a module, but given it's size maybe just have it be in UMD format would work out? Or even just an IIFE, to be honest. For example:

(function() {  
  var updateScrollbarWidth = function() {
    const scrollbarWidth =
      window.innerWidth - document.documentElement.clientWidth + "px";

    document.documentElement.style.setProperty(
      "--scrollbar-width",
      scrollbarWidth
    );
  };

  var scrollbarObserver = new MutationObserver(updateScrollbarWidth);
  scrollbarObserver.observe(document.body, {
    attributes: true,
    childList: true,
    characterData: true
  });

  updateScrollbarWidth();
})();