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(material/card): support filled variant #29868

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<mat-card>
<mat-card-content>Elevated</mat-card-content>
</mat-card>

<mat-card appearance="outlined">
<mat-card-content>Simple card</mat-card-content>
<mat-card-content>Outlined</mat-card-content>
</mat-card>

<mat-card appearance="filled">
<mat-card-content>Filled</mat-card-content>
</mat-card>
2 changes: 1 addition & 1 deletion src/dev-app/card/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ng_module(
deps = [
"//src/material/button",
"//src/material/card",
"//src/material/checkbox",
"//src/material/radio",
],
)

Expand Down
6 changes: 5 additions & 1 deletion src/dev-app/card/card-demo.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<div class="demo-card-container">
<mat-checkbox (change)="toggleAppearance()">Use outlined cards</mat-checkbox>
<mat-radio-group (change)="appearance = $event.value">
<mat-radio-button value="raised">Raised</mat-radio-button>
<mat-radio-button value="outlined">Outlined</mat-radio-button>
<mat-radio-button value="filled">Filled</mat-radio-button>
</mat-radio-group>

<!-- TODO(jelbourn): re-add dividers and footers with progress bars once the MDC versions exist -->
<mat-card [appearance]="appearance">
Expand Down
4 changes: 4 additions & 0 deletions src/dev-app/card/card-demo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
text-transform: uppercase;
}
}

mat-radio-group {
margin-bottom: 10px;
}
7 changes: 2 additions & 5 deletions src/dev-app/card/card-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/co
import {FormsModule} from '@angular/forms';
import {MatButtonModule} from '@angular/material/button';
import {MatCardAppearance, MatCardModule} from '@angular/material/card';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {MatRadioModule} from '@angular/material/radio';

@Component({
selector: 'card-demo',
templateUrl: 'card-demo.html',
styleUrl: 'card-demo.css',
encapsulation: ViewEncapsulation.None,
standalone: true,
imports: [MatCardModule, MatButtonModule, MatCheckboxModule, FormsModule],
imports: [MatCardModule, MatButtonModule, MatRadioModule, FormsModule],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CardDemo {
Expand All @@ -29,7 +29,4 @@ export class CardDemo {
As of some one gently rapping, rapping at my chamber door.
“’Tis some visitor,” I muttered, “tapping at my chamber door—
Only this and nothing more.”`;
toggleAppearance() {
this.appearance = this.appearance == 'raised' ? 'outlined' : 'raised';
}
}
26 changes: 26 additions & 0 deletions src/material/card/_card-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@use '../core/tokens/m2/mat/card' as tokens-mat-card;
@use '../core/tokens/m2/mdc/elevated-card' as tokens-mdc-elevated-card;
@use '../core/tokens/m2/mdc/outlined-card' as tokens-mdc-outlined-card;
@use '../core/tokens/m2/mdc/filled-card' as tokens-mdc-filled-card;

@mixin base($theme) {
@if inspection.get-theme-version($theme) == 1 {
Expand All @@ -22,6 +23,10 @@
tokens-mdc-outlined-card.$prefix,
tokens-mdc-outlined-card.get-unthemable-tokens()
);
@include token-utils.create-token-values(
tokens-mdc-filled-card.$prefix,
tokens-mdc-filled-card.get-unthemable-tokens()
);
@include token-utils.create-token-values(
tokens-mat-card.$prefix,
tokens-mat-card.get-unthemable-tokens()
Expand All @@ -43,6 +48,10 @@
tokens-mdc-outlined-card.$prefix,
tokens-mdc-outlined-card.get-color-tokens($theme)
);
@include token-utils.create-token-values(
tokens-mdc-filled-card.$prefix,
tokens-mdc-filled-card.get-color-tokens($theme)
);
@include token-utils.create-token-values(
tokens-mat-card.$prefix,
tokens-mat-card.get-color-tokens($theme)
Expand All @@ -64,6 +73,10 @@
tokens-mdc-outlined-card.$prefix,
tokens-mdc-outlined-card.get-typography-tokens($theme)
);
@include token-utils.create-token-values(
tokens-mdc-filled-card.$prefix,
tokens-mdc-filled-card.get-typography-tokens($theme)
);
@include token-utils.create-token-values(
tokens-mat-card.$prefix,
tokens-mat-card.get-typography-tokens($theme)
Expand All @@ -85,6 +98,10 @@
tokens-mdc-outlined-card.$prefix,
tokens-mdc-outlined-card.get-density-tokens($theme)
);
@include token-utils.create-token-values(
tokens-mdc-filled-card.$prefix,
tokens-mdc-filled-card.get-density-tokens($theme)
);
@include token-utils.create-token-values(
tokens-mat-card.$prefix,
tokens-mat-card.get-density-tokens($theme)
Expand All @@ -109,6 +126,11 @@
namespace: tokens-mdc-outlined-card.$prefix,
tokens: tokens-mdc-outlined-card.get-token-slots(),
prefix: 'outlined-',
),
(
namespace: tokens-mdc-filled-card.$prefix,
tokens: tokens-mdc-filled-card.get-token-slots(),
prefix: 'filled-',
)
);
}
Expand Down Expand Up @@ -145,6 +167,10 @@
tokens-mdc-outlined-card.$prefix,
map.get($tokens, tokens-mdc-outlined-card.$prefix)
);
@include token-utils.create-token-values(
tokens-mdc-filled-card.$prefix,
map.get($tokens, tokens-mdc-filled-card.$prefix)
);
@include token-utils.create-token-values(
tokens-mat-card.$prefix,
map.get($tokens, tokens-mat-card.$prefix)
Expand Down
12 changes: 12 additions & 0 deletions src/material/card/card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@use '../core/tokens/m2/mat/card' as tokens-mat-card;
@use '../core/tokens/m2/mdc/elevated-card' as tokens-mdc-elevated-card;
@use '../core/tokens/m2/mdc/outlined-card' as tokens-mdc-outlined-card;
@use '../core/tokens/m2/mdc/filled-card' as tokens-mdc-filled-card;

// Size of the `mat-card-header` region custom to Angular Material.
$mat-card-header-size: 40px !default;
Expand Down Expand Up @@ -68,6 +69,17 @@ $mat-card-default-padding: 16px !default;
}
}

.mat-mdc-card-filled {
@include token-utils.use-tokens(
tokens-mdc-filled-card.$prefix,
tokens-mdc-filled-card.get-token-slots()
) {
@include token-utils.create-token-slot(background-color, container-color);
@include token-utils.create-token-slot(border-radius, container-shape);
@include token-utils.create-token-slot(box-shadow, container-elevation);
}
}

.mdc-card__media {
position: relative;
box-sizing: border-box;
Expand Down
4 changes: 3 additions & 1 deletion src/material/card/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
inject,
} from '@angular/core';

export type MatCardAppearance = 'outlined' | 'raised';
export type MatCardAppearance = 'outlined' | 'raised' | 'filled';

/** Object that can be used to configure the default options for the card module. */
export interface MatCardConfig {
Expand All @@ -41,6 +41,8 @@ export const MAT_CARD_CONFIG = new InjectionToken<MatCardConfig>('MAT_CARD_CONFI
'class': 'mat-mdc-card mdc-card',
'[class.mat-mdc-card-outlined]': 'appearance === "outlined"',
'[class.mdc-card--outlined]': 'appearance === "outlined"',
'[class.mat-mdc-card-filled]': 'appearance === "filled"',
'[class.mdc-card--filled]': 'appearance === "filled"',
},
exportAs: 'matCard',
encapsulation: ViewEncapsulation.None,
Expand Down
4 changes: 3 additions & 1 deletion src/material/core/tokens/m2/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
@use './mdc/switch' as tokens-mdc-switch;
@use './mdc/secondary-navigation-tab' as tokens-mdc-secondary-navigation-tab;
@use './mdc/tab-indicator' as tokens-mdc-tab-indicator;
@use './mdc/filled-card' as tokens-mdc-filled-card;
@use '../../theming/inspection';

/// Gets the tokens for the given theme, m2 tokens module, and theming system.
Expand Down Expand Up @@ -182,6 +183,7 @@
_get-tokens-for-module($theme, tokens-mdc-switch),
_get-tokens-for-module($theme, tokens-mdc-tab-indicator),
_get-tokens-for-module($theme, tokens-mdc-secondary-navigation-tab),
_get-tokens-for-module($theme, tokens-mdc-text-button)
_get-tokens-for-module($theme, tokens-mdc-text-button),
_get-tokens-for-module($theme, tokens-mdc-filled-card),
);
}
81 changes: 81 additions & 0 deletions src/material/core/tokens/m2/mdc/_filled-card.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
@use '../../../style/elevation';
@use '../../../theming/inspection';
@use '../../../style/sass-utils';
@use '../../token-definition';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mdc, filled-card);

// Tokens that can't be configured through Angular Material's current theming API,
// but may be in a future version of the theming API.
//
// Tokens that are available in MDC, but not used in Angular Material should be mapped to `null`.
// `null` indicates that we are intentionally choosing not to emit a slot or value for the token in
// our CSS.
@function get-unthemable-tokens() {
@return (
// The border-radius of the card.
container-shape: 4px,
// =============================================================================================
// = TOKENS NOT USED IN ANGULAR MATERIAL =
// =============================================================================================
// Angular Material's card is not an interactive element, and therefore does not support states.
disabled-container-elevation: null,
disabled-outline-color: null,
disabled-outline-opacity: null,
dragged-container-elevation: null,
dragged-outline-color: null,
dragged-state-layer-color: null,
dragged-state-layer-opacity: null,
focus-container-elevation: null,
focus-outline-color: null,
focus-state-layer-color: null,
focus-state-layer-opacity: null,
hover-container-elevation: null,
hover-outline-color: null,
hover-state-layer-color: null,
hover-state-layer-opacity: null,
pressed-container-elevation: null,
pressed-outline-color: null,
pressed-state-layer-color: null,
pressed-state-layer-opacity: null,
container-shadow-color: null,
// Angular Material does not currently support surface tint.
container-surface-tint-layer-color: null,
// MDC does not seem to use these tokens.
icon-color: null,
icon-size: null,
);
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($theme) {
$elevation: inspection.get-theme-color($theme, foreground, elevation);

@return (
// The background color of the card.
container-color: inspection.get-theme-color($theme, background, card),
container-elevation: elevation.get-box-shadow(0),
);
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($theme) {
@return ();
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($theme) {
@return ();
}

// 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-definition.$placeholder-color-config),
get-typography-tokens(token-definition.$placeholder-typography-config),
get-density-tokens(token-definition.$placeholder-density-config)
);
}
4 changes: 3 additions & 1 deletion src/material/core/tokens/m3/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
@use './mdc/switch' as tokens-mdc-switch;
@use './mdc/secondary-navigation-tab' as tokens-mdc-secondary-navigation-tab;
@use './mdc/tab-indicator' as tokens-mdc-tab-indicator;
@use './mdc/filled-card' as tokens-mdc-filled-card;

$_module-names: (
// Custom tokens
Expand Down Expand Up @@ -140,7 +141,8 @@ $_module-names: (
tokens-mdc-snack-bar,
tokens-mdc-switch,
tokens-mdc-secondary-navigation-tab,
tokens-mdc-tab-indicator
tokens-mdc-tab-indicator,
tokens-mdc-filled-card,
);

/// Gets the full set of M3 tokens for the given theme object.
Expand Down
22 changes: 22 additions & 0 deletions src/material/core/tokens/m3/mdc/_filled-card.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@use 'sass:map';
@use '../../../style/elevation';
@use '../../token-definition';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mdc, filled-card);

/// Generates the tokens for MDC filled-card
/// @param {Map} $systems The MDC system tokens
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
/// @param {Map} $token-slots Possible token slots
/// @return {Map} A set of tokens for the MDC filled-card
@function get-tokens($systems, $exclude-hardcoded, $token-slots) {
$tokens: token-definition.get-mdc-tokens('filled-card', $systems, $exclude-hardcoded);
$elevation: map.get($tokens, container-elevation);

@if ($elevation != null) {
$tokens: map.set($tokens, container-elevation, elevation.get-box-shadow($elevation));
}

@return token-definition.namespace-tokens($prefix, $tokens, $token-slots);
}
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/card.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class MatCardActions {
}

// @public (undocumented)
export type MatCardAppearance = 'outlined' | 'raised';
export type MatCardAppearance = 'outlined' | 'raised' | 'filled';

// @public
export class MatCardAvatar {
Expand Down
Loading