Skip to content

Commit 5669a1e

Browse files
committed
refactor: update calls to afterRender with an explicit phase to the new API
1 parent 9c8c0fb commit 5669a1e

File tree

4 files changed

+72
-66
lines changed

4 files changed

+72
-66
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#### `5.2.5`
66

77
- chore(dependencies): update to Angular 18.1
8+
- refactor: update calls to `afterRender` with an explicit phase to the new API
89

910
---
1011

Diff for: projects/coreui-angular-chartjs/src/lib/chartjs.component.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
afterRender,
3-
AfterRenderPhase,
43
AfterViewInit,
54
booleanAttribute,
65
ChangeDetectionStrategy,
@@ -41,7 +40,6 @@ let nextId = 0;
4140
// host: { ngSkipHydration: 'true' }
4241
})
4342
export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
44-
4543
/**
4644
* Enables custom html based tooltips instead of standard tooltips.
4745
* @type boolean
@@ -65,7 +63,8 @@ export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
6563
* @default 150
6664
*/
6765
@HostBinding('style.height.px')
68-
@Input({ transform: (value: string | number) => numberAttribute(value, undefined) }) height?: number;
66+
@Input({ transform: (value: string | number) => numberAttribute(value, undefined) })
67+
height?: number;
6968

7069
/**
7170
* ID attribute applied to the rendered canvas.
@@ -102,7 +101,8 @@ export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
102101
* @default 300
103102
*/
104103
@HostBinding('style.width.px')
105-
@Input({ transform: (value: string | number) => numberAttribute(value, undefined) }) width?: number;
104+
@Input({ transform: (value: string | number) => numberAttribute(value, undefined) })
105+
width?: number;
106106

107107
/**
108108
* Put the chart into the wrapper div element.
@@ -134,10 +134,12 @@ export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
134134
private readonly changeDetectorRef: ChangeDetectorRef
135135
) {
136136
// todo: verify afterRender / afterNextRender for chartjs (spec fails with 17.0.10)
137-
afterRender(() => {
138-
this.ctx = this.canvasElement?.nativeElement?.getContext('2d');
139-
this.chartRender();
140-
}, { phase: AfterRenderPhase.Write });
137+
afterRender({
138+
write: () => {
139+
this.ctx = this.canvasElement?.nativeElement?.getContext('2d');
140+
this.chartRender();
141+
}
142+
});
141143
}
142144

143145
ngAfterViewInit(): void {
@@ -159,13 +161,28 @@ export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
159161
return;
160162
}
161163

162-
const datasetAtEvent: InteractionItem[] = this.chart.getElementsAtEventForMode($event, 'dataset', { intersect: true }, false);
164+
const datasetAtEvent: InteractionItem[] = this.chart.getElementsAtEventForMode(
165+
$event,
166+
'dataset',
167+
{ intersect: true },
168+
false
169+
);
163170
this.getDatasetAtEvent.emit(datasetAtEvent);
164171

165-
const elementAtEvent: InteractionItem[] = this.chart.getElementsAtEventForMode($event, 'nearest', { intersect: true }, false);
172+
const elementAtEvent: InteractionItem[] = this.chart.getElementsAtEventForMode(
173+
$event,
174+
'nearest',
175+
{ intersect: true },
176+
false
177+
);
166178
this.getElementAtEvent.emit(elementAtEvent);
167179

168-
const elementsAtEvent: InteractionItem[] = this.chart.getElementsAtEventForMode($event, 'index', { intersect: true }, false);
180+
const elementsAtEvent: InteractionItem[] = this.chart.getElementsAtEventForMode(
181+
$event,
182+
'index',
183+
{ intersect: true },
184+
false
185+
);
169186
this.getElementsAtEvent.emit(elementsAtEvent);
170187
}
171188

@@ -278,5 +295,5 @@ export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
278295
}
279296
});
280297
}
281-
};
298+
}
282299
}

Diff for: projects/coreui-angular/src/lib/services/color-mode.service.ts

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import { DOCUMENT } from '@angular/common';
2-
import {
3-
afterNextRender,
4-
AfterRenderPhase,
5-
DestroyRef,
6-
effect,
7-
inject,
8-
Injectable,
9-
signal,
10-
WritableSignal
11-
} from '@angular/core';
2+
import { afterNextRender, DestroyRef, effect, inject, Injectable, signal, WritableSignal } from '@angular/core';
123
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
134
import { tap } from 'rxjs/operators';
145
import { LocalStorageService } from './local-storage.service';
@@ -19,7 +10,6 @@ export type ColorMode = 'light' | 'dark' | 'auto' | string | undefined;
1910
providedIn: 'root'
2011
})
2112
export class ColorModeService {
22-
2313
readonly #destroyRef: DestroyRef = inject(DestroyRef);
2414
readonly #document: Document = inject(DOCUMENT);
2515
readonly #localStorage: LocalStorageService = inject(LocalStorageService);
@@ -39,16 +29,18 @@ export class ColorModeService {
3929
});
4030

4131
constructor() {
42-
afterNextRender(() => {
43-
this.localStorageItemName$
44-
.pipe(
45-
tap(params => {
46-
this.colorMode.set(this.getDefaultScheme(params));
47-
}),
48-
takeUntilDestroyed(this.#destroyRef)
49-
)
50-
.subscribe();
51-
}, { phase: AfterRenderPhase.Read });
32+
afterNextRender({
33+
read: () => {
34+
this.localStorageItemName$
35+
.pipe(
36+
tap((params) => {
37+
this.colorMode.set(this.getDefaultScheme(params));
38+
}),
39+
takeUntilDestroyed(this.#destroyRef)
40+
)
41+
.subscribe();
42+
}
43+
});
5244
}
5345

5446
getStoredTheme(localStorageItemName: string) {
@@ -71,23 +63,25 @@ export class ColorModeService {
7163
const storedTheme = localStorageItemName && this.getStoredTheme(localStorageItemName);
7264

7365
return storedTheme ?? this.getDatasetTheme();
74-
};
66+
}
7567

7668
getPrefersColorScheme() {
77-
return this.#document.defaultView?.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' :
78-
this.#document.defaultView?.matchMedia('(prefers-color-scheme: light)').matches ? 'light' :
79-
undefined;
69+
return this.#document.defaultView?.matchMedia('(prefers-color-scheme: dark)').matches
70+
? 'dark'
71+
: this.#document.defaultView?.matchMedia('(prefers-color-scheme: light)').matches
72+
? 'light'
73+
: undefined;
8074
}
8175

8276
getDatasetTheme(): ColorMode {
83-
return <ColorMode>(this.#document.documentElement.dataset['coreuiTheme']);
77+
return <ColorMode>this.#document.documentElement.dataset['coreuiTheme'];
8478
}
8579

8680
#setTheme(colorMode: ColorMode) {
87-
this.#document.documentElement.dataset['coreuiTheme'] = (colorMode === 'auto' ? this.getPrefersColorScheme() : colorMode);
81+
this.#document.documentElement.dataset['coreuiTheme'] =
82+
colorMode === 'auto' ? this.getPrefersColorScheme() : colorMode;
8883

8984
const event = new Event(this.eventName());
9085
this.#document.documentElement.dispatchEvent(event);
91-
};
92-
86+
}
9387
}

Diff for: projects/coreui-icons-angular/src/lib/icon/icon.directive.ts

+19-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
import {
2-
afterNextRender,
3-
AfterRenderPhase,
4-
computed,
5-
Directive,
6-
ElementRef,
7-
HostBinding,
8-
inject,
9-
Input,
10-
signal
11-
} from '@angular/core';
1+
import { afterNextRender, computed, Directive, ElementRef, HostBinding, inject, Input, signal } from '@angular/core';
122
import { DomSanitizer } from '@angular/platform-browser';
133

144
import { IconSetService } from '../icon-set';
@@ -21,21 +11,22 @@ import { transformName } from './icon.utils';
2111
standalone: true
2212
})
2313
export class IconDirective implements IIcon {
24-
2514
readonly #elementRef = inject(ElementRef);
2615
readonly #sanitizer = inject(DomSanitizer);
2716
readonly #iconSet = inject(IconSetService);
2817

2918
constructor() {
30-
afterNextRender(() => {
31-
this.#elementRef.nativeElement.innerHTML = this.innerHtml();
32-
}, { phase: AfterRenderPhase.Write });
19+
afterNextRender({
20+
write: () => {
21+
this.#elementRef.nativeElement.innerHTML = this.innerHtml();
22+
}
23+
});
3324
}
3425

3526
@Input('cIcon')
3627
set content(value: string | string[] | any[]) {
3728
this.#content.set(value);
38-
};
29+
}
3930

4031
readonly #content = signal<string | string[] | any[]>('');
4132

@@ -48,7 +39,7 @@ export class IconDirective implements IIcon {
4839
@Input({ transform: transformName })
4940
set name(value: string) {
5041
this.#name.set(value);
51-
};
42+
}
5243

5344
get name() {
5445
return this.#name();
@@ -71,13 +62,16 @@ export class IconDirective implements IIcon {
7162
@HostBinding('attr.aria-hidden') ariaHidden = true;
7263

7364
@HostBinding('attr.xmlns')
74-
@Input() xmlns = 'http://www.w3.org/2000/svg';
65+
@Input()
66+
xmlns = 'http://www.w3.org/2000/svg';
7567

7668
@HostBinding('attr.pointer-events')
77-
@Input('pointer-events') pointerEvents = 'none';
69+
@Input('pointer-events')
70+
pointerEvents = 'none';
7871

7972
@HostBinding('attr.role')
80-
@Input() role = 'img';
73+
@Input()
74+
role = 'img';
8175

8276
@HostBinding('class')
8377
get hostClasses() {
@@ -90,10 +84,10 @@ export class IconDirective implements IIcon {
9084
}
9185

9286
readonly innerHtml = computed(() => {
93-
const code = Array.isArray(this.code()) ? (this.code()[1] ?? this.code()[0] ?? '') : this.code() || '';
87+
const code = Array.isArray(this.code()) ? this.code()[1] ?? this.code()[0] ?? '' : this.code() || '';
9488
// todo proper sanitize
9589
// const sanitized = this.sanitizer.sanitize(SecurityContext.HTML, code);
96-
return this.#sanitizer.bypassSecurityTrustHtml((this.titleCode + code) || '');
90+
return this.#sanitizer.bypassSecurityTrustHtml(this.titleCode + code || '');
9791
});
9892

9993
get titleCode(): string {
@@ -108,8 +102,9 @@ export class IconDirective implements IIcon {
108102
return this.#iconSet.getIcon(this.#name());
109103
}
110104
if (this.#name() && !this.#iconSet?.icons[this.#name()]) {
111-
console.warn(`c-icon component: icon name '${this.#name()}' does not exist for IconSet service. ` +
112-
`To use icon by 'name' prop you need to add it to IconSet service. \n`,
105+
console.warn(
106+
`c-icon component: icon name '${this.#name()}' does not exist for IconSet service. ` +
107+
`To use icon by 'name' prop you need to add it to IconSet service. \n`,
113108
this.#name()
114109
);
115110
}
@@ -132,5 +127,4 @@ export class IconDirective implements IIcon {
132127
};
133128
return this.customClasses ?? classes;
134129
}
135-
136130
}

0 commit comments

Comments
 (0)