An alternative to naming conventions and heavy abstractions – Smashing Magazine


By learning the principles of basic CSS, you are taught to write modular, reusable, and descriptive styles to ensure maintainability. But when developers get involved with real-world applications, it often seems impossible to add UI features without styles leaking into unwanted areas.

This problem often becomes a self-fulfilling snowball; Styles that are theoretically limited to one element or class start appearing where they don’t belong. This forces the developer to create even more specific selectors to override filtered styles, which then accidentally override global styles, and so on.

Rigid class naming conventions, such as GOODThey are a theoretical solution to this problem. He BEM Methodology (Block, Element, Modifier) it’s a systematic way to name CSS classes to ensure reusability and structure within CSS files. Naming conventions like this can Reduce cognitive load by leveraging domain language to describe elements and their state.and if implemented correctly, It can make styles for large applications easier to maintain..

However, in the real world this is not always the case. Priorities can change, and with change, implementation becomes inconsistent. Small changes to the HTML structure can require many CSS class name revisions. With highly interactive UI applications, class names that follow the BEM pattern can become long and unwieldy (e.g. app-user-overview__status--is-authenticating), and not fully complying with the nomenclature rules breaks the structure of the system, thus denying its benefits.

Given these challenges, it’s no surprise that developers have turned to frameworks, with Tailwind being the most popular CSS framework. Instead of trying to fight what seems like an unwinnable war of specificity between styles, it’s easier to give up the CSS Cascade and use tools that guarantee total isolation.

Developers rely more on utilities

How do we know that some developers want to avoid cascading styles? It is the rise of “modern” front-end tools, such as CSS frameworks in JS — designed specifically for that purpose. Working with isolated styles that are strictly limited to specific components can seem like a breath of fresh air. Eliminates the need to name things. remains one of the most hated and time-consuming front-end tasks – and allows developers to be productive without fully understanding or taking advantage of the benefits of CSS inheritance.

But getting rid of CSS Cascade comes with its own problems. For example, composing styles in JavaScript requires heavy compilation configurations and often leads to styles that are awkwardly intermixed with component or HTML markup. Instead of carefully considered naming conventions, we allow build tools to automatically generate selectors and identifiers (e.g. .jsx-3130221066), requiring developers to keep up with another pseudolanguage itself. (As if the cognitive load of understanding what all your components are useEffectWhat we did was no longer enough!)

Further abstracting the work of naming classes for tools means that basic debugging is often limited to specific application versions compiled for development, rather than taking advantage of native browser features that support live debugging, such as developer tools.

It’s almost as if we need to develop tools to debug the tools we use to abstract what the web already provides, all in order to avoid the “pain” of writing standard CSS.

Fortunately, modern CSS features not only make writing standard CSS more flexible, but they also give developers like us a lot more power to manage the cascade and make it work for us. CSS Cascading Layers are a great example, but there is another feature that receives a surprising lack of attention, although that is changing now that it has recently become Baseline compatible.

The CSS @scope In order

I consider the css @scope in order is a potential cure for the kind of style leak-induced anxiety we’ve covered, one that doesn’t force us to compromise the advantages of the native web for additional abstractions and compilation tools.

“He @scope CSS at-rule allows you to select elements in specific DOM subtrees, targeting elements precisely without writing overly specific selectors that are difficult to override and without coupling your selectors too closely to the DOM structure.”

MDN

In other words, we can work with isolated styles in specific instances. without sacrificing inheritance, cascading, or even basic separation of concerns that’s been a long-standing guiding principle of front-end development.

Furthermore, it has excellent browser coverage. In fact, Firefox 146 additional support for @scope in December, doing so Baseline compatible for the first time. Here is a simple comparison between a button that uses the BEM pattern versus the @scope ruler:

 



 



He @scope the rule allows precision with less complexity. The developer no longer needs to create boundaries using class names, which in turn allows them to write selectors based on native HTML elements, thus eliminating the need for prescriptive CSS class naming patterns. By simply eliminating the need to manage class names, @scope can alleviate the fear associated with CSS in large projects.

Basic use

To get started, add the @scope rule to your CSS and insert a root selector to which the styles will be applied:

@scope () {
  /* Styles scoped to the  */
}

So for example, if we were to apply styles to a





Source link

Leave a Reply

Your email address will not be published. Required fields are marked *