@a11d/lit 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -177
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,180 +1,8 @@
|
|
|
1
1
|
# Lit
|
|
2
2
|
|
|
3
|
-
A thin wrapper around the Lit library consisting of some additional features:
|
|
3
|
+
A thin wrapper around the [Lit](https://github.com/lit/lit) library consisting of some additional features:
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
import { component, Component, html, state, updated } from '@a11d/lit'
|
|
11
|
-
|
|
12
|
-
@component('lit-data')
|
|
13
|
-
class Data extends Component {
|
|
14
|
-
@updated(async function(this: Data) {
|
|
15
|
-
await this.fetch()
|
|
16
|
-
})
|
|
17
|
-
@state() code?: string
|
|
18
|
-
|
|
19
|
-
private fetch() {
|
|
20
|
-
if (this.code) {
|
|
21
|
-
this.data = await fetch(`https://api.example.com/${this.code}`)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get template() {
|
|
26
|
-
return html`
|
|
27
|
-
<input placeholder='code' .value=${this.count} @change=${(e: Event) => this.count = Number((e.target as HTMLInputElement).value)} />
|
|
28
|
-
${this.data.map(item => html`<div>${item}</div>`)}
|
|
29
|
-
`;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
Additionally the `state` and `property` decorators have been upgraded to allow for an `updated` callback directly an a property. The below example is equivalent to the above example.
|
|
35
|
-
|
|
36
|
-
```ts
|
|
37
|
-
import { component, Component, html, state, updated } from '@a11d/lit'
|
|
38
|
-
|
|
39
|
-
@component('lit-data')
|
|
40
|
-
class Data extends Component {
|
|
41
|
-
@state({
|
|
42
|
-
async updated(this: Data) {
|
|
43
|
-
await this.fetch()
|
|
44
|
-
}
|
|
45
|
-
}) code?: string
|
|
46
|
-
|
|
47
|
-
private fetch() {
|
|
48
|
-
if (this.code) {
|
|
49
|
-
this.data = await fetch(`https://api.example.com/${this.code}`)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get template() {
|
|
54
|
-
return html`
|
|
55
|
-
<input placeholder='code' .value=${this.count} @change=${(e: Event) => this.count = Number((e.target as HTMLInputElement).value)} />
|
|
56
|
-
${this.data.map(item => html`<div>${item}</div>`)}
|
|
57
|
-
`;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
# `event` Decorator
|
|
63
|
-
|
|
64
|
-
The `event` decorator allows you to make a class field an event dispatcher. This is useful for dispatching custom events on your components.
|
|
65
|
-
|
|
66
|
-
```ts
|
|
67
|
-
import { component, Component, html, event } from '@a11d/lit'
|
|
68
|
-
|
|
69
|
-
@component('lit-event')
|
|
70
|
-
class Event extends Component {
|
|
71
|
-
@event() readonly delete!: EventDispatcher<'single' | 'all'>
|
|
72
|
-
|
|
73
|
-
get template() {
|
|
74
|
-
return html`
|
|
75
|
-
<button @click=${() => this.delete.dispatch('single')}>Delete Single</button>
|
|
76
|
-
<button @click=${() => this.delete.dispatch('all')}>Delete All</button>
|
|
77
|
-
`;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
The `event` decorator will convert the field into a getter that returns an instance of either `HTMLEventDispatcher<T>` or `PureEventDispatcher<T>` based on the context it's used in. Both of which implement the `EventDispatcher<T>` interface.
|
|
83
|
-
|
|
84
|
-
## Options
|
|
85
|
-
|
|
86
|
-
The `event` decorator accepts an options object of type `EventInit` as its first argument. The following options are available:
|
|
87
|
-
- `bubbles` - A boolean value indicating whether the event bubbles. The default is false.
|
|
88
|
-
- `cancelable` - A boolean value indicating whether the event can be cancelled. The default is false.
|
|
89
|
-
- `composed` - A boolean value indicating whether the event will trigger listeners outside of a shadow root. The default is false.
|
|
90
|
-
|
|
91
|
-
```ts
|
|
92
|
-
import { component, Component, html, event } from '@a11d/lit'
|
|
93
|
-
|
|
94
|
-
@component('lit-event-bubbles')
|
|
95
|
-
class EventBubbles extends Component {
|
|
96
|
-
@event({ bubbles: true, composed: true }) readonly delete!: EventDispatcher<'single' | 'all'>
|
|
97
|
-
|
|
98
|
-
get template() {
|
|
99
|
-
return html`
|
|
100
|
-
<button @click=${() => this.delete.dispatch('single')}>Delete Single</button>
|
|
101
|
-
<button @click=${() => this.delete.dispatch('all')}>Delete All</button>
|
|
102
|
-
`;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
# `eventListener` Decorator
|
|
108
|
-
|
|
109
|
-
The `eventListener` decorator allows you to make a method to be an event listener.
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
import { component, Component, html, eventListener } from '@a11d/lit'
|
|
113
|
-
|
|
114
|
-
@component('lit-event-listener')
|
|
115
|
-
class EventListener extends Component {
|
|
116
|
-
@eventListener('delete')
|
|
117
|
-
private handleClick(e: CustomEvent<'single' | 'all'>) {
|
|
118
|
-
// This should have been (hopefully!) dispatched by the `lit-event-bubbles` component
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
get template() {
|
|
122
|
-
return html`
|
|
123
|
-
<lit-event-bubbles></lit-event-bubbles>
|
|
124
|
-
<lit-event-bubbles></lit-event-bubbles>
|
|
125
|
-
<lit-event-bubbles></lit-event-bubbles>
|
|
126
|
-
`;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
Providing a shorthand parameter of type `string` will automatically add an event listener to the root component. This can be customized by providing an object with the following properties:
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
```ts
|
|
135
|
-
import { component, Component, html, eventListener } from '@a11d/lit'
|
|
136
|
-
|
|
137
|
-
@component('lit-event-listener')
|
|
138
|
-
class EventListener extends Component {
|
|
139
|
-
@eventListener({
|
|
140
|
-
type: 'delete',
|
|
141
|
-
async target(this: EventListener) {
|
|
142
|
-
await this.updateComplete
|
|
143
|
-
return this.renderRoot.querySelectorAll('lit-event')
|
|
144
|
-
},
|
|
145
|
-
})
|
|
146
|
-
private handleDelete(e: CustomEvent<'single' | 'all'>) {
|
|
147
|
-
// Now we know for sure that this event was dispatched by a `lit-event` components belonging to this component
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
@eventListener({ type: 'keydown', target: document })
|
|
151
|
-
private handleDocumentClick(e: PointerEvent) {
|
|
152
|
-
// Demonstration of listening to events on the document
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
get template() {
|
|
156
|
-
return html`
|
|
157
|
-
<lit-event></lit-event>
|
|
158
|
-
<lit-event></lit-event>
|
|
159
|
-
<lit-event></lit-event>
|
|
160
|
-
`;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
# `style` Directive
|
|
166
|
-
|
|
167
|
-
The `style` directive allows you to define styles for the underlying element.
|
|
168
|
-
|
|
169
|
-
```ts
|
|
170
|
-
import { component, Component, html, style } from '@a11d/lit'
|
|
171
|
-
|
|
172
|
-
@component('lit-style')
|
|
173
|
-
class Style extends Component {
|
|
174
|
-
get template() {
|
|
175
|
-
return html`
|
|
176
|
-
<div ${style({ color: 'red' })}>Hello World</div>
|
|
177
|
-
`;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
```
|
|
5
|
+
- ### [`updated` Decorator (along with upgraded `state` and `property` decorators)](./src/updated/README.md)
|
|
6
|
+
- ### [`event` Decorator](./src/event/README.md)
|
|
7
|
+
- ### [`eventListener` Decorator](./src/eventListener/README.md)
|
|
8
|
+
- ### [`style` Directive](./src/style/README.md)
|