diff --git a/core/js/src/bootstrap/toggler.ts b/core/js/src/bootstrap/toggler.ts index 0e8653a41..cccb895b7 100644 --- a/core/js/src/bootstrap/toggler.ts +++ b/core/js/src/bootstrap/toggler.ts @@ -94,7 +94,10 @@ class Toggler extends BaseComponent { } if (attribute === 'class') { - this._element.classList.toggle(String(value)) + for (const className of String(value).split(/\s+/).filter(Boolean)) { + this._element.classList.toggle(className) + } + return } diff --git a/core/js/tests/unit/toggler.spec.ts b/core/js/tests/unit/toggler.spec.ts index b296314e4..4869d43b1 100644 --- a/core/js/tests/unit/toggler.spec.ts +++ b/core/js/tests/unit/toggler.spec.ts @@ -46,6 +46,21 @@ describe('Toggler', () => { expect(togglerEl.classList.contains('bg-warning')).toBe(false) }) + it('should toggle every class from a space separated value', () => { + fixtureEl.innerHTML = '
' + + const togglerEl = fixtureEl.querySelector('[data-bs-toggle="toggler"]')! + const toggler = new Toggler(togglerEl) + + toggler.toggle() + expect(togglerEl.classList.contains('bg-warning')).toBe(true) + expect(togglerEl.classList.contains('bg-info')).toBe(false) + + toggler.toggle() + expect(togglerEl.classList.contains('bg-warning')).toBe(false) + expect(togglerEl.classList.contains('bg-info')).toBe(true) + }) + it('should toggle attribute on the element', () => { fixtureEl.innerHTML = '
' @@ -264,6 +279,7 @@ describe('Toggler', () => { triggerEl.click() expect(targetEl.classList.contains('active')).toBe(false) }) + it('should work with the data-tblr- prefix', () => { fixtureEl.innerHTML = ['', '
'].join('') diff --git a/docs/content/ui/components/toggler.mdx b/docs/content/ui/components/toggler.mdx index 1f3ee29df..214aa4c29 100644 --- a/docs/content/ui/components/toggler.mdx +++ b/docs/content/ui/components/toggler.mdx @@ -37,12 +37,12 @@ Use `data-bs-target` to change another element. Put `data-bs-value` on the targe ### Multiple elements -A class selector in `data-bs-target` toggles every matching element. Each one can have its own value. +When the selector in `data-bs-target` matches many elements, Toggler changes every one of them. Each one can have its own value. - -
Toggles bg-blue-lt
-
Toggles bg-green-lt
+ +
Toggles bg-blue-lt
+
Toggles bg-green-lt
### Toggle an attribute @@ -63,7 +63,7 @@ You can use the same pattern to toggle `disabled` on a `fieldset` and lock all c | `data-bs-toggle="toggler"` | Turns the element into a trigger. | | `data-bs-target` | Optional CSS selector of the element to change. Without it, the trigger changes itself. | | `data-bs-attribute` | Attribute to toggle. Default is `class`. | -| `data-bs-value` | Class name or attribute value to toggle. Without it, nothing happens. | +| `data-bs-value` | Class name or attribute value to toggle. You can give several class names separated by spaces. Without it, nothing happens. | ### JavaScript @@ -92,6 +92,8 @@ toggler.toggle() | `toggle.bs.toggler` | Fires before the change. Call `event.preventDefault()` to stop it. | | `toggled.bs.toggler` | Fires after the change. | +Both events fire on the element that changes. With `data-bs-target`, this is the target, not the trigger. + ```js document.getElementById('my-element').addEventListener('toggled.bs.toggler', () => { // do something @@ -101,5 +103,5 @@ document.getElementById('my-element').addEventListener('toggled.bs.toggler', () ## Accessibility - Use a ` + A selector with many matches toggles every one, each with its own value. +
{ colors.map((color) => ( -
+
Toggles bg-{color}
))