Skip to content

Commit 09c552d

Browse files
committed
Brought over webcomponent from solid-ui
will see about exporting it from solid-ui later
1 parent cac3f76 commit 09c552d

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/primitives/WebComponent.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { html, LitElement } from 'lit'
2+
// copied from solid-ui for now so I can follow same structure as other webcomponets
3+
// i think we need to export this, but want to talk to team
4+
export default abstract class WebComponent extends LitElement {
5+
static states?: Record<string, Function>
6+
7+
protected internals?: ElementInternals
8+
9+
protected willUpdate (changedProperties: Map<string, any>) {
10+
super.willUpdate(changedProperties)
11+
12+
const states = this.static().states
13+
14+
if (!states) {
15+
return
16+
}
17+
18+
const internals = this.getInternals()
19+
20+
for (const [state, condition] of Object.entries(states)) {
21+
const matches = condition(this)
22+
23+
if (matches && !internals.states.has(state)) {
24+
internals.states.add(state)
25+
} else if (!matches && internals.states.has(state)) {
26+
internals.states.delete(state)
27+
}
28+
}
29+
}
30+
31+
protected render () {
32+
return html`<slot></slot>`
33+
}
34+
35+
protected getInternals (): ElementInternals {
36+
this.internals ??= this.attachInternals()
37+
38+
return this.internals
39+
}
40+
41+
private static (): typeof WebComponent {
42+
return this.constructor as typeof WebComponent
43+
}
44+
}

0 commit comments

Comments
 (0)