Skip to content
Menu

utility-class-selector

Reports a selector that tests the class attribute for a utility name, such as [class*='size-'].

On this page

This rule reports a selector variant that tests the class attribute for a utility name, such as [class*='size-'] or .border-b. Stillwind writes generated class names, not utility names, to the class attribute. Such a test therefore does not see the utilities of an element that style() styles.

Component libraries such as shadcn/ui use this test to apply a default size to an icon unless the icon sets its own size. In Stillwind, :not([class*='size-']) always matches, so the default replaces the size that the icon sets.

Reported

function Button(props: ButtonProps) {
  return (
    <button
      {...style(
        "inline-flex items-center gap-2 [&_svg:not([class*='size-'])]:size-4",utility-class-selector: `[class*='size-']` tests the class attribute for a utility name, but Stillwind writes generated class names, so it does not see the utilities of an element
        style.forward(props),
      )}
    />
  );
}

Fixed

function Button(props: ButtonProps) {
  return (
    <button
      {...style("inline-flex items-center gap-2 [:where(&_svg)]:size-4", style.forward(props))}
    />
  );
}

:where() gives the rule zero specificity. A class on the icon that sets the same property, such as the class of style("size-6"), then wins over the default.

How to fix

  • For a variant such as [&_svg:not([class*='size-'])]:size-4, whose utility has the root that the test names, use [:where(&_svg)]:size-4. The automatic fix makes this change.
  • Without &, include the *: or **: variant before it in :where(), because the class of *: keeps the specificity of one class. The automatic fix changes *:[svg:not([class*=size-])]:size-8 to [:where(&>svg)]:size-8, **: to &_svg, and [svg:not([class*=size-])]:size-7 without *: to [:where(&:is(svg))]:size-7.
  • For other tests, such as [.border-b]:pb-6, pass the state in another way. For example, add a prop, or set the property on the element where the caller adds the utility.

Rule settings

PresetSeverity
recommendedwarn
strictdeny
stylisticoff

Configure lint to change the severity or options.

Type to search the documentation.