Skip to content

feat(material-experimental): add MDC-based mat-option and mdc-core entry point #19557

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 1 commit into from
Jul 29, 2020
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
/src/material-experimental/mdc-checkbox/** @mmalerba
/src/material-experimental/mdc-chips/** @mmalerba
/src/material-experimental/mdc-color/** @jelbourn @devversion
/src/material-experimental/mdc-core/** @crisbeto
/src/material-experimental/mdc-density/** @devversion
/src/material-experimental/mdc-dialog/** @devversion
/src/material-experimental/mdc-form-field/** @devversion @mmalerba
Expand Down
1 change: 1 addition & 0 deletions src/material-experimental/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ entryPoints = [
"mdc-checkbox/testing",
"mdc-chips",
"mdc-chips/testing",
"mdc-core",
"mdc-form-field",
"mdc-form-field/testing",
"mdc-input",
Expand Down
80 changes: 80 additions & 0 deletions src/material-experimental/mdc-core/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
load(
"//tools:defaults.bzl",
"ng_module",
"ng_test_library",
"ng_web_test_suite",
"sass_binary",
"sass_library",
)

package(default_visibility = ["//visibility:public"])

ng_module(
name = "mdc-core",
srcs = glob(
["**/*.ts"],
exclude = ["**/*.spec.ts"],
),
assets = [
":option/option.css",
":option/optgroup.css",
] + glob(["**/*.html"]),
module_name = "@angular/material-experimental/mdc-core",
deps = [
"//src/material/core",
"@npm//@angular/common",
"@npm//@angular/core",
"@npm//rxjs",
],
)

sass_library(
name = "mdc_core_scss_lib",
srcs = glob(["**/_*.scss"]),
)

sass_binary(
name = "option_scss",
src = "option/option.scss",
include_paths = [
"external/npm/node_modules",
],
deps = [
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",
],
)

sass_binary(
name = "optgroup_scss",
src = "option/optgroup.scss",
include_paths = [
"external/npm/node_modules",
],
deps = [
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",
],
)

#################
# Test targets
#################

ng_test_library(
name = "unit_test_sources",
srcs = glob(
["**/*.spec.ts"],
),
deps = [
":mdc-core",
"//src/cdk/keycodes",
"//src/cdk/testing/private",
"@npm//@angular/platform-browser",
],
)

ng_web_test_suite(
name = "unit_tests",
deps = [":unit_test_sources"],
)
15 changes: 15 additions & 0 deletions src/material-experimental/mdc-core/_core.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@import '../../material/core/theming/check-duplicate-styles';
@import './option/option-theme';
@import './option/optgroup-theme';

// Mixin that renders all of the core styles that depend on the theme.
@mixin mat-mdc-core-theme($theme-or-color-config) {
$theme: _mat-legacy-get-theme($theme-or-color-config);
// Wrap the sub-theme includes in the duplicate theme styles mixin. This ensures that
// there won't be multiple warnings. e.g. if `mat-mdc-core-theme` reports a warning, then
// the imported themes (such as `mat-ripple-theme`) should not report again.
@include _mat-check-duplicate-theme-styles($theme, 'mat-mdc-core') {
@include mat-mdc-option-theme($theme);
@include mat-mdc-optgroup-theme($theme);
}
}
9 changes: 9 additions & 0 deletions src/material-experimental/mdc-core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './public-api';
46 changes: 46 additions & 0 deletions src/material-experimental/mdc-core/option/_optgroup-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@import '@material/list/mixins.import';
@import '@material/list/variables.import';
@import '@material/theme/functions.import';
@import '../../mdc-helpers/mdc-helpers';
@import '../../../material/core/theming/check-duplicate-styles';

@mixin mat-mdc-optgroup-color($config-or-theme) {
$config: mat-get-color-config($config-or-theme);

@include mat-using-mdc-theme($config) {
.mat-mdc-optgroup-label {
// Since this will usually be rendered in an overlay,
// we have explicitly set the default color.
@include mdc-theme-prop(color, text-primary-on-background);
@include mdc-list-item-disabled-text-color($mdc-list-text-disabled-color,
$query: $mat-theme-styles-query);
}
}
}

@mixin mat-mdc-optgroup-typography($config-or-theme) {
$config: mat-get-typography-config($config-or-theme);
}

@mixin mat-mdc-optgroup-density($config-or-theme) {
$density-scale: mat-get-density-config($config-or-theme);
}

@mixin mat-mdc-optgroup-theme($theme-or-color-config) {
$theme: _mat-legacy-get-theme($theme-or-color-config);
@include _mat-check-duplicate-theme-styles($theme, 'mat-mdc-optgroup') {
$color: mat-get-color-config($theme);
$density: mat-get-density-config($theme);
$typography: mat-get-typography-config($theme);

@if $color != null {
@include mat-mdc-optgroup-color($color);
}
@if $density != null {
@include mat-mdc-optgroup-density($density);
}
@if $typography != null {
@include mat-mdc-optgroup-typography($typography);
}
}
}
68 changes: 68 additions & 0 deletions src/material-experimental/mdc-core/option/_option-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@import '@material/list/mixins.import';
@import '@material/list/variables.import';
@import '@material/theme/functions.import';
@import '../../mdc-helpers/mdc-helpers';
@import '../../../material/core/theming/check-duplicate-styles';

@mixin mat-mdc-option-color($config-or-theme) {
$config: mat-get-color-config($config-or-theme);

@include mat-using-mdc-theme($config) {
.mat-mdc-option {
// Since this will usually be rendered in an overlay,
// we have explicitly set the default color.
@include mdc-theme-prop(color, text-primary-on-background);
@include mdc-list-item-disabled-text-color($mdc-list-text-disabled-color,
$query: $mat-theme-styles-query);

&:hover:not(.mdc-list-item--disabled),
&:focus:not(.mdc-list-item--disabled),
&.mat-mdc-option-active,

// In multiple mode there is a checkbox to show that the option is selected.
&.mdc-list-item--selected:not(.mat-mdc-option-multiple):not(.mdc-list-item--disabled) {
$color: $mdc-theme-on-surface;
background: rgba($color, mdc-states-opacity($color, hover));
}
}

.mat-primary .mat-mdc-option.mdc-list-item--selected:not(.mdc-list-item--disabled) {
@include mdc-list-item-primary-text-ink-color(primary, $query: $mat-theme-styles-query);
}

.mat-accent .mat-mdc-option.mdc-list-item--selected:not(.mdc-list-item--disabled) {
@include mdc-list-item-primary-text-ink-color(secondary, $query: $mat-theme-styles-query);
}

.mat-warn .mat-mdc-option.mdc-list-item--selected:not(.mdc-list-item--disabled) {
@include mdc-list-item-primary-text-ink-color(error, $query: $mat-theme-styles-query);
}
}
}

@mixin mat-mdc-option-typography($config-or-theme) {
$config: mat-get-typography-config($config-or-theme);
}

@mixin mat-mdc-option-density($config-or-theme) {
$density-scale: mat-get-density-config($config-or-theme);
}

@mixin mat-mdc-option-theme($theme-or-color-config) {
$theme: _mat-legacy-get-theme($theme-or-color-config);
@include _mat-check-duplicate-theme-styles($theme, 'mat-mdc-option') {
$color: mat-get-color-config($theme);
$density: mat-get-density-config($theme);
$typography: mat-get-typography-config($theme);

@if $color != null {
@include mat-mdc-option-color($color);
}
@if $density != null {
@include mat-mdc-option-density($density);
}
@if $typography != null {
@include mat-mdc-option-typography($typography);
}
}
}
32 changes: 32 additions & 0 deletions src/material-experimental/mdc-core/option/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {MatRippleModule, MatPseudoCheckboxModule} from '@angular/material/core';
import {MatOption} from './option';
import {MatOptgroup} from './optgroup';


@NgModule({
imports: [MatRippleModule, CommonModule, MatPseudoCheckboxModule],
exports: [MatOption, MatOptgroup],
declarations: [MatOption, MatOptgroup]
})
export class MatOptionModule {}


export * from './option';
export * from './optgroup';
export {
MatOptionSelectionChange,
MatOptionParentComponent,
MAT_OPTION_PARENT_COMPONENT,
_countGroupLabelsBeforeOption,
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to re-export these internal things?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's something we're reusing in mat-select and mat-autocomplete.

_getOptionScrollPosition
} from '@angular/material/core';
8 changes: 8 additions & 0 deletions src/material-experimental/mdc-core/option/optgroup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<label
class="mat-mdc-optgroup-label"
[class.mdc-list-item--disabled]="disabled"
[id]="_labelId">
<span class="mdc-list-item__text">{{ label }} <ng-content></ng-content></span>
</label>

<ng-content select="mat-option, ng-container"></ng-content>
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, do we include ng-container in the existing version too? Feels kinda yucky, but I can see why we'd do that

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what we have in the current version. It's there, because sometimes people wrap multiple options in an ng-container so they can use a single ngIf rather than having to repeat it on each option.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I figured, wish Angular could fix it somehow instead of us trying to work around it. It works in this case because we can pretty much assume we know where the user is trying to put the ng-container, but its not always so clear

13 changes: 13 additions & 0 deletions src/material-experimental/mdc-core/option/optgroup.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@import '@material/list/mixins.import';
@import '@material/list/variables.import';
@import '../../mdc-helpers/mdc-helpers';

.mat-mdc-optgroup-label {
@include mdc-list-item-base_;
@include mdc-list-list-item-padding-variant(
$mdc-list-textual-variant-config, $query: $mat-base-styles-query);
@include mdc-list-list-item-height-variant(
$mdc-list-textual-variant-config, $query: $mat-base-styles-query);
@include mdc-list-item-disabled-text-opacity($mdc-list-text-disabled-opacity,
$query: $mat-base-styles-query);
}
32 changes: 32 additions & 0 deletions src/material-experimental/mdc-core/option/optgroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Component, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
import {_MatOptgroupBase} from '@angular/material/core';


/**
* Component that is used to group instances of `mat-option`.
*/
@Component({
selector: 'mat-optgroup',
exportAs: 'matOptgroup',
templateUrl: 'optgroup.html',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
inputs: ['disabled'],
styleUrls: ['optgroup.css'],
host: {
'class': 'mat-mdc-optgroup',
'role': 'group',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.aria-labelledby]': '_labelId',
}
})
export class MatOptgroup extends _MatOptgroupBase {
}
9 changes: 9 additions & 0 deletions src/material-experimental/mdc-core/option/option.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<mat-pseudo-checkbox *ngIf="multiple" class="mat-mdc-option-pseudo-checkbox"
[state]="selected ? 'checked' : 'unchecked'" [disabled]="disabled"></mat-pseudo-checkbox>

<span class="mdc-list-item__text"><ng-content></ng-content></span>

<div class="mat-mdc-option-ripple" mat-ripple
[matRippleTrigger]="_getHostElement()"
[matRippleDisabled]="disabled || disableRipple">
</div>
58 changes: 58 additions & 0 deletions src/material-experimental/mdc-core/option/option.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@import '@material/list/mixins.import';
@import '@material/list/variables.import';
@import '../../mdc-helpers/mdc-helpers';
@import '../../../material/core/style/vendor-prefixes';
@import '../../../cdk/a11y/a11y';

.mat-mdc-option {
// Note that we include this private mixin, because the public
// one adds a bunch of styles that we aren't using for the menu.
Copy link
Member

Choose a reason for hiding this comment

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

For a follow-up: It might be good capturing things we actually need. If we come back to this in the future, we don't know what is actually needed / what the public mixin currently provides that we don't need.

@include mdc-list-item-base_;
@include mdc-list-list-item-padding-variant(
$mdc-list-textual-variant-config, $query: $mat-base-styles-query);
@include mdc-list-list-item-height-variant(
$mdc-list-textual-variant-config, $query: $mat-base-styles-query);
@include mdc-list-item-disabled-text-opacity($mdc-list-text-disabled-opacity,
$query: $mat-base-styles-query);
@include user-select(none);

&:not(.mdc-list-item--disabled) {
cursor: pointer;
}

// Note that we bump the padding here, rather than padding inside the
// group so that ripples still reach to the edges of the panel.
.mat-mdc-optgroup &:not(.mat-mdc-option-multiple) {
padding-left: $mdc-list-side-padding * 2;

[dir='rtl'] & {
padding-left: $mdc-list-side-padding;
padding-right: $mdc-list-side-padding * 2;
}
}

.mat-pseudo-checkbox {
margin-right: $mdc-list-side-padding;

[dir='rtl'] & {
margin-right: 0;
margin-left: $mdc-list-side-padding;
}
}

// Increase specificity because ripple styles are part of the `mat-core` mixin and can
// potentially overwrite the absolute position of the container.
.mat-mdc-option-ripple {
@include mat-fill;

// Disable pointer events for the ripple container because the container will overlay the
// user content and we don't want to disable mouse events on the user content.
// Pointer events can be safely disabled because the ripple trigger element is the host element.
pointer-events: none;

// Prevents the ripple from completely covering the option in high contrast mode.
@include cdk-high-contrast(active, off) {
opacity: 0.5;
}
}
}
Loading