Debugger CSS uses global classNames, risks conflicts
Working in the CSS codebase, I'm seeing a lot of CSS class names that are both global and very generic:
/* SomeSpecificComponent.css */
.pane {
/* a bunch of styles */
}
.title {
/* a bunch of styles */
}
.bottom {
/* a bunch of styles */
}
Sometimes we have class names following a naming convention like BEM or SMACSS or Bootstrap-like names (.component, .component-variant, .component-child), or a "root" class named is used to scope styles (.component .child), but that's far from systematic.
CSS selectors share a global namespace. We can see CSS classes as functions, and <element class="myClass"> as a call site of the myClass function. So a lot of the Debugger CSS is the rough equivalent of writing JS like this:
window.pane = function() { ... }
window.title = function() { ... }
window.bottom = function() { ... }
I also wonder if contributors to this project may end up publishing very generic names to the global namespace because they have an expectation that the styles they write are scoped to the React component. After all, most stylesheets are named after a React component and intended for styling that component:
src/components/PrimaryPanes/Outline.css
src/components/PrimaryPanes/Outline.js
So it's fairly logical for the unsuspecting developer to think that since Outline.js is a module with its own local scope, then Outline.css is itself a module with a local scope.
This creates a few issues:
- We may run into issues when we add generic class names to elements, for instance if I create
<span class="arrow bottom">somewhere I'm going to inherit the styles for bottom toolbars! Woops. - We have a lot of selector specificity fighting going on, with long selectors meant to ramp up the number of classes in the selector so those styles override some other styles.
- For shared components such as reps or frames, we end up putting DOM in the Console or other consumers that has very short and generic class names, for instance:
<div class="frame">
<span class="title">React</span>
<span class="img react"></span>
<span class="file">...</span>
<span class="path">...</span>
</div>
Are we sure that webconsole.css doesn't already define styles for .frame, .title, .img, .react, .file or .path? Nope. (Those are not the exact class names used but it's similar.)
Also if we export the styles for this DOM and inject it in the Console, we may end up styling completely unrelated elements.
This is a well-known problem in CSS for big sites and applications, and there are a bunch of solutions that people came up with: manual namespacing with prefixes (BEM, SMACSS etc.), automated prefixing with CSS Modules, and a bunch of CSS-in-JS (or CSS-through-JS) tools like styled-components or CSS Blocks.
I would be in favor of picking a style management strategy (and/or tool), and progressively updating components to use it. There's probably no need to refactor everything, but we could focus on:
- Shared components, so that we can share their styles safely in different panels (Debugger, Console, Network details...)
- All new CSS. :)
Given that we use component snapshots, tools that produce very mangled class names are probably out of the picture. I suspect we need predictable and stable classNames for tests in both debugger.html and mozilla-central. This leaves us with two solutions:
- Use a manual namespacing strategy, like BEM (
.component-name__child-name) or some other style. - Use CSS Modules with Webpack's
css-loaderand a customlocalIdentNameconfig (probably producing something like.ComponentName-childName).
I prefer the second option, because it fixes the "contributors expect styles to be scoped to the component" problem.
For the record, usage looks like:
/* MyComponent.module.css */
.container {}
.title {}
.path {}
/* MyComponent.js */
import style from "./MyComponent.module.css";
export default const MyComponent = () => (
<div className={style.container}>
<span className={style.title}>...</span>
<span className={style.path}>...</span>
</div>
)
Resulting in a dom like this:
<div class="MyComponent-container">
<span class="MyComponent-title">...</span>
<span class="MyComponent-path">...</span>
</div>
Pros:
- The
.module.cssconvention (or some other name) makes it clear which CSS files are scoped. - Classnames are still readable, and it's easy enough to locate where they come from.
Cons:
- There's still a theoretical conflict risk if some other panel uses the same convention and the same component name (e.g.
.SourceList-item). We could add a debugger prefix if we expect that to be an issue (.dbg-SourceList-item). - Generated classnames cannot be grepped. Searching for
MyComponent-titlewhould only return snapshots (and generated CSS, in m-c).
If the cons are too problematic, we could go with the first solution and a manual convention like BEM, but that requires more training and careful code reviews.
@jasonLaster @darkwing What do you think? Pinging @nchevobbe for the Console/reps/frames use case too.
@fvsch Namespacing shouldn't even be needed if we have well-named shared components.
@nt1m What kind of namespacing, though?
For instance, the current Debugger CSS codebase uses the breakpoint class in a number of places:
- In legacy CSS for the CodeMirror gutter (styles which seem not to apply anymore)
- In the new breakpoint indicator markup in the CodeMirror gutter (
<i class="breakpoint">...</i>) - In the breakpoints list in the right sidebar
Sometimes this is scoped with selectors like this:
.breakpoint-list .breakpoint { ... }
This will be low risk if (and that's a big if) we never forget to prefix with the correct parent or ancestor class, and never decide that breakpoint on its own is a good component name (another big if).
I'm also seeing a lot of CSS code like this:
.some-component .title { ... }
If our convention is "prefix selectors with the component's root class" and we follow it correctly, this mostly works. It means most of our selectors will have 0020 specificity, which can increase problems, but that's manageable. Trouble happens when the right-hand part of your selector, using a generic name for its class name, happens at two levels in your DOM tree:
<div class="accordion">
<div class="title">...</div>
<div class="content">
<div class="other-component">
<h2 class="title">...</h2>
...
</div>
</div>
</div>
Now both .accordion .title and .other-component .title are going to match the inner title, and you're in full-on conflict mode.
Prior discussion: https://github.com/devtools-html/debugger.html/issues/1869
I kinda disagree with the suggestion to use Sass; while I like Sass in general, having CSS variables + webpack limits the need for it (major selling points of Sass were the variables and the partials, in a "big 3k lines stylesheet" world), and Sass nesting is a footgun if you're not careful.
Much more amenable to BEM, which I've successfully introduced in other places as a "frontend lead". But I think CSS modules can give us BEM-like scoping for free (or cheaper, at least).
I'm a bit reluctant to introduce more CSS tooling while we're working on the node tooling in MC.
I think BEM could work for us though. Could we enumerate the big offenders, perhaps we could fix things like .frame or .webpack
Some stylesheets are mostly okay, for instance src/components/SecondaryPanes/XHRBreakpoints.css only has selectors which look like .xhr-*, with the occasional modifier class or element selector (e.g. .xhr-container label).
It would be great if we could have a linting rule that flags regressions in that code style for that stylesheet. E.g. if you introduce a .whatever {...} style in that stylesheet: linting error. Could be a rule that we enable progressively. Do we run stylelint systematically?
Some problematic stylesheets:
AccessibleImage.css and SourceIcon.css
Main selector is .img.
That name is fine in the Debugger, but is too generic to load those styles in other panels (Console, Network; related issue: #7782).
src/components/SecondaryPanes/Frames/Frames.css
Main selector is .frame, which might be too generic.
Secondary selectors are like .frame .location, which can create problems if we have another CSS component styling .location.
Finally, that stylesheet also publishes those global classNames:
.show-more.annotation-logo.clipboard-only
src/components/SecondaryPanes/Breakpoints/Breakpoints.css
Not too bad, but some selectors like .breakpoint .close can be a bit generic. Also I'm pretty sure that className="breakpoint" is used both in the Editor and in the SecondaryPanes.
src/components/SecondaryPanes/Frames/Group.css
.group-description-nameis long but generic enough to risk a clash if the same class is used in a different part of the app.
src/components/WelcomeBox.css
.alignLabel.shortcutKey
src/components/shared/Accordion.css
- Base selector is
.accordion. Hopefully that's specific enough. - Sub selectors are like
.accordion ._header, so we rely onclassName="_header"not being styled by other CSS, which can be a stretch. I would move to selectors like.accordion-header.
src/components/SecondaryPanes/Scopes.css
Styles global selectors such as .object-node, .object-label, .objectBox-*. Do we want to do that work globally? If so, it should probably be moved to a stylesheet that doesn't imply a scope limited to the Scopes pane. If not, those selectors should be scoped as .scopes-list .object-node, .scopes-list .object-label, etc.
src/components/PrimaryPanes/Sources.css
A few rogue classes:
.tree-indent.thread-header
src/components/Editor/Editor.css
Not found of the .editor class, could be a bit specific.
Unless Editor instances are always in iframes, but I'm not sure that's the case?
- Styles
.CodeMirror, but we may have other CodeMirror instances in other places in Debugger? .inline-bp.function-search.debug-expression.visible(!).download-anchor
packages/devtools-reps/src/reps/reps.css
That one is a winner. :)
- Styles a lot of
.objectBox-*classes, but we also style them in other stylesheets? .inline(!).location(remember when I said earlier that if someone else styles.location, the more precise selector.some-context .locationcould still be an issue? well here it is).accessible-role.tag-name.attrName.caption(!)button.open-inspector(nice to have a "accidentally shared" CSS component for this button; maybe it should be in its own component or in m-c'sdevtools/client/themes/common.cssinstead though)button.jump-definition(same).more-ellipsis- and a few more
src/components/PrimaryPanes/Outline.css
Most selectors are specific enough, but these two could be problematic:
.outline("outline" is a pretty common word).outline > div
src/components/SecondaryPanes/CommandBar.css
Main offender in this stylesheet:
.bottom {
border-bottom: none;
background-color: var(--theme-body-background);
border-top: 1px solid var(--theme-splitter-color);
flex: none;
height: var(--editor-footer-height);
}
That's a very specific style to publish under a global name called "bottom".
src/components/SecondaryPanes/SecondaryPanes.css
.pane.dropdown
this is a fantastic list of rogue styles. I'd love it if we could clean up this list.
And, yes we do use stylelint systematically
/claim
Thanks for claiming the issue! :wave:
Here are some links for getting setup, contributing, and developing. We're always happy to answer questions in slack! If you become busy, feel free to /unclaim it.
:fox_face: Debugger team!
Assigning myself, with the following goals:
- Make progressive progress on this issue, based on the list above.
- Explore ways in which we can use Stylelint to prevent regressions (such as adding "global" class names to a component's stylesheet).
@jasonLaster Here's an attempt at adding stylelint rules that address this problem: https://github.com/fvsch/debugger.html/commit/9cfc7168ab4aa723f4db942124f8ce133aa3ba7d
- Add a maximum specificity of
0,4,1(we have roughtly 30 offenders) - Add
postcss-bem-linterto allow us to define some stylesheets as BEM components
The way to define a stylesheet as a BEM component is to add a comment near the top of the file:
/* @define MyComponent */
And then all selectors in the stylesheet are expected to look like:
.MyComponent {}
.MyComponent-element {}
.MyComponent-other-element {}
I've set it up so that the component name allows lowercase and hyphens.
To allow a pattern where the start of the selector must be a component class but after that you just do what you want (which we probably need, a lot), you can use "weak" mode:
/* @define welcomeBox; weak */
/* all valid */
.welcomeBox {}
.welcomeBox-foo {}
.welcomeBox-bar button {}
.welcomeBox [role="listitem"] a > span {}
/* but not this one */
.selected .welcomeBox-bar {}
I've only ran a few tests, but it looks like a pretty nice tool. We can create new components as "strict" BEM components, roll out "weak" mode to most of our current CSS and prevent rogue global styles.
that sounds good. i'd love to do that!
Cloned into Bugzilla: https://bugzilla.mozilla.org/show_bug.cgi?id=1695623
Firefox Developer Tools (including Debugger tool) development is now entirely happening in Bugzilla and new issues should be reported here: https://bugzilla.mozilla.org/enter_bug.cgi?product=DevTools&component=Debugger
Honza