What is @material/feature-targeting?
@material/feature-targeting is a utility package from Material Components for the web (MDC Web) that provides a way to target specific CSS features. It allows developers to write CSS that is conditionally applied based on the presence of certain features, such as CSS variables or specific browser capabilities.
What are @material/feature-targeting's main functionalities?
CSS Variable Support
This feature allows you to write CSS rules that are only applied if the browser supports CSS variables. The mixin `supports-css-variables` checks for CSS variable support and applies the enclosed styles if the feature is available.
/* Import the feature targeting mixins */
@use '@material/feature-targeting/feature-targeting';
/* Define a CSS rule that only applies if CSS variables are supported */
@include feature-targeting.supports-css-variables() {
.my-class {
color: var(--my-color, black);
}
}
Custom Property Fallbacks
This feature allows you to provide fallback styles for browsers that do not support CSS variables. The mixin `supports-css-variables` is used to conditionally apply the CSS variable-based styles, while the fallback styles are applied unconditionally.
/* Import the feature targeting mixins */
@use '@material/feature-targeting/feature-targeting';
/* Define a CSS rule with a fallback for browsers that do not support CSS variables */
.my-class {
color: black;
@include feature-targeting.supports-css-variables() {
color: var(--my-color, black);
}
}
Other packages similar to @material/feature-targeting
postcss-preset-env
postcss-preset-env is a PostCSS plugin that allows you to use future CSS features today. It includes polyfills for CSS features that are not yet widely supported, similar to how @material/feature-targeting provides conditional CSS based on feature support. However, postcss-preset-env is more comprehensive and supports a wider range of CSS features.
modernizr
Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user’s browser. It allows you to conditionally apply styles and scripts based on the presence of specific features, similar to @material/feature-targeting. Modernizr provides a more extensive set of feature detections and is not limited to CSS features.
Feature Targeting
MDC Feature Targeting provides infrastructure to allow CSS styles to be included or excluded categorically.
Most of the time, you shouldn't need to depend on mdc-feature-targeting
directly. However, understanding it can be useful if you're interested in having more control over when certain types of MDC styles are emitted.
Installation
npm install @material/feature-targeting
Basic Usage
Styles
Authoring component styles:
@import "@material/feature-targeting/functions";
@import "@material/feature-targeting/mixins";
@mixin my-component-core-styles($query: mdc-feature-all()) {
$feat-structure: mdc-feature-create-target($query, structure);
@include mdc-feature-targets($feat-structure) {
}
}
Consuming component styles:
@import "@material/feature-targeting/functions";
@import "my-component-mixins";
@include my-component-core-styles;
@include my-component-core-styles(structure);
@include my-component-core-styles(mdc-feature-any(color, typography));
@include my-component-core-styles(mdc-feature-without(animation));
Sass Mixins and Functions
Mixin | Description |
---|
mdc-feature-targets($feature-targets...) | Conditionalizes content to only be emitted if the given feature target(s) is/are queried. |
Function | Description |
---|
mdc-feature-create-target($feature-query, $targeted-feature) | Returns a variable to be passed to mdc-feature-targets in order to filter emitted styles. |
mdc-feature-all($feature-queries...) | Returns a query object which will result in emitting mdc-feature-targets blocks that match all of the specified features. Passing no arguments results in all blocks being emitted, which is the most common use case. |
mdc-feature-any($feature-queries...) | Returns a query object which will result in emitting mdc-feature-targets blocks that match any of the specified features. Passing no arguments results in no blocks being emitted. |
mdc-feature-without($feature-query) | Returns a query object which will result in emitting mdc-feature-targets blocks that do not match the specified feature. |
$feature-query
and $feature-queries
refer to one or more of the values listed below under Supported Features.
Supported Features
MDC Web's styles are currently split according to the following features:
structure
- All baseline styles that don't fit into any other categoryanimation
- Styles responsible for causing animations and transitions to occurcolor
- Color-specific styles which rely on mdc-theme
variablestypography
- Typography-specific styles which rely on mdc-typography