diff options
author | GeoSot <geo.sotis@gmail.com> | 2021-07-28 17:39:32 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-28 17:39:32 +0300 |
commit | 4bfd8a2cbcb10610b4078cefa45756b4a96301a0 (patch) | |
tree | 2358a92299ff8fa105623932caacddbf8cd83a41 /js/src/util | |
parent | 047145e8086793e7c39747e70f5d74a8860c2e50 (diff) |
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>
Diffstat (limited to 'js/src/util')
-rw-r--r-- | js/src/util/component-functions.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/js/src/util/component-functions.js b/js/src/util/component-functions.js new file mode 100644 index 0000000000..b7d180e0db --- /dev/null +++ b/js/src/util/component-functions.js @@ -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 +} |