Skip to content

Commit 91e81f1

Browse files
committed
readme + event test improvements
1 parent 22c1ae4 commit 91e81f1

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,42 @@ window.customElements.define('my-element', CustomElement)
2525

2626
### Props
2727

28+
- All `props` declared in the Vue component are exposed on the custom element as its properties. Kebab-case props are converted to camelCase properties, similar to how they are converted in Vue.
29+
30+
- Setting properties on the custom element updates the props passed to the inner Vue component.
31+
32+
- Setting attributes on the custom element updates corresponding declared props. Attributes are mapped to kebab-case. For example, a prop named `someProp` will have a corresponding attribute named `some-prop`.
33+
34+
- Attributes that map to props declared with `type: Boolean` are auto-casted into boolean values in the following rules:
35+
36+
- `""` or same value as attribute name: -> `true`
37+
38+
- `"true"` -> `true`
39+
40+
- `"false"` -> `false`
41+
42+
- Attributes that map to props declared with `type: Number` are auto-casted into numbers if the value is a parsable number.
43+
2844
### Events
2945

46+
Custom events emitted on the inner Vue component are dispatched on the custom element as a `CustomEvent`. Additional arguments passed to `$emit` will be exposed as an Array as `event.detail`.
47+
3048
### Slots
3149

50+
Slots work the same way as expected, including named slots. They also update when changed (using `MutationObserver`).
51+
52+
Scoped slots however, are not supported as they are a Vue specific concept.
53+
3254
### Lifecycle
3355

56+
When the custom element is removed from the document, the Vue component behaves just as if it's inside a `<keep-alive>` and its `deactivated` hook will be called. When it's inserted again, the `activated` hook will be called.
57+
58+
If you wish to destroy the inner component, you'd have to do that explicitly:
59+
60+
``` js
61+
myElement.vueComponent.$destroy()
62+
```
63+
3464
## Acknowledgments
3565

3666
Special thanks to the prior work by @karol-f in [vue-custom-element](https://github.com/karol-f/vue-custom-element).

test/fixtures/events.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
</div>`
88
}))
99
window.el = document.querySelector('my-element')
10-
el.addEventListener('foo', () => {
10+
el.addEventListener('foo', e => {
1111
window.emitted = true
12+
window.emittedDetail = e.detail
1213
})
1314
console.log('ready')
1415
})

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ test('events', async () => {
9595
el._shadowRoot.querySelector('button').click()
9696
})
9797
expect(await page.evaluate(() => window.emitted)).toBe(true)
98+
expect(await page.evaluate(() => window.emittedDetail)).toEqual([123])
9899
})
99100

100101
test('slots', async () => {

0 commit comments

Comments
 (0)