@c4h/chuci 0.1.0 → 0.2.0

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.
@@ -1,77 +1,77 @@
1
- /**
2
- * Base class for Chuci web components without Lit dependency
3
- */
4
- export abstract class ChuciElement extends HTMLElement {
5
- private _shadowRoot: ShadowRoot
6
- private _connected = false
7
-
8
- constructor() {
9
- super()
10
- this._shadowRoot = this.attachShadow({ mode: 'open' })
11
- }
12
-
13
- connectedCallback() {
14
- if (!this._connected) {
15
- this._connected = true
16
- this.render()
17
- this.firstUpdated()
18
- } else {
19
- this.render()
20
- }
21
- }
22
-
23
- disconnectedCallback() {
24
- this._connected = false
25
- }
26
-
27
- attributeChangedCallback(_name: string, oldValue: string | null, newValue: string | null) {
28
- if (oldValue !== newValue) {
29
- this.render()
30
- }
31
- }
32
-
33
- protected firstUpdated(): void {
34
- // Override in subclasses for initialization logic
35
- }
36
-
37
- protected abstract render(): void
38
-
39
- // Template literal values can be any type that can be converted to string
40
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
- protected html(strings: TemplateStringsArray, ...values: any[]): string {
42
- return strings.reduce((result, str, i) => {
43
- return result + str + (values[i] || '')
44
- }, '')
45
- }
46
-
47
- // Template literal values can be any type that can be converted to string
48
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
- protected css(strings: TemplateStringsArray, ...values: any[]): string {
50
- const cssText = strings.reduce((result, str, i) => {
51
- return result + str + (values[i] || '')
52
- }, '')
53
- return `<style>${cssText}</style>`
54
- }
55
-
56
- protected updateShadowRoot(content: string) {
57
- this._shadowRoot.innerHTML = content
58
- }
59
-
60
- protected query<T extends HTMLElement>(selector: string): T | null {
61
- return this._shadowRoot.querySelector<T>(selector)
62
- }
63
-
64
- protected queryAll<T extends HTMLElement>(selector: string): NodeListOf<T> {
65
- return this._shadowRoot.querySelectorAll<T>(selector)
66
- }
67
-
68
- // Custom event detail can be any type
69
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
- protected dispatch(eventName: string, detail?: any) {
71
- this.dispatchEvent(new CustomEvent(eventName, {
72
- detail,
73
- bubbles: true,
74
- composed: true
75
- }))
76
- }
1
+ /**
2
+ * Base class for Chuci web components without Lit dependency
3
+ */
4
+ export abstract class ChuciElement extends HTMLElement {
5
+ private _shadowRoot: ShadowRoot
6
+ private _connected = false
7
+
8
+ constructor() {
9
+ super()
10
+ this._shadowRoot = this.attachShadow({ mode: 'open' })
11
+ }
12
+
13
+ connectedCallback() {
14
+ if (!this._connected) {
15
+ this._connected = true
16
+ this.render()
17
+ this.firstUpdated()
18
+ } else {
19
+ this.render()
20
+ }
21
+ }
22
+
23
+ disconnectedCallback() {
24
+ this._connected = false
25
+ }
26
+
27
+ attributeChangedCallback(_name: string, oldValue: string | null, newValue: string | null) {
28
+ if (oldValue !== newValue) {
29
+ this.render()
30
+ }
31
+ }
32
+
33
+ protected firstUpdated(): void {
34
+ // Override in subclasses for initialization logic
35
+ }
36
+
37
+ protected abstract render(): void
38
+
39
+ // Template literal values can be any type that can be converted to string
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ protected html(strings: TemplateStringsArray, ...values: any[]): string {
42
+ return strings.reduce((result, str, i) => {
43
+ return result + str + (values[i] || '')
44
+ }, '')
45
+ }
46
+
47
+ // Template literal values can be any type that can be converted to string
48
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
+ protected css(strings: TemplateStringsArray, ...values: any[]): string {
50
+ const cssText = strings.reduce((result, str, i) => {
51
+ return result + str + (values[i] || '')
52
+ }, '')
53
+ return `<style>${cssText}</style>`
54
+ }
55
+
56
+ protected updateShadowRoot(content: string) {
57
+ this._shadowRoot.innerHTML = content
58
+ }
59
+
60
+ protected query<T extends HTMLElement>(selector: string): T | null {
61
+ return this._shadowRoot.querySelector<T>(selector)
62
+ }
63
+
64
+ protected queryAll<T extends HTMLElement>(selector: string): NodeListOf<T> {
65
+ return this._shadowRoot.querySelectorAll<T>(selector)
66
+ }
67
+
68
+ // Custom event detail can be any type
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ protected dispatch(eventName: string, detail?: any) {
71
+ this.dispatchEvent(new CustomEvent(eventName, {
72
+ detail,
73
+ bubbles: true,
74
+ composed: true
75
+ }))
76
+ }
77
77
  }
Binary file