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:
@use "@material/feature-targeting";
@mixin my-component-core-styles($query: feature-targeting.all()) {
$feat-structure: feature-targeting.create-target($query, structure);
@include feature-targeting.targets($feat-structure) {
}
}
Consuming component styles:
@use "@material/feature-targeting";
@use "my-component-mixins";
@include my-component-core-styles;
@include my-component-core-styles(structure);
@include my-component-core-styles(feature-targeting.any(color, typography));
@include my-component-core-styles(feature-targeting.without(animation));
Sass Mixins and Functions
Mixin | Description |
---|
targets($feature-targets...) | Conditionalizes content to only be emitted if the given feature target(s) is/are queried. |
Function | Description |
---|
create-target($feature-query, $targeted-feature) | Returns a variable to be passed to targets in order to filter emitted styles. |
all($feature-queries...) | Returns a query object which will result in emitting 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. |
any($feature-queries...) | Returns a query object which will result in emitting targets blocks that match any of the specified features. Passing no arguments results in no blocks being emitted. |
without($feature-query) | Returns a query object which will result in emitting 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
11.0.0 (2021-04-15)
Bug Fixes
- banner: Use role alertdialog. (a07b6d4)
- button: add missing feature-targeting import (71fe9a0)
- button: Fixed button's icon size scaling on browser zoom (bc104ba)
- chips: Expose deprecated resources in top-level TypeScript file (67d780c)
- chips: Fix incorrect references between deprecated and non-deprecated resources (f8579b7)
- chips: Make chips wrap by default (24255c4)
- chips: Remove obsolete chips resources now in chips/deprecated/* (87ac2fd)
- chips: Remove obsolete resources (40dd242)
- chips: rename deprecated trailing action classes (48f4b67)
- chips: Un-remove obsolete chips resources now in chips/deprecated/* (7cf6782)
- chips: Use deprecated chips in autoinit (d2a39d3)
- circular-progress: add annotation (06dead2)
- dialog: Add transparent border to dialog surface for HCM support. (b2fa996)
- dialog: Remove the unnecessary border on the dialog title when not needed, this adds an extra line in the UI on high contrast mode. With margins it is possible to keep the previous spacing and only add the border when needed. (3344d12)
- dom: do not cache focusable elements in focus-trap (7899e0f)
- fab: add alternate decorator only when necessary (0fd56a8)
- fab: Apply extended shape radius in Extended FAB's theme mixin (81911b7)
- list: Correcting the selector mapping for CHILD_ELEMENTS_TO_TOGGLE_TABINDEX and FOCUSABLE_CHILD_ELEMENTS. (8943b99), closes #6829 #6829
- list: do not activate typeahead on certain modifier keys (f1b1fd5)
- progress-indicators: hide from screenreaders on close (d3a6862)
- ripple: Update states-selector() to use
:active:active
to match active specificity styles. (faa7d32) - select: do not conduct anchor typeahead when modifier keys pressed (6f678a9)
- select: set hidden input value before firing change event (2d6ba2c), closes #6904
- shape: duplication bug with nested custom properties (f77a4dd)
- slider: Fire custom
input
event on input change (i.e. value change via keyboard), mirroring the native input
event behavior for range inputs. (ec8f846) - slider: Fix #quantize to use min value as the baseline. (0f358dd)
- slider: Fix JS floating point rounding errors by rounding values to a set number of decimal places based on the step size. (6072ed6)
- slider: Fix track height. (67eb0df)
- slider: Improve HCM borders, add missing @noflip annotations. (e7202cb)
- slider: Modify behavior such that for range sliders, presses in the middle of the range change the value (of the closest thumb). This provides a single-pointer alternative option to an otherwise gesture-based interaction. (0b8cff7)
- slider: Throttle slider UI updates. (7d6a4bb)
- slider: Throw error for invalid initial values based on the step. (3955d8d)
- tab: Update ripple adapter to reflect sass ripple-target. (97c4d40)
- theme: do not emit when theme.property() replacements are null (aa0aaf0)
- theme: parsing error when @import-ing mdc-theme (b62b126)
- theme: replace works for multiple replacements (95322b1)
- update README to correct links. (71e615b)
- tooltip: flip precedence of data-tooltip-id and aria-describedby when finding TT id (b2d22df)
- typography: do not emit styles when setting null from global variable (f5f1b61)
Code Refactoring
- snackbar: Update a11y structure (c60449b)
- tooltip: Moved the anchor element blur event listener from the component to within the foundation. (0b4a4b2)
- typography: Rename typography Sass function from pxToRem() to px-to-rem() (8f0a11e)
Features
- base: add non-statics foundation constructor type (e3ec22f)
- base: add observer mixin (4ceb422)
- chips: Expose "action" component (03d34bb)
- chips: Expose "chip" component (cbc57c6)
- chips: Expose "chipset" component (d6c5bcf)
- chips: Expose top-level resources (fefc668)
- chips: Remove touch target wrapper selector from chip set spacing (367d88b)
- chips: Start deprecation of chip (e683bdf)
- chips: Start deprecation of chip root directory (73a2271)
- chips: Start deprecation of chip set (148e8cf)
- chips: Start deprecation of chip trailing action (9eeb35c)
- chips: Truncate long chip labels by default (f5c6db8)
- dialog: Adding
resize
and orientationchange
event handlers into MDCDialogFoundation
. (1e06534) - dialog: Adds support for "surface-scrim" over full-screen dialogs. This prevents a "double scrim" from appearing when showing a secondary dialog over a full-screen dialog on larger screens. (cddb035)
- dom: add option to skip restoring focus on release focus (5c0ab74)
- dom: add tab key keyboard.ts (dc9c840)
- fab: Add theming API to Extended FABs (f19c86d)
- fab: Added
$focus-outline-width
param to extended-padding() FAB mixin (8ecd7c9) - fab: Added focus outline theme keys to FAB theme mixin (d6d8d04)
- fab: Added theme mixin to primary FAB variant. (f19bbc4)
- fab: border custom prop support & add CPs for padding (a6b3101)
- fix: Ensure that secondary controls do not ripple. (1f636b2)
- fix: Fix divider layout in right-to-left locales. (f524626)
- fix: Remove old MDC list class names, preparing to release evolution. (5f0fc44)
- fix: Remove the "evolution" prefix from list evolution's class names. (0cde52f)
- fix: Simplify divider styles to reflect new design guidance. (f77c508)
- linear-progress: add getBuffer (9c85d50)
- list: Add "deprecated" aliases for old list mixins / variables. (f9cac96)
- list: Add missing "deprecated" aliases for old list mixin. (302c7a9)
- list: Finalize list mixin/variable rename. (c97d7d8)
- list: Rename deprecated MDC list class names. (a678806)
- list: Rename deprecated MDC list class names. (941ca3b)
- list: Update deprecated list class names so evolution can become default. (606e767)
- list: Update styles to reference "deprecated" mixins/variables. (3201cae)
- list: Update styles to remove "evolution" prefix from mixins/variables. (f9c9e39)
- menu: add maxHeight setter (bf670da)
- menu-surface: add option to always horizontally center on viewport (23ea2d8)
- ripple: add active() mixin for styling active styles. (9f2e85f)
- select: allow programmatic change without firing event (79ce087), closes #6166
- slider: Add mixin to customize thumb color in the activated (hover, focus, pressed) state. (94f50b2)
- Add support for "mdc-deprecated-list-*" class names. (9e52f55)
- switch: add high-contrast mode focus shim mixin (c91e8d1)
- theme: add configuration support for custom-properties (1f318ff)
- theme: add create-varname() for custom properties (b522724)
- theme: add key store (07ff0c4)
- tooltip: Adding logic to position the caret relative to the tooltip. (76da787)
- tooltip: Adding touchstart/touchend event listeners to tooltip. This allows tooltips attached to non-focusable elements to be surfaced on mobile. (7cd26af)
- tooltip: Creating an
mdc-tooltip__surface-animation
class that holds all the style properties responsible for animating the tooltip in and out of the page. The existing mdc-tooltip__surface
class will hold all the style properties that impact the visual appearance of the tooltip. (56fc269) - tooltip: Expose method that allows users to register additional scroll handlers on elements in the DOM. This should be used in situations where the tooltip anchor is a child of a scrollable element, and will ensure that the tooltip remains attached to the anchor when this element is scrolled. (24609b8)
BREAKING CHANGES
- typography: Renamed typography Sass function from pxToRem() to px-to-rem()
PiperOrigin-RevId: 368489085
- fix: the old list implementation has been deprecated and now requires that class names use an "mdc-deprecated-list-" prefix. The new implementation (list evolution), no longer uses a prefix ("mdc-evolution-list-" is now just "mdc-list-*").
PiperOrigin-RevId: 364441086
- snackbar: Dom structure change, see README.md
PiperOrigin-RevId: 363926666
- tooltip: Added adapter method:
- registerAnchorEventHandler<K extends EventType>(
evtType: K, handler: SpecificEventListener<K>): void;
- deregisterAnchorEventHandler<K extends EventType>(
evtType: K, handler: SpecificEventListener<K>): void;
PiperOrigin-RevId: 358401984