Keep your coding agent up to date with the latest web best practices
Release v0.0.109
github-actions[bot]521308c1a784
diff --git a/README.md b/README.mdindex 6a888f3..dfc9306 100644--- a/README.md+++ b/README.md@@ -39,13 +39,14 @@ Just a tiny sampling of the **134+ use-case-centric guides**:#### Full Skill Coverage (v0.0.109)<details>-<summary>Includes expert guidance across <strong>84 modern web features</strong></summary>+<summary>Includes expert guidance across <strong>85 modern web features</strong></summary>- [::backdrop](https://web-platform-dx.github.io/web-features-explorer/features/backdrop/)- [:autofill](https://web-platform-dx.github.io/web-features-explorer/features/autofill/)- [:has()](https://web-platform-dx.github.io/web-features-explorer/features/has/)- [:not()](https://web-platform-dx.github.io/web-features-explorer/features/not/)- [:user-valid and :user-invalid](https://web-platform-dx.github.io/web-features-explorer/features/user-pseudos/)+- [@function](https://web-platform-dx.github.io/web-features-explorer/features/function/)- [@starting-style](https://web-platform-dx.github.io/web-features-explorer/features/starting-style/)- [<details>](https://web-platform-dx.github.io/web-features-explorer/features/details/)- [<dialog closedby>](https://web-platform-dx.github.io/web-features-explorer/features/dialog-closedby/)@@ -128,7 +129,7 @@ Just a tiny sampling of the **134+ use-case-centric guides**:</details><details>-<summary>Covers <strong>108 real-world developer use cases</strong> with production-ready code patterns</summary>+<summary>Covers <strong>109 real-world developer use cases</strong> with production-ready code patterns</summary><h3>accessibility</h3>@@ -232,6 +233,7 @@ Just a tiny sampling of the **134+ use-case-centric guides**:- **[precise-text-alignment](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/precise-text-alignment.md)**: Achieve precise vertical alignment with text of any font. For example, exactly equal visual padding above and below text, or aligning text perfectly flush with adjacent icons or images.- **[prevent-text-wrapping](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/prevent-text-wrapping.md)**: Ensure the browser does not insert line breaks into text and will allow text to overflow its container.- **[pull-to-reveal](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/pull-to-reveal.md)**: Build a pull-to-reveal feature that would enable the user to pull down on the screen to reveal more content, like a search bar.+- **[reduce-style-repetition](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/reduce-style-repetition.md)**: Reduce excessive style repetition by encapsulating complex or dynamic styling logic into reusable functions (such as a function that computes a gradient based on a set of input parameters).- **[resilient-context-menus-and-nested-dropdowns](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/resilient-context-menus-and-nested-dropdowns.md)**: Build accessible, responsive menus, tooltips, dropdowns, or contextual overlays that must be tethered to specific UI elements, guaranteeing that the overlay automatically repositions itself (e.g., flipping axes) when it encounters viewport edges, ensuring it never gets cut off.- **[same-document-transitions](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/same-document-transitions.md)**: Visually connect persisting elements across different page states or navigations in a Single Page Application (SPA) (e.g. expanding a product thumbnail into a full-bleed hero image) by smoothly morphing their size, position, or other styling properties.- **[scroll-entry-exit-effects](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/user-experience/scroll-entry-exit-effects.md)**: Create fade-in, scale-up, or other complex reveal-type effects on elements as they enter and exit the scrollport (or viewport) while the user is scrolling.diff --git a/skills/modern-web-guidance/guides/user-experience/reduce-style-repetition.md b/skills/modern-web-guidance/guides/user-experience/reduce-style-repetition.mdnew file mode 100644index 0000000..7fd6e9f--- /dev/null+++ b/skills/modern-web-guidance/guides/user-experience/reduce-style-repetition.md@@ -0,0 +1,85 @@+# Reduce Style Repetition with CSS Functions++Maintaining large stylesheets often leads to repetitive logic, especially when dealing with design system tokens like gradients or responsive layout patterns.++The CSS `@function` at-rule allows you to encapsulate this logic into reusable, parameterized functions, making your CSS more maintainable, consistent and DRY (Don't Repeat Yourself).++## The `@function` Syntax++A custom function is defined using the `@function` rule followed by a dashed name and a list of parameters. The function returns a value using the `result` property.++```css+@function --my-function(--input1 <length>, --input2: default-value) returns <length> {+ /* Logic goes here */+ result: var(--input1);+}+```++### Key Concepts+- **Parameters:** Must start with a double dash (`--`).+- **Defaults:** You can provide default values using a colon (`:`).+- **Result:** The `result` property determines the value the function returns. The last `result` declared in the function body wins.+- **Scoping:** Parameters and variables defined inside the function are locally scoped.+- **Types:** You can require parameters and the returned value to match a CSS type with bracket notation (e.g., `<color>`) and allow multiple types with the `type` function (e.g., `type(<number> | <percentage>)`).++## Practical Examples++### 1. Design System Tokens (Gradients)+Ensure consistent color gradients across your app by encapsulating gradient logic. The `--angle` provides a default value to provide consistency that can be overridden.++```css+@function --fancy-gradient(--start-color <color>, --end-color <color>, --angle: 98deg) returns <image>{+ result: linear-gradient(in oklab var(--angle), var(--start-color), var(--end-color) );+}++.card {+ background: --fancy-gradient(#ed73d7, #5d87e9);+}+```++### 2. Conditional Layout Logic+You can use `@media` or other queries directly inside a function to return different values based on the environment. When using conditional logic in a function, note that the `@function` does not "return" at the first value of `result`, but rather follows the CSS cascade, and resolves to the last value that matches based on the screen size, container size, or other query.++```css+@function --grid-template(--count <number>){+ /* MANDATORY: Put default value first. */+ result: 1fr; /* Default: stack */+ @media (min-width: 800px) {+ result: repeat(var(--count), 1fr); /* Grid on larger screens */+ }+}++main {+ display: grid;+ grid-template-columns: --grid-template(2);+}+```++## Best Practices+- **Use Dashed Names:** Always prefix your function names and parameters with `--`.+- **Provide Defaults:** Make your functions more robust by providing sensible default values.+- **Keep it Simple:** Use functions for logic that is actually repeated or complex. Don't over-engineer simple property-value pairs.+- **Use Types:** Ensure your parameters and return values are the expected types.+- **Consider Precompiled Alternatives:** For functions that do not depend on user input, media queries or other client-side variation, consider using a CSS precompiler to avoid doing unnecessary work on the client.++### Fallback strategies++@function has limited availability.++In browsers that do not support CSS Functions, values set using CSS functions will be invalid. To support other browsers, provide a fallback value for the property first. This will be overridden in browsers with CSS function support.++```css+.card {+ /* Provide fallback, in this case a solid color. */+ background: #5d87e9;+ background: --fancy-gradient(#ed73d7, #5d87e9);+}++main {+ /* Provide fallback, in this case a simple stacked default. */+ grid-template-columns: 1fr;+ grid-template-columns: --grid-template(2);+}+```++If it is a requirement to reduce style repetition while supporting other browsers, consider a CSS precompiler with functions, like Sass.