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

being ssr friendly when accessing dom objects #33131

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add usage to offcanvas.js, scrollbar.js & change on modal, base-compo…
…nent
GeoSot authored and XhmikosR committed Mar 17, 2021
commit 952d68b4536f1928fbf7b95bc46b99070dba24f2
2 changes: 1 addition & 1 deletion js/src/base-component.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions js/src/dom/manipulator.js
Original file line number Diff line number Diff line change
@@ -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
}
},

2 changes: 1 addition & 1 deletion js/src/modal.js
Original file line number Diff line number Diff line change
@@ -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
}

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

import {
defineJQueryPlugin,
getDocument,
getElementFromSelector,
getSelectorFromElement,
getTransitionDurationFromElement,
@@ -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) {
@@ -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) {
@@ -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)) {
17 changes: 10 additions & 7 deletions js/src/util/scrollbar.js
Original file line number Diff line number Diff line change
@@ -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)
@@ -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')
@@ -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)