Skip to content

Commit be49235

Browse files
committed
fix(listen): ensure window events from Listen decorator are captured
1 parent 1213da3 commit be49235

5 files changed

Lines changed: 76 additions & 3 deletions

File tree

src/renderer/dom-api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const createDomApi = (App: AppGlobal, win: any, doc: Document): DomApi =>
131131
assignersUnregListeners[assignersEventName]();
132132
}
133133

134-
if (_BUILD_.event && typeof attachTo === 'string') {
134+
if ((_BUILD_.event || _BUILD_.listener) && typeof attachTo === 'string') {
135135
// attachTo is a string, and is probably something like
136136
// "parent", "window", or "document"
137137
// and the eventName would be like "mouseover" or "mousemove"
@@ -141,7 +141,7 @@ export const createDomApi = (App: AppGlobal, win: any, doc: Document): DomApi =>
141141
// we were passed in an actual element to attach to
142142
attachToElm = attachTo;
143143

144-
} else if (_BUILD_.event) {
144+
} else if (_BUILD_.event || _BUILD_.listener) {
145145
// depending on the event name, we could actually be attaching
146146
// this element to something like the document or window
147147
splt = eventName.split(':');
@@ -159,7 +159,7 @@ export const createDomApi = (App: AppGlobal, win: any, doc: Document): DomApi =>
159159
// somehow we're referencing an element that doesn't exist
160160
// let's not continue
161161

162-
if (_BUILD_.event) {
162+
if (_BUILD_.event || _BUILD_.listener) {
163163
// test to see if we're looking for an exact keycode
164164
splt = eventName.split('.');
165165

test/karma/test-app/components.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ export namespace Components {
204204
interface LifecycleUnloadRoot {}
205205
interface LifecycleUnloadRootAttributes extends StencilHTMLAttributes {}
206206

207+
interface ListenWindow {}
208+
interface ListenWindowAttributes extends StencilHTMLAttributes {}
209+
207210
interface NodeGlobals {}
208211
interface NodeGlobalsAttributes extends StencilHTMLAttributes {}
209212

@@ -412,6 +415,7 @@ declare global {
412415
'LifecycleUnloadA': Components.LifecycleUnloadA;
413416
'LifecycleUnloadB': Components.LifecycleUnloadB;
414417
'LifecycleUnloadRoot': Components.LifecycleUnloadRoot;
418+
'ListenWindow': Components.ListenWindow;
415419
'NodeGlobals': Components.NodeGlobals;
416420
'ReflectToAttr': Components.ReflectToAttr;
417421
'ScopedBasicRoot': Components.ScopedBasicRoot;
@@ -490,6 +494,7 @@ declare global {
490494
'lifecycle-unload-a': Components.LifecycleUnloadAAttributes;
491495
'lifecycle-unload-b': Components.LifecycleUnloadBAttributes;
492496
'lifecycle-unload-root': Components.LifecycleUnloadRootAttributes;
497+
'listen-window': Components.ListenWindowAttributes;
493498
'node-globals': Components.NodeGlobalsAttributes;
494499
'reflect-to-attr': Components.ReflectToAttrAttributes;
495500
'scoped-basic-root': Components.ScopedBasicRootAttributes;
@@ -743,6 +748,12 @@ declare global {
743748
new (): HTMLLifecycleUnloadRootElement;
744749
};
745750

751+
interface HTMLListenWindowElement extends Components.ListenWindow, HTMLStencilElement {}
752+
var HTMLListenWindowElement: {
753+
prototype: HTMLListenWindowElement;
754+
new (): HTMLListenWindowElement;
755+
};
756+
746757
interface HTMLNodeGlobalsElement extends Components.NodeGlobals, HTMLStencilElement {}
747758
var HTMLNodeGlobalsElement: {
748759
prototype: HTMLNodeGlobalsElement;
@@ -1019,6 +1030,7 @@ declare global {
10191030
'lifecycle-unload-a': HTMLLifecycleUnloadAElement
10201031
'lifecycle-unload-b': HTMLLifecycleUnloadBElement
10211032
'lifecycle-unload-root': HTMLLifecycleUnloadRootElement
1033+
'listen-window': HTMLListenWindowElement
10221034
'node-globals': HTMLNodeGlobalsElement
10231035
'reflect-to-attr': HTMLReflectToAttrElement
10241036
'scoped-basic-root': HTMLScopedBasicRootElement
@@ -1097,6 +1109,7 @@ declare global {
10971109
'lifecycle-unload-a': HTMLLifecycleUnloadAElement;
10981110
'lifecycle-unload-b': HTMLLifecycleUnloadBElement;
10991111
'lifecycle-unload-root': HTMLLifecycleUnloadRootElement;
1112+
'listen-window': HTMLListenWindowElement;
11001113
'node-globals': HTMLNodeGlobalsElement;
11011114
'reflect-to-attr': HTMLReflectToAttrElement;
11021115
'scoped-basic-root': HTMLScopedBasicRootElement;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Component, Listen, State } from '../../../../dist';
2+
3+
4+
@Component({
5+
tag: 'listen-window'
6+
})
7+
export class ListenWindow {
8+
@State() clicked = 0;
9+
@State() scrolled = 0;
10+
11+
@Listen('window:click')
12+
winClick() {
13+
this.clicked++;
14+
}
15+
16+
@Listen('window:scroll')
17+
winScroll() {
18+
this.scrolled++;
19+
}
20+
21+
render() {
22+
return (
23+
<div>
24+
<div id="clicked">Clicked: {this.clicked}</div>
25+
<div>Scrolled: {this.scrolled}</div>
26+
<button>Click!</button>
27+
<div style={{background: 'gray', paddingTop: '2000px'}}></div>
28+
</div>
29+
);
30+
}
31+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf8">
3+
<script src="/build/testapp.js"></script>
4+
5+
<listen-window></listen-window>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { setupDomTests, waitForChanges } from '../util';
2+
3+
describe('listen-window', () => {
4+
const { setupDom, tearDownDom } = setupDomTests(document);
5+
let app: HTMLElement;
6+
7+
beforeEach(async () => {
8+
app = await setupDom('/listen-window/index.html');
9+
});
10+
afterEach(tearDownDom);
11+
12+
it('window should receive click events', async () => {
13+
const clicked = app.querySelector('#clicked');
14+
expect(clicked.textContent.trim()).toBe('Clicked: 0');
15+
16+
const btn = app.querySelector('button');
17+
btn.click();
18+
19+
await waitForChanges();
20+
21+
expect(clicked.textContent.trim()).toBe('Clicked: 1');
22+
});
23+
24+
});

0 commit comments

Comments
 (0)