Skip to content

perf(cdk/a11y): only check for high contrast mode once #22561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export const HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS = 'cdk-high-contrast-active';
*/
@Injectable({providedIn: 'root'})
export class HighContrastModeDetector {
/**
* Figuring out the high contrast mode and adding the body classes can cause
* some expensive layouts. This flag is used to ensure that we only do it once.
*/
private _hasCheckedHighContrastMode: boolean;
private _document: Document;

constructor(private _platform: Platform, @Inject(DOCUMENT) document: any) {
Expand Down Expand Up @@ -80,12 +85,13 @@ export class HighContrastModeDetector {

/** Applies CSS classes indicating high-contrast mode to document body (browser-only). */
_applyBodyHighContrastModeCssClasses(): void {
if (this._platform.isBrowser && this._document.body) {
if (!this._hasCheckedHighContrastMode && this._platform.isBrowser && this._document.body) {
const bodyClasses = this._document.body.classList;
// IE11 doesn't support `classList` operations with multiple arguments
bodyClasses.remove(HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS);
bodyClasses.remove(BLACK_ON_WHITE_CSS_CLASS);
bodyClasses.remove(WHITE_ON_BLACK_CSS_CLASS);
this._hasCheckedHighContrastMode = true;

const mode = this.getHighContrastMode();
if (mode === HighContrastMode.BLACK_ON_WHITE) {
Expand Down