1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-06 21:56:42 +02:00

Add static backdrop to offcanvas (#35832)

* Add static backdrop option,  to offcanvas
* Trigger prevented event on esc with keyboard=false
* Change offcanvas doc , moving backdrop examples to examples section
This commit is contained in:
Jann Westermann
2022-03-02 01:20:37 +01:00
committed by GitHub
parent d788d2efac
commit 8d7358f231
3 changed files with 146 additions and 54 deletions

View File

@@ -39,6 +39,7 @@ const OPEN_SELECTOR = '.offcanvas.show'
const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const EVENT_HIDE = `hide${EVENT_KEY}`
const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
@@ -52,7 +53,7 @@ const Default = {
}
const DefaultType = {
backdrop: 'boolean',
backdrop: '(boolean|string)',
keyboard: 'boolean',
scroll: 'boolean'
}
@@ -164,12 +165,24 @@ class Offcanvas extends BaseComponent {
// Private
_initializeBackDrop() {
const clickCallback = () => {
if (this._config.backdrop === 'static') {
EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
return
}
this.hide()
}
// 'static' option will be translated to true, and booleans will keep their value
const isVisible = Boolean(this._config.backdrop)
return new Backdrop({
className: CLASS_NAME_BACKDROP,
isVisible: this._config.backdrop,
isVisible,
isAnimated: true,
rootElement: this._element.parentNode,
clickCallback: () => this.hide()
clickCallback: isVisible ? clickCallback : null
})
}
@@ -181,9 +194,16 @@ class Offcanvas extends BaseComponent {
_addEventListeners() {
EventHandler.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
if (this._config.keyboard && event.key === ESCAPE_KEY) {
this.hide()
if (event.key !== ESCAPE_KEY) {
return
}
if (!this._config.keyboard) {
EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
return
}
this.hide()
})
}