Skip to content

Commit 3511e50

Browse files
authored
fix: use afterRender, to prevent warnings (#514)
1 parent f7af815 commit 3511e50

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

projects/angular-split/src/lib/split/split.component.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1+
import { DOCUMENT, NgStyle, NgTemplateOutlet } from '@angular/common'
12
import {
23
ChangeDetectionStrategy,
34
Component,
45
ElementRef,
56
HostBinding,
67
InjectionToken,
78
NgZone,
8-
Renderer2,
9+
afterRenderEffect,
910
booleanAttribute,
1011
computed,
1112
contentChild,
1213
contentChildren,
13-
effect,
1414
inject,
1515
input,
1616
isDevMode,
1717
output,
1818
signal,
1919
} from '@angular/core'
2020
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'
21-
import type { SplitAreaComponent } from '../split-area/split-area.component'
2221
import {
2322
Subject,
2423
filter,
@@ -33,26 +32,26 @@ import {
3332
takeUntil,
3433
tap,
3534
} from 'rxjs'
35+
import { ANGULAR_SPLIT_DEFAULT_OPTIONS } from '../angular-split-config.token'
36+
import { SplitGutterDynamicInjectorDirective } from '../gutter/split-gutter-dynamic-injector.directive'
37+
import { SplitGutterDirective } from '../gutter/split-gutter.directive'
38+
import { SplitAreaSize, SplitGutterInteractionEvent } from '../models'
39+
import type { SplitAreaComponent } from '../split-area/split-area.component'
40+
import { SplitCustomEventsBehaviorDirective } from '../split-custom-events-behavior.directive'
3641
import {
3742
ClientPoint,
43+
assertUnreachable,
3844
createClassesString,
39-
gutterEventsEqualWithDelta,
4045
fromMouseMoveEvent,
4146
fromMouseUpEvent,
4247
getPointFromEvent,
48+
gutterEventsEqualWithDelta,
4349
leaveNgZone,
4450
numberAttributeWithFallback,
4551
sum,
4652
toRecord,
47-
assertUnreachable,
4853
} from '../utils'
49-
import { DOCUMENT, NgStyle, NgTemplateOutlet } from '@angular/common'
50-
import { SplitGutterInteractionEvent, SplitAreaSize } from '../models'
51-
import { SplitCustomEventsBehaviorDirective } from '../split-custom-events-behavior.directive'
5254
import { areAreasValid } from '../validations'
53-
import { SplitGutterDirective } from '../gutter/split-gutter.directive'
54-
import { SplitGutterDynamicInjectorDirective } from '../gutter/split-gutter-dynamic-injector.directive'
55-
import { ANGULAR_SPLIT_DEFAULT_OPTIONS } from '../angular-split-config.token'
5655

5756
interface MouseDownContext {
5857
mouseDownEvent: MouseEvent | TouchEvent
@@ -88,7 +87,6 @@ export const SPLIT_AREA_CONTRACT = new InjectionToken<SplitAreaComponent>('Split
8887
})
8988
export class SplitComponent {
9089
private readonly document = inject(DOCUMENT)
91-
private readonly renderer = inject(Renderer2)
9290
private readonly elementRef = inject<ElementRef<HTMLElement>>(ElementRef)
9391
private readonly ngZone = inject(NgZone)
9492
private readonly defaultOptions = inject(ANGULAR_SPLIT_DEFAULT_OPTIONS)
@@ -164,22 +162,28 @@ export class SplitComponent {
164162
constructor() {
165163
if (isDevMode()) {
166164
// Logs warnings to console when the provided areas sizes are invalid
167-
effect(() => {
168-
// Special mode when no size input was declared which is a valid mode
169-
if (this.unit() === 'percent' && this._visibleAreas().every((area) => area.size() === 'auto')) {
170-
return
171-
}
165+
afterRenderEffect({
166+
// we use the afterRender read phase here,
167+
// because we want to run this after all processing is done.
168+
// and we are not updating anything in the DOM
169+
read: () => {
170+
// Special mode when no size input was declared which is a valid mode
171+
if (this.unit() === 'percent' && this._visibleAreas().every((area) => area.size() === 'auto')) {
172+
return
173+
}
172174

173-
areAreasValid(this._visibleAreas(), this.unit(), true)
175+
areAreasValid(this._visibleAreas(), this.unit(), true)
176+
},
174177
})
175178
}
176179

177-
// Responsible for updating grid template style. Must be this way and not based on HostBinding
178-
// as change detection for host binding is bound to the parent component and this style
179-
// is updated on every mouse move. Doing it this way will prevent change detection cycles in parent.
180-
effect(() => {
181-
const gridTemplateColumnsStyle = this.gridTemplateColumnsStyle()
182-
this.renderer.setStyle(this.elementRef.nativeElement, 'grid-template', gridTemplateColumnsStyle)
180+
// we are running this after Angular has completed its CD loop
181+
// as we are updating the style of the host, and we don't want to re-trigger the CD loop
182+
// doing this in the host of the component would retrigger the CD too many times
183+
afterRenderEffect({
184+
write: () => {
185+
this.elementRef.nativeElement.style.gridTemplate = this.gridTemplateColumnsStyle()
186+
},
183187
})
184188

185189
this.gutterMouseDownSubject

src/app/examples/split-transitions/split-transitions.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export class SplitTransitionsComponent extends AComponent {
267267
setTimeout(() => {
268268
const logsEl = this.logsEl()
269269
if (logsEl.nativeElement.scroll) {
270-
;(<HTMLElement>logsEl.nativeElement).scroll({ top: this.logMessages.length * 30 })
270+
(<HTMLElement>logsEl.nativeElement).scroll({ top: this.logMessages.length * 30 })
271271
}
272272
})
273273
}

0 commit comments

Comments
 (0)