From b8bf8b8c41b19e82207af841029a362f6a7678f3 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Wed, 6 Apr 2022 13:14:56 +0100 Subject: [PATCH] fix rootElement when running in /tests --- addon/components/dialog.ts | 24 +++++++-------------- index.js | 12 ----------- package.json | 1 - tests/integration/components/dialog-test.js | 3 +-- 4 files changed, 9 insertions(+), 31 deletions(-) diff --git a/addon/components/dialog.ts b/addon/components/dialog.ts index 8bf7684..64f4dde 100644 --- a/addon/components/dialog.ts +++ b/addon/components/dialog.ts @@ -3,27 +3,13 @@ import { action } from '@ember/object'; import { guidFor } from '@ember/object/internals'; import { inject as service } from '@ember/service'; import { typeOf } from '@ember/utils'; +import { getOwner } from '@ember/application'; -import { getOwnConfig } from '@embroider/macros'; import { Keys } from 'ember-headlessui/utils/keyboard'; import { modifier } from 'ember-modifier'; import type DialogStackProvider from 'ember-headlessui/services/dialog-stack-provider'; -/** - * Expose the element that the `Dialog` should be "slotted" into - * - * This is exported _only_ for testing purposes; do not consider this API to be public - * - * @private - */ -export function getPortalRoot() { - const { rootElement } = getOwnConfig(); - - // If we looked up a `rootElement` config at build-time, use that; otherwise use the body - return rootElement ? document.querySelector(rootElement) : document.body; -} - interface Args { isOpen: boolean; onClose: () => void; @@ -36,7 +22,7 @@ export default class DialogComponent extends Component { DEFAULT_TAG_NAME = 'div'; guid = `${guidFor(this)}-headlessui-dialog`; - $portalRoot = getPortalRoot(); + $portalRoot: HTMLElement; outsideClickedElement: HTMLElement | null = null; handleEscapeKey = modifier( @@ -85,6 +71,12 @@ export default class DialogComponent extends Component { constructor(owner: unknown, args: Args) { super(owner, args); + const { + APP: { rootElement }, + } = getOwner(this).resolveRegistration('config:environment') + + this.$portalRoot = rootElement ? document.querySelector(rootElement) : document.body; + let { isOpen, onClose } = this.args; if (isOpen === undefined && onClose === undefined) { diff --git a/index.js b/index.js index 5de90c9..0ca063d 100644 --- a/index.js +++ b/index.js @@ -2,16 +2,4 @@ module.exports = { name: require('./package').name, - - options: { - '@embroider/macros': { - setOwnConfig: {}, - }, - }, - - config(_emberEnv, config) { - // Pass `rootElement` config into a location where it can be looked up at run-time - this.options['@embroider/macros'].setOwnConfig.rootElement = - config.APP.rootElement; - }, }; diff --git a/package.json b/package.json index 0a9eef3..ad7132a 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ }, "dependencies": { "@ember/render-modifiers": "^2.0.0", - "@embroider/macros": "^1.2.0", "ember-auto-import": "^2.2.0", "ember-cli-babel": "^7.26.3", "ember-cli-htmlbars": "^6.0.1", diff --git a/tests/integration/components/dialog-test.js b/tests/integration/components/dialog-test.js index bab848c..b9e33a2 100644 --- a/tests/integration/components/dialog-test.js +++ b/tests/integration/components/dialog-test.js @@ -9,7 +9,6 @@ import { module, test, todo } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import userEvent from '@testing-library/user-event'; -import { getPortalRoot } from 'ember-headlessui/components/dialog'; import { Keys } from 'ember-headlessui/utils/keyboard'; import { @@ -235,7 +234,7 @@ module('Integration | Component | ', function (hooks) { ); test('it should add a scroll lock to the html tag', async function (assert) { - const portalRoot = getPortalRoot(); + const portalRoot = document.querySelector('#ember-testing'); this.set('isOpen', false); await render(hbs`