mirror of
https://github.com/tenrok/bootstrap.git
synced 2026-06-17 19:21:23 +03:00
Add dispose and toggler
This commit is contained in:
@@ -18,6 +18,7 @@ export { default as Popover } from './src/popover'
|
|||||||
export { default as ScrollSpy } from './src/scrollspy'
|
export { default as ScrollSpy } from './src/scrollspy'
|
||||||
export { default as Tab } from './src/tab'
|
export { default as Tab } from './src/tab'
|
||||||
export { default as Toast } from './src/toast'
|
export { default as Toast } from './src/toast'
|
||||||
|
export { default as Toggler } from './src/toggler'
|
||||||
export { default as Tooltip } from './src/tooltip'
|
export { default as Tooltip } from './src/tooltip'
|
||||||
|
|
||||||
enableMagicActions()
|
enableMagicActions()
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import Popover from './src/popover'
|
|||||||
import ScrollSpy from './src/scrollspy'
|
import ScrollSpy from './src/scrollspy'
|
||||||
import Tab from './src/tab'
|
import Tab from './src/tab'
|
||||||
import Toast from './src/toast'
|
import Toast from './src/toast'
|
||||||
|
import Toggler from './src/toggler'
|
||||||
import Tooltip from './src/tooltip'
|
import Tooltip from './src/tooltip'
|
||||||
import { enableMagicActions } from './src/dom/magic-actions'
|
import { enableMagicActions } from './src/dom/magic-actions'
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ export default {
|
|||||||
ScrollSpy,
|
ScrollSpy,
|
||||||
Tab,
|
Tab,
|
||||||
Toast,
|
Toast,
|
||||||
|
Toggler,
|
||||||
Tooltip
|
Tooltip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
/**
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
* Bootstrap (v5.1.3): dispose.js
|
||||||
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { defineJQueryPlugin } from './util/index'
|
||||||
|
import EventHandler from './dom/event-handler'
|
||||||
|
import BaseComponent from './base-component'
|
||||||
|
import { enableDismissTrigger } from './dom/magic-actions'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constants
|
||||||
|
*/
|
||||||
|
|
||||||
|
const NAME = 'dispose'
|
||||||
|
const DATA_KEY = 'bs.dispose'
|
||||||
|
const EVENT_KEY = `.${DATA_KEY}`
|
||||||
|
|
||||||
|
const EVENT_DISPOSE = `dispose${EVENT_KEY}`
|
||||||
|
const EVENT_DISPOSED = `disposed${EVENT_KEY}`
|
||||||
|
const CLASS_NAME_FADE = 'fade'
|
||||||
|
const CLASS_NAME_SHOW = 'show'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class definition
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Dispose extends BaseComponent {
|
||||||
|
// Getters
|
||||||
|
static get NAME() {
|
||||||
|
return NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private
|
||||||
|
dispose() {
|
||||||
|
const closeEvent = EventHandler.trigger(this._element, EVENT_DISPOSE)
|
||||||
|
|
||||||
|
if (closeEvent.defaultPrevented) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this._element.classList.remove(CLASS_NAME_SHOW)
|
||||||
|
|
||||||
|
const isAnimated = this._element.classList.contains(CLASS_NAME_FADE)
|
||||||
|
const completeCallback = () => {
|
||||||
|
this._element.remove()
|
||||||
|
EventHandler.trigger(this._element, EVENT_DISPOSED)
|
||||||
|
}
|
||||||
|
|
||||||
|
this._queueCallback(completeCallback, this._element, isAnimated)
|
||||||
|
super.dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data API implementation
|
||||||
|
*/
|
||||||
|
|
||||||
|
enableDismissTrigger(Dispose, 'dispose')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jQuery
|
||||||
|
*/
|
||||||
|
|
||||||
|
defineJQueryPlugin(Dispose)
|
||||||
|
|
||||||
|
export default Dispose
|
||||||
+9
-22
@@ -5,14 +5,14 @@
|
|||||||
* --------------------------------------------------------------------------
|
* --------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { defineJQueryPlugin, getElementFromSelector, isRTL, isVisible, reflow } from './util/index'
|
import { defineJQueryPlugin, isRTL, isVisible, reflow } from './util/index'
|
||||||
import EventHandler from './dom/event-handler'
|
import EventHandler from './dom/event-handler'
|
||||||
import SelectorEngine from './dom/selector-engine'
|
import SelectorEngine from './dom/selector-engine'
|
||||||
import ScrollBarHelper from './util/scrollbar'
|
import ScrollBarHelper from './util/scrollbar'
|
||||||
import BaseComponent from './base-component'
|
import BaseComponent from './base-component'
|
||||||
import Backdrop from './util/backdrop'
|
import Backdrop from './util/backdrop'
|
||||||
import FocusTrap from './util/focustrap'
|
import FocusTrap from './util/focustrap'
|
||||||
import { enableDismissTrigger } from './util/component-functions'
|
import { enableDismissTrigger, eventActionOnPlugin } from './dom/magic-actions'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constants
|
* Constants
|
||||||
@@ -21,7 +21,6 @@ import { enableDismissTrigger } from './util/component-functions'
|
|||||||
const NAME = 'modal'
|
const NAME = 'modal'
|
||||||
const DATA_KEY = 'bs.modal'
|
const DATA_KEY = 'bs.modal'
|
||||||
const EVENT_KEY = `.${DATA_KEY}`
|
const EVENT_KEY = `.${DATA_KEY}`
|
||||||
const DATA_API_KEY = '.data-api'
|
|
||||||
const ESCAPE_KEY = 'Escape'
|
const ESCAPE_KEY = 'Escape'
|
||||||
|
|
||||||
const EVENT_HIDE = `hide${EVENT_KEY}`
|
const EVENT_HIDE = `hide${EVENT_KEY}`
|
||||||
@@ -31,7 +30,6 @@ const EVENT_SHOW = `show${EVENT_KEY}`
|
|||||||
const EVENT_SHOWN = `shown${EVENT_KEY}`
|
const EVENT_SHOWN = `shown${EVENT_KEY}`
|
||||||
const EVENT_RESIZE = `resize${EVENT_KEY}`
|
const EVENT_RESIZE = `resize${EVENT_KEY}`
|
||||||
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
|
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
|
||||||
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
|
||||||
|
|
||||||
const CLASS_NAME_OPEN = 'modal-open'
|
const CLASS_NAME_OPEN = 'modal-open'
|
||||||
const CLASS_NAME_FADE = 'fade'
|
const CLASS_NAME_FADE = 'fade'
|
||||||
@@ -341,35 +339,24 @@ class Modal extends BaseComponent {
|
|||||||
* Data API implementation
|
* Data API implementation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
eventActionOnPlugin(Modal, 'click', SELECTOR_DATA_TOGGLE, 'toggle', data => {
|
||||||
const target = getElementFromSelector(this)
|
EventHandler.one(data.target, EVENT_SHOW, showEvent => {
|
||||||
|
|
||||||
if (['A', 'AREA'].includes(this.tagName)) {
|
|
||||||
event.preventDefault()
|
|
||||||
}
|
|
||||||
|
|
||||||
EventHandler.one(target, EVENT_SHOW, showEvent => {
|
|
||||||
if (showEvent.defaultPrevented) {
|
if (showEvent.defaultPrevented) {
|
||||||
// only register focus restorer if modal will actually get shown
|
// only register focus restorer if modal will actually get shown
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
EventHandler.one(target, EVENT_HIDDEN, () => {
|
EventHandler.one(data.target, EVENT_HIDDEN, () => {
|
||||||
if (isVisible(this)) {
|
if (isVisible(data.event.target)) {
|
||||||
this.focus()
|
data.event.target.focus()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
// avoid conflict when clicking a toggler of an offcanvas, while another is open
|
||||||
// avoid conflict when clicking modal toggler while another one is open
|
|
||||||
const alreadyOpen = SelectorEngine.findOne(OPEN_SELECTOR)
|
const alreadyOpen = SelectorEngine.findOne(OPEN_SELECTOR)
|
||||||
if (alreadyOpen) {
|
if (alreadyOpen && alreadyOpen !== data.target) {
|
||||||
Modal.getInstance(alreadyOpen).hide()
|
Modal.getInstance(alreadyOpen).hide()
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = Modal.getOrCreateInstance(target)
|
|
||||||
|
|
||||||
data.toggle(this)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
enableDismissTrigger(Modal)
|
enableDismissTrigger(Modal)
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
/**
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
* Bootstrap (v5.1.3): toggle.js
|
||||||
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { defineJQueryPlugin, getElement } from './util/index'
|
||||||
|
import EventHandler from './dom/event-handler'
|
||||||
|
import BaseComponent from './base-component'
|
||||||
|
import { eventActionOnPlugin } from './dom/magic-actions'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constants
|
||||||
|
*/
|
||||||
|
|
||||||
|
const NAME = 'toggler'
|
||||||
|
const DATA_KEY = 'bs.toggle'
|
||||||
|
const EVENT_KEY = `.${DATA_KEY}`
|
||||||
|
|
||||||
|
const EVENT_TOGGLE = `toggle${EVENT_KEY}`
|
||||||
|
const EVENT_TOGGLED = `toggled${EVENT_KEY}`
|
||||||
|
|
||||||
|
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="toggler"]'
|
||||||
|
/**
|
||||||
|
* Class definition
|
||||||
|
*/
|
||||||
|
|
||||||
|
const DefaultType = {
|
||||||
|
attribute: 'string',
|
||||||
|
target: 'element',
|
||||||
|
value: 'string'
|
||||||
|
}
|
||||||
|
|
||||||
|
const Default = {
|
||||||
|
attribute: 'class',
|
||||||
|
target: null,
|
||||||
|
value: null
|
||||||
|
}
|
||||||
|
|
||||||
|
class Toggler extends BaseComponent {
|
||||||
|
// Getters
|
||||||
|
static get Default() {
|
||||||
|
return Default
|
||||||
|
}
|
||||||
|
|
||||||
|
static get DefaultType() {
|
||||||
|
return DefaultType
|
||||||
|
}
|
||||||
|
|
||||||
|
static get NAME() {
|
||||||
|
return NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
_configAfterMerge(config) {
|
||||||
|
config.target = getElement(config.target) || this._element
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private
|
||||||
|
toggle() {
|
||||||
|
const toggleEvent = EventHandler.trigger(this._element, EVENT_TOGGLE)
|
||||||
|
|
||||||
|
if (toggleEvent.defaultPrevented) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this._execute()
|
||||||
|
|
||||||
|
EventHandler.trigger(this._element, EVENT_TOGGLED)
|
||||||
|
}
|
||||||
|
|
||||||
|
_execute() {
|
||||||
|
const { attribute, target, value } = this._config
|
||||||
|
|
||||||
|
if (attribute === 'id') {
|
||||||
|
return // You have to be kidding
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attribute === 'class') {
|
||||||
|
target.classList.toggle(value)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target.getAttribute(attribute) === value) {
|
||||||
|
target.removeAttribute(attribute)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
target.setAttribute(attribute, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data API implementation
|
||||||
|
*/
|
||||||
|
eventActionOnPlugin(Toggler, 'click', SELECTOR_DATA_TOGGLE, 'toggle')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jQuery
|
||||||
|
*/
|
||||||
|
|
||||||
|
defineJQueryPlugin(Toggler)
|
||||||
|
|
||||||
|
export default Toggler
|
||||||
@@ -7,12 +7,15 @@ toc: true
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
<hr>
|
||||||
|
<button class="btn btn-outline-dark" data-bs-toggle="toggler" data-bs-target="#anotherTarget"> Toggle next element class</button>
|
||||||
<div class="alert alert-primary" data-bs-act="click:alert:close">Working example. Click on it</div>
|
<div id="anotherTarget" class="alert alert-secondary" data-bs-value="alert-danger" data-bs-attribute="class">Toggler: Toggle Class, using above button</div>
|
||||||
<div class="alert alert-danger" data-bs-act="click:kalert:close">NOT working example. Click on it</div>
|
<div class="alert alert-primary" data-bs-toggle="toggler" data-bs-value="alert-warning" data-bs-attribute="class">Toggler: Toggles Class</div>
|
||||||
|
<div class="alert alert-primary" data-bs-toggle="toggler" data-bs-value="test" data-bs-attribute="data-tost">Toggler: Toggles attribute</div>
|
||||||
|
<div class="alert alert-secondary" data-bs-act="click:alert:close"> Remover: Working example. Click on it and it will be removed</div>
|
||||||
|
<div class="alert alert-danger" data-bs-act="click:kalert:close">Remover: NOT working example. Click on it</div>
|
||||||
|
<hr>
|
||||||
Alerts are available for any length of text, as well as an optional close button. For proper styling, use one of the eight **required** contextual classes (e.g., `.alert-success`). For inline dismissal, use the [alerts JavaScript plugin](#dismissing).
|
Alerts are available for any length of text, as well as an optional close button. For proper styling, use one of the eight **required** contextual classes (e.g., `.alert-success`). For inline dismissal, use the [alerts JavaScript plugin](#dismissing).
|
||||||
|
|
||||||
{{< example >}}
|
{{< example >}}
|
||||||
{{< alerts.inline >}}
|
{{< alerts.inline >}}
|
||||||
{{- range (index $.Site.Data "theme-colors") }}
|
{{- range (index $.Site.Data "theme-colors") }}
|
||||||
|
|||||||
Reference in New Issue
Block a user