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

modal: move common code to a new isAnimated method #33056

Merged
merged 2 commits into from Mar 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 17 additions & 16 deletions js/src/modal.js
Expand Up @@ -113,7 +113,7 @@ class Modal extends BaseComponent {
return
}

if (this._element.classList.contains(CLASS_NAME_FADE)) {
if (this._isAnimated()) {
this._isTransitioning = true
}

Expand Down Expand Up @@ -164,9 +164,9 @@ class Modal extends BaseComponent {
}

this._isShown = false
const transition = this._element.classList.contains(CLASS_NAME_FADE)
const isAnimated = this._isAnimated()

if (transition) {
if (isAnimated) {
this._isTransitioning = true
}

Expand All @@ -180,7 +180,7 @@ class Modal extends BaseComponent {
EventHandler.off(this._element, EVENT_CLICK_DISMISS)
EventHandler.off(this._dialog, EVENT_MOUSEDOWN_DISMISS)

if (transition) {
if (isAnimated) {
const transitionDuration = getTransitionDurationFromElement(this._element)

EventHandler.one(this._element, 'transitionend', event => this._hideModal(event))
Expand Down Expand Up @@ -229,7 +229,7 @@ class Modal extends BaseComponent {
}

_showElement(relatedTarget) {
const transition = this._element.classList.contains(CLASS_NAME_FADE)
const isAnimated = this._isAnimated()
const modalBody = SelectorEngine.findOne(SELECTOR_MODAL_BODY, this._dialog)

if (!this._element.parentNode || this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
Expand All @@ -247,7 +247,7 @@ class Modal extends BaseComponent {
modalBody.scrollTop = 0
}

if (transition) {
if (isAnimated) {
reflow(this._element)
}

Expand All @@ -268,7 +268,7 @@ class Modal extends BaseComponent {
})
}

if (transition) {
if (isAnimated) {
const transitionDuration = getTransitionDurationFromElement(this._dialog)

EventHandler.one(this._dialog, 'transitionend', transitionComplete)
Expand Down Expand Up @@ -332,16 +332,13 @@ class Modal extends BaseComponent {
}

_showBackdrop(callback) {
const animate = this._element.classList.contains(CLASS_NAME_FADE) ?
CLASS_NAME_FADE :
''

const isAnimated = this._isAnimated()
if (this._isShown && this._config.backdrop) {
this._backdrop = document.createElement('div')
this._backdrop.className = CLASS_NAME_BACKDROP

if (animate) {
this._backdrop.classList.add(animate)
if (isAnimated) {
this._backdrop.classList.add(CLASS_NAME_FADE)
}

document.body.appendChild(this._backdrop)
Expand All @@ -363,13 +360,13 @@ class Modal extends BaseComponent {
}
})

if (animate) {
if (isAnimated) {
reflow(this._backdrop)
}

this._backdrop.classList.add(CLASS_NAME_SHOW)

if (!animate) {
if (!isAnimated) {
callback()
return
}
Expand All @@ -386,7 +383,7 @@ class Modal extends BaseComponent {
callback()
}

if (this._element.classList.contains(CLASS_NAME_FADE)) {
if (isAnimated) {
const backdropTransitionDuration = getTransitionDurationFromElement(this._backdrop)
EventHandler.one(this._backdrop, 'transitionend', callbackRemove)
emulateTransitionEnd(this._backdrop, backdropTransitionDuration)
Expand All @@ -398,6 +395,10 @@ class Modal extends BaseComponent {
}
}

_isAnimated() {
return this._element.classList.contains(CLASS_NAME_FADE)
}

_triggerBackdropTransition() {
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
if (hideEvent.defaultPrevented) {
Expand Down