Skip to content

Commit 38ff738

Browse files
committed
Add a changeset for event-target
1 parent 56cf52e commit 38ff738

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

.changeset/stupid-beds-draw.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
"corset": minor
3+
---
4+
5+
Allow specifying an alternative event target with event-target
6+
7+
This now allows specifying an alternative event target via the new `event-target` property. You can use it like this:
8+
9+
```js
10+
import sheet, { mount } from 'https://cdn.corset.dev/v2';
11+
12+
mount(document, class {
13+
onpopstate(ev) {
14+
console.log(`location: ${document.location}, state: ${JSON.stringify(event.state)}`);
15+
}
16+
bind() {
17+
return sheet`
18+
#app {
19+
event: popstate ${this.onpopstate};
20+
event-target: popstate ${window};
21+
}
22+
`;
23+
}
24+
});
25+
```
26+
27+
In the above we are able to use a selector to listen to the [popstate](https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event) event that is on the window object.
28+
29+
This can also be used with any object that follows the [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) interface:
30+
31+
```js
32+
import sheet, { mount } from 'https://cdn.corset.dev/v2';
33+
34+
let target = new EventTarget();
35+
// Now this can be shared around
36+
37+
mount(document, class {
38+
bind() {
39+
return sheet`
40+
#app {
41+
event: foo ${() => console.log('foo event occurred!')};
42+
event-target: foo ${target};
43+
}
44+
45+
some-custom-element {
46+
prop: events ${target};
47+
}
48+
`;
49+
}
50+
});
51+
```

0 commit comments

Comments
 (0)