From f5ca90bd808a2841f07a301974bdc6fab97abca4 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sat, 12 Dec 2020 15:40:51 +0200 Subject: [PATCH 1/3] util: change `isRTL` to a function This allows the bundler to tree-shake the function. --- js/src/carousel.js | 8 ++++---- js/src/dropdown.js | 12 ++++++------ js/src/modal.js | 7 +++---- js/src/tooltip.js | 10 +++++++--- js/src/util/index.js | 2 +- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/js/src/carousel.js b/js/src/carousel.js index 06a391419..d38eb481d 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -251,7 +251,7 @@ class Carousel extends BaseComponent { // swipe left if (direction > 0) { - if (isRTL) { + if (isRTL()) { this.next() } else { this.prev() @@ -260,7 +260,7 @@ class Carousel extends BaseComponent { // swipe right if (direction < 0) { - if (isRTL) { + if (isRTL()) { this.prev() } else { this.next() @@ -348,14 +348,14 @@ class Carousel extends BaseComponent { if (event.key === ARROW_LEFT_KEY) { event.preventDefault() - if (isRTL) { + if (isRTL()) { this.next() } else { this.prev() } } else if (event.key === ARROW_RIGHT_KEY) { event.preventDefault() - if (isRTL) { + if (isRTL()) { this.prev() } else { this.next() diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 4d65008f8..ba588a606 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -64,12 +64,12 @@ const SELECTOR_MENU = '.dropdown-menu' const SELECTOR_NAVBAR_NAV = '.navbar-nav' const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)' -const PLACEMENT_TOP = isRTL ? 'top-end' : 'top-start' -const PLACEMENT_TOPEND = isRTL ? 'top-start' : 'top-end' -const PLACEMENT_BOTTOM = isRTL ? 'bottom-end' : 'bottom-start' -const PLACEMENT_BOTTOMEND = isRTL ? 'bottom-start' : 'bottom-end' -const PLACEMENT_RIGHT = isRTL ? 'left-start' : 'right-start' -const PLACEMENT_LEFT = isRTL ? 'right-start' : 'left-start' +const PLACEMENT_TOP = isRTL() ? 'top-end' : 'top-start' +const PLACEMENT_TOPEND = isRTL() ? 'top-start' : 'top-end' +const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start' +const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end' +const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start' +const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start' const Default = { offset: 0, diff --git a/js/src/modal.js b/js/src/modal.js index 87c22943a..b1656803d 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -431,14 +431,13 @@ class Modal extends BaseComponent { // ---------------------------------------------------------------------- _adjustDialog() { - const isModalOverflowing = - this._element.scrollHeight > document.documentElement.clientHeight + const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight - if ((!this._isBodyOverflowing && isModalOverflowing && !isRTL) || (this._isBodyOverflowing && !isModalOverflowing && isRTL)) { + if ((!this._isBodyOverflowing && isModalOverflowing && !isRTL()) || (this._isBodyOverflowing && !isModalOverflowing && isRTL())) { this._element.style.paddingLeft = `${this._scrollbarWidth}px` } - if ((this._isBodyOverflowing && !isModalOverflowing && !isRTL) || (!this._isBodyOverflowing && isModalOverflowing && isRTL)) { + if ((this._isBodyOverflowing && !isModalOverflowing && !isRTL()) || (!this._isBodyOverflowing && isModalOverflowing && isRTL())) { this._element.style.paddingRight = `${this._scrollbarWidth}px` } } diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 63a30cf2f..77a4dcc3d 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -63,9 +63,9 @@ const DefaultType = { const AttachmentMap = { AUTO: 'auto', TOP: 'top', - RIGHT: isRTL ? 'left' : 'right', + RIGHT: isRTL() ? 'left' : 'right', BOTTOM: 'bottom', - LEFT: isRTL ? 'right' : 'left' + LEFT: isRTL() ? 'right' : 'left' } const Default = { @@ -533,7 +533,11 @@ class Tooltip extends BaseComponent { triggers.forEach(trigger => { if (trigger === 'click') { - EventHandler.on(this._element, this.constructor.Event.CLICK, this.config.selector, event => this.toggle(event) + EventHandler.on( + this._element, + this.constructor.Event.CLICK, + this.config.selector, + event => this.toggle(event) ) } else if (trigger !== TRIGGER_MANUAL) { const eventIn = trigger === TRIGGER_HOVER ? diff --git a/js/src/util/index.js b/js/src/util/index.js index 9ccad1cbb..dce4f893f 100644 --- a/js/src/util/index.js +++ b/js/src/util/index.js @@ -186,7 +186,7 @@ const onDOMContentLoaded = callback => { } } -const isRTL = document.documentElement.dir === 'rtl' +const isRTL = () => document.documentElement.dir === 'rtl' const defineJQueryPlugin = (name, plugin) => { onDOMContentLoaded(() => { From 1c2a3bc418298a3944551c579405747a449276ca Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Wed, 16 Dec 2020 11:07:37 +0200 Subject: [PATCH 2/3] WIP --- js/src/carousel.js | 10 ++++++---- js/src/dropdown.js | 14 ++++++++------ js/src/modal.js | 6 ++++-- js/src/tooltip.js | 5 +++-- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/js/src/carousel.js b/js/src/carousel.js index d38eb481d..6199023f6 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -96,6 +96,8 @@ const SELECTOR_DATA_RIDE = '[data-bs-ride="carousel"]' const POINTER_TYPE_TOUCH = 'touch' const POINTER_TYPE_PEN = 'pen' +const IS_RTL = isRTL() + /** * ------------------------------------------------------------------------ * Class Definition @@ -251,7 +253,7 @@ class Carousel extends BaseComponent { // swipe left if (direction > 0) { - if (isRTL()) { + if (IS_RTL) { this.next() } else { this.prev() @@ -260,7 +262,7 @@ class Carousel extends BaseComponent { // swipe right if (direction < 0) { - if (isRTL()) { + if (IS_RTL) { this.prev() } else { this.next() @@ -348,14 +350,14 @@ class Carousel extends BaseComponent { if (event.key === ARROW_LEFT_KEY) { event.preventDefault() - if (isRTL()) { + if (IS_RTL) { this.next() } else { this.prev() } } else if (event.key === ARROW_RIGHT_KEY) { event.preventDefault() - if (isRTL()) { + if (IS_RTL) { this.prev() } else { this.next() diff --git a/js/src/dropdown.js b/js/src/dropdown.js index ba588a606..8a6e75bfa 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -64,12 +64,14 @@ const SELECTOR_MENU = '.dropdown-menu' const SELECTOR_NAVBAR_NAV = '.navbar-nav' const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)' -const PLACEMENT_TOP = isRTL() ? 'top-end' : 'top-start' -const PLACEMENT_TOPEND = isRTL() ? 'top-start' : 'top-end' -const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start' -const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end' -const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start' -const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start' +const IS_RTL = isRTL() + +const PLACEMENT_TOP = IS_RTL ? 'top-end' : 'top-start' +const PLACEMENT_TOPEND = IS_RTL ? 'top-start' : 'top-end' +const PLACEMENT_BOTTOM = IS_RTL ? 'bottom-end' : 'bottom-start' +const PLACEMENT_BOTTOMEND = IS_RTL ? 'bottom-start' : 'bottom-end' +const PLACEMENT_RIGHT = IS_RTL ? 'left-start' : 'right-start' +const PLACEMENT_LEFT = IS_RTL ? 'right-start' : 'left-start' const Default = { offset: 0, diff --git a/js/src/modal.js b/js/src/modal.js index b1656803d..b20cabc23 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -72,6 +72,8 @@ const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="modal"]' const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top' const SELECTOR_STICKY_CONTENT = '.sticky-top' +const IS_RTL = isRTL() + /** * ------------------------------------------------------------------------ * Class Definition @@ -433,11 +435,11 @@ class Modal extends BaseComponent { _adjustDialog() { const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight - if ((!this._isBodyOverflowing && isModalOverflowing && !isRTL()) || (this._isBodyOverflowing && !isModalOverflowing && isRTL())) { + if ((!this._isBodyOverflowing && isModalOverflowing && !IS_RTL) || (this._isBodyOverflowing && !isModalOverflowing && IS_RTL)) { this._element.style.paddingLeft = `${this._scrollbarWidth}px` } - if ((this._isBodyOverflowing && !isModalOverflowing && !isRTL()) || (!this._isBodyOverflowing && isModalOverflowing && isRTL())) { + if ((this._isBodyOverflowing && !isModalOverflowing && !IS_RTL) || (!this._isBodyOverflowing && isModalOverflowing && IS_RTL)) { this._element.style.paddingRight = `${this._scrollbarWidth}px` } } diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 77a4dcc3d..634c803ad 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -40,6 +40,7 @@ const EVENT_KEY = `.${DATA_KEY}` const CLASS_PREFIX = 'bs-tooltip' const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g') const DISALLOWED_ATTRIBUTES = new Set(['sanitize', 'allowList', 'sanitizeFn']) +const IS_RTL = isRTL() const DefaultType = { animation: 'boolean', @@ -63,9 +64,9 @@ const DefaultType = { const AttachmentMap = { AUTO: 'auto', TOP: 'top', - RIGHT: isRTL() ? 'left' : 'right', + RIGHT: IS_RTL ? 'left' : 'right', BOTTOM: 'bottom', - LEFT: isRTL() ? 'right' : 'left' + LEFT: IS_RTL ? 'right' : 'left' } const Default = { From ec2172dfdbf6a7111934200049660905726ce6ca Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Wed, 16 Dec 2020 11:24:14 +0200 Subject: [PATCH 3/3] Revert "WIP" --- js/src/carousel.js | 10 ++++------ js/src/dropdown.js | 14 ++++++-------- js/src/modal.js | 6 ++---- js/src/tooltip.js | 5 ++--- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/js/src/carousel.js b/js/src/carousel.js index 6199023f6..d38eb481d 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -96,8 +96,6 @@ const SELECTOR_DATA_RIDE = '[data-bs-ride="carousel"]' const POINTER_TYPE_TOUCH = 'touch' const POINTER_TYPE_PEN = 'pen' -const IS_RTL = isRTL() - /** * ------------------------------------------------------------------------ * Class Definition @@ -253,7 +251,7 @@ class Carousel extends BaseComponent { // swipe left if (direction > 0) { - if (IS_RTL) { + if (isRTL()) { this.next() } else { this.prev() @@ -262,7 +260,7 @@ class Carousel extends BaseComponent { // swipe right if (direction < 0) { - if (IS_RTL) { + if (isRTL()) { this.prev() } else { this.next() @@ -350,14 +348,14 @@ class Carousel extends BaseComponent { if (event.key === ARROW_LEFT_KEY) { event.preventDefault() - if (IS_RTL) { + if (isRTL()) { this.next() } else { this.prev() } } else if (event.key === ARROW_RIGHT_KEY) { event.preventDefault() - if (IS_RTL) { + if (isRTL()) { this.prev() } else { this.next() diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 8a6e75bfa..ba588a606 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -64,14 +64,12 @@ const SELECTOR_MENU = '.dropdown-menu' const SELECTOR_NAVBAR_NAV = '.navbar-nav' const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)' -const IS_RTL = isRTL() - -const PLACEMENT_TOP = IS_RTL ? 'top-end' : 'top-start' -const PLACEMENT_TOPEND = IS_RTL ? 'top-start' : 'top-end' -const PLACEMENT_BOTTOM = IS_RTL ? 'bottom-end' : 'bottom-start' -const PLACEMENT_BOTTOMEND = IS_RTL ? 'bottom-start' : 'bottom-end' -const PLACEMENT_RIGHT = IS_RTL ? 'left-start' : 'right-start' -const PLACEMENT_LEFT = IS_RTL ? 'right-start' : 'left-start' +const PLACEMENT_TOP = isRTL() ? 'top-end' : 'top-start' +const PLACEMENT_TOPEND = isRTL() ? 'top-start' : 'top-end' +const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start' +const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end' +const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start' +const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start' const Default = { offset: 0, diff --git a/js/src/modal.js b/js/src/modal.js index b20cabc23..b1656803d 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -72,8 +72,6 @@ const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="modal"]' const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top' const SELECTOR_STICKY_CONTENT = '.sticky-top' -const IS_RTL = isRTL() - /** * ------------------------------------------------------------------------ * Class Definition @@ -435,11 +433,11 @@ class Modal extends BaseComponent { _adjustDialog() { const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight - if ((!this._isBodyOverflowing && isModalOverflowing && !IS_RTL) || (this._isBodyOverflowing && !isModalOverflowing && IS_RTL)) { + if ((!this._isBodyOverflowing && isModalOverflowing && !isRTL()) || (this._isBodyOverflowing && !isModalOverflowing && isRTL())) { this._element.style.paddingLeft = `${this._scrollbarWidth}px` } - if ((this._isBodyOverflowing && !isModalOverflowing && !IS_RTL) || (!this._isBodyOverflowing && isModalOverflowing && IS_RTL)) { + if ((this._isBodyOverflowing && !isModalOverflowing && !isRTL()) || (!this._isBodyOverflowing && isModalOverflowing && isRTL())) { this._element.style.paddingRight = `${this._scrollbarWidth}px` } } diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 634c803ad..77a4dcc3d 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -40,7 +40,6 @@ const EVENT_KEY = `.${DATA_KEY}` const CLASS_PREFIX = 'bs-tooltip' const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g') const DISALLOWED_ATTRIBUTES = new Set(['sanitize', 'allowList', 'sanitizeFn']) -const IS_RTL = isRTL() const DefaultType = { animation: 'boolean', @@ -64,9 +63,9 @@ const DefaultType = { const AttachmentMap = { AUTO: 'auto', TOP: 'top', - RIGHT: IS_RTL ? 'left' : 'right', + RIGHT: isRTL() ? 'left' : 'right', BOTTOM: 'bottom', - LEFT: IS_RTL ? 'right' : 'left' + LEFT: isRTL() ? 'right' : 'left' } const Default = {