Skip to content

Commit

Permalink
add usage to offcanvas.js, scrollbar.js & change on modal, base-compo…
Browse files Browse the repository at this point in the history
…nent
  • Loading branch information
GeoSot authored and XhmikosR committed Mar 17, 2021
1 parent b3f8fc8 commit 952d68b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion js/src/base-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const VERSION = '5.0.0-beta2'

class BaseComponent {
constructor(element) {
element = typeof element === 'string' ? document.querySelector(element) : element
element = typeof element === 'string' ? getDocument().querySelector(element) : element

if (!element) {
return
Expand Down
6 changes: 3 additions & 3 deletions js/src/dom/manipulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ const Manipulator = {

offset(element) {
const rect = element.getBoundingClientRect()
const document = getDocument()
const documentRef = getDocument()

return {
top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft
top: rect.top + documentRef.body.scrollTop,
left: rect.left + documentRef.body.scrollLeft
}
},

Expand Down
2 changes: 1 addition & 1 deletion js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class Modal extends BaseComponent {
_setElementAttributes(selector, styleProp, callback) {
SelectorEngine.find(selector)
.forEach(element => {
if (element !== document.body && window.innerWidth > element.clientWidth + this._scrollbarWidth) {
if (element !== this._document.body && this._window.innerWidth > element.clientWidth + this._scrollbarWidth) {
return
}

Expand Down
7 changes: 4 additions & 3 deletions js/src/offcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {
defineJQueryPlugin,
getDocument,
getElementFromSelector,
getSelectorFromElement,
getTransitionDurationFromElement,
Expand Down Expand Up @@ -107,7 +108,7 @@ class Offcanvas extends BaseComponent {
this._element.style.visibility = 'visible'

if (this._config.backdrop) {
document.body.classList.add(CLASS_NAME_BACKDROP_BODY)
this._document.body.classList.add(CLASS_NAME_BACKDROP_BODY)
}

if (!this._config.scroll) {
Expand Down Expand Up @@ -153,7 +154,7 @@ class Offcanvas extends BaseComponent {
this._element.style.visibility = 'hidden'

if (this._config.backdrop) {
document.body.classList.remove(CLASS_NAME_BACKDROP_BODY)
this._document.body.classList.remove(CLASS_NAME_BACKDROP_BODY)
}

if (!this._config.scroll) {
Expand Down Expand Up @@ -233,7 +234,7 @@ class Offcanvas extends BaseComponent {
* ------------------------------------------------------------------------
*/

EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
EventHandler.on(getDocument(), EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
const target = getElementFromSelector(this)

if (['A', 'AREA'].includes(this.tagName)) {
Expand Down
17 changes: 10 additions & 7 deletions js/src/util/scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@

import SelectorEngine from '../dom/selector-engine'
import Manipulator from '../dom/manipulator'
import { getDocument, getWindow } from './index'

const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed'
const SELECTOR_STICKY_CONTENT = '.sticky-top'
const windowRef = getWindow()
const documentRef = getDocument()

const getWidth = () => {
// https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
const documentWidth = document.documentElement.clientWidth
return Math.abs(window.innerWidth - documentWidth)
const documentWidth = documentRef.documentElement.clientWidth
return Math.abs(getWindow().innerWidth - documentWidth)
}

const hide = (width = getWidth()) => {
document.body.style.overflow = 'hidden'
documentRef.body.style.overflow = 'hidden'
_setElementAttributes(SELECTOR_FIXED_CONTENT, 'paddingRight', calculatedValue => calculatedValue + width)
_setElementAttributes(SELECTOR_STICKY_CONTENT, 'marginRight', calculatedValue => calculatedValue - width)
_setElementAttributes('body', 'paddingRight', calculatedValue => calculatedValue + width)
Expand All @@ -28,19 +31,19 @@ const _setElementAttributes = (selector, styleProp, callback) => {
const scrollbarWidth = getWidth()
SelectorEngine.find(selector)
.forEach(element => {
if (element !== document.body && window.innerWidth > element.clientWidth + scrollbarWidth) {
if (element !== documentRef.body && windowRef.innerWidth > element.clientWidth + scrollbarWidth) {
return
}

const actualValue = element.style[styleProp]
const calculatedValue = window.getComputedStyle(element)[styleProp]
const calculatedValue = windowRef.getComputedStyle(element)[styleProp]
Manipulator.setDataAttribute(element, styleProp, actualValue)
element.style[styleProp] = callback(Number.parseFloat(calculatedValue)) + 'px'
})
}

const reset = () => {
document.body.style.overflow = 'auto'
documentRef.body.style.overflow = 'auto'
_resetElementAttributes(SELECTOR_FIXED_CONTENT, 'paddingRight')
_resetElementAttributes(SELECTOR_STICKY_CONTENT, 'marginRight')
_resetElementAttributes('body', 'paddingRight')
Expand All @@ -49,7 +52,7 @@ const reset = () => {
const _resetElementAttributes = (selector, styleProp) => {
SelectorEngine.find(selector).forEach(element => {
const value = Manipulator.getDataAttribute(element, styleProp)
if (typeof value === 'undefined' && element === document.body) {
if (typeof value === 'undefined' && element === documentRef.body) {
element.style.removeProperty(styleProp)
} else {
Manipulator.removeDataAttribute(element, styleProp)
Expand Down

0 comments on commit 952d68b

Please sign in to comment.