ghosh / Micromodal

⭕ Tiny javascript library for creating accessible modal dialogs
https://micromodal.now.sh/
MIT License
3.54k stars 230 forks source link

Just strange line in the source code #329

Open DimaGashko opened 4 years ago

DimaGashko commented 4 years ago

I've just found one line in the source code that made me confused

https://github.com/ghosh/Micromodal/blob/master/lib/src/index.js#L106

const body = document.querySelector('body')

Why would you use querySelector to find the body element? There's document.body to do this - shorter, faster, full support

DimaGashko commented 4 years ago

Ok, I don't like that function at all. Too complicated to do so simple thing. I would write so (

/** @param {'enable'|'disable'} value */
scrollBehaviour(value) {
   if (!this.config.disableScroll) return

   if (value === 'enable') {
      document.body.style.overflow = '';
   } else if (value === 'disable') {
      document.body.style.overflow = 'hidden';
   }
}
Or:
/** @param {'enable'|'disable'} value */
scrollBehaviour(value) {
   if (!this.config.disableScroll) return
   const overflow = (value === 'disable') ? 'hidden' : '';
   document.body.style.overflow = overflow;
}
Or:
/** @param {boolean} value */
function toggleScrolling(value) {
   if (!this.config.disableScroll) return
   document.body.style.overflow = (!value) ? 'hidden' : '';
}