Skip to content

Commit 83aac50

Browse files
committed
fix(carousel): improve carousel context handling with async context
1 parent 0b29759 commit 83aac50

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/components/carousel/carousel-slide.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { consume } from '@lit/context';
21
import { html, LitElement } from 'lit';
32
import { property } from 'lit/decorators.js';
43
import { EaseInOut } from '../../animations/easings.js';
54
import { addAnimationController } from '../../animations/player.js';
65
import { carouselContext } from '../common/context.js';
6+
import { createAsyncContext } from '../common/controllers/async-consumer.js';
77
import { addInternalsController } from '../common/controllers/internals.js';
88
import { registerComponent } from '../common/definitions/register.js';
99
import { formatString } from '../common/util.js';
@@ -38,29 +38,44 @@ export default class IgcCarouselSlideComponent extends LitElement {
3838

3939
private readonly _player = addAnimationController(this);
4040

41-
@consume({ context: carouselContext, subscribe: true })
42-
private readonly _carousel?: IgcCarouselComponent;
41+
private _carousel?: IgcCarouselComponent;
42+
43+
private readonly _context = createAsyncContext(
44+
this,
45+
carouselContext,
46+
(carousel) => {
47+
this._carousel = carousel;
48+
}
49+
);
50+
51+
private get _carouselInstance(): IgcCarouselComponent | undefined {
52+
return this._carousel ?? this._context.value;
53+
}
4354

4455
protected get _index(): number {
45-
return this._carousel ? this._carousel.slides.indexOf(this) : 0;
56+
return this._carouselInstance
57+
? this._carouselInstance.slides.indexOf(this)
58+
: 0;
4659
}
4760

4861
protected get _total(): number {
49-
return this._carousel ? this._carousel.slides.length : 0;
62+
return this._carouselInstance ? this._carouselInstance.slides.length : 0;
5063
}
5164

5265
protected get _animation() {
53-
const animation = this._carousel?.animationType ?? 'slide';
66+
const animation = this._carouselInstance?.animationType ?? 'slide';
5467

5568
if (animation === 'slide') {
56-
return this._carousel?.vertical ? 'slideVer' : 'slideHor';
69+
return this._carouselInstance?.vertical ? 'slideVer' : 'slideHor';
5770
}
5871

5972
return animation;
6073
}
6174

6275
protected get _labelFormat(): string {
63-
return this._carousel ? this._carousel.slidesLabelFormat : '';
76+
return this._carouselInstance
77+
? this._carouselInstance.slidesLabelFormat
78+
: '';
6479
}
6580

6681
/**

src/components/carousel/carousel.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ export default class IgcCarouselComponent extends EventEmitterMixin<
145145
initialValue: this,
146146
});
147147

148+
private _setCarouselContext(): void {
149+
this._context.setValue(this, true);
150+
}
151+
148152
@queryAll(IgcCarouselIndicatorComponent.tagName)
149153
private readonly _defaultIndicators!: NodeListOf<IgcCarouselIndicatorComponent>;
150154

@@ -350,7 +354,7 @@ export default class IgcCarouselComponent extends EventEmitterMixin<
350354
@watch('slidesLabelFormat')
351355
@watch('indicatorsLabelFormat')
352356
protected _contextChanged(): void {
353-
this._context.setValue(this, true);
357+
this._setCarouselContext();
354358
}
355359

356360
@watch('interval')
@@ -421,6 +425,7 @@ export default class IgcCarouselComponent extends EventEmitterMixin<
421425

422426
protected override async firstUpdated(): Promise<void> {
423427
await this.updateComplete;
428+
this._setCarouselContext();
424429

425430
if (!isEmpty(this._slides)) {
426431
this._activateSlide(

0 commit comments

Comments
 (0)