-
-
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
Offset option for dropdown can be function #24222
Conversation
js/src/dropdown.js
Outdated
@@ -39,6 +39,8 @@ const Dropdown = (() => { | |||
const ARROW_DOWN_KEYCODE = 40 // KeyboardEvent.which value for down arrow key | |||
const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse) | |||
const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`) | |||
const POPPER_OFFSET_KEY = 'offset' | |||
const POPPER_OFFSET_FN_KEY = 'fn' |
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.
Not a big fan to add new const for that 🤔
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.
Agree, const is redundant. Updated
js/src/dropdown.js
Outdated
let key = POPPER_OFFSET_KEY | ||
if (typeof this._config.offset === 'function') { | ||
key = POPPER_OFFSET_FN_KEY | ||
} |
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.
IMO something like that would be enough :
const offsetConf = {}
if (typeof this._config.offset === 'function') {
offsetConf.offsetFn = (data) => this._getOffset(data)
} else {
offsetConf.offset = this._config.offset
}
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 agree with your. It's better solution. Const is redundant
_getPopperConfig() {
const offsetConf = {}
if (typeof this._config.offset === 'function') {
offsetConf.fn = (data) => this._getOffset(data)
} else {
offsetConf.offset = this._config.offset
}
const popperConfig = {
placement : this._getPlacement(),
modifiers : {
offset : offsetConf,
flip : {
enabled : this._config.flip
}
}
}
// Disable Popper.js for Dropdown in Navbar
if (this._inNavbar) {
popperConfig.modifiers.applyStyle = {
enabled: !this._inNavbar
}
}
return popperConfig
}
js/src/dropdown.js
Outdated
@@ -241,16 +243,28 @@ const Dropdown = (() => { | |||
return placement | |||
} | |||
|
|||
_getOffset(data) { | |||
const popperOffset = $.extend({}, data.offsets, this._config.offset(data.offsets) || {}) | |||
Object.keys(popperOffset).forEach((key) => { |
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.
Sorry to ask I don't know well offset
function from Popper.js but why do you need that ?
Ok I read a bit and IMO if you just do return $.extend({}, data.offsets, this._config.offset(data.offsets) || {})
it will be enough here
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.
data object has many another keys. We need to change only data.offsets and not other. If we return only extended object with data.offsets - Popper is broken
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.
That's right 👍 but instead of a Foreach you just have to do :
data.offsets = popperOffset
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.
Yup. Agree :) Sorry don't know how I can stick new changes in this PR romanlex@9be393b
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.
Thank you very much @romanlex to tackled that 👍
js/src/dropdown.js
Outdated
const offsetConf = {} | ||
if (typeof this._config.offset === 'function') { | ||
offsetConf.fn = (data) => this._getOffset(data) | ||
} else { |
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 was thinking maybe we should remove _getOffset
method because that's not something we will use outside of this and remplaced it by :
offsetConf.fn = (data) => {
data.offsets = $.extend({}, data.offsets, this._config.offset(data.offsets) || {})
return data
}
Can you give it a try ?
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.
Remove this method but in my opinion this is less readable. On the other hand, the method is not needed, because obviously not planned to manage offseting inside boostrap
Fix #24208
Offset option can be function (Popper.js)
With this code I can change any dropdown position as on picture
Fix : #24223