Closed Bug 1594402 Opened 5 years ago Closed 5 years ago

Display a color swatch for all color types in color-taking CSS properties, even when they are variables

Categories

(DevTools :: Inspector: Rules, enhancement, P3)

enhancement

Tracking

(firefox77 fixed)

RESOLVED FIXED
Firefox 77
Tracking Status
firefox77 --- fixed

People

(Reporter: pbro, Assigned: jdescottes)

References

(Blocks 2 open bugs)

Details

(Keywords: parity-chrome)

Attachments

(6 files, 2 obsolete files)

47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
201.43 KB, image/png
Details

Consider the following test case:

:root {
  --foo: red;
  --bar: black;
}
body {
  background: var(--bar);
  color: var(--foo);
}

which you can open in your browser with the following URL:
data:text/html,<style>:root{--foo: red;--bar: black} body {background: var(--bar); color:var(--foo)}</style>Welcome

If you select the <body> element and look at the color property in the Rules panel, you will see that it does not look like other colors in devtools. That's because it's not a color type, it's a var() function.
The CSS property parser in Firefox DevTools (here) does not parse variables like colors.

This means that there is no color swatch displayed next to var(--bar) or var(--foo).

In Chrome there is. This is useful in particular because the color contrast ratio is displayed inside the color picker that appears when you click on the swatch.

Changing the color in the color picker in chrome means you're loosing the variable, but it might be useful anyway in certain cases.

Worth noting: Chrome also shows the color swatch in cases like border: 2px solid var(--bar); and background: var(--foo) url(test.png) no-repeat.
The way they seem to do it is checking if the resolved value of the variable looks like a valid color.

Summary: Display a color swatch for all color-taking CSS properties, even when their values is a variable → Display a color swatch for all color types in color-taking CSS properties, even when they are variables
Assignee: nobody → mratcliffe
Status: NEW → ASSIGNED

The changes are fairly simple:

  • devtools/client/shared/output-parser.js
    • We append a Color Swatch when parsing values if a css-variable is a valid CSS color.
    • We add a data-cssvar attribute to the Color Swatch color name span to mark it as a CSS variable. The color name node is hidden by a new rule in rules.css.
  • devtools/client/inspector/rules/views/text-property-editor.js
    • Because the CSS Swatch can now contain a CSS variable we need to pass a method to the inplace editor to get the color value from the correct location. If we encounter a CSS variable then element.textContent would contain the CSS variable value appended with the CSS variable e.g. whitevar(--foo). To work around this we need to grab the value from the appropriate element to get the correct value e.g. var(--foo).
  • devtools/client/shared/inplace-editor.js
    • Added the valueCallback argument to allow us to get the color name from a non-default location.
  • devtools/client/shared/widgets/tooltip/SwatchColorPickerTooltip.js
    • If a swatch is clicked we need to drop the CSS variable and change it to a normal color swatch. We do this by showing the color name and removing the CSS variable name span.
  • devtools/client/themes/rules.css
    • Added a simple rule to hide the color name span in the case that we are using a CSS variable.
  • devtools/client/inspector/rules/test/browser.ini
    • Added test.
  • devtools/client/inspector/rules/test/browser_rules_colorpicker-works-with-css-vars.js
    • Check that the following work okay and that normal color swatches also still work:
      • color: red;
      • background-color: var(--main-bg-color);
      • border: 1px solid var(--main-bg-color);
Priority: -- → P3
Assignee: michael → nobody
Status: ASSIGNED → NEW
Assignee: nobody → jdescottes
Status: NEW → ASSIGNED
Attachment #9114161 - Attachment is obsolete: true
See Also: → 1626234

Depends on D68971

Until have a shared component to build to color swatch, some additional comments.

Attachment #9137106 - Attachment is obsolete: true
Pushed by jdescottes@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/7942f0a77334 Store the swatchcolorpicker color in a data attribute instead of textcontent r=ladybenko,rcaliman https://hg.mozilla.org/integration/autoland/rev/f84cf278044c Display a color swatch for CSS variables in CSS autocomplete r=ladybenko,rcaliman https://hg.mozilla.org/integration/autoland/rev/64df37c1e0cc Test that switching color format in DevTools works with the color picker r=ladybenko,rcaliman https://hg.mozilla.org/integration/autoland/rev/711e0a80ad2f Support setting the data-color attribute on the swatch directly r=ladybenko,rcaliman https://hg.mozilla.org/integration/autoland/rev/035ce4dce72f Add comments about the supported activeSwatch markup in SwatchColorPickerTooltip.js r=ladybenko,rcaliman
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: