Skip to content

Commit c1f14b4

Browse files
committed
refactor(progress): Use SlotController
1 parent 120c565 commit c1f14b4

File tree

5 files changed

+50
-50
lines changed

5 files changed

+50
-50
lines changed

src/components/progress/base.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { html, LitElement, nothing } from 'lit';
2-
import {
3-
property,
4-
query,
5-
queryAssignedElements,
6-
state,
7-
} from 'lit/decorators.js';
2+
import { property, query, state } from 'lit/decorators.js';
83
import type { StyleInfo } from 'lit/directives/style-map.js';
94
import { addInternalsController } from '../common/controllers/internals.js';
5+
import type { SlotController } from '../common/controllers/slot.js';
106
import { watch } from '../common/decorators/watch.js';
117
import { partMap } from '../common/part-map.js';
12-
import { asPercent, clamp, formatString, isEmpty } from '../common/util.js';
8+
import { asPercent, clamp, formatString } from '../common/util.js';
139
import type { StyleVariant } from '../types.js';
1410

1511
export abstract class IgcProgressBaseComponent extends LitElement {
@@ -21,8 +17,7 @@ export abstract class IgcProgressBaseComponent extends LitElement {
2117
},
2218
});
2319

24-
@queryAssignedElements()
25-
protected _assignedElements!: HTMLElement[];
20+
protected abstract _slots: SlotController<any>;
2621

2722
@query('[part="base"]', true)
2823
protected _base!: HTMLElement;
@@ -97,14 +92,14 @@ export abstract class IgcProgressBaseComponent extends LitElement {
9792
public labelFormat!: string;
9893

9994
@watch('indeterminate')
100-
protected indeterminateChange() {
95+
protected _indeterminateChange(): void {
10196
if (!this.indeterminate) {
10297
this._updateProgress();
10398
}
10499
}
105100

106101
@watch('max')
107-
protected maxChange() {
102+
protected _maxChange(): void {
108103
this.max = Math.max(0, this.max);
109104
if (this.value > this.max) {
110105
this.value = this.max;
@@ -116,26 +111,22 @@ export abstract class IgcProgressBaseComponent extends LitElement {
116111
}
117112

118113
@watch('value')
119-
protected valueChange() {
114+
protected _valueChange(): void {
120115
this.value = clamp(this.value, 0, this.max);
121116

122117
if (!this.indeterminate) {
123118
this._updateProgress();
124119
}
125120
}
126121

127-
protected override createRenderRoot() {
128-
const root = super.createRenderRoot();
129-
root.addEventListener('slotchange', () => this.requestUpdate());
130-
return root;
131-
}
132-
133-
protected override updated() {
122+
protected override updated(): void {
134123
this._updateARIA();
135124
}
136125

137-
private _updateARIA() {
138-
const text = this.labelFormat ? this.renderLabelFormat() : `${this.value}%`;
126+
private _updateARIA(): void {
127+
const text = this.labelFormat
128+
? this._renderLabelFormat()
129+
: `${this.value}%`;
139130

140131
this._internals.setARIA({
141132
ariaValueMax: this.max.toString(),
@@ -144,7 +135,7 @@ export abstract class IgcProgressBaseComponent extends LitElement {
144135
});
145136
}
146137

147-
private _updateProgress() {
138+
private _updateProgress(): void {
148139
const percentage = asPercent(this.value, Math.max(1, this.max));
149140
const fractionValue = Math.round((percentage % 1) * 100);
150141
this._hasFraction = fractionValue > 0;
@@ -157,29 +148,31 @@ export abstract class IgcProgressBaseComponent extends LitElement {
157148
};
158149
}
159150

160-
protected renderLabel() {
151+
protected _renderLabel() {
161152
const parts = {
162153
label: true,
163154
value: true,
164155
fraction: this._hasFraction,
165156
};
166157

167158
return this.labelFormat
168-
? html`<span part=${partMap(parts)}>${this.renderLabelFormat()}</span>`
159+
? html`<span part=${partMap(parts)}>${this._renderLabelFormat()}</span>`
169160
: html`<span part=${partMap({ ...parts, counter: true })}></span>`;
170161
}
171162

172-
protected renderLabelFormat() {
163+
protected _renderLabelFormat(): string {
173164
return formatString(this.labelFormat, this.value, this.max);
174165
}
175166

176-
protected renderDefaultSlot() {
167+
protected _renderDefaultSlot() {
177168
const hideDefaultLabel =
178-
this.indeterminate || this.hideLabel || !isEmpty(this._assignedElements);
169+
this.indeterminate ||
170+
this.hideLabel ||
171+
this._slots.hasAssignedElements('[default]');
179172

180173
return html`
181174
<slot part="label"></slot>
182-
${hideDefaultLabel ? nothing : this.renderLabel()}
175+
${hideDefaultLabel ? nothing : this._renderLabel()}
183176
`;
184177
}
185178
}

src/components/progress/circular-gradient.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { LitElement, nothing } from 'lit';
22
import { property } from 'lit/decorators.js';
3-
43
import { registerComponent } from '../common/definitions/register.js';
54

65
/**
@@ -16,7 +15,7 @@ export default class IgcCircularGradientComponent extends LitElement {
1615
public static readonly tagName = 'igc-circular-gradient';
1716

1817
/* blazorSuppress */
19-
public static register() {
18+
public static register(): void {
2019
registerComponent(IgcCircularGradientComponent);
2120
}
2221

src/components/progress/circular-progress.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { html, svg } from 'lit';
2-
import { queryAssignedElements } from 'lit/decorators.js';
32
import { styleMap } from 'lit/directives/style-map.js';
43
import { addThemingController } from '../../theming/theming-controller.js';
4+
import { addSlotController, setSlots } from '../common/controllers/slot.js';
55
import { registerComponent } from '../common/definitions/register.js';
66
import { partMap } from '../common/part-map.js';
7-
import { createCounter, isEmpty } from '../common/util.js';
87
import { IgcProgressBaseComponent } from './base.js';
98
import IgcCircularGradientComponent from './circular-gradient.js';
109
import { styles } from './themes/circular/circular.progress.base.css.js';
1110
import { styles as shared } from './themes/circular/shared/circular.progress.common.css.js';
1211
import { all } from './themes/circular/themes.js';
1312

13+
let nextId = 1;
14+
1415
/**
1516
* A circular progress indicator used to express unspecified wait time or display
1617
* the length of a process.
@@ -38,32 +39,34 @@ export default class IgcCircularProgressComponent extends IgcProgressBaseCompone
3839
public static readonly tagName = 'igc-circular-progress';
3940
public static override styles = [styles, shared];
4041

41-
private static readonly increment = createCounter();
42-
4342
/* blazorSuppress */
44-
public static register() {
43+
public static register(): void {
4544
registerComponent(
4645
IgcCircularProgressComponent,
4746
IgcCircularGradientComponent
4847
);
4948
}
5049

51-
private _gradientId = `circular-progress-${IgcCircularProgressComponent.increment()}`;
52-
53-
@queryAssignedElements({ slot: 'gradient' })
54-
private _assignedGradients!: IgcCircularGradientComponent[];
50+
private readonly _gradientId = `circular-progress-${nextId++}`;
51+
protected override readonly _slots = addSlotController(this, {
52+
slots: setSlots('gradient'),
53+
});
5554

5655
constructor() {
5756
super();
5857
addThemingController(this, all);
5958
}
6059

61-
protected renderSvg() {
62-
const gradients = !isEmpty(this._assignedGradients)
63-
? this._assignedGradients.map(
64-
({ offset, color, opacity }) =>
65-
svg`<stop offset=${offset} stop-color=${color} stop-opacity=${opacity}/>`
66-
)
60+
protected _renderSvg() {
61+
const gradients = this._slots.hasAssignedElements('gradient')
62+
? this._slots
63+
.getAssignedElements<IgcCircularGradientComponent>('gradient', {
64+
selector: IgcCircularGradientComponent.tagName,
65+
})
66+
.map(
67+
({ offset, color, opacity }) =>
68+
svg`<stop offset=${offset} stop-color=${color} stop-opacity=${opacity}/>`
69+
)
6770
: svg`
6871
<stop offset="0%" part="gradient_start" />
6972
<stop offset="100%" part="gradient_end" />
@@ -85,10 +88,10 @@ export default class IgcCircularProgressComponent extends IgcProgressBaseCompone
8588
return html`
8689
<div part="base" style=${styleMap(this._styleInfo)}>
8790
<svg part=${partMap({ svg: true, indeterminate: this.indeterminate })}>
88-
${this.renderSvg()}
91+
${this._renderSvg()}
8992
</svg>
9093
<slot name="gradient"></slot>
91-
${this.renderDefaultSlot()}
94+
${this._renderDefaultSlot()}
9295
</div>
9396
`;
9497
}

src/components/progress/linear-progress.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html } from 'lit';
22
import { property } from 'lit/decorators.js';
33
import { styleMap } from 'lit/directives/style-map.js';
44
import { addThemingController } from '../../theming/theming-controller.js';
5+
import { addSlotController, setSlots } from '../common/controllers/slot.js';
56
import { registerComponent } from '../common/definitions/register.js';
67
import { partMap } from '../common/part-map.js';
78
import type { LinearProgressLabelAlign } from '../types.js';
@@ -35,10 +36,14 @@ export default class IgcLinearProgressComponent extends IgcProgressBaseComponent
3536
public static override styles = [styles, shared];
3637

3738
/* blazorSuppress */
38-
public static register() {
39+
public static register(): void {
3940
registerComponent(IgcLinearProgressComponent);
4041
}
4142

43+
protected override readonly _slots = addSlotController(this, {
44+
slots: setSlots(),
45+
});
46+
4247
/**
4348
* Sets the striped look of the control.
4449
* @attr
@@ -72,7 +77,7 @@ export default class IgcLinearProgressComponent extends IgcProgressBaseComponent
7277
<div part=${partMap(parts)}></div>
7378
<div part=${partMap({ ...parts, secondary: true })}></div>
7479
</div>
75-
${this.renderDefaultSlot()}
80+
${this._renderDefaultSlot()}
7681
</div>
7782
`;
7883
}

src/components/slider/slider-label.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class IgcSliderLabelComponent extends LitElement {
2121
`;
2222

2323
/* blazorSuppress */
24-
public static register() {
24+
public static register(): void {
2525
registerComponent(IgcSliderLabelComponent);
2626
}
2727

0 commit comments

Comments
 (0)