Skip to content

Commit f8740f2

Browse files
authored
refactor: partNameMap as a Lit directive (#1673)
1 parent 877b16c commit f8740f2

File tree

39 files changed

+239
-207
lines changed

39 files changed

+239
-207
lines changed

src/components/button-group/toggle-button.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { property, query } from 'lit/decorators.js';
44
import { themes } from '../../theming/theming-decorator.js';
55
import { addKeyboardFocusRing } from '../common/controllers/focus-ring.js';
66
import { registerComponent } from '../common/definitions/register.js';
7-
import { partNameMap } from '../common/util.js';
7+
import { partMap } from '../common/part-map.js';
88
import { styles } from './themes/button.base.css.js';
99
import { all } from './themes/button.js';
1010
import { styles as shared } from './themes/shared/button/button.common.css.js';
@@ -80,10 +80,7 @@ export default class IgcToggleButtonComponent extends LitElement {
8080
protected override render() {
8181
return html`
8282
<button
83-
part=${partNameMap({
84-
toggle: true,
85-
focused: this._kbFocus.focused,
86-
})}
83+
part=${partMap({ toggle: true, focused: this._kbFocus.focused })}
8784
type="button"
8885
?disabled=${this.disabled}
8986
.ariaLabel=${this.ariaLabel}

src/components/button/button-base.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EventEmitterMixin } from '../common//mixins/event-emitter.js';
66
import { addKeyboardFocusRing } from '../common/controllers/focus-ring.js';
77
import { blazorDeepImport } from '../common/decorators/blazorDeepImport.js';
88
import type { Constructor } from '../common/mixins/constructor.js';
9-
import { partNameMap } from '../common/util.js';
9+
import { partMap } from '../common/part-map.js';
1010

1111
export interface IgcButtonEventMap {
1212
// For analyzer meta only:
@@ -140,10 +140,7 @@ export abstract class IgcButtonBaseComponent extends EventEmitterMixin<
140140
private renderButton() {
141141
return html`
142142
<button
143-
part=${partNameMap({
144-
base: true,
145-
focused: this._kbFocus.focused,
146-
})}
143+
part=${partMap({ base: true, focused: this._kbFocus.focused })}
147144
aria-label=${ifDefined(this.ariaLabel ?? nothing)}
148145
?disabled=${this.disabled}
149146
type=${ifDefined(this.type)}
@@ -158,13 +155,10 @@ export abstract class IgcButtonBaseComponent extends EventEmitterMixin<
158155
private renderLinkButton() {
159156
return html`
160157
<a
161-
part=${partNameMap({
162-
base: true,
163-
focused: this._kbFocus.focused,
164-
})}
158+
part=${partMap({ base: true, focused: this._kbFocus.focused })}
165159
role="button"
166160
aria-label=${ifDefined(this.ariaLabel ?? nothing)}
167-
aria-disabled=${this.disabled ? 'true' : 'false'}
161+
aria-disabled=${this.disabled}
168162
href=${ifDefined(this.href)}
169163
target=${ifDefined(this.target)}
170164
download=${ifDefined(this.download)}

src/components/calendar/calendar.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import { IgcCalendarResourceStringEN } from '../common/i18n/calendar.resources.j
2323
import { createDateTimeFormatters } from '../common/localization/intl-formatters.js';
2424
import type { Constructor } from '../common/mixins/constructor.js';
2525
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
26+
import { partMap } from '../common/part-map.js';
2627
import {
2728
clamp,
2829
findElementFromEventPath,
2930
first,
3031
formatString,
3132
last,
32-
partNameMap,
3333
} from '../common/util.js';
3434
import IgcIconComponent from '../icon/icon.js';
3535
import type { ContentOrientation } from '../types.js';
@@ -427,15 +427,15 @@ export default class IgcCalendarComponent extends EventEmitterMixin<
427427
}
428428

429429
protected renderNavigationButtons() {
430-
const parts = partNameMap({
430+
const parts = {
431431
'navigation-button': true,
432432
vertical: this.orientation === 'vertical',
433-
});
433+
};
434434

435435
return html`
436436
<div part="navigation-buttons">
437437
<button
438-
part=${parts}
438+
part=${partMap(parts)}
439439
aria-label=${this.previousButtonLabel}
440440
@click=${this.navigatePrevious}
441441
>
@@ -447,7 +447,7 @@ export default class IgcCalendarComponent extends EventEmitterMixin<
447447
</button>
448448
449449
<button
450-
part=${parts}
450+
part=${partMap(parts)}
451451
aria-label=${this.nextButtonLabel}
452452
@click=${this.navigateNext}
453453
>

src/components/calendar/days-view/days-view.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { IgcCalendarResourceStringEN } from '../../common/i18n/calendar.resource
1111
import { createDateTimeFormatters } from '../../common/localization/intl-formatters.js';
1212
import type { Constructor } from '../../common/mixins/constructor.js';
1313
import { EventEmitterMixin } from '../../common/mixins/event-emitter.js';
14-
import { chunk, first, last, partNameMap, take } from '../../common/util.js';
14+
import { partMap } from '../../common/part-map.js';
15+
import { chunk, first, last, take } from '../../common/util.js';
1516
import { IgcCalendarBaseComponent } from '../base.js';
1617
import {
1718
areSameMonth,
@@ -382,7 +383,6 @@ export default class IgcDaysViewComponent extends EventEmitterMixin<
382383
const inactive = !areSameMonth(day, this._activeDate);
383384

384385
return {
385-
date: true,
386386
disabled: disabled || hidden,
387387
first: this.isFirstInRange(day),
388388
last: this.isLastInRange(day),
@@ -403,13 +403,12 @@ export default class IgcDaysViewComponent extends EventEmitterMixin<
403403
const { changePreview, clearPreview } = this.getDayHandlers(day);
404404

405405
const props = this.getDayProperties(day, today);
406-
const parts = partNameMap(props);
407406

408407
return html`
409-
<span part=${parts}>
408+
<span part=${partMap({ date: true, ...props })}>
410409
<span
411410
role="gridcell"
412-
part=${parts.replace('date', 'date-inner')}
411+
part=${partMap({ 'date-inner': true, ...props })}
413412
aria-label=${ariaLabel}
414413
aria-disabled=${props.disabled}
415414
aria-selected=${props.selected}
@@ -438,8 +437,8 @@ export default class IgcDaysViewComponent extends EventEmitterMixin<
438437

439438
protected renderWeekNumber(start: CalendarDay, last: boolean) {
440439
return html`
441-
<span role="rowheader" part=${partNameMap({ 'week-number': true, last })}>
442-
<span part=${partNameMap({ 'week-number-inner': true, last })}>
440+
<span role="rowheader" part=${partMap({ 'week-number': true, last })}>
441+
<span part=${partMap({ 'week-number-inner': true, last })}>
443442
${start.week}
444443
</span>
445444
</span>

src/components/calendar/helpers.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function getDayViewDOM(element: IgcDaysViewComponent) {
4747
get disabled() {
4848
return Array.from(
4949
root.querySelectorAll<HTMLElement>(
50-
'span[part*="date-inner disabled"]'
50+
'span[part~="date-inner"][part~="disabled"]'
5151
)
5252
);
5353
},

src/components/calendar/months-view/months-view.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { registerComponent } from '../../common/definitions/register.js';
1111
import { createDateTimeFormatters } from '../../common/localization/intl-formatters.js';
1212
import type { Constructor } from '../../common/mixins/constructor.js';
1313
import { EventEmitterMixin } from '../../common/mixins/event-emitter.js';
14-
import { chunk, partNameMap } from '../../common/util.js';
14+
import { partMap } from '../../common/part-map.js';
15+
import { chunk } from '../../common/util.js';
1516
import { MONTHS_PER_ROW, areSameMonth, getViewElement } from '../helpers.js';
1617
import { CalendarDay } from '../model.js';
1718
import { styles } from '../themes/year-month-view.base.css.js';
@@ -119,14 +120,12 @@ export default class IgcMonthsViewComponent extends EventEmitterMixin<
119120
const current = areSameMonth(now, entry);
120121
const selected = this._value.month === entry.month;
121122

122-
const parts = { selected, current };
123-
124123
return html`
125-
<span part=${partNameMap({ month: true, ...parts })}>
124+
<span part=${partMap({ month: true, selected, current })}>
126125
<span
127126
role="gridcell"
128127
data-value=${entry.month}
129-
part=${partNameMap({ 'month-inner': true, ...parts })}
128+
part=${partMap({ 'month-inner': true, selected, current })}
130129
aria-selected=${selected}
131130
aria-label=${ariaLabel}
132131
tabindex=${active ? 0 : -1}

src/components/calendar/years-view/years-view.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { blazorSuppressComponent } from '../../common/decorators/blazorSuppressC
99
import { registerComponent } from '../../common/definitions/register.js';
1010
import type { Constructor } from '../../common/mixins/constructor.js';
1111
import { EventEmitterMixin } from '../../common/mixins/event-emitter.js';
12-
import { chunk, partNameMap } from '../../common/util.js';
12+
import { partMap } from '../../common/part-map.js';
13+
import { chunk } from '../../common/util.js';
1314
import { YEARS_PER_ROW, getViewElement, getYearRange } from '../helpers.js';
1415
import { CalendarDay } from '../model.js';
1516
import { styles } from '../themes/year-month-view.base.css.js';
@@ -96,14 +97,13 @@ export default class IgcYearsViewComponent extends EventEmitterMixin<
9697
protected renderYear(year: number, now: CalendarDay) {
9798
const selected = this._value.year === year;
9899
const current = year === now.year;
99-
const parts = { selected, current };
100100

101101
return html`
102-
<span part=${partNameMap({ year: true, ...parts })}>
102+
<span part=${partMap({ year: true, selected, current })}>
103103
<span
104104
role="gridcell"
105105
data-value=${year}
106-
part=${partNameMap({ 'year-inner': true, ...parts })}
106+
part=${partMap({ 'year-inner': true, selected, current })}
107107
aria-selected=${selected}
108108
tabindex=${selected ? 0 : -1}
109109
>

src/components/carousel/carousel-indicator-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { LitElement, html } from 'lit';
22
import { themes } from '../../theming/theming-decorator.js';
33
import { addKeyboardFocusRing } from '../common/controllers/focus-ring.js';
44
import { registerComponent } from '../common/definitions/register.js';
5-
import { partNameMap } from '../common/util.js';
5+
import { partMap } from '../common/part-map.js';
66
import IgcCarouselIndicatorComponent from './carousel-indicator.js';
77
import { styles } from './themes/carousel-indicator-container.base.css.js';
88
import { all } from './themes/indicator-container.js';
@@ -39,7 +39,7 @@ export default class IgcCarouselIndicatorContainerComponent extends LitElement {
3939
protected override render() {
4040
return html`
4141
<div
42-
part=${partNameMap({
42+
part=${partMap({
4343
base: true,
4444
focused: this._kbFocus.focused,
4545
})}

src/components/carousel/carousel.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { watch } from '../common/decorators/watch.js';
3232
import { registerComponent } from '../common/definitions/register.js';
3333
import type { Constructor } from '../common/mixins/constructor.js';
3434
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
35+
import { partMap } from '../common/part-map.js';
3536
import {
3637
asNumber,
3738
createCounter,
@@ -40,7 +41,6 @@ import {
4041
formatString,
4142
isLTR,
4243
last,
43-
partNameMap,
4444
wrap,
4545
} from '../common/util.js';
4646
import IgcIconComponent from '../icon/icon.js';
@@ -740,14 +740,18 @@ export default class IgcCarouselComponent extends EventEmitterMixin<
740740
}
741741

742742
private indicatorTemplate() {
743-
const parts = partNameMap({
743+
const parts = {
744744
indicators: true,
745745
start: this.indicatorsOrientation === 'start',
746-
});
746+
};
747747

748748
return html`
749749
<igc-carousel-indicator-container>
750-
<div ${ref(this._indicatorsContainerRef)} role="tablist" part=${parts}>
750+
<div
751+
${ref(this._indicatorsContainerRef)}
752+
role="tablist"
753+
part=${partMap(parts)}
754+
>
751755
<slot
752756
name="indicator"
753757
@slotchange=${this.handleIndicatorSlotChange}
@@ -763,19 +767,19 @@ export default class IgcCarouselComponent extends EventEmitterMixin<
763767
}
764768

765769
private labelTemplate() {
766-
const parts = partNameMap({
770+
const parts = {
767771
label: true,
768772
indicators: true,
769773
start: this.indicatorsOrientation === 'start',
770-
});
774+
};
771775
const value = formatString(
772776
this.slidesLabelFormat,
773777
this.current + 1,
774778
this.total
775779
);
776780

777781
return html`
778-
<div part=${parts}>
782+
<div part=${partMap(parts)}>
779783
<span>${value}</span>
780784
</div>
781785
`;

src/components/checkbox/checkbox.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { live } from 'lit/directives/live.js';
55

66
import { getThemeController, themes } from '../../theming/theming-decorator.js';
77
import { registerComponent } from '../common/definitions/register.js';
8-
import { createCounter, partNameMap } from '../common/util.js';
8+
import { partMap } from '../common/part-map.js';
9+
import { createCounter } from '../common/util.js';
910
import IgcValidationContainerComponent from '../validation-container/validation-container.js';
1011
import { IgcCheckboxBaseComponent } from './checkbox-base.js';
1112
import { all } from './themes/checkbox-themes.js';
@@ -89,7 +90,7 @@ export default class IgcCheckboxComponent extends IgcCheckboxBaseComponent {
8990

9091
return html`
9192
<label
92-
part=${partNameMap({
93+
part=${partMap({
9394
base: true,
9495
checked,
9596
focused: this._kbFocus.focused,
@@ -113,14 +114,14 @@ export default class IgcCheckboxComponent extends IgcCheckboxBaseComponent {
113114
@blur=${this.handleBlur}
114115
@focus=${this.handleFocus}
115116
/>
116-
<span part=${partNameMap({ control: true, checked })}>
117-
<span part=${partNameMap({ indicator: true, checked })}>
117+
<span part=${partMap({ control: true, checked })}>
118+
<span part=${partMap({ indicator: true, checked })}>
118119
${this._isIndigo ? this.renderIndigo() : this.renderStandard()}
119120
</span>
120121
</span>
121122
<span
122123
.hidden=${this.hideLabel}
123-
part=${partNameMap({ label: true, checked })}
124+
part=${partMap({ label: true, checked })}
124125
id=${this.labelId}
125126
><slot></slot>
126127
</span>

0 commit comments

Comments
 (0)