-
-
Notifications
You must be signed in to change notification settings - Fork 646
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
Remove object-assign-deep, refactor setting popper options #516
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const addHasTitleClass = (step) => { | ||
return { addHasTitleClass: _createClassModifier(`${step.classPrefix}shepherd-has-title`) }; | ||
}; | ||
|
||
/** | ||
* Create a popper modifier for adding the passed className to the popper | ||
* @param {string} className The class to add to the popper | ||
* @return {{fn(*): *, enabled: boolean}|*} | ||
* @private | ||
*/ | ||
function _createClassModifier(className) { | ||
return { | ||
enabled: true, | ||
fn(data) { | ||
data.instance.popper.classList.add(className); | ||
return data; | ||
} | ||
}; | ||
} | ||
|
||
function _getCenteredStylePopperModifier(styles) { | ||
return { | ||
computeStyle: { | ||
enabled: true, | ||
fn(data) { | ||
data.styles = Object.assign({}, data.styles, { | ||
left: '50%', | ||
top: '50%', | ||
transform: 'translate(-50%, -50%)' | ||
}); | ||
|
||
return data; | ||
} | ||
}, | ||
addShepherdClass: _createClassModifier(styles.shepherd.trim()) | ||
}; | ||
} | ||
|
||
/** | ||
* Used to compose settings for tippyOptions.popperOptions (https://atomiks.github.io/tippyjs/#popper-options-option) | ||
* @private | ||
*/ | ||
function _getDefaultPopperOptions(styles) { | ||
return { | ||
positionFixed: true, | ||
modifiers: { | ||
addShepherdClass: _createClassModifier(styles.shepherd.trim()) | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Generates the hash of options that will be passed to `Tippy` instances | ||
* target an element in the DOM. | ||
* | ||
* @param {Object} attachToOptions The local `attachTo` options | ||
* @param {Step} step The step instance | ||
* @return {Object} The final tippy options object | ||
*/ | ||
export function makeAttachedTippyOptions(attachToOptions, step) { | ||
let { popperOptions, tippyOptions } = _makeCommonTippyOptions(step); | ||
|
||
tippyOptions.flipOnUpdate = true; | ||
tippyOptions.placement = attachToOptions.on || 'right'; | ||
|
||
if (step.options.tippyOptions && step.options.tippyOptions.popperOptions) { | ||
popperOptions = { | ||
...popperOptions, | ||
...step.options.tippyOptions.popperOptions | ||
}; | ||
} | ||
|
||
tippyOptions.popperOptions = popperOptions; | ||
|
||
return tippyOptions; | ||
} | ||
|
||
/** | ||
* Generates the hash of options for a tooltip that doesn't have a | ||
* target element in the DOM -- and thus is positioned in the center | ||
* of the view | ||
* | ||
* @param {Step} step The step instance | ||
* @return {Object} The final tippy options object | ||
*/ | ||
export function makeCenteredTippy(step) { | ||
const centeredStylePopperModifier = _getCenteredStylePopperModifier(step.styles); | ||
let { popperOptions, tippyOptions } = _makeCommonTippyOptions(step); | ||
|
||
tippyOptions.placement = 'top'; | ||
tippyOptions.arrow = false; | ||
tippyOptions.popperOptions = tippyOptions.popperOptions || {}; | ||
|
||
popperOptions.modifiers = { | ||
...popperOptions.modifiers, | ||
...centeredStylePopperModifier, | ||
...tippyOptions.popperOptions.modifiers | ||
}; | ||
|
||
popperOptions = { | ||
...popperOptions, | ||
...tippyOptions.popperOptions | ||
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. if tippyOptions.popperOptions has modifiers they will override the tippy modifiers, which was the issue in the first place. |
||
}; | ||
|
||
tippyOptions.popperOptions = popperOptions; | ||
|
||
return tippyOptions; | ||
} | ||
|
||
function _makeCommonTippyOptions(step) { | ||
const popperOptions = _getDefaultPopperOptions(step.styles); | ||
|
||
const tippyOptions = { | ||
content: step.el, | ||
...step.options.tippyOptions | ||
}; | ||
|
||
if (step.options.title) { | ||
popperOptions.modifiers = { | ||
...popperOptions.modifiers, | ||
...addHasTitleClass(step) | ||
}; | ||
} | ||
|
||
return { popperOptions, tippyOptions }; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { makeAttachedTippyOptions } from '../../../src/js/utils/tippy-popper-options'; | ||
|
||
describe('Tippy/Popper Options Utils', function() { | ||
describe('makeAttachedTippyOptions()', function() { | ||
it('passing tippyOptions.popperOptions sets nested values', function() { | ||
const stepOptions = | ||
{ | ||
options: { | ||
tippyOptions: { | ||
popperOptions: { | ||
modifiers: { | ||
foo: 'bar', | ||
preventOverflow: { | ||
escapeWithReference: true | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
styles: { | ||
shepherd: ' shepherd' | ||
} | ||
}; | ||
|
||
const attachToOpts = { | ||
on: 'top' | ||
}; | ||
|
||
const tippyOptions = makeAttachedTippyOptions(attachToOpts, stepOptions); | ||
expect(tippyOptions.popperOptions.modifiers.foo).toBe('bar'); | ||
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. The issue was that if you have custom modifiers, shepherd poper modifiers get removed. |
||
expect(tippyOptions.popperOptions.modifiers.preventOverflow.escapeWithReference).toBe(true); | ||
}); | ||
}); | ||
}); |
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.
if step.options.tippyOptions.popperOptions has modifiers they will override the tippy modifiers, which was the issue in the first place.
please look at my original Pull request that solves the issue.