2021-03-02 19:10:10 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2022-10-03 10:44:02 +03:00
|
|
|
* Bootstrap (v5.2.2): offcanvas.js
|
2021-09-08 16:58:50 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2021-03-02 19:10:10 +02:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {
|
|
|
|
defineJQueryPlugin,
|
|
|
|
getElementFromSelector,
|
2021-03-16 18:35:03 +02:00
|
|
|
isDisabled,
|
2021-12-10 18:18:18 +02:00
|
|
|
isVisible
|
2022-10-26 08:26:51 +03:00
|
|
|
} from './util/index.js'
|
|
|
|
import ScrollBarHelper from './util/scrollbar.js'
|
|
|
|
import EventHandler from './dom/event-handler.js'
|
|
|
|
import BaseComponent from './base-component.js'
|
|
|
|
import SelectorEngine from './dom/selector-engine.js'
|
|
|
|
import Backdrop from './util/backdrop.js'
|
|
|
|
import FocusTrap from './util/focustrap.js'
|
|
|
|
import { enableDismissTrigger } from './util/component-functions.js'
|
2021-03-02 19:10:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
|
|
|
|
const NAME = 'offcanvas'
|
|
|
|
const DATA_KEY = 'bs.offcanvas'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2021-03-23 08:22:59 +02:00
|
|
|
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
|
2021-03-02 19:10:10 +02:00
|
|
|
const ESCAPE_KEY = 'Escape'
|
2021-03-16 18:35:03 +02:00
|
|
|
|
2021-03-02 19:10:10 +02:00
|
|
|
const CLASS_NAME_SHOW = 'show'
|
2022-01-05 17:20:15 +00:00
|
|
|
const CLASS_NAME_SHOWING = 'showing'
|
|
|
|
const CLASS_NAME_HIDING = 'hiding'
|
2021-06-25 13:41:15 -07:00
|
|
|
const CLASS_NAME_BACKDROP = 'offcanvas-backdrop'
|
2021-03-23 08:22:59 +02:00
|
|
|
const OPEN_SELECTOR = '.offcanvas.show'
|
2021-03-02 19:10:10 +02:00
|
|
|
|
|
|
|
const EVENT_SHOW = `show${EVENT_KEY}`
|
|
|
|
const EVENT_SHOWN = `shown${EVENT_KEY}`
|
|
|
|
const EVENT_HIDE = `hide${EVENT_KEY}`
|
2022-03-02 01:20:37 +01:00
|
|
|
const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`
|
2021-03-02 19:10:10 +02:00
|
|
|
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
v5.2.0 design refresh, plus responsive offcanvas classes (#35736)
* Add responsive offcanvas classes
- Updates navbar-expand classes to de-dupe some styles—these shouldn't interfere now.
- Adds some JS to the offcanvas component to help with responsiveness
Co-Authored-By: GeoSot <geo.sotis@gmail.com>
* Redesign homepage, docs, and examples
Homepage:
- New Bootstrap purple navbar
- Redesigned masthead
- Rewrote and redesigned homepage content
- Replace Copy text with icons like Bootstrap Icons site across all ClipboardJS instances
- Fixed padding issues in site footer
- Match homepage button styles to examples page, use gap instead of tons of responsive margin utils
Docs:
- New navbar, no more subnav. Migrated search and version picker into the main navbar and refreshed the design of it all, including the responsive toggles.
- New sidebar navigation is always expanded, and now features Bootstrap Icons alongside section headings
- Sidebar navigation autoscrolls to active link for better usability
- Subnav and navbar padding issues ironed out
- Enhanced the version picker in anticipation of v5.2: we can now link right to the same page in the previous version.
- Redesign callouts to add more color to our pages
- Collapse table of contents on mobile
- Cleanup and redesign button styles with CSS variables
- Update design for subnav version dropdown
- Update highlight and example to be full-width until md
- Improve the Added In badges
- Turn the ToC into a well on mobile
- Redesign code snippets to better house two action buttons
Examples:
- Redesign Examples page layout
- Add new example for responsive offcanvases in navbars
* Convert offcanvas to CSS vars
* Feat: add resize handler to Offcanvas.js.
If we could use as default the `.offcanvas` class without modifiers, we then, could add a simplified selector
The selector itself, ignores the .offcanvas class as it doesn't have any responsive behavior
The `aria-modal` addon is to protect us, selection backdrop elements
* Separate examples code, Add some selectors, fix stackblitz btn
Co-authored-by: GeoSot <geo.sotis@gmail.com>
2022-04-17 22:17:50 -07:00
|
|
|
const EVENT_RESIZE = `resize${EVENT_KEY}`
|
2021-03-02 19:10:10 +02:00
|
|
|
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
2021-04-19 08:20:25 +03:00
|
|
|
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
|
2021-03-02 19:10:10 +02:00
|
|
|
|
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="offcanvas"]'
|
|
|
|
|
2021-10-13 15:19:28 +03:00
|
|
|
const Default = {
|
|
|
|
backdrop: true,
|
|
|
|
keyboard: true,
|
|
|
|
scroll: false
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultType = {
|
2022-03-02 01:20:37 +01:00
|
|
|
backdrop: '(boolean|string)',
|
2021-10-13 15:19:28 +03:00
|
|
|
keyboard: 'boolean',
|
|
|
|
scroll: 'boolean'
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:10:10 +02:00
|
|
|
/**
|
2021-10-13 15:19:28 +03:00
|
|
|
* Class definition
|
2021-03-02 19:10:10 +02:00
|
|
|
*/
|
|
|
|
|
2021-03-16 10:51:04 +05:30
|
|
|
class Offcanvas extends BaseComponent {
|
2021-03-16 18:35:03 +02:00
|
|
|
constructor(element, config) {
|
2021-12-10 18:18:18 +02:00
|
|
|
super(element, config)
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-03-23 08:22:59 +02:00
|
|
|
this._isShown = false
|
2021-04-19 08:20:25 +03:00
|
|
|
this._backdrop = this._initializeBackDrop()
|
2021-07-27 01:01:04 -04:00
|
|
|
this._focustrap = this._initializeFocusTrap()
|
2021-03-02 19:10:10 +02:00
|
|
|
this._addEventListeners()
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:35:03 +02:00
|
|
|
// Getters
|
2021-05-11 10:49:30 +03:00
|
|
|
static get Default() {
|
|
|
|
return Default
|
2021-03-16 18:35:03 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 18:18:18 +02:00
|
|
|
static get DefaultType() {
|
|
|
|
return DefaultType
|
|
|
|
}
|
|
|
|
|
|
|
|
static get NAME() {
|
|
|
|
return NAME
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:10:10 +02:00
|
|
|
// Public
|
|
|
|
toggle(relatedTarget) {
|
|
|
|
return this._isShown ? this.hide() : this.show(relatedTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
show(relatedTarget) {
|
|
|
|
if (this._isShown) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, { relatedTarget })
|
|
|
|
|
|
|
|
if (showEvent.defaultPrevented) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this._isShown = true
|
2021-04-19 08:20:25 +03:00
|
|
|
this._backdrop.show()
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-03-16 18:35:03 +02:00
|
|
|
if (!this._config.scroll) {
|
2021-06-06 09:26:36 +03:00
|
|
|
new ScrollBarHelper().hide()
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this._element.setAttribute('aria-modal', true)
|
|
|
|
this._element.setAttribute('role', 'dialog')
|
2022-01-05 17:20:15 +00:00
|
|
|
this._element.classList.add(CLASS_NAME_SHOWING)
|
2021-03-02 19:10:10 +02:00
|
|
|
|
|
|
|
const completeCallBack = () => {
|
2022-07-14 12:06:06 +03:00
|
|
|
if (!this._config.scroll || this._config.backdrop) {
|
2021-07-27 01:01:04 -04:00
|
|
|
this._focustrap.activate()
|
|
|
|
}
|
|
|
|
|
2022-01-05 17:20:15 +00:00
|
|
|
this._element.classList.add(CLASS_NAME_SHOW)
|
|
|
|
this._element.classList.remove(CLASS_NAME_SHOWING)
|
2021-03-02 19:10:10 +02:00
|
|
|
EventHandler.trigger(this._element, EVENT_SHOWN, { relatedTarget })
|
|
|
|
}
|
|
|
|
|
2021-04-11 02:27:18 +03:00
|
|
|
this._queueCallback(completeCallBack, this._element, true)
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
|
|
|
if (!this._isShown) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE)
|
|
|
|
|
|
|
|
if (hideEvent.defaultPrevented) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-27 01:01:04 -04:00
|
|
|
this._focustrap.deactivate()
|
2021-03-02 19:10:10 +02:00
|
|
|
this._element.blur()
|
|
|
|
this._isShown = false
|
2022-01-05 17:20:15 +00:00
|
|
|
this._element.classList.add(CLASS_NAME_HIDING)
|
2021-04-19 08:20:25 +03:00
|
|
|
this._backdrop.hide()
|
2021-03-02 19:10:10 +02:00
|
|
|
|
|
|
|
const completeCallback = () => {
|
2022-01-05 17:20:15 +00:00
|
|
|
this._element.classList.remove(CLASS_NAME_SHOW, CLASS_NAME_HIDING)
|
2021-03-02 19:10:10 +02:00
|
|
|
this._element.removeAttribute('aria-modal')
|
|
|
|
this._element.removeAttribute('role')
|
|
|
|
|
2021-03-16 18:35:03 +02:00
|
|
|
if (!this._config.scroll) {
|
2021-06-06 09:26:36 +03:00
|
|
|
new ScrollBarHelper().reset()
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EventHandler.trigger(this._element, EVENT_HIDDEN)
|
|
|
|
}
|
|
|
|
|
2021-04-11 02:27:18 +03:00
|
|
|
this._queueCallback(completeCallback, this._element, true)
|
2021-04-19 08:20:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this._backdrop.dispose()
|
2021-07-27 01:01:04 -04:00
|
|
|
this._focustrap.deactivate()
|
2021-04-19 08:20:25 +03:00
|
|
|
super.dispose()
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:35:03 +02:00
|
|
|
// Private
|
2021-04-19 08:20:25 +03:00
|
|
|
_initializeBackDrop() {
|
2022-03-02 01:20:37 +01:00
|
|
|
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)
|
|
|
|
|
2021-04-19 08:20:25 +03:00
|
|
|
return new Backdrop({
|
2021-06-25 13:41:15 -07:00
|
|
|
className: CLASS_NAME_BACKDROP,
|
2022-03-02 01:20:37 +01:00
|
|
|
isVisible,
|
2021-04-19 08:20:25 +03:00
|
|
|
isAnimated: true,
|
|
|
|
rootElement: this._element.parentNode,
|
2022-03-02 01:20:37 +01:00
|
|
|
clickCallback: isVisible ? clickCallback : null
|
2021-04-19 08:20:25 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-27 01:01:04 -04:00
|
|
|
_initializeFocusTrap() {
|
|
|
|
return new FocusTrap({
|
|
|
|
trapElement: this._element
|
2021-03-02 19:10:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_addEventListeners() {
|
2021-04-19 08:20:25 +03:00
|
|
|
EventHandler.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
|
2022-03-02 01:20:37 +01:00
|
|
|
if (event.key !== ESCAPE_KEY) {
|
|
|
|
return
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
2022-03-02 01:20:37 +01:00
|
|
|
|
|
|
|
if (!this._config.keyboard) {
|
|
|
|
EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hide()
|
2021-03-02 19:10:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static
|
|
|
|
static jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
2021-06-03 18:53:27 +03:00
|
|
|
const data = Offcanvas.getOrCreateInstance(this, config)
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-03-16 18:35:03 +02:00
|
|
|
if (typeof config !== 'string') {
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-03-16 18:35:03 +02:00
|
|
|
if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
|
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
2021-03-16 18:35:03 +02:00
|
|
|
|
|
|
|
data[config](this)
|
2021-03-02 19:10:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-13 15:19:28 +03:00
|
|
|
* Data API implementation
|
2021-03-02 19:10:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
|
|
|
const target = getElementFromSelector(this)
|
|
|
|
|
|
|
|
if (['A', 'AREA'].includes(this.tagName)) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:35:03 +02:00
|
|
|
if (isDisabled(this)) {
|
2021-03-02 19:10:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
EventHandler.one(target, EVENT_HIDDEN, () => {
|
|
|
|
// focus on trigger when it is closed
|
|
|
|
if (isVisible(this)) {
|
|
|
|
this.focus()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// avoid conflict when clicking a toggler of an offcanvas, while another is open
|
2021-10-13 15:19:28 +03:00
|
|
|
const alreadyOpen = SelectorEngine.findOne(OPEN_SELECTOR)
|
|
|
|
if (alreadyOpen && alreadyOpen !== target) {
|
|
|
|
Offcanvas.getInstance(alreadyOpen).hide()
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
|
|
|
|
2021-06-03 18:53:27 +03:00
|
|
|
const data = Offcanvas.getOrCreateInstance(target)
|
2021-03-02 19:10:10 +02:00
|
|
|
data.toggle(this)
|
|
|
|
})
|
|
|
|
|
2021-07-30 09:28:51 +03:00
|
|
|
EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
|
2021-10-29 10:38:35 +03:00
|
|
|
for (const selector of SelectorEngine.find(OPEN_SELECTOR)) {
|
|
|
|
Offcanvas.getOrCreateInstance(selector).show()
|
2021-07-30 09:28:51 +03:00
|
|
|
}
|
|
|
|
})
|
2021-03-23 08:22:59 +02:00
|
|
|
|
v5.2.0 design refresh, plus responsive offcanvas classes (#35736)
* Add responsive offcanvas classes
- Updates navbar-expand classes to de-dupe some styles—these shouldn't interfere now.
- Adds some JS to the offcanvas component to help with responsiveness
Co-Authored-By: GeoSot <geo.sotis@gmail.com>
* Redesign homepage, docs, and examples
Homepage:
- New Bootstrap purple navbar
- Redesigned masthead
- Rewrote and redesigned homepage content
- Replace Copy text with icons like Bootstrap Icons site across all ClipboardJS instances
- Fixed padding issues in site footer
- Match homepage button styles to examples page, use gap instead of tons of responsive margin utils
Docs:
- New navbar, no more subnav. Migrated search and version picker into the main navbar and refreshed the design of it all, including the responsive toggles.
- New sidebar navigation is always expanded, and now features Bootstrap Icons alongside section headings
- Sidebar navigation autoscrolls to active link for better usability
- Subnav and navbar padding issues ironed out
- Enhanced the version picker in anticipation of v5.2: we can now link right to the same page in the previous version.
- Redesign callouts to add more color to our pages
- Collapse table of contents on mobile
- Cleanup and redesign button styles with CSS variables
- Update design for subnav version dropdown
- Update highlight and example to be full-width until md
- Improve the Added In badges
- Turn the ToC into a well on mobile
- Redesign code snippets to better house two action buttons
Examples:
- Redesign Examples page layout
- Add new example for responsive offcanvases in navbars
* Convert offcanvas to CSS vars
* Feat: add resize handler to Offcanvas.js.
If we could use as default the `.offcanvas` class without modifiers, we then, could add a simplified selector
The selector itself, ignores the .offcanvas class as it doesn't have any responsive behavior
The `aria-modal` addon is to protect us, selection backdrop elements
* Separate examples code, Add some selectors, fix stackblitz btn
Co-authored-by: GeoSot <geo.sotis@gmail.com>
2022-04-17 22:17:50 -07:00
|
|
|
EventHandler.on(window, EVENT_RESIZE, () => {
|
|
|
|
for (const element of SelectorEngine.find('[aria-modal][class*=show][class*=offcanvas-]')) {
|
|
|
|
if (getComputedStyle(element).position !== 'fixed') {
|
|
|
|
Offcanvas.getOrCreateInstance(element).hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-28 17:39:32 +03:00
|
|
|
enableDismissTrigger(Offcanvas)
|
2021-10-13 15:19:28 +03:00
|
|
|
|
2021-03-02 19:10:10 +02:00
|
|
|
/**
|
|
|
|
* jQuery
|
|
|
|
*/
|
|
|
|
|
2021-05-11 10:49:30 +03:00
|
|
|
defineJQueryPlugin(Offcanvas)
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-03-16 10:51:04 +05:30
|
|
|
export default Offcanvas
|