-
-
Notifications
You must be signed in to change notification settings - Fork 78.9k
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
Collapse with flex elements #22758
Collapse with flex elements #22758
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ const Collapse = (() => { | |
|
||
const ClassName = { | ||
SHOW : 'show', | ||
FLEXSHOW : 'flexshow', | ||
COLLAPSE : 'collapse', | ||
COLLAPSING : 'collapsing', | ||
COLLAPSED : 'collapsed' | ||
|
@@ -87,7 +88,8 @@ const Collapse = (() => { | |
} | ||
} | ||
|
||
this._parent = this._config.parent ? this._getParent() : null | ||
this._parent = this._config.parent ? this._getParent() : null | ||
this._showClass = this._getShowClass() | ||
|
||
if (!this._config.parent) { | ||
this._addAriaAndCollapsedClass(this._element, this._triggerArray) | ||
|
@@ -113,7 +115,7 @@ const Collapse = (() => { | |
// public | ||
|
||
toggle() { | ||
if ($(this._element).hasClass(ClassName.SHOW)) { | ||
if ($(this._element).hasClass(this._showClass)) { | ||
this.hide() | ||
} else { | ||
this.show() | ||
|
@@ -122,7 +124,7 @@ const Collapse = (() => { | |
|
||
show() { | ||
if (this._isTransitioning || | ||
$(this._element).hasClass(ClassName.SHOW)) { | ||
$(this._element).hasClass(this._showClass)) { | ||
return | ||
} | ||
|
||
|
@@ -176,7 +178,7 @@ const Collapse = (() => { | |
$(this._element) | ||
.removeClass(ClassName.COLLAPSING) | ||
.addClass(ClassName.COLLAPSE) | ||
.addClass(ClassName.SHOW) | ||
.addClass(this._showClass) | ||
|
||
this._element.style[dimension] = '' | ||
|
||
|
@@ -202,7 +204,7 @@ const Collapse = (() => { | |
|
||
hide() { | ||
if (this._isTransitioning || | ||
!$(this._element).hasClass(ClassName.SHOW)) { | ||
!$(this._element).hasClass(this._showClass)) { | ||
return | ||
} | ||
|
||
|
@@ -212,16 +214,15 @@ const Collapse = (() => { | |
return | ||
} | ||
|
||
const dimension = this._getDimension() | ||
|
||
const dimension = this._getDimension() | ||
this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px` | ||
|
||
Util.reflow(this._element) | ||
|
||
$(this._element) | ||
.addClass(ClassName.COLLAPSING) | ||
.removeClass(ClassName.COLLAPSE) | ||
.removeClass(ClassName.SHOW) | ||
.removeClass(this._showClass) | ||
|
||
if (this._triggerArray.length) { | ||
for (let i = 0; i < this._triggerArray.length; i++) { | ||
|
@@ -263,6 +264,16 @@ const Collapse = (() => { | |
this._isTransitioning = isTransitioning | ||
} | ||
|
||
update() { | ||
if ($(this._element).hasClass(this._showClass)) { | ||
$(this._element).removeClass(this._showClass) | ||
this._showClass = this._getShowClass() | ||
$(this._element).addClass(this._showClass) | ||
} else { | ||
this._showClass = this._getShowClass() | ||
} | ||
} | ||
|
||
dispose() { | ||
$.removeData(this._element, DATA_KEY) | ||
|
||
|
@@ -316,7 +327,7 @@ const Collapse = (() => { | |
|
||
_addAriaAndCollapsedClass(element, triggerArray) { | ||
if (element) { | ||
const isOpen = $(element).hasClass(ClassName.SHOW) | ||
const isOpen = $(element).hasClass(ClassName.SHOW) || $(element).hasClass(ClassName.FLEXSHOW) | ||
|
||
if (triggerArray.length) { | ||
$(triggerArray) | ||
|
@@ -326,6 +337,32 @@ const Collapse = (() => { | |
} | ||
} | ||
|
||
_getShowClass() { | ||
const tabClass = this._element.classList | ||
|
||
// Detect flex for inline style | ||
let useFlex = $(this._element).css('display').indexOf('flex') !== -1 | ||
|
||
// Create a wrapper to hide our flex detection | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can use that for the line 316 yes, but not for the detection below because getComputedStyle return the current applied style and that's not what I want here. |
||
const $tmpWrapper = $('<div class="d-none"></div>') | ||
$tmpWrapper.insertAfter($(this._element)) | ||
|
||
const $tmpElem = $('<div></div>') | ||
$tmpWrapper.append($tmpElem) | ||
|
||
// Detect flex in used classes | ||
for (let i = 0; i < tabClass.length; i++) { | ||
$tmpElem.addClass(tabClass[i]) | ||
const tmpDisplay = $tmpElem.css('display') | ||
$tmpElem.removeClass(tabClass[i]) | ||
if (tmpDisplay.indexOf('flex') !== -1) { | ||
useFlex = true | ||
break | ||
} | ||
} | ||
$tmpWrapper.remove() | ||
return !useFlex ? ClassName.SHOW : ClassName.FLEXSHOW | ||
} | ||
|
||
// static | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,21 @@ | |
&.show { | ||
display: block; | ||
} | ||
&.flexshow { | ||
display: flex; | ||
} | ||
} | ||
|
||
tr { | ||
&.collapse.show { | ||
&.collapse.show, | ||
&.collapse.flexshow { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should |
||
display: table-row; | ||
} | ||
} | ||
|
||
tbody { | ||
&.collapse.show { | ||
&.collapse.show, | ||
&.collapse.flexshow { | ||
display: table-row-group; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about just using https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I switched to
includes
but they are no support on IE 10 so back toindexOf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it's transpiled isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't checked but unit tests failed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But since we use babel it should be taken care of. Please try one more time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just keep in mind we can do this if we just load the babel polyfill and use ES6 methods. Not sure what the exact size diff is, just thinking out loud for another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know which polyfill to use ? I can make a test 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://babeljs.io/docs/usage/polyfill/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked
babel-polyfill
and currently when we import babel-polyfill we will import all the available polyfill not theincludes
one so for now we should merge this PR and open an issue on how to integrate babel-polyfill with only specific es6 polyfillThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, it's not so important currently as long as we don't use features that aren't available for the browsers we support.