feat(collapse): use native details for disclosure Use a details element with a summary to open and close Collapse panels. Keep external controls, groups, hash links, and the JavaScript API. Animate horizontal panels with a grid track. Keep the content visible until the close transition ends. Update examples and tests for the new markup.
diff --git a/js/src/collapse.ts b/js/src/collapse.ts index f0bb1a6..5d313a8 100644 --- a/js/src/collapse.ts +++ b/js/src/collapse.ts
@@ -31,12 +31,36 @@ const EVENT_HIDDEN = `hidden${EVENT_KEY}` const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}` -const CLASS_NAME_SHOW = 'show' -const CLASS_NAME_COLLAPSE = 'collapse' -const CLASS_NAME_DEEPER_CHILDREN = `:scope .${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}` - -const SELECTOR_ACTIVES = '.collapse.show' +const CLASS_NAME_HORIZONTAL = 'collapse-horizontal' +const CLASS_NAME_CLOSING = 'collapse-closing' +const SELECTOR_COLLAPSE = 'details.collapse' +const SELECTOR_ACTIVES = `${SELECTOR_COLLAPSE}[open]:not(.${CLASS_NAME_CLOSING})` +const SELECTOR_DEEPER_CHILDREN = `:scope ${SELECTOR_COLLAPSE} ${SELECTOR_COLLAPSE}` const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]' +const SELECTOR_SUMMARY_CONTROL = `a[href], button, input, select, textarea, label, audio[controls], video[controls], [contenteditable]:not([contenteditable="false"]), ${SELECTOR_DATA_TOGGLE}` +const DETAILS_CONTENT = '::details-content' + +const isCollapse = (element: Element | null): element is HTMLDetailsElement => + element?.matches(SELECTOR_COLLAPSE) ?? false + +const isShown = (element: HTMLDetailsElement): boolean => + element.open && !element.classList.contains(CLASS_NAME_CLOSING) + +const getCollapseTargets = (trigger: Element): HTMLDetailsElement[] => { + try { + return SelectorEngine.getMultipleElementsFromSelector(trigger).filter(isCollapse) + } catch (error) { + // A malformed selector must not block other Collapse controls. + if (error instanceof DOMException && error.name === 'SyntaxError') { + return [] + } + + throw error + } +} + +const getCollapseTriggers = (element: HTMLDetailsElement): HTMLElement[] => + SelectorEngine.find(SELECTOR_DATA_TOGGLE).filter(trigger => getCollapseTargets(trigger).includes(element)) type CollapseConfig = { parent: string | Element | null @@ -55,32 +79,26 @@ */ class Collapse extends BaseComponent { + protected declare _element: HTMLDetailsElement protected declare _config: CollapseConfig protected declare _isTransitioning: boolean protected declare _triggerArray: HTMLElement[] constructor(element?: string | Element | null, config?: Partial<CollapseConfig> | null) { - super(element, config) + const target = getElement(element) + if (!isCollapse(target)) { + throw new TypeError('COLLAPSE: Expected a <details class="collapse"> element.') + } + + super(target, config) this._isTransitioning = false - this._triggerArray = [] - - const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE) - - for (const elem of toggleList) { - const selector = SelectorEngine.getSelectorFromElement(elem) - const filterElement = SelectorEngine.find(selector!) - .filter(foundElement => foundElement === this._element) - - if (selector !== null && filterElement.length) { - this._triggerArray.push(elem) - } - } + this._triggerArray = getCollapseTriggers(this._element) this._initializeChildren() if (!this._config.parent) { - this._setAriaExpanded(this._triggerArray, this._isShown()) + this._syncTriggers() } } @@ -98,12 +116,17 @@ } // Public + override dispose(): void { + this._finishClose() + super.dispose() + } + toggle(): Promise<void> { - return this._isShown() ? this.hide() : this.show() + return this._element.open ? this.hide() : this.show() } async show(): Promise<void> { - if (this._isTransitioning || this._isShown()) { + if (this._isTransitioning || this._element.open) { return } @@ -111,7 +134,7 @@ // find active children if (this._config.parent) { - activeChildren = this._getFirstLevelChildren(SELECTOR_ACTIVES) + activeChildren = this._getFirstLevelChildren<HTMLDetailsElement>(SELECTOR_ACTIVES) .filter(element => element !== this._element && !this._sharesTrigger(element)) .map(element => Collapse.getOrCreateInstance(element)) } @@ -121,7 +144,7 @@ } const startEvent = EventHandler.trigger(this._element, EVENT_SHOW) - if (startEvent.defaultPrevented) { + if (startEvent.defaultPrevented || !this._element) { return } @@ -129,67 +152,66 @@ activeInstance.hide() } - // The CSS owns the animation: .show is the only state, and the browser - // interpolates the size from 0 to the shown size on its own. - this._element.classList.add(CLASS_NAME_SHOW) - - this._setAriaExpanded(this._triggerArray, true) - this._isTransitioning = true - - const complete = () => { - this._isTransitioning = false - EventHandler.trigger(this._element, EVENT_SHOWN) - } - - await this._queueCallback(complete, this._element, this._isAnimated()) + await this._setShown(true) } async hide(): Promise<void> { - if (this._isTransitioning || !this._isShown()) { + if (this._isTransitioning || !this._element.open) { return } const startEvent = EventHandler.trigger(this._element, EVENT_HIDE) - if (startEvent.defaultPrevented) { + if (startEvent.defaultPrevented || !this._element) { return } - this._element.classList.remove(CLASS_NAME_SHOW) - - for (const trigger of this._triggerArray) { - const element = SelectorEngine.getElementFromSelector(trigger) - - if (element && !this._isShown(element)) { - this._setAriaExpanded([trigger], false) - } - } - - this._isTransitioning = true - - const complete = () => { - this._isTransitioning = false - EventHandler.trigger(this._element, EVENT_HIDDEN) - } - - await this._queueCallback(complete, this._element, this._isAnimated()) + await this._setShown(false) } // Private - protected _isShown(element: HTMLElement = this._element): boolean { - return element.classList.contains(CLASS_NAME_SHOW) + protected async _setShown(open: boolean): Promise<void> { + const element = this._element + const horizontalClose = !open && element.classList.contains(CLASS_NAME_HORIZONTAL) + + // Keep the native content visible while its width shrinks. + if (horizontalClose) { + element.classList.add(CLASS_NAME_CLOSING) + } else { + element.open = open + } + + this._isTransitioning = true + this._syncTriggers() + // Browsers can animate ::details-content without exposing its transition events. + const duration = getTransitionDurationFromElement(element, DETAILS_CONTENT) + if (duration > 0) { + // Allow one frame for the browser to start the transition. + await new Promise(resolve => { + setTimeout(resolve, duration + 20) + }) + } + + if (this._element) { + this._finishClose() + this._isTransitioning = false + if (element.open === open) { + EventHandler.trigger(element, open ? EVENT_SHOWN : EVENT_HIDDEN) + } + } } - // The collapse declares its transition in CSS, so a zero computed duration - // (reduced motion, .transition-none, or transitions disabled) means no wait. - protected _isAnimated(): boolean { - return getTransitionDurationFromElement(this._element) > 0 + protected _finishClose(): void { + if (this._element.classList.contains(CLASS_NAME_CLOSING)) { + this._element.open = false + this._element.classList.remove(CLASS_NAME_CLOSING) + } } // One trigger can target more than one collapse. Those collapses open // together, so a shared parent must not close them against each other. - protected _sharesTrigger(element: HTMLElement): boolean { + protected _sharesTrigger(element: HTMLDetailsElement): boolean { return this._triggerArray.some( - trigger => SelectorEngine.getMultipleElementsFromSelector(trigger).includes(element) + trigger => getCollapseTargets(trigger).includes(element) ) } @@ -203,28 +225,25 @@ return } - const children = this._getFirstLevelChildren(SELECTOR_DATA_TOGGLE) + this._syncTriggers(this._getFirstLevelChildren(SELECTOR_DATA_TOGGLE)) + } - for (const element of children) { - const selected = SelectorEngine.getElementFromSelector(element) + protected _getFirstLevelChildren<T extends HTMLElement = HTMLElement>(selector: string): T[] { + const children = SelectorEngine.find(SELECTOR_DEEPER_CHILDREN, this._config.parent as HTMLElement) + // remove children if greater depth + return SelectorEngine.find<T>(selector, this._config.parent as HTMLElement).filter(element => !children.includes(element)) + } - if (selected) { - this._setAriaExpanded([element], this._isShown(selected)) + protected _syncTriggers(triggers = this._triggerArray): void { + for (const trigger of triggers) { + const targets = getCollapseTargets(trigger) + if (targets.length) { + this._setAriaExpanded([trigger], targets.some(isShown)) } } } - protected _getFirstLevelChildren(selector: string): HTMLElement[] { - const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent as HTMLElement) - // remove children if greater depth - return SelectorEngine.find(selector, this._config.parent as HTMLElement).filter(element => !children.includes(element)) - } - protected _setAriaExpanded(triggerArray: HTMLElement[], isOpen: boolean): void { - if (!triggerArray.length) { - return - } - for (const element of triggerArray) { setAriaAttribute(element, 'aria-expanded', isOpen) } @@ -241,39 +260,72 @@ event.preventDefault() } - for (const element of SelectorEngine.getMultipleElementsFromSelector(this)) { + for (const element of getCollapseTargets(this)) { + if (this === SelectorEngine.findOne(':scope > summary', element)) { + event.preventDefault() + } + Collapse.getOrCreateInstance(element).toggle() } }) -/** - * Find a trigger that targets this collapse. - */ -const getCollapseTrigger = (collapseEl: HTMLElement): HTMLElement | null => { - for (const trigger of SelectorEngine.find(SELECTOR_DATA_TOGGLE)) { - if (SelectorEngine.getMultipleElementsFromSelector(trigger).includes(collapseEl)) { - return trigger - } +EventHandler.on(document, EVENT_CLICK_DATA_API, event => { + if (event.defaultPrevented || !(event.target instanceof Element)) { + return } - return null -} + const summary = SelectorEngine.closest(event.target, 'summary') + const details = summary?.parentElement ?? null + if (!summary || !isCollapse(details) || summary !== SelectorEngine.findOne(':scope > summary', details)) { + return + } + + const control = SelectorEngine.closest(event.target, SELECTOR_SUMMARY_CONTROL) + if (control && control !== summary && summary.contains(control)) { + return + } + + event.preventDefault() + Collapse.getOrCreateInstance(details).toggle() +}) + +// Native grouping and direct changes to `open` also update external controls. +document.addEventListener('toggle', event => { + if (event.target instanceof HTMLDetailsElement && isCollapse(event.target)) { + for (const trigger of SelectorEngine.find(SELECTOR_DATA_TOGGLE)) { + const targets = getCollapseTargets(trigger) + if (targets.includes(event.target)) { + setAriaAttribute(trigger, 'aria-expanded', targets.some(isShown)) + } + } + } +}, true) /** * Open opted-in collapses when the URL fragment matches their id. * Put `data-bs-hash` on the collapsible target. */ enableHashTarget(`${EVENT_KEY}${DATA_API_KEY}`, { - matches: element => element.classList.contains(CLASS_NAME_COLLAPSE), - getAnchor: getCollapseTrigger, + matches: isCollapse, + getAnchor(element) { + if (!isCollapse(element)) { + return null + } + + return SelectorEngine.find(SELECTOR_DATA_TOGGLE).find(trigger => getCollapseTargets(trigger).includes(element)) ?? null + }, async open(element, done) { - if (element.classList.contains(CLASS_NAME_SHOW)) { + if (!isCollapse(element)) { + return + } + + if (isShown(element)) { done() return } await Collapse.getOrCreateInstance(element).show() - if (element.classList.contains(CLASS_NAME_SHOW)) { + if (isShown(element)) { done() } }
diff --git a/js/src/util/index.ts b/js/src/util/index.ts index 8232611..39a2e64 100644 --- a/js/src/util/index.ts +++ b/js/src/util/index.ts
@@ -48,13 +48,13 @@ return prefix } -const getTransitionDurationFromElement = (element: Element | null): number => { +const getTransitionDurationFromElement = (element: Element | null, pseudoElement?: string): number => { if (!element) { return 0 } // Get transition-duration of the element - let { transitionDuration, transitionDelay } = window.getComputedStyle(element) + let { transitionDuration, transitionDelay } = window.getComputedStyle(element, pseudoElement) const floatTransitionDuration = Number.parseFloat(transitionDuration) const floatTransitionDelay = Number.parseFloat(transitionDelay)
diff --git a/js/tests/types/api.ts b/js/tests/types/api.ts index 74459e5..149fbd3 100644 --- a/js/tests/types/api.ts +++ b/js/tests/types/api.ts
@@ -51,7 +51,7 @@ const tooltipFromSelector: Tooltip = new Tooltip('#tip', { animation: false }) const tooltipFromElement: Tooltip = new Tooltip(element) const toast: Toast = new Toast(element, { autohide: false, delay: 5000 }) -const collapse: Collapse = new Collapse(element, { parent: '#accordion' }) +const collapse: Collapse = new Collapse('details.collapse', { parent: '#accordion' }) const carousel: Carousel = new Carousel(element, { interval: 2000 }) const dialog: Dialog = new Dialog(element, { modal: true, keyboard: false }) const drawer: Drawer = new Drawer(element, { scroll: true })
diff --git a/js/tests/types/consumer.ts b/js/tests/types/consumer.ts index eb3df76..df01e23 100644 --- a/js/tests/types/consumer.ts +++ b/js/tests/types/consumer.ts
@@ -58,7 +58,7 @@ const tooltipFromSelector: Tooltip = new Tooltip('#tip', { animation: false }) const tooltipFromElement: Tooltip = new Tooltip(element) const toast: Toast = new Toast(element, { autohide: false, delay: 5000 }) -const collapse: Collapse = new Collapse(element, { parent: '#accordion' }) +const collapse: Collapse = new Collapse('details.collapse', { parent: '#accordion' }) const carousel: Carousel = new Carousel(element, { interval: 2000 }) const dialog: Dialog = new Dialog(element, { modal: true, keyboard: false }) const drawer: Drawer = new Drawer(element, { scroll: true })
diff --git a/js/tests/unit/collapse.spec.js b/js/tests/unit/collapse.spec.js index 85edc6f..b97035f 100644 --- a/js/tests/unit/collapse.spec.js +++ b/js/tests/unit/collapse.spec.js
@@ -43,11 +43,19 @@ }) describe('constructor', () => { - it('should take care of element either passed as a CSS selector or DOM element', () => { - fixtureEl.innerHTML = '<div class="my-collapse"></div>' + it('should reject elements that are not Collapse disclosures', () => { + const panel = document.createElement('div') + panel.className = 'collapse' - const collapseEl = fixtureEl.querySelector('div.my-collapse') - const collapseBySelector = new Collapse('div.my-collapse') + expect(() => new Collapse(panel)).toThrow(TypeError) + expect(Collapse.getInstance(panel)).toBeNull() + }) + + it('should take care of element either passed as a CSS selector or DOM element', () => { + fixtureEl.innerHTML = '<details class="collapse my-collapse"><summary>Toggle</summary></details>' + + const collapseEl = fixtureEl.querySelector('details.my-collapse') + const collapseBySelector = new Collapse('details.my-collapse') expect(collapseBySelector._element).toEqual(collapseEl) const collapseByElement = new Collapse(collapseEl) @@ -59,12 +67,12 @@ '<div class="my-collapse">', ' <div class="item">', ' <a data-bs-toggle="collapse" href="#">Toggle item</a>', - ' <div class="collapse">Lorem ipsum</div>', + ' <details class="collapse"><summary>Toggle</summary>Lorem ipsum</details>', ' </div>', '</div>' ].join('') - const collapseEl = fixtureEl.querySelector('div.collapse') + const collapseEl = fixtureEl.querySelector('details.collapse') const myCollapseEl = fixtureEl.querySelector('.my-collapse') const collapse = new Collapse(collapseEl, { parent: myCollapseEl @@ -78,12 +86,12 @@ '<div class="my-collapse">', ' <div class="item">', ' <a data-bs-toggle="collapse" href="#">Toggle item</a>', - ' <div class="collapse">Lorem ipsum</div>', + ' <details class="collapse"><summary>Toggle</summary>Lorem ipsum</details>', ' </div>', '</div>' ].join('') - const collapseEl = fixtureEl.querySelector('div.collapse') + const collapseEl = fixtureEl.querySelector('details.collapse') const myCollapseEl = fixtureEl.querySelector('.my-collapse') const collapse = new Collapse(collapseEl, { parent: 'div.my-collapse' @@ -94,10 +102,10 @@ }) describe('toggle', () => { - it('should call show method if show class is not present', () => { - fixtureEl.innerHTML = '<div></div>' + it('should call show method when the details is closed', () => { + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) const spy = spyOn(collapse, 'show') @@ -107,10 +115,10 @@ expect(spy).toHaveBeenCalled() }) - it('should call hide method if show class is present', () => { - fixtureEl.innerHTML = '<div class="show"></div>' + it('should call hide method when the details is open', () => { + fixtureEl.innerHTML = '<details class="collapse" open><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('.show') + const collapseEl = fixtureEl.querySelector('[open]') const collapse = new Collapse(collapseEl) const spy = spyOn(collapse, 'hide') @@ -126,11 +134,11 @@ '<div class="my-collapse">', ' <div class="item">', ' <a data-bs-toggle="collapse" href="#">Toggle item 1</a>', - ' <div id="collapse1" class="collapse show">Lorem ipsum 1</div>', + ' <details id="collapse1" class="collapse" open><summary>Toggle</summary>Lorem ipsum 1</details>', ' </div>', ' <div class="item">', ' <a id="triggerCollapse2" data-bs-toggle="collapse" href="#">Toggle item 2</a>', - ' <div id="collapse2" class="collapse">Lorem ipsum 2</div>', + ' <details id="collapse2" class="collapse"><summary>Toggle</summary>Lorem ipsum 2</details>', ' </div>', '</div>' ].join('') @@ -145,8 +153,8 @@ })) collapseEl2.addEventListener('shown.bs.collapse', () => { - expect(collapseEl2).toHaveClass('show') - expect(collapseEl1).not.toHaveClass('show') + expect(collapseEl2.open).toBeTrue() + expect(collapseEl1.open).toBeFalse() resolve() }) @@ -157,11 +165,11 @@ describe('show', () => { it('should do nothing if is transitioning', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' const spy = spyOn(EventHandler, 'trigger') - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) collapse._isTransitioning = true @@ -171,11 +179,11 @@ }) it('should do nothing if already shown', () => { - fixtureEl.innerHTML = '<div class="show"></div>' + fixtureEl.innerHTML = '<details class="collapse" open><summary>Toggle</summary></details>' const spy = spyOn(EventHandler, 'trigger') - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) collapse.show() @@ -185,9 +193,9 @@ it('should show a collapsed element', () => { return new Promise(resolve => { - fixtureEl.innerHTML = '<div class="collapse"></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) // The CSS animates the size, so the element keeps its own styles @@ -195,7 +203,7 @@ expect(collapseEl.style.height).toEqual('') }) collapseEl.addEventListener('shown.bs.collapse', () => { - expect(collapseEl).toHaveClass('show') + expect(collapseEl.open).toBeTrue() expect(collapseEl.style.height).toEqual('') resolve() }) @@ -206,16 +214,16 @@ it('should show a collapsed element on width', () => { return new Promise(resolve => { - fixtureEl.innerHTML = '<div class="collapse collapse-horizontal"></div>' + fixtureEl.innerHTML = '<details class="collapse collapse-horizontal"><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) collapseEl.addEventListener('show.bs.collapse', () => { expect(collapseEl.style.width).toEqual('') }) collapseEl.addEventListener('shown.bs.collapse', () => { - expect(collapseEl).toHaveClass('show') + expect(collapseEl.open).toBeTrue() expect(collapseEl.style.width).toEqual('') resolve() }) @@ -224,14 +232,38 @@ }) }) + it('should close a horizontal panel after the transition or on disposal', async () => { + fixtureEl.innerHTML = ` + <style>#horizontalPanel::details-content { transition-duration: .04s; }</style> + <button data-bs-toggle="collapse" data-bs-target="#horizontalPanel">Toggle</button> + <details class="collapse collapse-horizontal" id="horizontalPanel" open><summary>Toggle</summary></details>` + const trigger = fixtureEl.querySelector('button') + const panel = fixtureEl.querySelector('details') + const collapse = new Collapse(panel) + + const hiding = collapse.hide() + expect(panel.open).toBeTrue() + expect(trigger.getAttribute('aria-expanded')).toEqual('false') + await hiding + expect(panel.open).toBeFalse() + expect(panel).not.toHaveClass('collapse-closing') + + await collapse.show() + const hidingAgain = collapse.hide() + collapse.dispose() + expect(panel.open).toBeFalse() + expect(panel).not.toHaveClass('collapse-closing') + await hidingAgain + }) + it('should collapse only the first collapse', () => { return new Promise(resolve => { fixtureEl.innerHTML = [ '<div class="card" id="accordion1">', - ' <div id="collapse1" class="collapse"></div>', + ' <details id="collapse1" class="collapse"><summary>Toggle</summary></details>', '</div>', '<div class="card" id="accordion2">', - ' <div id="collapse2" class="collapse show"></div>', + ' <details id="collapse2" class="collapse" open><summary>Toggle</summary></details>', '</div>' ].join('') @@ -240,8 +272,8 @@ const collapse = new Collapse(el1) el1.addEventListener('shown.bs.collapse', () => { - expect(el1).toHaveClass('show') - expect(el2).toHaveClass('show') + expect(el1.open).toBeTrue() + expect(el2.open).toBeTrue() resolve() }) @@ -256,28 +288,28 @@ ' <div class="accordion-header">', ' <button data-bs-target="#parentContent" data-bs-toggle="collapse" class="accordion-toggle">Parent</button>', ' </div>', - ' <div id="parentContent" class="collapse" data-bs-parent="#parentGroup">', + ' <details id="parentContent" class="collapse" data-bs-parent="#parentGroup"><summary>Toggle</summary>', ' <div class="accordion-body">', ' <div id="childGroup" class="accordion">', ' <div class="accordion-item">', ' <div class="accordion-header">', ' <button data-bs-target="#childContent1" data-bs-toggle="collapse" class="accordion-toggle">Child 1</button>', ' </div>', - ' <div id="childContent1" class="collapse" data-bs-parent="#childGroup">', + ' <details id="childContent1" class="collapse" data-bs-parent="#childGroup"><summary>Toggle</summary>', ' <div>content</div>', - ' </div>', + ' </details>', ' </div>', ' <div class="accordion-item">', ' <div class="accordion-header">', ' <button data-bs-target="#childContent2" data-bs-toggle="collapse" class="accordion-toggle">Child 2</button>', ' </div>', - ' <div id="childContent2" class="collapse" data-bs-parent="#childGroup">', + ' <details id="childContent2" class="collapse" data-bs-parent="#childGroup"><summary>Toggle</summary>', ' <div>content</div>', - ' </div>', + ' </details>', ' </div>', ' </div>', ' </div>', - ' </div>', + ' </details>', '</div>' ].join('') @@ -292,16 +324,16 @@ const childCollapseEl2 = el('#childContent2') parentCollapseEl.addEventListener('shown.bs.collapse', () => { - expect(parentCollapseEl).toHaveClass('show') + expect(parentCollapseEl.open).toBeTrue() childBtn1.click() }) childCollapseEl1.addEventListener('shown.bs.collapse', () => { - expect(childCollapseEl1).toHaveClass('show') + expect(childCollapseEl1.open).toBeTrue() childBtn2.click() }) childCollapseEl2.addEventListener('shown.bs.collapse', () => { - expect(childCollapseEl2).toHaveClass('show') - expect(childCollapseEl1).not.toHaveClass('show') + expect(childCollapseEl2.open).toBeTrue() + expect(childCollapseEl1.open).toBeFalse() resolve() }) @@ -319,7 +351,7 @@ ' Accordion Item #1', ' </button>', ' </h2>', - ' <div id="collapseOne" class="collapse show" data-bs-parent="#accordionExample">', + ' <details id="collapseOne" class="collapse" open data-bs-parent="#accordionExample"><summary>Toggle</summary>', ' <div class="accordion-body">', ' <nav>', ' <div class="nav nav-tabs" id="nav-tab" role="tablist">', @@ -332,7 +364,7 @@ ' <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">Profile</div>', ' </div>', ' </div>', - ' </div>', + ' </details>', ' </div>', '</div>' ].join('') @@ -363,9 +395,9 @@ it('should not fire shown when show is prevented', () => { return new Promise((resolve, reject) => { - fixtureEl.innerHTML = '<div class="collapse"></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) const expectEnd = () => { @@ -391,11 +423,11 @@ describe('hide', () => { it('should do nothing if is transitioning', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' const spy = spyOn(EventHandler, 'trigger') - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) collapse._isTransitioning = true @@ -405,11 +437,11 @@ }) it('should do nothing if already shown', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' const spy = spyOn(EventHandler, 'trigger') - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) collapse.hide() @@ -419,13 +451,13 @@ it('should hide a collapsed element', () => { return new Promise(resolve => { - fixtureEl.innerHTML = '<div class="collapse show"></div>' + fixtureEl.innerHTML = '<details class="collapse" open><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) collapseEl.addEventListener('hidden.bs.collapse', () => { - expect(collapseEl).not.toHaveClass('show') + expect(collapseEl.open).toBeFalse() expect(collapseEl.style.height).toEqual('') resolve() }) @@ -436,9 +468,9 @@ it('should not fire hidden when hide is prevented', () => { return new Promise((resolve, reject) => { - fixtureEl.innerHTML = '<div class="collapse show"></div>' + fixtureEl.innerHTML = '<details class="collapse" open><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) const expectEnd = () => { @@ -464,9 +496,9 @@ describe('dispose', () => { it('should destroy a collapse', () => { - fixtureEl.innerHTML = '<div class="collapse show"></div>' + fixtureEl.innerHTML = '<details class="collapse" open><summary>Toggle</summary></details>' - const collapseEl = fixtureEl.querySelector('div') + const collapseEl = fixtureEl.querySelector('details') const collapse = new Collapse(collapseEl) expect(Collapse.getInstance(collapseEl)).toEqual(collapse) @@ -478,13 +510,26 @@ }) describe('data-api', () => { + it('should ignore other elements matched by a Collapse trigger', () => { + fixtureEl.innerHTML = ` + <button type="button" data-bs-toggle="collapse" data-bs-target=".target">Toggle</button> + <div class="collapse target"></div> + <details class="collapse target"><summary>Toggle</summary></details>` + + const [trigger, other, panel] = fixtureEl.children + trigger.click() + + expect(Collapse.getInstance(other)).toBeNull() + expect(panel.open).toBeTrue() + }) + it('should prevent url change if click on nested elements', () => { return new Promise(resolve => { fixtureEl.innerHTML = [ '<a role="button" data-bs-toggle="collapse" href="#collapse">', ' <span id="nested"></span>', '</a>', - '<div id="collapse" class="collapse"></div>' + '<details id="collapse" class="collapse"><summary>Toggle</summary></details>' ].join('') const triggerEl = fixtureEl.querySelector('a') @@ -507,8 +552,8 @@ return new Promise(resolve => { fixtureEl.innerHTML = [ '<a role="button" data-bs-toggle="collapse" href=".multi"></a>', - '<div id="collapse1" class="collapse multi"></div>', - '<div id="collapse2" class="collapse multi"></div>' + '<details id="collapse1" class="collapse multi"><summary>Toggle</summary></details>', + '<details id="collapse2" class="collapse multi"><summary>Toggle</summary></details>' ].join('') const trigger = fixtureEl.querySelector('a') @@ -517,8 +562,8 @@ collapse2.addEventListener('shown.bs.collapse', () => { expect(trigger.getAttribute('aria-expanded')).toEqual('true') - expect(collapse1).toHaveClass('show') - expect(collapse1).toHaveClass('show') + expect(collapse1.open).toBeTrue() + expect(collapse2.open).toBeTrue() resolve() }) @@ -530,8 +575,8 @@ return new Promise(resolve => { fixtureEl.innerHTML = [ '<a role="button" data-bs-toggle="collapse" href=".multi"></a>', - '<div id="collapse1" class="collapse multi show"></div>', - '<div id="collapse2" class="collapse multi show"></div>' + '<details id="collapse1" class="collapse multi" open><summary>Toggle</summary></details>', + '<details id="collapse2" class="collapse multi" open><summary>Toggle</summary></details>' ].join('') const trigger = fixtureEl.querySelector('a') @@ -540,8 +585,8 @@ collapse2.addEventListener('hidden.bs.collapse', () => { expect(trigger.getAttribute('aria-expanded')).toEqual('false') - expect(collapse1).not.toHaveClass('show') - expect(collapse1).not.toHaveClass('show') + expect(collapse1.open).toBeFalse() + expect(collapse2.open).toBeFalse() resolve() }) @@ -554,7 +599,7 @@ fixtureEl.innerHTML = [ '<a id="link1" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>', '<a id="link2" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>', - '<div id="test1"></div>' + '<details id="test1" class="collapse"><summary>Toggle</summary></details>' ].join('') const link1 = fixtureEl.querySelector('#link1') @@ -576,7 +621,7 @@ fixtureEl.innerHTML = [ '<a id="link1" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>', '<a id="link2" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>', - '<div id="test1" class="show"></div>' + '<details id="test1" class="collapse" open><summary>Toggle</summary></details>' ].join('') const link1 = fixtureEl.querySelector('#link1') @@ -599,11 +644,11 @@ '<div id="accordion">', ' <div class="item">', ' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>', - ' <div id="collapseOne" class="collapse" role="tabpanel" data-bs-parent="#accordion"></div>', + ' <details id="collapseOne" class="collapse" data-bs-parent="#accordion"><summary>Toggle</summary></details>', ' </div>', ' <div class="item">', ' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>', - ' <div id="collapseTwo" class="collapse show" role="tabpanel" data-bs-parent="#accordion"></div>', + ' <details id="collapseTwo" class="collapse" open data-bs-parent="#accordion"><summary>Toggle</summary></details>', ' </div>', '</div>' ].join('') @@ -614,12 +659,12 @@ const collapseTwo = fixtureEl.querySelector('#collapseTwo') collapseOne.addEventListener('shown.bs.collapse', () => { - expect(collapseOne).toHaveClass('show') - expect(collapseTwo).not.toHaveClass('show') + expect(collapseOne.open).toBeTrue() + expect(collapseTwo.open).toBeFalse() collapseTwo.addEventListener('shown.bs.collapse', () => { - expect(collapseOne).not.toHaveClass('show') - expect(collapseTwo).toHaveClass('show') + expect(collapseOne.open).toBeFalse() + expect(collapseTwo.open).toBeTrue() resolve() }) @@ -634,14 +679,14 @@ return new Promise(resolve => { fixtureEl.innerHTML = [ '<input type="checkbox" data-bs-toggle="collapse" data-bs-target="#collapsediv1">', - '<div id="collapsediv1"></div>' + '<details id="collapsediv1" class="collapse"><summary>Toggle</summary></details>' ].join('') const target = fixtureEl.querySelector('input') const collapseEl = fixtureEl.querySelector('#collapsediv1') collapseEl.addEventListener('shown.bs.collapse', () => { - expect(collapseEl).toHaveClass('show') + expect(collapseEl.open).toBeTrue() expect(target.checked).toBeTrue() resolve() }) @@ -658,13 +703,13 @@ ' <div class="lg:col-6">', ' <div class="item">', ' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>', - ' <div id="collapseOne" class="collapse" role="tabpanel" data-bs-parent="#accordion"></div>', + ' <details id="collapseOne" class="collapse" data-bs-parent="#accordion"><summary>Toggle</summary></details>', ' </div>', ' </div>', ' <div class="lg:col-6">', ' <div class="item">', ' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>', - ' <div id="collapseTwo" class="collapse show" role="tabpanel" data-bs-parent="#accordion"></div>', + ' <details id="collapseTwo" class="collapse" open data-bs-parent="#accordion"><summary>Toggle</summary></details>', ' </div>', ' </div>', ' </div>', @@ -677,17 +722,17 @@ const collapseTwoEl = fixtureEl.querySelector('#collapseTwo') collapseOneEl.addEventListener('shown.bs.collapse', () => { - expect(collapseOneEl).toHaveClass('show') + expect(collapseOneEl.open).toBeTrue() expect(triggerEl.getAttribute('aria-expanded')).toEqual('true') - expect(collapseTwoEl).not.toHaveClass('show') + expect(collapseTwoEl.open).toBeFalse() expect(triggerTwoEl.getAttribute('aria-expanded')).toEqual('false') collapseTwoEl.addEventListener('shown.bs.collapse', () => { - expect(collapseOneEl).not.toHaveClass('show') + expect(collapseOneEl.open).toBeFalse() expect(triggerEl.getAttribute('aria-expanded')).toEqual('false') - expect(collapseTwoEl).toHaveClass('show') + expect(collapseTwoEl.open).toBeTrue() expect(triggerTwoEl.getAttribute('aria-expanded')).toEqual('true') resolve() }) @@ -705,10 +750,10 @@ '<div id="accordion">', ' <a id="linkTriggerOne" data-bs-toggle="collapse" data-bs-target=".collapseOne" href="#" aria-expanded="false" aria-controls="collapseOne"></a>', ' <a id="linkTriggerTwo" data-bs-toggle="collapse" data-bs-target=".collapseTwo" href="#" aria-expanded="false" aria-controls="collapseTwo"></a>', - ' <div id="collapseOneOne" class="collapse collapseOne" role="tabpanel" data-bs-parent="#accordion"></div>', - ' <div id="collapseOneTwo" class="collapse collapseOne" role="tabpanel" data-bs-parent="#accordion"></div>', - ' <div id="collapseTwoOne" class="collapse collapseTwo" role="tabpanel" data-bs-parent="#accordion"></div>', - ' <div id="collapseTwoTwo" class="collapse collapseTwo" role="tabpanel" data-bs-parent="#accordion"></div>', + ' <details id="collapseOneOne" class="collapse collapseOne" data-bs-parent="#accordion"><summary>Toggle</summary></details>', + ' <details id="collapseOneTwo" class="collapse collapseOne" data-bs-parent="#accordion"><summary>Toggle</summary></details>', + ' <details id="collapseTwoOne" class="collapse collapseTwo" data-bs-parent="#accordion"><summary>Toggle</summary></details>', + ' <details id="collapseTwoTwo" class="collapse collapseTwo" data-bs-parent="#accordion"><summary>Toggle</summary></details>', '</div>' ].join('') @@ -724,21 +769,21 @@ } function firstTest() { - expect(collapseOneOne).toHaveClass('show') - expect(collapseOneTwo).toHaveClass('show') + expect(collapseOneOne.open).toBeTrue() + expect(collapseOneTwo.open).toBeTrue() - expect(collapseTwoOne).not.toHaveClass('show') - expect(collapseTwoTwo).not.toHaveClass('show') + expect(collapseTwoOne.open).toBeFalse() + expect(collapseTwoTwo.open).toBeFalse() triggerTwo.click() } function secondTest() { - expect(collapseOneOne).not.toHaveClass('show') - expect(collapseOneTwo).not.toHaveClass('show') + expect(collapseOneOne.open).toBeFalse() + expect(collapseOneTwo.open).toBeFalse() - expect(collapseTwoOne).toHaveClass('show') - expect(collapseTwoTwo).toHaveClass('show') + expect(collapseTwoOne.open).toBeTrue() + expect(collapseTwoTwo.open).toBeTrue() resolve() } @@ -784,18 +829,18 @@ '<div id="accordion">', ' <div class="item">', ' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>', - ' <div id="collapseOne" data-bs-parent="#accordion" class="collapse" role="tabpanel">', + ' <details id="collapseOne" data-bs-parent="#accordion" class="collapse"><summary>Toggle</summary>', ' <div id="nestedAccordion">', ' <div class="item">', ' <a id="nestedLinkTrigger" data-bs-toggle="collapse" href="#nestedCollapseOne" aria-expanded="false" aria-controls="nestedCollapseOne"></a>', - ' <div id="nestedCollapseOne" data-bs-parent="#nestedAccordion" class="collapse" role="tabpanel"></div>', + ' <details id="nestedCollapseOne" data-bs-parent="#nestedAccordion" class="collapse"><summary>Toggle</summary></details>', ' </div>', ' </div>', - ' </div>', + ' </details>', ' </div>', ' <div class="item">', ' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>', - ' <div id="collapseTwo" data-bs-parent="#accordion" class="collapse show" role="tabpanel"></div>', + ' <details id="collapseTwo" data-bs-parent="#accordion" class="collapse" open><summary>Toggle</summary></details>', ' </div>', '</div>' ].join('') @@ -812,9 +857,9 @@ function handlerCollapseOne() { collapseOne.removeEventListener('shown.bs.collapse', handlerCollapseOne) - expect(collapseOne).toHaveClass('show') - expect(collapseTwo).not.toHaveClass('show') - expect(nestedCollapseOne).not.toHaveClass('show') + expect(collapseOne.open).toBeTrue() + expect(collapseTwo.open).toBeFalse() + expect(nestedCollapseOne.open).toBeFalse() nestedCollapseOne.addEventListener('shown.bs.collapse', handlerNestedCollapseOne) nestedTrigger.click() @@ -823,14 +868,14 @@ function handlerNestedCollapseOne() { nestedCollapseOne.removeEventListener('shown.bs.collapse', handlerNestedCollapseOne) - expect(collapseOne).toHaveClass('show') - expect(collapseTwo).not.toHaveClass('show') - expect(nestedCollapseOne).toHaveClass('show') + expect(collapseOne.open).toBeTrue() + expect(collapseTwo.open).toBeFalse() + expect(nestedCollapseOne.open).toBeTrue() collapseTwo.addEventListener('shown.bs.collapse', () => { - expect(collapseOne).not.toHaveClass('show') - expect(collapseTwo).toHaveClass('show') - expect(nestedCollapseOne).toHaveClass('show') + expect(collapseOne.open).toBeFalse() + expect(collapseTwo.open).toBeTrue() + expect(nestedCollapseOne.open).toBeTrue() resolve() }) @@ -848,8 +893,8 @@ '<a id="trigger1" role="button" data-bs-toggle="collapse" href="#test1"></a>', '<a id="trigger2" role="button" data-bs-toggle="collapse" href="#0/my/id"></a>', '<a id="trigger3" role="button" data-bs-toggle="collapse" href=".multi"></a>', - '<div id="test1" class="multi"></div>', - '<div id="0/my/id" class="multi"></div>' + '<details id="test1" class="collapse multi"><summary>Toggle</summary></details>', + '<details id="0/my/id" class="collapse multi"><summary>Toggle</summary></details>' ].join('') const trigger1 = fixtureEl.querySelector('#trigger1') @@ -895,52 +940,52 @@ describe('getInstance', () => { it('should return collapse instance', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const div = fixtureEl.querySelector('div') - const collapse = new Collapse(div) + const panel = fixtureEl.querySelector('details') + const collapse = new Collapse(panel) - expect(Collapse.getInstance(div)).toEqual(collapse) - expect(Collapse.getInstance(div)).toBeInstanceOf(Collapse) + expect(Collapse.getInstance(panel)).toEqual(collapse) + expect(Collapse.getInstance(panel)).toBeInstanceOf(Collapse) }) it('should return null when there is no collapse instance', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const div = fixtureEl.querySelector('div') + const panel = fixtureEl.querySelector('details') - expect(Collapse.getInstance(div)).toBeNull() + expect(Collapse.getInstance(panel)).toBeNull() }) }) describe('getOrCreateInstance', () => { it('should return collapse instance', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const div = fixtureEl.querySelector('div') - const collapse = new Collapse(div) + const panel = fixtureEl.querySelector('details') + const collapse = new Collapse(panel) - expect(Collapse.getOrCreateInstance(div)).toEqual(collapse) - expect(Collapse.getInstance(div)).toEqual(Collapse.getOrCreateInstance(div, {})) - expect(Collapse.getOrCreateInstance(div)).toBeInstanceOf(Collapse) + expect(Collapse.getOrCreateInstance(panel)).toEqual(collapse) + expect(Collapse.getInstance(panel)).toEqual(Collapse.getOrCreateInstance(panel, {})) + expect(Collapse.getOrCreateInstance(panel)).toBeInstanceOf(Collapse) }) it('should return new instance when there is no collapse instance', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const div = fixtureEl.querySelector('div') + const panel = fixtureEl.querySelector('details') - expect(Collapse.getInstance(div)).toBeNull() - expect(Collapse.getOrCreateInstance(div)).toBeInstanceOf(Collapse) + expect(Collapse.getInstance(panel)).toBeNull() + expect(Collapse.getOrCreateInstance(panel)).toBeInstanceOf(Collapse) }) it('should return new instance when there is no collapse instance with given configuration', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const div = fixtureEl.querySelector('div') + const panel = fixtureEl.querySelector('details') - expect(Collapse.getInstance(div)).toBeNull() - const collapse = Collapse.getOrCreateInstance(div, { + expect(Collapse.getInstance(panel)).toBeNull() + const collapse = Collapse.getOrCreateInstance(panel, { parent: fixtureEl }) expect(collapse).toBeInstanceOf(Collapse) @@ -949,15 +994,15 @@ }) it('should return the instance when exists without given configuration', () => { - fixtureEl.innerHTML = '<div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary></details>' - const div = fixtureEl.querySelector('div') - const collapse = new Collapse(div, { + const panel = fixtureEl.querySelector('details') + const collapse = new Collapse(panel, { parent: fixtureEl }) - expect(Collapse.getInstance(div)).toEqual(collapse) + expect(Collapse.getInstance(panel)).toEqual(collapse) - const collapse2 = Collapse.getOrCreateInstance(div, { + const collapse2 = Collapse.getOrCreateInstance(panel, { parent: null }) expect(collapse).toBeInstanceOf(Collapse) @@ -967,12 +1012,122 @@ }) }) + describe('native disclosure', () => { + it('should keep the initial state and resolve after each completion event', async () => { + fixtureEl.innerHTML = '<details class="collapse" open><summary>Toggle</summary>Panel</details>' + const panel = fixtureEl.firstElementChild + const events = [] + for (const name of ['hide', 'hidden', 'show', 'shown']) { + panel.addEventListener(`${name}.bs.collapse`, () => events.push([name, panel.open])) + } + + const collapse = new Collapse(panel) + expect(panel.open).toBeTrue() + await collapse.hide() + expect(events).toEqual([['hide', true], ['hidden', false]]) + await collapse.toggle() + expect(events.slice(2)).toEqual([['show', false], ['shown', true]]) + }) + + it('should respect canceled summary clicks and before events', () => { + fixtureEl.innerHTML = '<details class="collapse"><summary><span>Toggle</span></summary>Panel</details>' + const panel = fixtureEl.firstElementChild + const label = panel.querySelector('span') + + label.addEventListener('click', event => event.preventDefault(), { once: true }) + label.click() + expect(Collapse.getInstance(panel)).toBeNull() + + panel.addEventListener('show.bs.collapse', event => event.preventDefault(), { once: true }) + label.click() + expect(panel.open).toBeFalse() + + label.click() + expect(panel.open).toBeTrue() + + panel.addEventListener('hide.bs.collapse', event => event.preventDefault(), { once: true }) + label.click() + expect(panel.open).toBeTrue() + }) + + it('should leave an interactive control inside the summary active', () => { + fixtureEl.innerHTML = '<details class="collapse"><summary><input type="checkbox"> Toggle</summary>Panel</details>' + const panel = fixtureEl.firstElementChild + const checkbox = panel.querySelector('input') + checkbox.click() + + expect(checkbox.checked).toBeTrue() + expect(panel.open).toBeFalse() + }) + + it('should toggle a summary with an explicit target only once', () => { + fixtureEl.innerHTML = ` + <details class="collapse" id="nativePanel"> + <summary data-bs-toggle="collapse" data-bs-target="#nativePanel">Toggle</summary> + Panel + </details>` + const panel = fixtureEl.firstElementChild + panel.querySelector('summary').click() + expect(panel.open).toBeTrue() + }) + + it('should keep a shared trigger expanded while any target is open', async () => { + fixtureEl.innerHTML = ` + <button type="button" data-bs-toggle="collapse" data-bs-target="[">Invalid</button> + <button type="button" data-bs-toggle="collapse" data-bs-target=".mixed-panel">Both</button> + <button type="button" data-bs-toggle="collapse" data-bs-target="#nativePanel">Native</button> + <details class="collapse mixed-panel" id="nativePanel"><summary>Toggle</summary>Native</details> + <details class="collapse mixed-panel"><summary>Toggle</summary>Second panel</details>` + + const [invalid, both, native, panel, second] = fixtureEl.children + invalid.click() + both.click() + expect(panel.open).toBeTrue() + expect(second.open).toBeTrue() + expect(native.getAttribute('aria-expanded')).toEqual('true') + native.click() + expect(panel.open).toBeFalse() + expect(both.getAttribute('aria-expanded')).toEqual('true') + await Collapse.getInstance(second).hide() + expect(both.getAttribute('aria-expanded')).toEqual('false') + }) + + it('should close siblings through data-bs-parent', () => { + fixtureEl.innerHTML = ` + <div id="mixedGroup"> + <details class="collapse" open><summary>Toggle</summary>Second panel</details> + <details class="collapse" data-bs-parent="#mixedGroup"><summary>Toggle</summary>Native</details> + </div>` + + const [first, panel] = fixtureEl.firstElementChild.children + panel.querySelector('summary').click() + expect(panel.open).toBeTrue() + expect(first.open).toBeFalse() + }) + + it('should synchronize external controls when the browser closes a named peer', async () => { + fixtureEl.innerHTML = ` + <button type="button" data-bs-toggle="collapse" data-bs-target="#namedPanel" aria-expanded="true">First</button> + <details class="collapse" name="collapse-test" id="namedPanel" open><summary>First</summary>Panel</details> + <details class="collapse" name="collapse-test"><summary>Second</summary>Panel</details>` + + const [trigger, first, second] = fixtureEl.children + const toggled = new Promise(resolve => { + first.addEventListener('toggle', resolve, { once: true }) + }) + second.open = true + await toggled + expect(first.open).toBeFalse() + expect(trigger.getAttribute('aria-expanded')).toEqual('false') + }) + }) + describe('hash target', () => { it('should scroll the trigger, show the collapse, then scroll the target on load', () => { return new Promise(resolve => { fixtureEl.innerHTML = [ '<a id="collapseHashTrigger" href="#collapseHash" data-bs-toggle="collapse">Toggle</a>', - '<div id="collapseHash" class="collapse" data-bs-hash></div>' + '<details id="collapseHash" class="collapse" data-bs-hash><summary>Toggle</summary></details>' ].join('') const triggerEl = fixtureEl.querySelector('#collapseHashTrigger') @@ -981,7 +1136,7 @@ spyOn(collapseEl, 'scrollIntoView').and.callFake(() => { expect(triggerScrollSpy).toHaveBeenCalled() - expect(collapseEl).toHaveClass('show') + expect(collapseEl.open).toBeTrue() resolve() }) @@ -994,7 +1149,7 @@ return new Promise(resolve => { fixtureEl.innerHTML = [ '<a id="collapseHashChangeTrigger" href="#collapseHashChange" data-bs-toggle="collapse">Toggle</a>', - '<div id="collapseHashChange" class="collapse" data-bs-hash></div>' + '<details id="collapseHashChange" class="collapse" data-bs-hash><summary>Toggle</summary></details>' ].join('') const triggerEl = fixtureEl.querySelector('#collapseHashChangeTrigger') @@ -1003,7 +1158,7 @@ spyOn(collapseEl, 'scrollIntoView').and.callFake(() => { expect(triggerScrollSpy).toHaveBeenCalled() - expect(collapseEl).toHaveClass('show') + expect(collapseEl.open).toBeTrue() resolve() }) @@ -1013,7 +1168,7 @@ }) it('should scroll an already-open hash target into view', () => { - fixtureEl.innerHTML = '<div id="collapseHashOpen" class="collapse show" data-bs-hash></div>' + fixtureEl.innerHTML = '<details id="collapseHashOpen" class="collapse" open data-bs-hash><summary>Toggle</summary></details>' const collapseEl = fixtureEl.querySelector('#collapseHashOpen') const scrollSpy = spyOn(collapseEl, 'scrollIntoView') @@ -1027,7 +1182,7 @@ }) it('should ignore hash targets without data-bs-hash', () => { - fixtureEl.innerHTML = '<div id="collapseNoHash" class="collapse"></div>' + fixtureEl.innerHTML = '<details id="collapseNoHash" class="collapse"><summary>Toggle</summary></details>' const collapseEl = fixtureEl.querySelector('#collapseNoHash') const showSpy = spyOn(Collapse.prototype, 'show').and.callThrough() @@ -1035,7 +1190,7 @@ setHash('collapseNoHash') window.dispatchEvent(createEvent('load')) - expect(collapseEl).not.toHaveClass('show') + expect(collapseEl.open).toBeFalse() expect(showSpy).not.toHaveBeenCalled() }) })
diff --git a/js/tests/unit/promise-api.spec.js b/js/tests/unit/promise-api.spec.js index 7aa705a..5e5d793 100644 --- a/js/tests/unit/promise-api.spec.js +++ b/js/tests/unit/promise-api.spec.js
@@ -42,7 +42,7 @@ describe('resolves after the lifecycle event', () => { it('Collapse show, hide, and toggle', async () => { - fixtureEl.innerHTML = '<div class="collapse"><div>content</div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary><div>content</div></details>' const collapseEl = fixtureEl.querySelector('.collapse') const collapse = new Collapse(collapseEl) @@ -142,7 +142,7 @@ describe('resolves without hanging when the call does nothing', () => { it('a prevented show event still settles', async () => { - fixtureEl.innerHTML = '<div class="collapse"><div>content</div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary><div>content</div></details>' const collapseEl = fixtureEl.querySelector('.collapse') const collapse = new Collapse(collapseEl) @@ -156,11 +156,11 @@ await collapse.show() expect(shownSpy).not.toHaveBeenCalled() - expect(collapseEl).not.toHaveClass('show') + expect(collapseEl.open).toBeFalse() }) it('showing an already shown component settles', async () => { - fixtureEl.innerHTML = '<div class="collapse"><div>content</div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary><div>content</div></details>' const collapseEl = fixtureEl.querySelector('.collapse') const collapse = new Collapse(collapseEl) @@ -173,13 +173,15 @@ await collapse.show() expect(shownSpy).not.toHaveBeenCalled() - expect(collapseEl).toHaveClass('show') + expect(collapseEl.open).toBeTrue() }) it('disposing mid-transition settles the pending promise', async () => { // The specs run without the stylesheet, so the fixture declares the // transition the collapse waits on - fixtureEl.innerHTML = '<div class="collapse" style="transition: block-size .1s"><div>content</div></div>' + fixtureEl.innerHTML = ` + <style>#dispose-panel::details-content { transition: block-size .1s; }</style> + <details class="collapse" id="dispose-panel"><summary>Toggle</summary><div>content</div></details>` const collapseEl = fixtureEl.querySelector('.collapse') const collapse = new Collapse(collapseEl) @@ -215,16 +217,16 @@ describe('sequencing', () => { it('awaits a full show and hide cycle without listeners', async () => { - fixtureEl.innerHTML = '<div class="collapse"><div>content</div></div>' + fixtureEl.innerHTML = '<details class="collapse"><summary>Toggle</summary><div>content</div></details>' const collapseEl = fixtureEl.querySelector('.collapse') const collapse = new Collapse(collapseEl) await collapse.show() - expect(collapseEl).toHaveClass('show') + expect(collapseEl.open).toBeTrue() await collapse.hide() - expect(collapseEl).not.toHaveClass('show') + expect(collapseEl.open).toBeFalse() }) }) })
diff --git a/js/tests/visual/collapse.html b/js/tests/visual/collapse.html index e775f9a..2e4a4ba 100644 --- a/js/tests/visual/collapse.html +++ b/js/tests/visual/collapse.html
@@ -4,87 +4,110 @@ <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="../../../dist/css/bootstrap.min.css" rel="stylesheet"> - <title>Collapse</title> + <title>Native Collapse prototype</title> </head> <body> - <div class="container"> - <h1>Collapse <small>Bootstrap Visual Test</small></h1> + <main class="container py-4"> + <h1>Native Collapse prototype</h1> + <p><a href="?native">Try without Bootstrap JavaScript</a> · <a href="?">Try with Bootstrap JavaScript</a></p> - <div id="accordion" role="tablist"> - <div class="card" role="presentation"> - <div class="card-header" role="tab" id="headingOne"> - <h5 class="mb-0"> - <a data-bs-toggle="collapse" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> - Collapsible Group Item #1 - </a> - </h5> - </div> + <section class="mb-4"> + <h2>Example</h2> + <div class="d-flex flex-wrap gap-2 mb-3"> + <a class="btn-solid theme-primary" href="#vertical" role="button" data-bs-toggle="collapse" aria-expanded="false" aria-controls="vertical">Link with href</a> + <button class="btn-solid theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#vertical" aria-expanded="false" aria-controls="vertical">Button with data-bs-target</button> + </div> + <details class="collapse" id="vertical"> + <summary hidden>Example panel</summary> + <div class="card card-body">The link and button both toggle this panel.</div> + </details> + </section> - <div id="collapseOne" class="collapse show" data-bs-parent="#accordion" role="tabpanel" aria-labelledby="headingOne"> - <div class="card-body"> - Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. - </div> - </div> + <section class="mb-4"> + <h2>Horizontal</h2> + <p> + <button class="btn-solid theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#horizontal" aria-expanded="false" aria-controls="horizontal">Toggle width collapse</button> + </p> + <div style="min-block-size: 140px;"> + <details class="collapse collapse-horizontal" id="horizontal"> + <summary hidden>Horizontal panel</summary> + <div class="card card-body" style="inline-size: 300px;">This panel expands from the inline start edge.</div> + </details> </div> - <div class="card" role="presentation"> - <div class="card-header" role="tab" id="headingTwo"> - <h5 class="mb-0"> - <a data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> - Collapsible Group Item #2 - </a> - </h5> - </div> - <div id="collapseTwo" class="collapse" data-bs-parent="#accordion" role="tabpanel" aria-labelledby="headingTwo"> - <div class="card-body"> - Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. - </div> - </div> - </div> - <div class="card" role="presentation"> - <div class="card-header" role="tab" id="headingThree"> - <h5 class="mb-0"> - <a data-bs-toggle="collapse" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree"> - Collapsible Group Item #3 - </a> - </h5> - </div> - <div id="collapseThree" class="collapse" data-bs-parent="#accordion" role="tabpanel" aria-labelledby="headingThree"> - <div class="card-body"> - Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. - </div> - </div> - </div> - <div class="card" role="presentation"> - <div class="card-header" role="tab" id="headingFour"> - <h5 class="mb-0"> - <a data-bs-toggle="collapse" href="#collapseFour" aria-expanded="false" aria-controls="collapseFour"> - Collapsible Group Item with XSS in data-bs-parent - </a> - </h5> - </div> - <div id="collapseFour" class="collapse" data-bs-parent="<img src=1 onerror=alert(123)>" role="tabpanel" aria-labelledby="headingFour"> - <div class="card-body"> - Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. - </div> - </div> - </div> - </div> + </section> - <h2 class="mt-4">Horizontal</h2> - <p> - <button class="btn-solid theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseHorizontal" aria-expanded="false" aria-controls="collapseHorizontal"> - Toggle inline size - </button> - </p> - <div style="min-height: 140px;"> - <div id="collapseHorizontal" class="collapse collapse-horizontal"> - <div class="card card-body" style="width: 300px;"> - This collapse animates its inline size, so it ends up as wide as its content. + <section class="mb-4"> + <h2>Multiple toggles and targets</h2> + <div class="d-flex flex-wrap gap-2 mb-3"> + <button class="btn-solid theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#first" aria-expanded="false" aria-controls="first">Toggle first element</button> + <button class="btn-solid theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#second" aria-expanded="false" aria-controls="second">Toggle second element</button> + <button class="btn-solid theme-primary" type="button" data-bs-toggle="collapse" data-bs-target=".shared-panel" aria-expanded="false" aria-controls="first second">Toggle both elements</button> + </div> + <div class="row"> + <div class="col"> + <details class="collapse shared-panel" id="first"> + <summary hidden>First panel</summary> + <div class="card card-body">First panel content.</div> + </details> + </div> + <div class="col"> + <details class="collapse shared-panel" id="second"> + <summary hidden>Second panel</summary> + <div class="card card-body">Second panel content.</div> + </details> </div> </div> - </div> - </div> + </section> - <script src="../../../dist/js/bootstrap.bundle.js" type="module"></script> + <section class="mb-4"> + <h2>Hash links</h2> + <div class="d-flex flex-wrap gap-2 mb-3"> + <a class="btn-outline theme-primary" href="#hash-panel">Open via hash link</a> + <button class="btn-solid theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#hash-panel" aria-expanded="false" aria-controls="hash-panel">Toggle</button> + </div> + <details class="collapse" id="hash-panel" data-bs-hash> + <summary hidden>Hash-linked panel</summary> + <div class="card card-body">Opening this URL fragment expands the panel.</div> + </details> + </section> + + <section class="mb-4" id="managed"> + <h2>Bootstrap group</h2> + <div class="d-flex flex-wrap gap-2 mb-3"> + <button class="btn-outline theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#managed-first" aria-expanded="true" aria-controls="managed-first">Toggle first panel</button> + <button class="btn-outline theme-primary" type="button" data-bs-toggle="collapse" data-bs-target="#managed-second" aria-expanded="false" aria-controls="managed-second">Toggle second panel</button> + </div> + <details class="collapse" id="managed-first" data-bs-parent="#managed" open> + <summary hidden>First panel</summary> + <div class="card card-body">Bootstrap closes this panel when the second opens.</div> + </details> + <details class="collapse" id="managed-second" data-bs-parent="#managed"> + <summary hidden>Second panel</summary> + <div class="card card-body">Bootstrap emits events for both panels.</div> + </details> + </section> + + <section class="mb-4"> + <h2>Browser-managed group</h2> + <details class="collapse" id="named-first" name="preview-group" open> + <summary>First panel</summary> + <div class="card card-body">The browser closes this panel when the second opens.</div> + </details> + <details class="collapse" id="named-second" name="preview-group"> + <summary>Second panel</summary> + <div class="card card-body">Both panels share the same name.</div> + </details> + </section> + + <section id="controls" hidden> + <h2>Transition controls</h2> + <label><input id="cancel" type="checkbox"> Cancel Collapse actions</label> + <label><input id="instant" type="checkbox"> Disable transitions</label> + <p class="mt-2"><button class="btn-outline" id="interrupt" type="button">Open and interrupt the native disclosure</button></p> + <pre id="events" class="border p-2" style="min-block-size: 8rem;"></pre> + </section> + </main> + + <script type="module" src="collapse.js"></script> </body> </html>
diff --git a/js/tests/visual/collapse.js b/js/tests/visual/collapse.js new file mode 100644 index 0000000..8dcc9ba --- /dev/null +++ b/js/tests/visual/collapse.js
@@ -0,0 +1,40 @@ +if (!new URLSearchParams(window.location.search).has('native')) { + const { Collapse } = await import('../../../dist/js/bootstrap.bundle.js') + const controls = document.querySelector('#controls') + const cancel = document.querySelector('#cancel') + const instant = document.querySelector('#instant') + const output = document.querySelector('#events') + const vertical = document.querySelector('#vertical') + const events = [] + + for (const name of ['show', 'shown', 'hide', 'hidden']) { + document.addEventListener(`${name}.bs.collapse`, event => { + const canceled = cancel.checked && (name === 'show' || name === 'hide') + if (canceled) { + event.preventDefault() + } + + events.unshift(`${event.target.id}: ${name}${canceled ? ' (canceled)' : ''}`) + events.length = Math.min(events.length, 6) + output.textContent = events.join('\n') + }) + } + + instant.addEventListener('change', () => { + for (const panel of document.querySelectorAll('.collapse')) { + panel.classList.toggle('collapse-instant', instant.checked) + } + }) + + document.querySelector('#interrupt').addEventListener('click', async () => { + const collapse = Collapse.getOrCreateInstance(vertical) + await collapse.hide() + const opening = collapse.show() + setTimeout(() => { + vertical.open = false + }, 75) + await opening + }) + + controls.hidden = false +}
diff --git a/scss/_transitions.scss b/scss/_transitions.scss index 8828e99..634a95f 100644 --- a/scss/_transitions.scss +++ b/scss/_transitions.scss
@@ -20,7 +20,7 @@ // stylelint-disable custom-property-no-missing-var-function $collapse-tokens: defaults( ( - --transition-collapse-property: "block-size, display", + --transition-collapse-property: "block-size, content-visibility", --transition-collapse-duration: .35s, --transition-collapse-timing: ease, ), @@ -41,59 +41,11 @@ } // scss-docs-start collapse-classes -// The browser animates the size itself. `interpolate-size` lets the `0` of the -// closed state interpolate to the `auto` of the open one, and `display` -// transitions discretely, so the element stays laid out until the size reaches -// `0`. The animated axis is clipped, so the content cannot spill while it moves. -// Adding .collapse-instant skips all animations. -.collapse { +// The summary stays visible. Only the content takes part in the transition. +.collapse:where(details) { @include tokens($collapse-tokens); - overflow-y: clip; - - // Browsers without `interpolate-size` open and close the element at once. - @media (prefers-reduced-motion: no-preference) { - interpolate-size: allow-keywords; - } - - &:not(.show) { - display: none; - block-size: 0; - } - - // A closed collapse is not rendered, so the open side needs a start value of - // its own. Without it the element appears at full size at once. - &.show:not(.collapse-instant) { - @starting-style { - block-size: 0; - } - } - - // Horizontal collapses animate the inline size instead. The open size is - // `fit-content`, so the element is as wide as its content, not as wide as its - // container. - &.collapse-horizontal { - --transition-collapse-property: inline-size, display; - - inline-size: fit-content; - overflow-x: clip; - overflow-y: visible; - - &:not(.show) { - inline-size: 0; - block-size: auto; - } - - &.show:not(.collapse-instant) { - @starting-style { - inline-size: 0; - } - } - } -} -// Put motion in a layer so the transition-none utility can override it. -@layer components { - .collapse:not(.collapse-instant) { + &:not(.collapse-instant, .transition-none)::details-content { @include transition-props( var(--transition-collapse-property), var(--transition-collapse-duration), @@ -101,5 +53,45 @@ allow-discrete ); } + + &.collapse-horizontal { + --transition-collapse-property: grid-template-columns; + + inline-size: fit-content; + + &::details-content { + display: grid; + grid-template-columns: minmax(0, 0fr); + align-items: start; + overflow-x: clip; + } + + &[open]:not(.collapse-closing)::details-content { + grid-template-columns: minmax(0, 1fr); + } + } + + &:not(.collapse-horizontal) { + @supports (interpolate-size: allow-keywords) { + @media (prefers-reduced-motion: no-preference) { + interpolate-size: allow-keywords; + } + + &::details-content { + block-size: 0; + overflow-y: clip; + } + + &[open]::details-content { + block-size: auto; + } + } + + @supports not (interpolate-size: allow-keywords) { + &::details-content { + @include transition(none); + } + } + } } // scss-docs-end collapse-classes
diff --git a/site/src/assets/examples/album/index.astro b/site/src/assets/examples/album/index.astro index 06ad314..7b947df 100644 --- a/site/src/assets/examples/album/index.astro +++ b/site/src/assets/examples/album/index.astro
@@ -6,7 +6,8 @@ --- <header data-bs-theme="dark"> - <div class="collapse bg-black" id="navbarHeader"> + <details class="collapse bg-black" id="navbarHeader"> + <summary class="container fg-white py-2">About this album</summary> <div class="container"> <div class="row"> <div class="sm:col-8 md:col-7 py-4"> @@ -23,7 +24,7 @@ </div> </div> </div> - </div> + </details> <nav class="navbar bg-black shadow-sm" aria-label="Album navigation"> <div class="container"> <a href="#" class="navbar-brand d-flex align-items-center">
diff --git a/site/src/assets/examples/cheatsheet/cheatsheet.css b/site/src/assets/examples/cheatsheet/cheatsheet.css index 6e8c4e1..1d65c1a 100644 --- a/site/src/assets/examples/cheatsheet/cheatsheet.css +++ b/site/src/assets/examples/cheatsheet/cheatsheet.css
@@ -64,7 +64,7 @@ transform-origin: .5em 50%; } -.bd-aside .btn[aria-expanded="true"]::before { +.bd-aside .collapse[open] > .btn::before { transform: rotate(90deg); }
diff --git a/site/src/assets/examples/cheatsheet/cheatsheet.js b/site/src/assets/examples/cheatsheet/cheatsheet.js index 000bee2..ae4ee89 100644 --- a/site/src/assets/examples/cheatsheet/cheatsheet.js +++ b/site/src/assets/examples/cheatsheet/cheatsheet.js
@@ -34,31 +34,33 @@ return } - const link = document.querySelector(`.bd-aside a[href="${hash}"]`) + const link = [...document.querySelectorAll('.bd-aside a')].find(anchor => anchor.hash === hash) if (!link) { return } const active = document.querySelector('.bd-aside .active') - const parent = link.parentNode.parentNode.previousElementSibling + const parent = link.closest('details.collapse') + + parent.open = true + + if (active === link) { + return + } link.classList.add('active') - if (parent.getAttribute('aria-expanded') === 'false') { - parent.click() - } - if (!active) { return } - const expanded = active.parentNode.parentNode.previousElementSibling + const expanded = active.closest('details.collapse') active.classList.remove('active') if (expanded && parent !== expanded) { - expanded.click() + expanded.open = false } }
diff --git a/site/src/assets/examples/cheatsheet/index.astro b/site/src/assets/examples/cheatsheet/index.astro index 362dc16..621c792 100644 --- a/site/src/assets/examples/cheatsheet/index.astro +++ b/site/src/assets/examples/cheatsheet/index.astro
@@ -23,49 +23,55 @@ <nav class="small" id="toc"> <ul class="list-unstyled"> <li class="my-2"> - <button type="button" class="btn d-inline-flex align-items-center border-0" data-bs-toggle="collapse" aria-expanded="false" data-bs-target="#contents-collapse" aria-controls="contents-collapse">Contents</button> - <ul class="list-unstyled ps-3 collapse" id="contents-collapse"> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#typography">Typography</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#images">Images</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#tables">Tables</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#figures">Figures</a></li> - </ul> + <details class="collapse" id="contents-collapse"> + <summary class="btn d-inline-flex align-items-center border-0">Contents</summary> + <ul class="list-unstyled ps-3"> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#typography">Typography</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#images">Images</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#tables">Tables</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#figures">Figures</a></li> + </ul> + </details> </li> <li class="my-2"> - <button type="button" class="btn d-inline-flex align-items-center border-0" data-bs-toggle="collapse" aria-expanded="false" data-bs-target="#forms-collapse" aria-controls="forms-collapse">Forms</button> - <ul class="list-unstyled ps-3 collapse" id="forms-collapse"> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#overview">Overview</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#disabled-forms">Disabled forms</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#sizing">Sizing</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#input-group">Input group</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#floating-labels">Floating labels</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#validation">Validation</a></li> - </ul> + <details class="collapse" id="forms-collapse"> + <summary class="btn d-inline-flex align-items-center border-0">Forms</summary> + <ul class="list-unstyled ps-3"> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#overview">Overview</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#disabled-forms">Disabled forms</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#sizing">Sizing</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#input-group">Input group</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#floating-labels">Floating labels</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#validation">Validation</a></li> + </ul> + </details> </li> <li class="my-2"> - <button type="button" class="btn d-inline-flex align-items-center border-0" data-bs-toggle="collapse" aria-expanded="false" data-bs-target="#components-collapse" aria-controls="components-collapse">Components</button> - <ul class="list-unstyled ps-3 collapse" id="components-collapse"> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#accordion">Accordion</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#alerts">Alerts</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#badge">Badge</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#breadcrumb">Breadcrumb</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#buttons">Buttons</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#button-group">Button group</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#card">Card</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#carousel">Carousel</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#dialog">Dialog</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#menus">Menus</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#list-group">List group</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#navs">Navs</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#navbar">Navbar</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#pagination">Pagination</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#popovers">Popovers</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#progress">Progress</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#scrollspy">Scrollspy</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#spinners">Spinners</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#toasts">Toasts</a></li> - <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#tooltips">Tooltips</a></li> - </ul> + <details class="collapse" id="components-collapse"> + <summary class="btn d-inline-flex align-items-center border-0">Components</summary> + <ul class="list-unstyled ps-3"> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#accordion">Accordion</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#alerts">Alerts</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#badge">Badge</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#breadcrumb">Breadcrumb</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#buttons">Buttons</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#button-group">Button group</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#card">Card</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#carousel">Carousel</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#dialog">Dialog</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#menus">Menus</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#list-group">List group</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#navs">Navs</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#navbar">Navbar</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#pagination">Pagination</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#popovers">Popovers</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#progress">Progress</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#scrollspy">Scrollspy</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#spinners">Spinners</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#toasts">Toasts</a></li> + <li><a class="d-inline-flex align-items-center rounded text-decoration-none" href="#tooltips">Tooltips</a></li> + </ul> + </details> </li> </ul> </nav>
diff --git a/site/src/assets/examples/dashboard/dashboard.css b/site/src/assets/examples/dashboard/dashboard.css index 33f414e..84f9cad 100644 --- a/site/src/assets/examples/dashboard/dashboard.css +++ b/site/src/assets/examples/dashboard/dashboard.css
@@ -13,9 +13,6 @@ position: sticky; top: 48px; } - .navbar-search { - display: block; - } } .sidebar .nav-link {
diff --git a/site/src/assets/examples/dashboard/index.astro b/site/src/assets/examples/dashboard/index.astro index 0a878d4..4619ed4 100644 --- a/site/src/assets/examples/dashboard/index.astro +++ b/site/src/assets/examples/dashboard/index.astro
@@ -73,9 +73,11 @@ </li> </ul> - <div id="navbarSearch" class="navbar-search w-100 collapse"> + <details id="navbarSearch" class="w-100 collapse md:d-none"> + <summary class="px-3 py-2 fg-white">Search</summary> <input class="form-control w-100 rounded-0 border-0" type="search" placeholder="Search" aria-label="Search"> - </div> + </details> + <input class="form-control w-100 rounded-0 border-0 d-none md:d-block" type="search" placeholder="Search" aria-label="Search"> </header> <div class="container-fluid">
diff --git a/site/src/assets/examples/sidebars/index.astro b/site/src/assets/examples/sidebars/index.astro index 1807725..cd1478a 100644 --- a/site/src/assets/examples/sidebars/index.astro +++ b/site/src/assets/examples/sidebars/index.astro
@@ -198,56 +198,48 @@ </a> <ul class="list-unstyled ps-0"> <li class="mb-1"> - <button class="btn btn-toggle d-inline-flex align-items-center rounded border-0" data-bs-toggle="collapse" data-bs-target="#home-collapse" aria-expanded="true"> - Home - </button> - <div class="collapse show" id="home-collapse"> + <details class="collapse" id="home-collapse" open> + <summary class="btn btn-toggle d-inline-flex align-items-center rounded border-0">Home</summary> <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small"> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Overview</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Updates</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Reports</a></li> </ul> - </div> + </details> </li> <li class="mb-1"> - <button class="btn btn-toggle d-inline-flex align-items-center rounded border-0" data-bs-toggle="collapse" data-bs-target="#dashboard-collapse" aria-expanded="false"> - Dashboard - </button> - <div class="collapse" id="dashboard-collapse"> + <details class="collapse" id="dashboard-collapse"> + <summary class="btn btn-toggle d-inline-flex align-items-center rounded border-0">Dashboard</summary> <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small"> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Overview</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Weekly</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Monthly</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Annually</a></li> </ul> - </div> + </details> </li> <li class="mb-1"> - <button class="btn btn-toggle d-inline-flex align-items-center rounded border-0" data-bs-toggle="collapse" data-bs-target="#orders-collapse" aria-expanded="false"> - Orders - </button> - <div class="collapse" id="orders-collapse"> + <details class="collapse" id="orders-collapse"> + <summary class="btn btn-toggle d-inline-flex align-items-center rounded border-0">Orders</summary> <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small"> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">New</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Processed</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Shipped</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Returned</a></li> </ul> - </div> + </details> </li> <li class="border-top my-3"></li> <li class="mb-1"> - <button class="btn btn-toggle d-inline-flex align-items-center rounded border-0" data-bs-toggle="collapse" data-bs-target="#account-collapse" aria-expanded="false"> - Account - </button> - <div class="collapse" id="account-collapse"> + <details class="collapse" id="account-collapse"> + <summary class="btn btn-toggle d-inline-flex align-items-center rounded border-0">Account</summary> <ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small"> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">New...</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Profile</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Settings</a></li> <li><a href="#" class="theme-inverse d-inline-flex text-decoration-none rounded">Sign out</a></li> </ul> - </div> + </details> </li> </ul> </div>
diff --git a/site/src/assets/examples/sidebars/sidebars.css b/site/src/assets/examples/sidebars/sidebars.css index 412a14b..c10442b 100644 --- a/site/src/assets/examples/sidebars/sidebars.css +++ b/site/src/assets/examples/sidebars/sidebars.css
@@ -36,10 +36,10 @@ content: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='rgba%28255,255,255,.5%29' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M5 14l6-6-6-6'/%3e%3c/svg%3e"); } -.btn-toggle[aria-expanded="true"] { +.collapse[open] > .btn-toggle { color: color-mix(in oklab, var(--bs-fg-1), transparent 15%); } -.btn-toggle[aria-expanded="true"]::before { +.collapse[open] > .btn-toggle::before { transform: rotate(90deg); }
diff --git a/site/src/content/docs/components/collapse.mdx b/site/src/content/docs/components/collapse.mdx index 96272f0..2a02485 100644 --- a/site/src/content/docs/components/collapse.mdx +++ b/site/src/content/docs/components/collapse.mdx
@@ -3,25 +3,22 @@ description: Toggle the visibility of content across your project with a few classes and our JavaScript plugins. toc: true css_layer: components -js: required +js: optional --- ## How it works -The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. The plugin only adds and removes `.show`—the CSS animates the size from `0` to the content size. Given how CSS handles animations, you cannot use `padding` on a `.collapse` element. Instead, use the class as an independent wrapping element. +Collapse uses `<details class="collapse">` to show and hide content. A visible `<summary>` works with the keyboard without JavaScript. Add `open` to show the content on first paint. Put any padding on the content inside the disclosure. -The animation needs [`interpolate-size`](https://developer.mozilla.org/en-US/docs/Web/CSS/interpolate-size). Browsers without it open and close the element at once. See [Transitions]([[docsref:/customize/transitions]]) to change the timing or to remove it. +Bootstrap’s JavaScript adds external controls, hash links, and the Collapse methods and events. + +Vertical animation needs [`interpolate-size`](https://developer.mozilla.org/en-US/docs/Web/CSS/interpolate-size). Browsers without it open and close vertical panels at once. See [Transitions]([[docsref:/customize/transitions]]) to change the timing or to remove it. <Callout name="info-prefersreducedmotion" /> ## Example -Click the buttons below to show and hide another element via class changes: - -- `.collapse` hides content -- `.collapse.show` shows content - -Generally, we recommend using a `<button>` with the `data-bs-target` attribute. While not recommended from a semantic point of view, you can also use an `<a>` link with the `href` attribute (and a `role="button"`). In both cases, the `data-bs-toggle="collapse"` is required. +External controls use `data-bs-toggle="collapse"` and a selector in `data-bs-target` or `href`. Add `hidden` to the summary when the external controls provide the only way to toggle the panel. <Example code={`<p class="d-inline-flex gap-1"> <a class="btn-solid theme-primary" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample"> @@ -31,18 +28,30 @@ Button with data-bs-target </button> </p> - <div class="collapse" id="collapseExample"> + <details class="collapse" id="collapseExample"> + <summary hidden>More information</summary> <div class="card card-body"> Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger. </div> - </div>`} /> + </details>`} /> + +Leave the summary visible to toggle the content without JavaScript. + +<Example code={`<details class="collapse"> + <summary>More information</summary> + <div class="card card-body">The summary stays visible while the content is closed.</div> + </details>`} /> ## Horizontal -The collapse plugin supports horizontal collapsing. Add the `.collapse-horizontal` modifier class to transition the inline size instead of the block size, and set a `width` on the immediate child element. Feel free to write your own custom Sass, use inline styles, or use our [width utilities]([[docsref:/utilities/width]]). +Add `.collapse-horizontal` to animate the width of the content. Set a width on the content so it keeps its layout as the grid track expands. A visible summary sets the minimum width of the disclosure. Use a style or one of our [width utilities]([[docsref:/utilities/width]]). + +The plugin keeps the `open` attribute until a horizontal close transition ends. External controls update their expanded state when the transition starts. <Callout> -Please note that while the example below has a `min-height` set to avoid excessive repaints in our docs, this is not explicitly required. **Only the `width` on the child element is required.** +Please note that while the example below has a `min-height` set to avoid excessive repaints in our docs, this is not explicitly required. **Only the width on the content is required.** + +WebKit can shrink a vertical disclosure to its closed height before the content transition ends. </Callout> <Example code={`<p> @@ -51,13 +60,18 @@ </button> </p> <div style="min-height: 140px;"> - <div class="collapse collapse-horizontal" id="collapseWidthExample"> + <details class="collapse collapse-horizontal" id="collapseWidthExample"> + <summary hidden>Horizontal panel</summary> <div class="card card-body" style="width: 300px;"> This is some placeholder content for a horizontal collapse. It’s hidden by default and shown when triggered. </div> - </div> + </details> </div>`} /> +## Groups + +Use `name` for browser-managed groups that work without JavaScript. The browser closes a named peer and emits a native `toggle` event. Use `data-bs-parent` for groups that need cancellable Bootstrap events or shared triggers. + ## Multiple toggles and targets A `<button>` or `<a>` element can show and hide multiple elements by referencing them with a selector in its `data-bs-target` or `href` attribute. @@ -70,18 +84,20 @@ </p> <div class="row"> <div class="col"> - <div class="collapse multi-collapse" id="multiCollapseExample1"> + <details class="collapse multi-collapse" id="multiCollapseExample1"> + <summary hidden>First panel</summary> <div class="card card-body"> Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger. </div> - </div> + </details> </div> <div class="col"> - <div class="collapse multi-collapse" id="multiCollapseExample2"> + <details class="collapse multi-collapse" id="multiCollapseExample2"> + <summary hidden>Second panel</summary> <div class="card card-body"> Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger. </div> - </div> + </details> </div> </div>`} /> @@ -97,19 +113,16 @@ Toggle </button> </p> - <div class="collapse" id="collapseHashExample" data-bs-hash> + <details class="collapse" id="collapseHashExample" data-bs-hash> + <summary hidden>Hash-linked panel</summary> <div class="card card-body"> <p>Some placeholder content for a hash-linked collapse. Visiting <code>#collapseHashExample</code> opens this panel and scrolls it into view.</p> </div> - </div>`} /> + </details>`} /> ## Accessibility -Be sure to add `aria-expanded` to the control element. This attribute explicitly conveys the current state of the collapsible element tied to the control to screen readers and similar assistive technologies. If the collapsible element is closed by default, the attribute on the control element should have a value of `aria-expanded="false"`. If you’ve set the collapsible element to be open by default using the `show` class, set `aria-expanded="true"` on the control instead. The plugin will automatically toggle this attribute on the control based on whether or not the collapsible element has been opened or closed (via JavaScript, or because the user triggered another control element also tied to the same collapsible element). If the control element’s HTML element is not a button (e.g., an `<a>` or `<div>`), the attribute `role="button"` should be added to the element. - -Collapse trigger state is represented by `aria-expanded`. - -If your control element is targeting a single collapsible element – i.e. the `data-bs-target` attribute is pointing to an `id` selector – you should add the `aria-controls` attribute to the control element, containing the `id` of the collapsible element. Modern screen readers and similar assistive technologies make use of this attribute to provide users with additional shortcuts to navigate directly to the collapsible element itself. +The browser exposes the summary’s expanded state. For an external control, set `aria-expanded` to match the initial state of its targets. The plugin keeps this value in sync. Set `aria-controls` to the target IDs. If you use a link as a control, add `role="button"`. Note that Bootstrap’s current implementation does not cover the various *optional* keyboard interactions described in the [ARIA Authoring Practices Guide accordion pattern](https://www.w3.org/WAI/ARIA/apg/patterns/accordion/) - you will need to include these yourself with custom JavaScript. @@ -121,24 +134,17 @@ ### Classes -Collapse transition classes can be found in `scss/_transitions.scss` as these are shared across multiple components (collapse and accordion). +Collapse uses `::details-content` to animate the content without moving the summary. <ScssDocs name="collapse-classes" file="scss/_transitions.scss" /> ## Usage -The collapse plugin utilizes two classes to handle the heavy lifting: - -- `.collapse` hides the content -- `.collapse.show` shows the content - -These classes can be found in `_transitions.scss`. The plugin toggles `.show` and then waits for the CSS transition to finish before it fires `shown.bs.collapse` or `hidden.bs.collapse`. Add `.collapse-instant` to the collapsible element to skip all transitions and expand or collapse immediately. +Add `.collapse-instant` or `.transition-none` to open and close at once. The plugin uses the computed CSS transition timing before it fires `shown.bs.collapse` or `hidden.bs.collapse`. ### Via data attributes -Just add `data-bs-toggle="collapse"` and a `data-bs-target` to the element to automatically assign control of one or more collapsible elements. The `data-bs-target` attribute accepts a CSS selector to apply the collapse to. Be sure to add the class `collapse` to the collapsible element. If you’d like it to default open, add the additional class `show`. - -To add accordion-like group management to a collapsible area, add the data attribute `data-bs-parent="#selector"`. Refer to the [accordion page]([[docsref:/components/accordion]]) for more information. +Add `data-bs-toggle="collapse"` and a `data-bs-target` selector to an external control. The selector can match one or more `<details class="collapse">` elements. <BsTable> | Attribute | Description | @@ -154,7 +160,7 @@ Enable manually with: ```js -const collapseElementList = document.querySelectorAll('.collapse') +const collapseElementList = document.querySelectorAll('details.collapse') const collapseList = [...collapseElementList].map(collapseEl => new bootstrap.Collapse(collapseEl)) ``` @@ -178,7 +184,7 @@ Activates your content as a collapsible element. Accepts an optional options `object`. -The constructor keeps the state that your markup declares. Add `.show` to open the element, and leave it off to keep the element closed. Call `show()`, `hide()`, or `toggle()` to change the state. +The constructor keeps the state that your markup declares. Add `open` to start open. Call `show()`, `hide()`, or `toggle()` to change the state. ```js const bsCollapse = new bootstrap.Collapse('#myCollapse')
diff --git a/site/src/content/docs/getting-started/javascript.mdx b/site/src/content/docs/getting-started/javascript.mdx index 549f94f..d9cbcba 100644 --- a/site/src/content/docs/getting-started/javascript.mdx +++ b/site/src/content/docs/getting-started/javascript.mdx
@@ -15,7 +15,7 @@ - Datepicker for date selection (also requires [Vanilla Calendar Pro](https://vanilla-calendar.pro/)) - [Dialog]([[docsref:/components/dialog]]) for displaying, positioning, and scroll behavior - Menus for displaying and positioning (also requires [Floating UI](https://floating-ui.com/)) -- Navbar for extending our Collapse and Drawer plugins to implement responsive behaviors +- Navbar for using Drawer to implement responsive behaviors - [Nav]([[docsref:/components/nav]]) for navigation markup and styles; [Tab]([[docsref:/components/tab]]) plugin for toggling tab panes - Drawers for displaying, positioning, and scroll behavior - Scrollspy for scroll behavior and navigation updates
diff --git a/site/src/content/docs/guides/migration.mdx b/site/src/content/docs/guides/migration.mdx index 93d97e8..acab475 100644 --- a/site/src/content/docs/guides/migration.mdx +++ b/site/src/content/docs/guides/migration.mdx
@@ -386,12 +386,7 @@ - **Tooltips and popovers animate from `@starting-style`.** The plugin no longer adds `.fade` to the tip. `animation: false` adds `.tooltip-instant` or `.popover-instant` instead, which you can also write in a custom `template`. - **Hover and focus tooltips stay open on the tip.** In v5 the tooltip hid as soon as the pointer left the trigger. v6 keeps it open while the pointer or focus is on the tip itself, so links inside the tooltip are reachable ([WCAG 1.4.13](https://www.w3.org/WAI/WCAG21/Understanding/content-on-hover-or-focus.html)). Press Escape or leave both the trigger and the tip to hide it. See [Tooltips]([[docsref:/components/tooltip]]). - **Placeholder loading animations now favor shimmer.** Use `.placeholder-wave` for the standard loading animation. The old opacity animation moved to `.placeholder-pulse`. The `.placeholder-glow` class remains as a compatibility alias for `.placeholder-pulse`. -- **Collapse animates its size natively.** The plugin toggles `.show` and nothing else. The CSS animates the size with [`interpolate-size`](https://developer.mozilla.org/en-US/docs/Web/CSS/interpolate-size), as the accordion already does: - - Removed the `.collapsing` class, and with it the inline `height` and `width` the plugin used to write. Replace any CSS, tests, or selectors that depend on either. - - Browsers without `interpolate-size` open and close the element at once. Firefox is the one to watch. - - `.collapse` now clips the animated axis at all times, so content can no longer spill out of the box. A `.collapse-horizontal` clips the inline axis instead. - - `shown.bs.collapse` and `hidden.bs.collapse` now fire off the CSS transition, tuned with `--bs-transition-collapse-property`, `--bs-transition-collapse-duration`, and `--bs-transition-collapse-timing`. - - A trigger that targets several collapses under one `data-bs-parent` still opens them together. The parent only closes collapses that the trigger does not target. +- **Collapse uses native disclosures.** Replace the collapsible wrapper with `<details class="collapse">` and put a `<summary>` first. Replace the `.show` class with the `open` attribute. External triggers still use `data-bs-toggle="collapse"`. The Collapse methods, events, and `data-bs-parent` grouping also apply to the new markup. - **Component JavaScript reads the computed transition duration.** Alert, collapse, tab, tooltip, and popover no longer look for a class to decide whether to wait for a transition, as the menu already did. A zero duration—from `.transition-none`, an `-instant` class, reduced motion, or `$enable-transitions: false`—resolves the promise and fires the event at once. - **Reworked button variants.** The v5 per-color classes like `.btn-primary`, `.btn-outline-primary`, `.btn-secondary`, etc. are replaced by a composition of variant + theme classes on the same element: - `.btn-primary` → `class="btn-solid theme-primary"`