mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-11 08:04:59 +02:00
Use a streamlined way to trigger component dismiss (#34170)
* use a streamlined way to trigger component dismiss * add documentation Co-authored-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
@@ -5,13 +5,10 @@
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
import {
|
||||
defineJQueryPlugin,
|
||||
getElementFromSelector,
|
||||
isDisabled
|
||||
} from './util/index'
|
||||
import { defineJQueryPlugin } from './util/index'
|
||||
import EventHandler from './dom/event-handler'
|
||||
import BaseComponent from './base-component'
|
||||
import { enableDismissTrigger } from './util/component-functions'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
@@ -22,15 +19,9 @@ import BaseComponent from './base-component'
|
||||
const NAME = 'alert'
|
||||
const DATA_KEY = 'bs.alert'
|
||||
const EVENT_KEY = `.${DATA_KEY}`
|
||||
const DATA_API_KEY = '.data-api'
|
||||
|
||||
const SELECTOR_DISMISS = '[data-bs-dismiss="alert"]'
|
||||
|
||||
const EVENT_CLOSE = `close${EVENT_KEY}`
|
||||
const EVENT_CLOSED = `closed${EVENT_KEY}`
|
||||
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
||||
|
||||
const CLASS_NAME_ALERT = 'alert'
|
||||
const CLASS_NAME_FADE = 'fade'
|
||||
const CLASS_NAME_SHOW = 'show'
|
||||
|
||||
@@ -94,20 +85,7 @@ class Alert extends BaseComponent {
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, function (event) {
|
||||
if (['A', 'AREA'].includes(this.tagName)) {
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
if (isDisabled(this)) {
|
||||
return
|
||||
}
|
||||
|
||||
const target = getElementFromSelector(this) || this.closest(`.${CLASS_NAME_ALERT}`)
|
||||
const alert = Alert.getOrCreateInstance(target)
|
||||
alert.close()
|
||||
})
|
||||
|
||||
enableDismissTrigger(Alert, 'close')
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* jQuery
|
||||
|
@@ -20,6 +20,7 @@ import ScrollBarHelper from './util/scrollbar'
|
||||
import BaseComponent from './base-component'
|
||||
import Backdrop from './util/backdrop'
|
||||
import FocusTrap from './util/focustrap'
|
||||
import { enableDismissTrigger } from './util/component-functions'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
@@ -62,11 +63,9 @@ const CLASS_NAME_FADE = 'fade'
|
||||
const CLASS_NAME_SHOW = 'show'
|
||||
const CLASS_NAME_STATIC = 'modal-static'
|
||||
|
||||
const SELECTOR = '.modal'
|
||||
const SELECTOR_DIALOG = '.modal-dialog'
|
||||
const SELECTOR_MODAL_BODY = '.modal-body'
|
||||
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="modal"]'
|
||||
const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="modal"]'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
@@ -143,11 +142,7 @@ class Modal extends BaseComponent {
|
||||
this._showBackdrop(() => this._showElement(relatedTarget))
|
||||
}
|
||||
|
||||
hide(event) {
|
||||
if (event && ['A', 'AREA'].includes(event.target.tagName)) {
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
hide() {
|
||||
if (!this._isShown || this._isTransitioning) {
|
||||
return
|
||||
}
|
||||
@@ -421,12 +416,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
|
||||
data.toggle(this)
|
||||
})
|
||||
|
||||
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_DISMISS, function (event) {
|
||||
const target = getElementFromSelector(this) || this.closest(SELECTOR)
|
||||
const modal = Modal.getOrCreateInstance(target)
|
||||
|
||||
modal.hide(event)
|
||||
})
|
||||
enableDismissTrigger(Modal)
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
|
@@ -19,6 +19,7 @@ import SelectorEngine from './dom/selector-engine'
|
||||
import Manipulator from './dom/manipulator'
|
||||
import Backdrop from './util/backdrop'
|
||||
import FocusTrap from './util/focustrap'
|
||||
import { enableDismissTrigger } from './util/component-functions'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
@@ -54,10 +55,8 @@ const EVENT_SHOWN = `shown${EVENT_KEY}`
|
||||
const EVENT_HIDE = `hide${EVENT_KEY}`
|
||||
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
||||
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
||||
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
|
||||
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
|
||||
|
||||
const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="offcanvas"]'
|
||||
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="offcanvas"]'
|
||||
|
||||
/**
|
||||
@@ -197,8 +196,6 @@ class Offcanvas extends BaseComponent {
|
||||
}
|
||||
|
||||
_addEventListeners() {
|
||||
EventHandler.on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, () => this.hide())
|
||||
|
||||
EventHandler.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
|
||||
if (this._config.keyboard && event.key === ESCAPE_KEY) {
|
||||
this.hide()
|
||||
@@ -263,6 +260,7 @@ EventHandler.on(window, EVENT_LOAD_DATA_API, () =>
|
||||
SelectorEngine.find(OPEN_SELECTOR).forEach(el => Offcanvas.getOrCreateInstance(el).show())
|
||||
)
|
||||
|
||||
enableDismissTrigger(Offcanvas)
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* jQuery
|
||||
|
@@ -13,6 +13,7 @@ import {
|
||||
import EventHandler from './dom/event-handler'
|
||||
import Manipulator from './dom/manipulator'
|
||||
import BaseComponent from './base-component'
|
||||
import { enableDismissTrigger } from './util/component-functions'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
@@ -24,7 +25,6 @@ const NAME = 'toast'
|
||||
const DATA_KEY = 'bs.toast'
|
||||
const EVENT_KEY = `.${DATA_KEY}`
|
||||
|
||||
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
|
||||
const EVENT_MOUSEOVER = `mouseover${EVENT_KEY}`
|
||||
const EVENT_MOUSEOUT = `mouseout${EVENT_KEY}`
|
||||
const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
|
||||
@@ -51,8 +51,6 @@ const Default = {
|
||||
delay: 5000
|
||||
}
|
||||
|
||||
const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="toast"]'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* Class Definition
|
||||
@@ -202,7 +200,6 @@ class Toast extends BaseComponent {
|
||||
}
|
||||
|
||||
_setListeners() {
|
||||
EventHandler.on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, () => this.hide())
|
||||
EventHandler.on(this._element, EVENT_MOUSEOVER, event => this._onInteraction(event, true))
|
||||
EventHandler.on(this._element, EVENT_MOUSEOUT, event => this._onInteraction(event, false))
|
||||
EventHandler.on(this._element, EVENT_FOCUSIN, event => this._onInteraction(event, true))
|
||||
@@ -231,6 +228,8 @@ class Toast extends BaseComponent {
|
||||
}
|
||||
}
|
||||
|
||||
enableDismissTrigger(Toast)
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* jQuery
|
||||
|
34
js/src/util/component-functions.js
Normal file
34
js/src/util/component-functions.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* --------------------------------------------------------------------------
|
||||
* Bootstrap (v5.0.2): util/component-functions.js
|
||||
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
import EventHandler from '../dom/event-handler'
|
||||
import { getElementFromSelector, isDisabled } from './index'
|
||||
|
||||
const enableDismissTrigger = (component, method = 'hide') => {
|
||||
const clickEvent = `click.dismiss${component.EVENT_KEY}`
|
||||
const name = component.NAME
|
||||
|
||||
EventHandler.on(document, clickEvent, `[data-bs-dismiss="${name}"]`, function (event) {
|
||||
if (['A', 'AREA'].includes(this.tagName)) {
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
if (isDisabled(this)) {
|
||||
return
|
||||
}
|
||||
|
||||
const target = getElementFromSelector(this) || this.closest(`.${name}`)
|
||||
const instance = component.getOrCreateInstance(target)
|
||||
|
||||
// Method argument is left, for Alert and only, as it doesn't implement the 'hide' method
|
||||
instance[method]()
|
||||
})
|
||||
}
|
||||
|
||||
export {
|
||||
enableDismissTrigger
|
||||
}
|
Reference in New Issue
Block a user