DEV: emoji picker - make it possible to choose picker's placement and add a dedicated class for an anchor (#16969)

This commit is contained in:
Andrei Prigorshnev 2022-06-01 22:24:23 +04:00 committed by GitHub
parent e579e9e61c
commit 0bbbd8371e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 8 deletions

View File

@ -38,6 +38,7 @@ export default Component.extend({
hoveredEmoji: null,
isActive: false,
usePopper: true,
placement: "auto", // one of popper.js' placements, see https://popper.js.org/docs/v2/constructors/#options
initialFilter: "",
init() {
@ -96,11 +97,9 @@ export default Component.extend({
return;
}
const textareaWrapper = document.querySelector(
".d-editor-textarea-wrapper"
);
const popperAnchor = this._getPopperAnchor();
if (!this.site.isMobileDevice && this.usePopper && textareaWrapper) {
if (!this.site.isMobileDevice && this.usePopper && popperAnchor) {
const modifiers = [
{
name: "preventOverflow",
@ -113,7 +112,10 @@ export default Component.extend({
},
];
if (window.innerWidth < textareaWrapper.clientWidth * 2) {
if (
this.placement === "auto" &&
window.innerWidth < popperAnchor.clientWidth * 2
) {
modifiers.push({
name: "computeStyles",
enabled: true,
@ -131,9 +133,8 @@ export default Component.extend({
});
}
this._popper = createPopper(textareaWrapper, emojiPicker, {
placement: "auto",
modifiers,
this._popper = createPopper(popperAnchor, emojiPicker, {
placement: this.placement,
});
}
@ -338,6 +339,15 @@ export default Component.extend({
);
},
_getPopperAnchor() {
// .d-editor-textarea-wrapper is only for backward compatibility here
// in new code use .emoji-picker-anchor
return (
document.querySelector(".emoji-picker-anchor") ??
document.querySelector(".d-editor-textarea-wrapper")
);
},
@bind
handleOutsideClick(event) {
const emojiPicker = document.querySelector(".emoji-picker");

View File

@ -0,0 +1,52 @@
import componentTest, {
setupRenderingTest,
} from "discourse/tests/helpers/component-test";
import { discourseModule, query } from "discourse/tests/helpers/qunit-helpers";
import hbs from "htmlbars-inline-precompile";
import { click } from "@ember/test-helpers";
discourseModule("Integration | Component | emoji-picker", function (hooks) {
setupRenderingTest(hooks);
componentTest("when placement == bottom, places the picker on the bottom", {
template: hbs`
{{d-button class="emoji-picker-anchor" action=showEmojiPicker}}
{{emoji-picker isActive=pickerIsActive placement="bottom"}}
`,
beforeEach() {
this.set("showEmojiPicker", () => {
this.set("pickerIsActive", true);
});
},
async test(assert) {
await click(".emoji-picker-anchor");
assert.equal(
query(".emoji-picker.opened").getAttribute("data-popper-placement"),
"bottom"
);
},
});
componentTest("when placement == right, places the picker on the right", {
template: hbs`
{{d-button class="emoji-picker-anchor" action=showEmojiPicker}}
{{emoji-picker isActive=pickerIsActive placement="right"}}
`,
beforeEach() {
this.set("showEmojiPicker", () => {
this.set("pickerIsActive", true);
});
},
async test(assert) {
await click(".emoji-picker-anchor");
assert.equal(
query(".emoji-picker.opened").getAttribute("data-popper-placement"),
"right"
);
},
});
});