Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@material/feature-targeting
Advanced tools
Material Components for the web Feature Targeting Scss helpers
@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.
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);
}
}
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 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.
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.
npm install @material/feature-targeting
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";
// To include all styles (using the default of mdc-feature-all() defined above):
@include my-component-core-styles;
// Or, to include a specific subset of styles:
@include my-component-core-styles(structure);
@include my-component-core-styles(feature-targeting.any(color, typography));
// The above two @includes and the following @include would produce equivalent results:
@include my-component-core-styles(feature-targeting.without(animation));
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.
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
FAQs
Material Components for the web Feature Targeting Scss helpers
The npm package @material/feature-targeting receives a total of 834,301 weekly downloads. As such, @material/feature-targeting popularity was classified as popular.
We found that @material/feature-targeting demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 15 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.