| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2024 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
import {LitElement, css, html} from 'lit'; |
| 8 |
import {classMap} from 'lit/directives/class-map.js'; |
| 9 |
import {property} from 'lit/decorators/property.js'; |
| 10 |
|
| 11 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 12 |
type Constructor<T> = new (...args: any[]) => T; |
| 13 |
|
| 14 |
export declare class HighlightableInterface { |
| 15 |
highlight: boolean; |
| 16 |
renderHighlight(content: unknown): unknown; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Hilightable mixin |
| 21 |
* @mixin |
| 22 |
*/ |
| 23 |
export const Highlightable = <T extends Constructor<LitElement>>( |
| 24 |
message: string, |
| 25 |
superClass: T |
| 26 |
) => { |
| 27 |
console.log(message); |
| 28 |
class HighlightableElement extends superClass { |
| 29 |
// Adds some styles... |
| 30 |
static styles = [ |
| 31 |
(superClass as unknown as typeof LitElement).styles ?? [], |
| 32 |
css` |
| 33 |
.highlight { |
| 34 |
background: yellow; |
| 35 |
} |
| 36 |
`, |
| 37 |
]; |
| 38 |
|
| 39 |
@property({type: Boolean}) highlight = false; |
| 40 |
|
| 41 |
renderHighlight(content: unknown) { |
| 42 |
return html` <div class=${classMap({highlight: this.highlight})}> |
| 43 |
${content} |
| 44 |
</div>`; |
| 45 |
} |
| 46 |
} |
| 47 |
return HighlightableElement as Constructor<HighlightableInterface> & T; |
| 48 |
}; |
| 49 |
|
| 50 |
export declare class IA { |
| 51 |
a: string; |
| 52 |
} |
| 53 |
|
| 54 |
export declare class IB { |
| 55 |
b: number; |
| 56 |
} |
| 57 |
|
| 58 |
export declare class IC { |
| 59 |
c: boolean; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Mixin A - const assignment to arrow function |
| 64 |
* @mixin |
| 65 |
*/ |
| 66 |
export const A = <T extends Constructor<LitElement>>( |
| 67 |
x: string, |
| 68 |
superClass: T, |
| 69 |
z: Constructor<{z: string}> |
| 70 |
) => { |
| 71 |
console.log(x, z); |
| 72 |
class A extends superClass { |
| 73 |
private pa = true; |
| 74 |
@property() |
| 75 |
a = 'hi'; |
| 76 |
} |
| 77 |
return A as Constructor<IA> & T; |
| 78 |
}; |
| 79 |
|
| 80 |
/** |
| 81 |
* Mixin B - function declaration |
| 82 |
* @mixin |
| 83 |
*/ |
| 84 |
export function B<T extends Constructor<LitElement>>( |
| 85 |
s: string, |
| 86 |
y: Constructor<{z: string}>, |
| 87 |
superClass: T |
| 88 |
) { |
| 89 |
console.log(s, y); |
| 90 |
class B extends superClass { |
| 91 |
private pb = true; |
| 92 |
@property({type: Number}) |
| 93 |
b = 5; |
| 94 |
} |
| 95 |
return B as Constructor<IB> & T; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Mixin C - const assignment to function expression |
| 100 |
* @mixin |
| 101 |
*/ |
| 102 |
export const C = function C<T extends Constructor<LitElement>>(superClass: T) { |
| 103 |
class C extends superClass { |
| 104 |
private pc = true; |
| 105 |
@property({type: Boolean}) |
| 106 |
c = false; |
| 107 |
} |
| 108 |
return C as Constructor<IC> & T; |
| 109 |
}; |
| 110 |
|
| 111 |
/** |
| 112 |
* Mixin with sub-mixin with multiple params |
| 113 |
* @mixin |
| 114 |
*/ |
| 115 |
export const ChildWithMultipleParams = function ChildWithMultipleParams< |
| 116 |
T extends Constructor<LitElement>, |
| 117 |
>(superClass: T, secondArg: string) { |
| 118 |
class ChildWithMultipleParams extends Highlightable(secondArg, superClass) {} |
| 119 |
return ChildWithMultipleParams; |
| 120 |
}; |