Skip to content
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

feat(theming): support font-family css variable #13142

Conversation

esotrope
Copy link

Before, the material font-family could only be set at application build-time. Now, developers can modify the material font-family at runtime by setting the --mat-font-family css variable. This will greatly improve the application's ability to be themed dynamically at runtime and with WYSIWYG material theme editors.

This commit partially addresses this feature request: #4352

Before, the material font-family could only be set at application build-time. Now, developers can modify the material font-family at runtime by setting the --mat-font-family css variable. This will greatly improve the application's ability to be themed dynamically at runtime and with WYSIWYG material theme editors.

This commit partially addresses this feature request: angular#4352
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 15, 2018
@return map-merge($config, (
font-family: $font-family,
use-css-variables: $use-css-variables
));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example usage in Angular:

// styles.scss
@import '~@angular/material/theming';

$typography-config: map-merge(
    mat-typography-config(),
    (use-css-variables: true)
);

@include mat-core($typography-config);
// app.component.ts
@Component({
  selector: 'app-root',
  template: `
    <h1>Font Family CSS Variable Demo</h1>
  `
})
export class AppComponent {

  constructor(private element: ElementRef) {
    this.element.nativeElement.style.setProperty('--mat-font-family', 'Comic Sans MS');
  }

}

@esotrope
Copy link
Author

closing to simplify developer setup using suggested @mixin on thread

@esotrope esotrope closed this Sep 16, 2018
Before, the material font-family could only be set at application build-time. Now, developers can modify the material font-family at runtime by setting the --mat-font-family css variable. This will greatly improve the application's ability to be themed dynamically at runtime and with WYSIWYG material theme editors.

This commit partially addresses this feature request: angular#4352
// or not supported by the browser
@function css-var($variable, $fallback) {
@return var(--mat-#{$variable}, $fallback);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods can be reused when expanding css variable support to colors

// Creates a CSS rule for font-family using the --mat-font-family css variable
@mixin mat-font-family-css-var($config) {
@include mat-css-var(font-family, font-family, mat-font-family($config));
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convenience method to wrap mat-font-family calls and provide the necessary css-variable css rules

Before, the material font-family could only be set at application build-time. Now, developers can modify the material font-family at runtime by setting the --mat-font-family css variable. This will greatly improve the application's ability to be themed dynamically at runtime and with WYSIWYG material theme editors.

This commit partially addresses this feature request: angular#4352
Before, the material font-family could only be set at application build-time. Now, developers can modify the material font-family at runtime by setting the --mat-font-family css variable. This will greatly improve the application's ability to be themed dynamically at runtime and with WYSIWYG material theme editors.

This commit partially addresses this feature request: angular#4352
@esotrope
Copy link
Author

Demo of setting the --mat-font-family css variable:
change-font-angular-mat

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do this only for font-family, without getting requests to do it for literally everything else that's using Sass variables at the moment, which we probably can't do, because the fallback approach will balloon the bundle size. The reason we haven't started using CSS variables in the first place is that we have to support IE11 for the foreseeable future.

@jelbourn
Copy link
Member

Thanks for the PR!

We unfortunately cannot use CSS variables until we end support for IE11. Once that happens, we will most likely change the entire theming system to use them. See #7374

@jelbourn jelbourn closed this Sep 16, 2018
@esotrope
Copy link
Author

esotrope commented Sep 16, 2018

Thank you for the quick feedback!

This will be bad news for my client however.
Would people be open to creating a parallel _theming.scss (maybe _css-variable-based-theming.scss) which people can optionally include instead to mitigate the bundle size issue?

I understand this would not be fun to maintain as it would likely necessitate parallel scss partials for all existing component stylesheets. However, I'm still curious if a PR of that nature would be welcome since i will likely be developing the code regardless of whether or not the main angular/material2 repo adopts it : /

@jelbourn
Copy link
Member

We unfortunately don't have the resources to maintain two parallel sets of theming approaches. If there were tooling that automatically did this, perhaps, but that would be a pretty significant undertaking.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants