Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add horizontal collapse support #34253

Merged
merged 1 commit into from Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion js/src/collapse.js
Expand Up @@ -50,6 +50,7 @@ const CLASS_NAME_SHOW = 'show'
const CLASS_NAME_COLLAPSE = 'collapse'
const CLASS_NAME_COLLAPSING = 'collapsing'
const CLASS_NAME_COLLAPSED = 'collapsed'
const CLASS_NAME_HORIZONTAL = 'collapse-horizontal'

const WIDTH = 'width'
const HEIGHT = 'height'
Expand Down Expand Up @@ -266,7 +267,7 @@ class Collapse extends BaseComponent {
}

_getDimension() {
return this._element.classList.contains(WIDTH) ? WIDTH : HEIGHT
return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT
}

_getParent() {
Expand Down
2 changes: 1 addition & 1 deletion js/tests/unit/collapse.spec.js
Expand Up @@ -225,7 +225,7 @@ describe('Collapse', () => {
})

it('should show a collapsed element on width', done => {
fixtureEl.innerHTML = '<div class="collapse width" style="width: 0px;"></div>'
fixtureEl.innerHTML = '<div class="collapse collapse-horizontal" style="width: 0px;"></div>'

const collapseEl = fixtureEl.querySelector('div')
const collapse = new Collapse(collapseEl, {
Expand Down
6 changes: 6 additions & 0 deletions scss/_transitions.scss
Expand Up @@ -17,5 +17,11 @@
height: 0;
overflow: hidden;
@include transition($transition-collapse);

&.collapse-horizontal {
width: 0;
height: auto;
@include transition($transition-collapse-width);
}
}
// scss-docs-end collapse-classes
1 change: 1 addition & 0 deletions scss/_variables.scss
Expand Up @@ -398,6 +398,7 @@ $transition-base: all .2s ease-in-out !default;
$transition-fade: opacity .15s linear !default;
// scss-docs-start collapse-transition
$transition-collapse: height .35s ease !default;
$transition-collapse-width: width .35s ease !default;
// scss-docs-end collapse-transition

// stylelint-disable function-disallowed-list
Expand Down
23 changes: 23 additions & 0 deletions site/content/docs/5.0/components/collapse.md
Expand Up @@ -40,6 +40,29 @@ Generally, we recommend using a button with the `data-bs-target` attribute. Whil
</div>
{{< /example >}}

## Horizontal

The collapse plugin also supports horizontal collapsing. Add the `.collapse-horizontal` modifier class to transition the `width` instead of `height` 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/sizing" >}}).

{{< callout info >}}
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.**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering if contains and will-change could help here? Can't play with this right now, but something to keep in mind for improving v5 regarding repaint / reflow and animation.

{{< /callout >}}

{{< example >}}
<p>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseWidthExample" aria-expanded="false" aria-controls="collapseWidthExample">
Toggle width collapse
</button>
</p>
<div style="min-height: 120px;">
<div class="collapse collapse-horizontal" id="collapseWidthExample">
<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>
</div>
{{< /example >}}

## Multiple targets

A `<button>` or `<a>` can show and hide multiple elements by referencing them with a selector in its `href` or `data-bs-target` attribute.
Expand Down