Skip to content

Commit e05795d

Browse files
committed
fix(material/button): calculate icon button padding based on tokens
Uses token values to calculate the padding for icon buttons, instead of doing it statically.
1 parent 1e48cd4 commit e05795d

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/material/button/icon-button.scss

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@use 'sass:map';
12
@use '@material/icon-button/icon-button' as mdc-icon-button;
23
@use '@material/icon-button/icon-button-theme' as mdc-icon-button-theme;
34
@use '@material/theme/custom-properties' as mdc-custom-properties;
@@ -18,15 +19,14 @@
1819

1920
.mat-mdc-icon-button {
2021
// Add the official slots for the MDC component.
21-
@include mdc-icon-button-theme.theme-styles($token-slots);
22+
@include mdc-icon-button-theme.theme-styles(map.merge($token-slots, (
23+
// Exclude the state layer size since we'll re-emit it below with a default value.
24+
state-layer-size: null,
25+
)));
2226
}
2327
}
2428

2529
.mat-mdc-icon-button {
26-
// Not all applications import the theming which would apply a default padding.
27-
// TODO: Determine how to enforce theming exists, otherwise padding will be unset.
28-
padding: 12px;
29-
3030
// Border radius is inherited by ripple to know its shape. Set to 50% so the ripple is round.
3131
border-radius: 50%;
3232

@@ -42,6 +42,18 @@
4242

4343
@include token-utils.use-tokens(
4444
tokens-mdc-icon-button.$prefix, tokens-mdc-icon-button.get-token-slots()) {
45+
$button-size: var(#{token-utils.get-token-variable(state-layer-size)}, 48px);
46+
$icon-size: var(#{token-utils.get-token-variable(icon-size)}, 24px);
47+
48+
// We emit these tokens ourselves here so we can provide a default value.
49+
// This avoids a lot internal breakages in apps that didn't include the icon button theme.
50+
width: $button-size;
51+
height: $button-size;
52+
53+
// Note: this is wrapped in an interpolation, because of an internal lint rule that bans
54+
// interpolations in `calc`, even though this is the only way to achieve what we're looking for.
55+
padding: #{calc(#{calc(#{$button-size} - #{$icon-size})} / 2)};
56+
4557
// Icon size used to be placed on the host element. Now, in `theme-styles` it is placed on
4658
// the unused `.mdc-button__icon` class. Explicitly set the font-size here.
4759
@include token-utils.create-token-slot(font-size, icon-size);

src/material/core/tokens/m2/mdc/_icon-button.scss

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
@use 'sass:map';
12
@use '../../../style/sass-utils';
3+
@use '../../../theming/theming';
24
@use '../../../theming/inspection';
35
@use '../../token-utils';
46

@@ -13,12 +15,6 @@ $prefix: (mdc, icon-button);
1315
// our CSS.
1416
@function get-unthemable-tokens() {
1517
@return (
16-
// =============================================================================================
17-
// = TOKENS THAT SHOULD NOT BE CUSTOMIZABLE =
18-
// =============================================================================================
19-
// Determines the size of the icon. Name is inaccurate - applies to the whole component,
20-
// not just the state layer.
21-
state-layer-size: 48px,
2218
// MDC's icon size applied to svg and img elements inside the component
2319
icon-size: 24px,
2420

@@ -68,7 +64,19 @@ $prefix: (mdc, icon-button);
6864

6965
// Tokens that can be configured through Angular Material's density theming API.
7066
@function get-density-tokens($theme) {
71-
@return ();
67+
$scale: theming.clamp-density(inspection.get-theme-density($theme), -5);
68+
69+
@return (
70+
// The diameter of the checkbox's ripple.
71+
state-layer-size: map.get((
72+
0: 48px,
73+
-1: 44px,
74+
-2: 40px,
75+
-3: 36px,
76+
-4: 32px,
77+
-5: 28px,
78+
), $scale)
79+
);
7280
}
7381

7482
// Combines the tokens generated by the above functions into a single map with placeholder values.

0 commit comments

Comments
 (0)