Skip to content

Commit d22683b

Browse files
committed
add scss mixin for styling input autofill colors
1 parent d4d5f05 commit d22683b

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

src/cdk/input/_autofill.scss

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
// Core styles that enable monitoring autofill state of inputs.
12
@mixin cdk-input-autofill {
23
// Keyframes that do nothing, but allow us to monitor when an input becomes autofilled.
3-
@keyframes cdk-input-autofill-start {from {} to {}}
4-
@keyframes cdk-input-autofill-end {from {} to {}}
4+
@keyframes cdk-input-autofill-start {}
5+
@keyframes cdk-input-autofill-end {}
56

67
.cdk-input-autofill-monitored:-webkit-autofill {
78
animation-name: cdk-input-autofill-start;
@@ -11,3 +12,27 @@
1112
animation-name: cdk-input-autofill-end;
1213
}
1314
}
15+
16+
// Used to generate UIDs for keyframes used to change the input autofill styles.
17+
$cdk-input-autofill-color-frame-count: 0;
18+
19+
// Mixin used to apply custom background and foreground colors to an autofilled input.
20+
// Based on:
21+
// https://stackoverflow.com/questions/2781549/removing-input-background-colour-for-chrome-autocomplete#answer-37432260
22+
@mixin cdk-input-autofill-color($background, $foreground:'') {
23+
@keyframes cdk-input-autofill-color-#{$cdk-input-autofill-color-frame-count} {
24+
to {
25+
background: $background;
26+
@if $foreground != '' { color: $foreground; }
27+
}
28+
}
29+
30+
&:-webkit-autofill,
31+
&.cdk-input-autofill-monitored:-webkit-autofill {
32+
animation-name: cdk-input-autofill-start,
33+
cdk-input-autofill-color-#{$cdk-input-autofill-color-frame-count};
34+
animation-fill-mode: both;
35+
}
36+
37+
$cdk-input-autofill-color-frame-count: $cdk-input-autofill-color-frame-count + 1 !global;
38+
}

src/demo-app/input/input-demo.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,16 @@ <h3>&lt;textarea&gt; with ngModel</h3>
528528
</mat-card>
529529

530530
<mat-card class="demo-card demo-basic">
531-
<mat-toolbar color="primary">Autofill Monitor</mat-toolbar>
531+
<mat-toolbar color="primary">Autofill</mat-toolbar>
532532
<mat-card-content>
533533
<form novalidate>
534+
<mat-checkbox [(ngModel)]="customAutofillStyle" name="custom">
535+
Use custom autofill style
536+
</mat-checkbox>
534537
<mat-form-field>
535-
<mat-label>Autofill monitor</mat-label>
536-
<input matInput (cdkAutofill)="isAutofilled = $event.isAutofilled" name="autofill">
538+
<mat-label>Autofill monitored</mat-label>
539+
<input matInput (cdkAutofill)="isAutofilled = $event.isAutofilled" name="autofill"
540+
[class.demo-custom-autofill-style]="customAutofillStyle">
537541
</mat-form-field>
538542
<button color="primary" mat-raised-button>Submit</button>
539543
<span> is autofilled? {{isAutofilled ? 'yes' : 'no'}}</span>

src/demo-app/input/input-demo.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import '../../../dist/packages/cdk/input/autofill';
2+
13
.demo-basic {
24
padding: 0;
35
}
@@ -23,3 +25,7 @@
2325
padding: 0;
2426
background: lightblue;
2527
}
28+
29+
.demo-custom-autofill-style {
30+
@include cdk-input-autofill-color(transparent, red);
31+
}

src/demo-app/input/input-demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class InputDemo {
5252
delayedFormControl = new FormControl('');
5353
model = 'hello';
5454
isAutofilled = false;
55+
customAutofillStyle = true;
5556

5657
constructor() {
5758
setTimeout(() => this.delayedFormControl.setValue('hello'), 100);

0 commit comments

Comments
 (0)