Skip to content

Commit 1fa72b3

Browse files
committed
fix: allow setting css variables as event colors
Closes #845
1 parent cf05f46 commit 1fa72b3

8 files changed

+132
-8
lines changed

projects/angular-calendar/src/modules/day/calendar-day-view-event.component.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import { PlacementArray } from 'positioning';
2222
>
2323
<div
2424
class="cal-event"
25-
[style.backgroundColor]="dayEvent.event.color?.secondary"
26-
[style.borderColor]="dayEvent.event.color?.primary"
25+
[ngStyle]="{
26+
backgroundColor: dayEvent.event.color?.secondary,
27+
borderColor: dayEvent.event.color?.primary
28+
}"
2729
[mwlCalendarTooltip]="
2830
dayEvent.event.title | calendarEventTitle: 'dayTooltip':dayEvent.event
2931
"

projects/angular-calendar/src/modules/month/calendar-month-cell.component.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { PlacementArray } from 'positioning';
3838
<div
3939
class="cal-event"
4040
*ngFor="let event of day.events; trackBy: trackByEventId"
41-
[style.backgroundColor]="event.color?.primary"
41+
[ngStyle]="{ backgroundColor: event.color?.primary }"
4242
[ngClass]="event?.cssClass"
4343
(mouseenter)="highlightDay.emit({ event: event })"
4444
(mouseleave)="unhighlightDay.emit({ event: event })"
@@ -87,8 +87,7 @@ import { PlacementArray } from 'positioning';
8787
'[class.cal-out-month]': '!day.inMonth',
8888
'[class.cal-has-events]': 'day.events.length > 0',
8989
'[class.cal-open]': 'day === openDay',
90-
'[class.cal-event-highlight]': '!!day.backgroundColor',
91-
'[style.backgroundColor]': 'day.backgroundColor'
90+
'[class.cal-event-highlight]': '!!day.backgroundColor'
9291
}
9392
})
9493
export class CalendarMonthCellComponent {

projects/angular-calendar/src/modules/month/calendar-month-view.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export interface CalendarMonthViewEventTimesChangedEvent<
8383
[tooltipTemplate]="tooltipTemplate"
8484
[tooltipDelay]="tooltipDelay"
8585
[customTemplate]="cellTemplate"
86+
[ngStyle]="{ backgroundColor: day.backgroundColor }"
8687
(mwlClick)="dayClicked.emit({ day: day })"
8788
(highlightDay)="toggleDayHighlight($event.event, true)"
8889
(unhighlightDay)="toggleDayHighlight($event.event, false)"

projects/angular-calendar/src/modules/month/calendar-open-day-events.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const collapseAnimation: AnimationTriggerMetadata = trigger('collapse', [
4848
>
4949
<span
5050
class="cal-event"
51-
[style.backgroundColor]="event.color?.primary"
51+
[ngStyle]="{ backgroundColor: event.color?.primary }"
5252
>
5353
</span>
5454
&ngsp;

projects/angular-calendar/src/modules/week/calendar-week-view-event.component.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import { PlacementArray } from 'positioning';
2323
>
2424
<div
2525
class="cal-event"
26-
[style.backgroundColor]="weekEvent.event.color?.secondary"
27-
[style.borderColor]="weekEvent.event.color?.primary"
26+
[ngStyle]="{
27+
backgroundColor: weekEvent.event.color?.secondary,
28+
borderColor: weekEvent.event.color?.primary
29+
}"
2830
[mwlCalendarTooltip]="
2931
!tooltipDisabled
3032
? (weekEvent.event.title

projects/angular-calendar/test/calendar-day-view.component.spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -1553,4 +1553,45 @@ describe('CalendarDayViewComponent component', () => {
15531553
.toDate()
15541554
});
15551555
});
1556+
1557+
it('should allow css variables for colors', () => {
1558+
const style = document.createElement('style');
1559+
style.setAttribute('type', 'text/css');
1560+
style.innerText = `
1561+
:root {
1562+
--white: #fff;
1563+
--black: #000;
1564+
}
1565+
`;
1566+
document.head.appendChild(style);
1567+
const fixture: ComponentFixture<
1568+
CalendarDayViewComponent
1569+
> = TestBed.createComponent(CalendarDayViewComponent);
1570+
fixture.componentInstance.ngOnInit();
1571+
fixture.componentInstance.viewDate = new Date('2016-06-01');
1572+
fixture.componentInstance.events = [
1573+
{
1574+
start: new Date('2016-05-30'),
1575+
end: new Date('2016-06-02'),
1576+
title: 'foo',
1577+
color: {
1578+
primary: 'var(--white)',
1579+
secondary: 'var(--black)'
1580+
}
1581+
}
1582+
];
1583+
fixture.componentInstance.ngOnChanges({ viewDate: {}, events: {} });
1584+
fixture.detectChanges();
1585+
1586+
const computedStyles: CSSStyleDeclaration = window.getComputedStyle(
1587+
fixture.nativeElement.querySelector('.cal-event')
1588+
);
1589+
expect(computedStyles.getPropertyValue('background-color')).to.equal(
1590+
'rgb(0, 0, 0)'
1591+
);
1592+
expect(computedStyles.getPropertyValue('border-color')).to.equal(
1593+
'rgb(255, 255, 255)'
1594+
);
1595+
document.head.appendChild(style);
1596+
});
15561597
});

projects/angular-calendar/test/calendar-month-view.component.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -974,4 +974,42 @@ describe('calendarMonthView component', () => {
974974
).to.equal(true);
975975
fixture.destroy();
976976
});
977+
978+
it('should allow CSS variables for colors', () => {
979+
const style = document.createElement('style');
980+
style.setAttribute('type', 'text/css');
981+
style.innerText = `
982+
:root {
983+
--white: #fff;
984+
--black: #000;
985+
}
986+
`;
987+
document.head.appendChild(style);
988+
const fixture: ComponentFixture<
989+
CalendarMonthViewComponent
990+
> = TestBed.createComponent(CalendarMonthViewComponent);
991+
fixture.componentInstance.ngOnInit();
992+
fixture.componentInstance.viewDate = new Date('2016-06-01');
993+
fixture.componentInstance.events = [
994+
{
995+
start: new Date('2016-05-30'),
996+
end: new Date('2016-06-02'),
997+
title: 'foo',
998+
color: {
999+
primary: 'var(--white)',
1000+
secondary: 'var(--black)'
1001+
}
1002+
}
1003+
];
1004+
fixture.componentInstance.ngOnChanges({ viewDate: {}, events: {} });
1005+
fixture.detectChanges();
1006+
1007+
const computedStyles: CSSStyleDeclaration = window.getComputedStyle(
1008+
fixture.nativeElement.querySelector('.cal-event')
1009+
);
1010+
expect(computedStyles.getPropertyValue('background-color')).to.equal(
1011+
'rgb(255, 255, 255)'
1012+
);
1013+
document.head.removeChild(style);
1014+
});
9771015
});

projects/angular-calendar/test/calendar-week-view.component.spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -2257,4 +2257,45 @@ describe('calendarWeekView component', () => {
22572257
.toDate()
22582258
});
22592259
});
2260+
2261+
it('should allow css variables for colors', () => {
2262+
const style = document.createElement('style');
2263+
style.setAttribute('type', 'text/css');
2264+
style.innerText = `
2265+
:root {
2266+
--white: #fff;
2267+
--black: #000;
2268+
}
2269+
`;
2270+
document.head.appendChild(style);
2271+
const fixture: ComponentFixture<
2272+
CalendarWeekViewComponent
2273+
> = TestBed.createComponent(CalendarWeekViewComponent);
2274+
fixture.componentInstance.ngOnInit();
2275+
fixture.componentInstance.viewDate = new Date('2016-06-01');
2276+
fixture.componentInstance.events = [
2277+
{
2278+
start: new Date('2016-05-30'),
2279+
end: new Date('2016-06-02'),
2280+
title: 'foo',
2281+
color: {
2282+
primary: 'var(--white)',
2283+
secondary: 'var(--black)'
2284+
}
2285+
}
2286+
];
2287+
fixture.componentInstance.ngOnChanges({ viewDate: {}, events: {} });
2288+
fixture.detectChanges();
2289+
2290+
const computedStyles: CSSStyleDeclaration = window.getComputedStyle(
2291+
fixture.nativeElement.querySelector('.cal-event')
2292+
);
2293+
expect(computedStyles.getPropertyValue('background-color')).to.equal(
2294+
'rgb(0, 0, 0)'
2295+
);
2296+
expect(computedStyles.getPropertyValue('border-color')).to.equal(
2297+
'rgb(255, 255, 255)'
2298+
);
2299+
document.head.appendChild(style);
2300+
});
22602301
});

0 commit comments

Comments
 (0)