-
Notifications
You must be signed in to change notification settings - Fork 6.8k
refactor(material/table): switch to tokens API #27192
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
@use 'sass:map'; | ||
@use '../../../theming/theming'; | ||
@use '../../../typography/typography-utils'; | ||
@use '../../token-utils'; | ||
@use '../../../style/sass-utils'; | ||
|
||
// The prefix used to generate the fully qualified name for tokens in this file. | ||
$prefix: (mat, table); | ||
|
||
// Tokens that can't be configured through Angular Material's current theming API, | ||
// but may be in a future version of the theming API. | ||
@function get-unthemable-tokens() { | ||
@return ( | ||
row-item-outline-width: 1px, | ||
); | ||
} | ||
|
||
// Tokens that can be configured through Angular Material's color theming API. | ||
@function get-color-tokens($config) { | ||
$foreground: map.get($config, foreground); | ||
$background: map.get($config, background); | ||
|
||
@return ( | ||
background-color: theming.get-color-from-palette($background, 'card'), | ||
|
||
header-headline-color: theming.get-color-from-palette($foreground, text), | ||
row-item-label-text-color: theming.get-color-from-palette($foreground, text), | ||
row-item-outline-color: theming.get-color-from-palette($foreground, divider), | ||
); | ||
} | ||
|
||
// Tokens that can be configured through Angular Material's typography theming API. | ||
@function get-typography-tokens($config) { | ||
$fallback-font: typography-utils.font-family($config); | ||
$cell-font-family: typography-utils.font-family($config, body-2) or $fallback-font; | ||
$cell-line-height: typography-utils.line-height($config, body-2); | ||
$cell-font-size: typography-utils.font-size($config, body-2); | ||
$cell-font-weight: typography-utils.font-weight($config, body-2); | ||
$cell-letter-spacing: typography-utils.letter-spacing($config, body-2); | ||
|
||
@return ( | ||
header-headline-font: typography-utils.font-family($config, subtitle-2) or $fallback-font, | ||
header-headline-line-height: typography-utils.line-height($config, subtitle-2), | ||
header-headline-size: typography-utils.font-size($config, subtitle-2), | ||
header-headline-weight: typography-utils.font-weight($config, subtitle-2), | ||
header-headline-tracking: typography-utils.letter-spacing($config, subtitle-2), | ||
|
||
// Plain cells and footer cells have the same typography. | ||
row-item-label-text-font: $cell-font-family, | ||
row-item-label-text-line-height: $cell-line-height, | ||
row-item-label-text-size: $cell-font-size, | ||
row-item-label-text-weight: $cell-font-weight, | ||
row-item-label-text-tracking: $cell-letter-spacing, | ||
|
||
footer-supporting-text-font: $cell-font-family, | ||
footer-supporting-text-line-height: $cell-line-height, | ||
footer-supporting-text-size: $cell-font-size, | ||
footer-supporting-text-weight: $cell-font-weight, | ||
footer-supporting-text-tracking: $cell-letter-spacing, | ||
); | ||
} | ||
|
||
// Tokens that can be configured through Angular Material's density theming API. | ||
@function get-density-tokens($config) { | ||
$scale: theming.clamp-density($config, -4); | ||
$header-scale: ( | ||
0: 56px, | ||
-1: 52px, | ||
-2: 48px, | ||
-3: 44px, | ||
-4: 40px | ||
); | ||
$cell-scale: ( | ||
0: 52px, | ||
-1: 48px, | ||
-2: 44px, | ||
-3: 40px, | ||
-4: 36px | ||
); | ||
|
||
@return ( | ||
header-container-height: map.get($header-scale, $scale), | ||
footer-container-height: map.get($cell-scale, $scale), | ||
row-item-container-height: map.get($cell-scale, $scale), | ||
); | ||
} | ||
|
||
// Combines the tokens generated by the above functions into a single map with placeholder values. | ||
// This is used to create token slots. | ||
@function get-token-slots() { | ||
@return sass-utils.deep-merge-all( | ||
get-unthemable-tokens(), | ||
get-color-tokens(token-utils.$placeholder-color-config), | ||
get-typography-tokens(token-utils.$placeholder-typography-config), | ||
get-density-tokens(token-utils.$placeholder-density-config) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,44 @@ | ||
@use 'sass:map'; | ||
@use '@material/theme/theme-color' as mdc-theme-color; | ||
@use '@material/data-table/data-table' as mdc-data-table; | ||
@use '@material/data-table' as mdc-data-table-theme; | ||
@use '../core/tokens/m2/mat/table' as tokens-mat-table; | ||
@use '../core/theming/theming'; | ||
@use '../core/mdc-helpers/mdc-helpers'; | ||
@use '../core/typography/typography'; | ||
@use '../core/tokens/token-utils'; | ||
|
||
@mixin color($config-or-theme) { | ||
$config: theming.get-color-config($config-or-theme); | ||
// Save original values of MDC global variables. We need to save these so we can restore the | ||
// variables to their original values and prevent unintended side effects from using this mixin. | ||
$orig-selected-row-fill-color: mdc-data-table-theme.$selected-row-fill-color; | ||
$orig-divider-color: mdc-data-table-theme.$divider-color; | ||
$orig-row-hover-fill-color: mdc-data-table-theme.$row-hover-fill-color; | ||
$orig-header-row-text-color: mdc-data-table-theme.$header-row-text-color; | ||
$orig-row-text-color: mdc-data-table-theme.$row-text-color; | ||
$orig-stroke-color: mdc-data-table-theme.$stroke-color; | ||
|
||
@include mdc-helpers.using-mdc-theme($config) { | ||
mdc-data-table-theme.$selected-row-fill-color: rgba(mdc-theme-color.prop-value(primary), 0.04); | ||
mdc-data-table-theme.$divider-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12); | ||
mdc-data-table-theme.$row-hover-fill-color: rgba(mdc-theme-color.prop-value(on-surface), 0.04); | ||
mdc-data-table-theme.$header-row-text-color: rgba(mdc-theme-color.prop-value(on-surface), 0.87); | ||
mdc-data-table-theme.$row-text-color: rgba(mdc-theme-color.prop-value(on-surface), 0.87); | ||
mdc-data-table-theme.$stroke-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12); | ||
|
||
@include mdc-data-table.table-styles($query: mdc-helpers.$mdc-theme-styles-query); | ||
@mixin _output-tokens { | ||
crisbeto marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use this function instead: https://github.com/angular/components/blob/main/src/material/core/style/_sass-utils.scss#L11 Also, I'm not sure we need to clean it up, I'd like to move toward emitting the tokens under the user's selector (or document root if they don't specify one) |
||
@if (&) { | ||
@content; | ||
} | ||
@else { | ||
html { | ||
@content; | ||
} | ||
} | ||
} | ||
|
||
// Restore original values of MDC global variables. | ||
mdc-data-table-theme.$selected-row-fill-color: $orig-selected-row-fill-color; | ||
mdc-data-table-theme.$divider-color: $orig-divider-color; | ||
mdc-data-table-theme.$row-hover-fill-color: $orig-row-hover-fill-color; | ||
mdc-data-table-theme.$header-row-text-color: $orig-header-row-text-color; | ||
mdc-data-table-theme.$row-text-color: $orig-row-text-color; | ||
mdc-data-table-theme.$stroke-color: $orig-stroke-color; | ||
@mixin color($config-or-theme) { | ||
$config: theming.get-color-config($config-or-theme); | ||
|
||
.mat-mdc-table { | ||
$background: map.get($config, background); | ||
background: theming.get-color-from-palette($background, 'card'); | ||
@include _output-tokens { | ||
@include token-utils.create-token-values(tokens-mat-table.$prefix, | ||
tokens-mat-table.get-color-tokens($config)); | ||
} | ||
} | ||
|
||
@mixin typography($config-or-theme) { | ||
$config: typography.private-typography-to-2018-config( | ||
theming.get-typography-config($config-or-theme)); | ||
@include mdc-helpers.using-mdc-typography($config) { | ||
@include mdc-data-table.table-styles($query: mdc-helpers.$mdc-typography-styles-query); | ||
|
||
@include _output-tokens { | ||
@include token-utils.create-token-values(tokens-mat-table.$prefix, | ||
tokens-mat-table.get-typography-tokens($config)); | ||
} | ||
} | ||
|
||
@mixin density($config-or-theme) { | ||
$density-scale: theming.get-density-config($config-or-theme); | ||
.mat-mdc-table { | ||
@include mdc-data-table-theme.density($density-scale, | ||
$query: mdc-helpers.$mdc-base-styles-query); | ||
|
||
@include _output-tokens { | ||
@include token-utils.create-token-values(tokens-mat-table.$prefix, | ||
tokens-mat-table.get-density-tokens($density-scale)); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we comment that having
emit-fallback = true
would be for temporary workarounds?