Skip to content

refactor: move density tokens to their token files #30839

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 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/material/autocomplete/_autocomplete-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
// There are no M3 density tokens for this component
} @else {
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values-mixed(
Expand Down
2 changes: 1 addition & 1 deletion src/material/badge/_badge-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
/// @param {Map} $theme The theme to generate density styles for.
@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
// There are no M3 density tokens for this component
} @else {
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/material/bottom-sheet/_bottom-sheet-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
// There are no M3 density tokens for this component
} @else {
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/material/button-toggle/_button-toggle-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@use '../core/tokens/token-utils';
@use '../core/typography/typography';
@use './m2-button-toggle';
@use './m3-button-toggle';

/// Outputs base theme styles (styles not dependent on the color, typography, or density settings)
/// for the mat-button-toggle.
Expand Down Expand Up @@ -59,7 +60,8 @@
/// @param {Map} $theme The theme to generate density styles for.
@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
@include token-utils.create-token-values(
m3-button-toggle.$prefix, m3-button-toggle.get-density-tokens($theme));
} @else {
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values-mixed(
Expand Down Expand Up @@ -97,6 +99,9 @@
@include theming.private-check-duplicate-theme-styles($theme, 'mat-button-toggle') {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
@if inspection.theme-has($theme, density) {
@include density($theme);
}
} @else {
@include base($theme);
@if inspection.theme-has($theme, color) {
Expand Down
13 changes: 13 additions & 0 deletions src/material/button-toggle/_m3-button-toggle.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@use 'sass:map';
@use 'sass:list';
@use '../core/style/sass-utils';
@use '../core/tokens/m3-utils';
@use '../core/theming/theming';
@use '../core/theming/inspection';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, button-toggle);
Expand Down Expand Up @@ -58,3 +61,13 @@ $prefix: (mat, button-toggle);

@return m3-utils.namespace($prefix, ($tokens, $color-variants), $token-slots);
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($theme) {
$scale: theming.clamp-density(inspection.get-theme-density($theme), -4);
$index: ($scale * -1) + 1;

@return (
height: list.nth((40px, 40px, 40px, 36px, 24px), $index),
);
}
7 changes: 6 additions & 1 deletion src/material/button/_button-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@use '../core/tokens/token-utils';
@use '../core/typography/typography';
@use './m2-button';
@use './m3-button';

@mixin _button-variant($theme, $palette) {
$mat-tokens: if(
Expand Down Expand Up @@ -91,7 +92,8 @@
/// @param {Map} $theme The theme to generate density styles for.
@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
@include token-utils.create-token-values(
m3-button.$prefix, m3-button.get-density-tokens($theme));
} @else {
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values-mixed(
Expand Down Expand Up @@ -127,6 +129,9 @@
@include theming.private-check-duplicate-theme-styles($theme, 'mat-button') {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
@if inspection.theme-has($theme, density) {
@include density($theme);
}
} @else {
@include base($theme);
@if inspection.theme-has($theme, color) {
Expand Down
6 changes: 5 additions & 1 deletion src/material/button/_fab-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@use '../core/theming/inspection';
@use '../core/theming/validation';
@use './m2-fab';
@use './m3-fab';
@use '../core/tokens/token-utils';
@use '../core/typography/typography';

Expand Down Expand Up @@ -80,7 +81,7 @@
/// @param {Map} $theme The theme to generate density styles for.
@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
@include token-utils.create-token-values(m3-fab.$prefix, m3-fab.get-density-tokens($theme));
} @else {
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values-mixed(
Expand Down Expand Up @@ -116,6 +117,9 @@
@include theming.private-check-duplicate-theme-styles($theme, 'mat-fab') {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
@if inspection.theme-has($theme, density) {
@include density($theme);
}
} @else {
@include base($theme);
@if inspection.theme-has($theme, color) {
Expand Down
7 changes: 6 additions & 1 deletion src/material/button/_icon-button-theme.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@use 'sass:map';
@use 'sass:math';
@use './m2-icon-button';
@use './m3-icon-button';
@use '../core/style/sass-utils';
@use '../core/tokens/token-utils';
@use '../core/theming/theming';
Expand Down Expand Up @@ -74,7 +75,8 @@

@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
@include token-utils.create-token-values(
m3-icon-button.$prefix, m3-icon-button.get-density-tokens($theme));
} @else {
$icon-size: 24px;
$density-scale: inspection.get-theme-density($theme);
Expand Down Expand Up @@ -137,6 +139,9 @@
@include theming.private-check-duplicate-theme-styles($theme, 'mat-icon-button') {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
@if inspection.theme-has($theme, density) {
@include density($theme);
}
} @else {
@include base($theme);
@if inspection.theme-has($theme, color) {
Expand Down
27 changes: 22 additions & 5 deletions src/material/button/_m3-button.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@use 'sass:map';
@use 'sass:list';
@use '../core/tokens/m3-utils';
@use '../core/style/sass-utils';
@use '../core/style/elevation';
@use '../core/theming/theming';
@use '../core/theming/inspection';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, button);
Expand All @@ -14,7 +17,6 @@ $prefix: (mat, button);
@function get-tokens($systems, $exclude-hardcoded, $token-slots) {
$tokens: (
filled-container-color: map.get($systems, md-sys-color, primary),
filled-container-height: if($exclude-hardcoded, null, 40px),
filled-container-shape: map.get($systems, md-sys-shape, corner-full),
filled-disabled-container-color: sass-utils.safe-color-change(
map.get($systems, md-sys-color, on-surface), $alpha: 0.12),
Expand All @@ -37,7 +39,6 @@ $prefix: (mat, button);
map.get($systems, md-sys-color, on-primary),
$alpha: map.get($systems, md-sys-state, pressed-state-layer-opacity) ),
filled-state-layer-color: map.get($systems, md-sys-color, on-primary),
outlined-container-height: if($exclude-hardcoded, null, 40px),
outlined-container-shape: map.get($systems, md-sys-shape, corner-full),
outlined-disabled-label-text-color: sass-utils.safe-color-change(
map.get($systems, md-sys-color, on-surface), $alpha: 0.38),
Expand All @@ -64,7 +65,6 @@ $prefix: (mat, button);
outlined-state-layer-color: map.get($systems, md-sys-color, primary),
protected-container-color: map.get($systems, md-sys-color, surface),
protected-container-elevation-shadow: map.get($systems, md-sys-elevation, level1),
protected-container-height: if($exclude-hardcoded, null, 40px),
protected-container-shape: map.get($systems, md-sys-shape, corner-full),
protected-disabled-container-color: sass-utils.safe-color-change(
map.get($systems, md-sys-color, on-surface), $alpha: 0.12),
Expand All @@ -91,7 +91,6 @@ $prefix: (mat, button);
map.get($systems, md-sys-color, primary),
$alpha: map.get($systems, md-sys-state, pressed-state-layer-opacity)),
protected-state-layer-color: map.get($systems, md-sys-color, primary),
text-container-height: if($exclude-hardcoded, null, 40px),
text-container-shape: map.get($systems, md-sys-shape, corner-full),
text-disabled-label-text-color: sass-utils.safe-color-change(
map.get($systems, md-sys-color, on-surface), $alpha: 0.38),
Expand All @@ -113,7 +112,6 @@ $prefix: (mat, button);
text-state-layer-color: map.get($systems, md-sys-color, primary),
text-with-icon-horizontal-padding: m3-utils.hardcode(16px, $exclude-hardcoded),
tonal-container-color: map.get($systems, md-sys-color, secondary-container),
tonal-container-height: if($exclude-hardcoded, null, 40px),
tonal-container-shape: map.get($systems, md-sys-shape, corner-full),
tonal-disabled-container-color: sass-utils.safe-color-change(
map.get($systems, md-sys-color, on-surface), $alpha: 0.12),
Expand Down Expand Up @@ -241,3 +239,22 @@ $prefix: (mat, button);

@return m3-utils.namespace($prefix, ($tokens, $variant-tokens), $token-slots);
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($theme) {
$scale: theming.clamp-density(inspection.get-theme-density($theme), -3);
$index: ($scale * -1) + 1;

@return (
filled-touch-target-display: list.nth((block, block, none, none), $index),
filled-container-height: list.nth((40px, 36px, 32px, 28px), $index),
outlined-container-height: list.nth((40px, 36px, 32px, 28px), $index),
outlined-touch-target-display: list.nth((block, block, none, none), $index),
protected-touch-target-display: list.nth((block, block, none, none), $index),
protected-container-height: list.nth((40px, 36px, 32px, 28px), $index),
text-touch-target-display: list.nth((block, block, none, none), $index),
text-container-height: list.nth((40px, 36px, 32px, 28px), $index),
tonal-container-height: list.nth((40px, 36px, 32px, 28px), $index),
tonal-touch-target-display: list.nth((block, block, none, none), $index),
);
}
13 changes: 13 additions & 0 deletions src/material/button/_m3-fab.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@use 'sass:map';
@use 'sass:list';
@use '../core/style/sass-utils';
@use '../core/tokens/m3-utils';
@use '../core/style/elevation';
@use '../core/theming/theming';
@use '../core/theming/inspection';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, fab);
Expand Down Expand Up @@ -172,3 +175,13 @@ $prefix: (mat, fab);

@return m3-utils.namespace($prefix, ($tokens, $variant-tokens), $token-slots);
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($theme) {
$scale: theming.clamp-density(inspection.get-theme-density($theme), -3);
$index: ($scale * -1) + 1;

@return (
touch-target-display: list.nth((block, block, none, none), $index),
);
}
16 changes: 15 additions & 1 deletion src/material/button/_m3-icon-button.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@use 'sass:map';
@use 'sass:list';
@use '../core/tokens/m3-utils';
@use '../core/style/sass-utils';
@use '../core/theming/theming';
@use '../core/theming/inspection';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, icon-button);
Expand All @@ -24,7 +27,6 @@ $prefix: (mat, icon-button);
disabled-icon-color: sass-utils.safe-color-change(
map.get($systems, md-sys-color, on-surface), $alpha: 0.38),
icon-size: if($exclude-hardcoded, null, 24px),
state-layer-size: if($exclude-hardcoded, null, 40px),
icon-color: map.get($systems, md-sys-color, on-surface-variant),
);

Expand Down Expand Up @@ -65,3 +67,15 @@ $prefix: (mat, icon-button);

@return m3-utils.namespace($prefix, ($tokens, $variant-tokens), $token-slots);
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($theme) {
$scale: theming.clamp-density(inspection.get-theme-density($theme), -5);
$index: ($scale * -1) + 1;

@return (
touch-target-display: list.nth((block, block, none, none, none, none), $index),
// The size caps out at 24px, because anything lower will be smaller than the icon.
state-layer-size: list.nth((40px, 36px, 32px, 28px, 24px, 24px), $index),
);
}
2 changes: 1 addition & 1 deletion src/material/card/_card-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
// There are no M3 density tokens for this component
} @else {
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values-mixed(
Expand Down
7 changes: 6 additions & 1 deletion src/material/checkbox/_checkbox-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@use '../core/tokens/token-utils';
@use '../core/typography/typography';
@use './m2-checkbox';
@use './m3-checkbox';

/// Outputs base theme styles (styles not dependent on the color, typography, or density settings)
/// for the mat-checkbox.
Expand Down Expand Up @@ -75,7 +76,8 @@
$density-scale: inspection.get-theme-density($theme);

@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
@include token-utils.create-token-values(
m3-checkbox.$prefix, m3-checkbox.get-density-tokens($theme));
} @else {
@include sass-utils.current-selector-or-root() {
@include token-utils.create-token-values-mixed(
Expand Down Expand Up @@ -111,6 +113,9 @@
@include theming.private-check-duplicate-theme-styles($theme, 'mat-checkbox') {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
@if inspection.theme-has($theme, density) {
@include density($theme);
}
} @else {
@include base($theme);
@if inspection.theme-has($theme, color) {
Expand Down
15 changes: 14 additions & 1 deletion src/material/checkbox/_m3-checkbox.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@use 'sass:map';
@use 'sass:list';
@use '../core/style/sass-utils';
@use '../core/tokens/m3-utils';
@use '../core/theming/theming';
@use '../core/theming/inspection';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, checkbox);
Expand Down Expand Up @@ -37,7 +40,6 @@ $prefix: (mat, checkbox);
selected-pressed-state-layer-color: map.get($systems, md-sys-color, on-surface),
selected-pressed-state-layer-opacity:
map.get($systems, md-sys-state, pressed-state-layer-opacity),
state-layer-size: if($exclude-hardcoded, null, 40px),
unselected-focus-icon-color: map.get($systems, md-sys-color, on-surface),
unselected-focus-state-layer-color: map.get($systems, md-sys-color, on-surface),
unselected-focus-state-layer-opacity:
Expand Down Expand Up @@ -88,3 +90,14 @@ $prefix: (mat, checkbox);

@return m3-utils.namespace($prefix, ($tokens, $variant-tokens), $token-slots);
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($theme) {
$scale: theming.clamp-density(inspection.get-theme-density($theme), -3);
$index: ($scale * -1) + 1;

@return (
touch-target-display: list.nth((block, block, none, none), $index),
state-layer-size: list.nth((40px, 36px, 32px, 28px), $index)
);
}
6 changes: 5 additions & 1 deletion src/material/chips/_chips-theme.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@use 'sass:color';
@use './m2-chip';
@use './m3-chip';
@use '../core/tokens/token-utils';
@use '../core/theming/theming';
@use '../core/theming/inspection';
Expand Down Expand Up @@ -72,7 +73,7 @@
/// @param {Map} $theme The theme to generate density styles for.
@mixin density($theme) {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
@include token-utils.create-token-values(m3-chip.$prefix, m3-chip.get-density-tokens($theme));
} @else {
.mat-mdc-chip.mat-mdc-standard-chip {
@include token-utils.create-token-values-mixed(
Expand Down Expand Up @@ -107,6 +108,9 @@
@include theming.private-check-duplicate-theme-styles($theme, 'mat-chips') {
@if inspection.get-theme-version($theme) == 1 {
@include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
@if inspection.theme-has($theme, density) {
@include density($theme);
}
} @else {
@include base($theme);
@if inspection.theme-has($theme, color) {
Expand Down
Loading
Loading