fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

Resulting class name on the DOM for CommandBar item is different from the provided className generated by mergeStyleSets

Open tamakisquare opened this issue 6 years ago • 2 comments

Environment Information

  • Package version(s): @fluentui/[email protected]
  • Browser and OS versions: Chrome 81.0.4044.138 / MacOS 10.15.4

Please provide a reproduction of the bug in a codepen:

https://codepen.io/tamakisquare/pen/PoPagbB?editors=0011

Actual behavior:

mergeStyleSets is used to generate a class name, which is then assigned to a CommandBar item's className prop.

const styles = mergeStyleSets({
  foo: {
    'background-color': 'orange'
  }
});

The embedded console.log(styles.foo) shows that the auto-generated class name is foo-xx, where xx is a number.

After all components are rendered, Chrome DevTools shows the class name assigned to the corresponding <button> DOM element is foo-yy, where yy is also a number but yy does not equal to xx.

Screenshot illustrating the issue

Expected behavior:

The class name assigned to the <button> DOM element should be the same as the auto-generated class name assigned to the CommandBar item. Or, simply put, xx and yy should be the same number.

In fact, if I assign a hard-coded string, instead of the auto-generated class name by mergeStyleSets, to the className prop of the CommandBar item, everything seems to work as expected. In other words, it seems like this issue has something to do with the auto-generated class name.

Priorities and help requested:

Are you willing to submit a PR to fix? No

Requested priority: (Blocking, High, Normal, Low) Normal. It may very well be High, depending on the impact area of this issue, which is unknown to me at this point.

Products/sites affected: (if applicable)

tamakisquare avatar May 12 '20 14:05 tamakisquare

This is by design. The classnames generated from merge styles equate to the unique result of the styles applied to the thing. Internally mergeStyles will merge the styles correctly when you pass in a classname previously created as well.

For instance:

const bgRed = mergeStyles({ background: 'red' });
const bgBlue = mergeStyles({ background: 'blue' });
const fgWhite = mergeStyles({ color: 'white' });

Alright, we have 3 class strings, each unique. mergeStyles will intelligently remember the classes it's registered, and correctly merge new classes together:

mergeStyles(bgRed, bgBlue); // results in blue class string
mergeStyles(bgBlue, bgRed); // results in red class string
mergeStyles(bgRed, fgWhite); // creates a new class that's red bg with white fg.

So, what's happening here is that you're creating a class, which is merging in on top of other default styles; the resulting classname is a new class that applies your styles on top of the defaults.

If you're trying to create a non expanding style, you can certainly define css using any tech you want and pass in a non merge-styles string classname. It will result in a concatenated string, preserving the original (as you found with the hardcoded string.)

Hope this helps.

dzearing avatar May 12 '20 23:05 dzearing

Thanks for the clear answer, @dzearing. I see the rationale behind it now.

tamakisquare avatar May 13 '20 00:05 tamakisquare