Open lynnmercier opened 6 years ago
I experimented with this because I was curious, but I definitely don't have a silver bullet.
https://gist.github.com/kfranqueiro/6a47cafa274f2d774b2d133ed7750f02
(Also disclaimer it could use a bit of cleanup, probably)
This tries to pretty exhaustively replace .mdc-bar
with .mdc-bar__sub
otherwise append it to the selector, but I can already identify a couple of problems:
.mdc-bar
(this is probably detectable at least in the simplest case of the selector only being that).my-modifier.mdc-bar
which IIUC ideally would also remove just the .mdc-bar
. I don't think this is even possible to do within Sass without risking false positives (e.g. looking for .mdc-bar
but ending up matching .mdc-bark
and replacing all but the last letter).It also occurs to me that maybe this is something we should seek to solve via documentation rather than implementing safeguards to attempt to tweak parent selectors, which seems likely to be error-prone.
I've already wondered this after seeing how some of our components' mixins include a selector vs. how some don't (e.g. look at how mdc-toolbar-icon-ink-color works vs. mdc-tab-icon-ink-color)...
Do you think it's viable to include some indication in our mixin documentation tables as to whether the mixin includes a sub-element selector, and write some centralized guidance somewhere on what that means for using it within custom selectors?
Take this made up example of the
mdc-foo
component using themdc-bar
component in its internal implementation:And there exists some Sass mixin to customize the color of mdc-bar
Now lets say that the
mdc-foo
component wants to have a bluemdc-bar
in the default state, but a redmdc-bar
in the invalid state. Then the stylesheet formdc-foo
haswhich outputs this CSS, once Sass is compiled.
...so far so good...
The problem is if we change the implementation of
mdc-bar
so that it has a sub-elementAnd we have to update the implementation of the Sass mixin
mdc-bar-color
All these changes happen in the
mdc-bar
node module, and none of them are breaking changes...but now themdc-foo
stylesheet outputswhich is unnecessarily specific
How do we handle this case more gracefully?