|
| 1 | +import { |
| 2 | + Component, |
| 3 | + DestroyRef, |
| 4 | + ElementRef, |
| 5 | + afterNextRender, |
| 6 | + inject, |
| 7 | + input, |
| 8 | + type Type, |
| 9 | +} from '@angular/core' |
| 10 | + |
| 11 | +export interface DevtoolsPanelProps { |
| 12 | + theme?: 'dark' | 'light' | 'system' |
| 13 | +} |
| 14 | + |
| 15 | +export function createAngularPanel< |
| 16 | + TComponentProps extends DevtoolsPanelProps, |
| 17 | + TCoreDevtoolsClass extends { |
| 18 | + mount: (el: HTMLElement, theme?: DevtoolsPanelProps['theme']) => void |
| 19 | + unmount: () => void |
| 20 | + }, |
| 21 | +>(CoreClass: new (props: TComponentProps) => TCoreDevtoolsClass): [Type<any>, Type<any>] { |
| 22 | + @Component({ |
| 23 | + selector: 'devtools-panel', |
| 24 | + standalone: true, |
| 25 | + template: '<div #panelHost style="height: 100%"></div>', |
| 26 | + }) |
| 27 | + class Panel { |
| 28 | + theme = input<DevtoolsPanelProps['theme']>() |
| 29 | + devtoolsProps = input<TComponentProps>() |
| 30 | + |
| 31 | + private hostRef = inject(ElementRef) |
| 32 | + private destroyRef = inject(DestroyRef) |
| 33 | + private devtools: TCoreDevtoolsClass | null = null |
| 34 | + |
| 35 | + constructor() { |
| 36 | + afterNextRender(() => { |
| 37 | + const el = this.hostRef.nativeElement.querySelector('div') |
| 38 | + if (!el) return |
| 39 | + |
| 40 | + const instance = new CoreClass( |
| 41 | + this.devtoolsProps() as TComponentProps, |
| 42 | + ) |
| 43 | + this.devtools = instance |
| 44 | + instance.mount(el, this.theme()) |
| 45 | + }) |
| 46 | + |
| 47 | + this.destroyRef.onDestroy(() => { |
| 48 | + this.devtools?.unmount() |
| 49 | + this.devtools = null |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Component({ |
| 55 | + selector: 'devtools-noop-panel', |
| 56 | + standalone: true, |
| 57 | + template: '', |
| 58 | + }) |
| 59 | + class NoOpPanel { |
| 60 | + theme = input<DevtoolsPanelProps['theme']>() |
| 61 | + devtoolsProps = input<TComponentProps>() |
| 62 | + } |
| 63 | + |
| 64 | + return [Panel, NoOpPanel] |
| 65 | +} |
0 commit comments