Skip to content

Commit f3dd00d

Browse files
committed
fix rootElement when running in /tests
1 parent 075fd5b commit f3dd00d

File tree

4 files changed

+11
-31
lines changed

4 files changed

+11
-31
lines changed

addon/components/dialog.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import Component from '@glimmer/component';
2+
import { getOwner } from '@ember/application';
23
import { action } from '@ember/object';
34
import { guidFor } from '@ember/object/internals';
45
import { inject as service } from '@ember/service';
56
import { typeOf } from '@ember/utils';
67

7-
import { getOwnConfig } from '@embroider/macros';
88
import { Keys } from 'ember-headlessui/utils/keyboard';
99
import { modifier } from 'ember-modifier';
1010

1111
import type DialogStackProvider from 'ember-headlessui/services/dialog-stack-provider';
1212

13-
/**
14-
* Expose the element that the `Dialog` should be "slotted" into
15-
*
16-
* This is exported _only_ for testing purposes; do not consider this API to be public
17-
*
18-
* @private
19-
*/
20-
export function getPortalRoot() {
21-
const { rootElement } = getOwnConfig();
22-
23-
// If we looked up a `rootElement` config at build-time, use that; otherwise use the body
24-
return rootElement ? document.querySelector(rootElement) : document.body;
25-
}
26-
2713
interface Args {
2814
isOpen: boolean;
2915
onClose: () => void;
@@ -36,7 +22,7 @@ export default class DialogComponent extends Component<Args> {
3622
DEFAULT_TAG_NAME = 'div';
3723

3824
guid = `${guidFor(this)}-headlessui-dialog`;
39-
$portalRoot = getPortalRoot();
25+
$portalRoot: HTMLElement;
4026
outsideClickedElement: HTMLElement | null = null;
4127

4228
handleEscapeKey = modifier(
@@ -85,6 +71,14 @@ export default class DialogComponent extends Component<Args> {
8571
constructor(owner: unknown, args: Args) {
8672
super(owner, args);
8773

74+
const {
75+
APP: { rootElement },
76+
} = getOwner(this).resolveRegistration('config:environment');
77+
78+
this.$portalRoot = rootElement
79+
? document.querySelector(rootElement)
80+
: document.body;
81+
8882
let { isOpen, onClose } = this.args;
8983

9084
if (isOpen === undefined && onClose === undefined) {

index.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,4 @@
22

33
module.exports = {
44
name: require('./package').name,
5-
6-
options: {
7-
'@embroider/macros': {
8-
setOwnConfig: {},
9-
},
10-
},
11-
12-
config(_emberEnv, config) {
13-
// Pass `rootElement` config into a location where it can be looked up at run-time
14-
this.options['@embroider/macros'].setOwnConfig.rootElement =
15-
config.APP.rootElement;
16-
},
175
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
},
3737
"dependencies": {
3838
"@ember/render-modifiers": "^2.0.0",
39-
"@embroider/macros": "^1.2.0",
4039
"ember-auto-import": "^2.2.0",
4140
"ember-cli-babel": "^7.26.3",
4241
"ember-cli-htmlbars": "^6.0.1",

tests/integration/components/dialog-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { module, test, todo } from 'qunit';
99
import { setupRenderingTest } from 'ember-qunit';
1010

1111
import userEvent from '@testing-library/user-event';
12-
import { getPortalRoot } from 'ember-headlessui/components/dialog';
1312
import { Keys } from 'ember-headlessui/utils/keyboard';
1413

1514
import {
@@ -235,7 +234,7 @@ module('Integration | Component | <Dialog>', function (hooks) {
235234
);
236235

237236
test('it should add a scroll lock to the html tag', async function (assert) {
238-
const portalRoot = getPortalRoot();
237+
const portalRoot = document.querySelector('#ember-testing');
239238
this.set('isOpen', false);
240239

241240
await render(hbs`

0 commit comments

Comments
 (0)