Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit e108c01

Browse files
committed
Add section about components styling other components
1 parent 5258303 commit e108c01

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

css/class-naming-conventions/readme.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [Modifiers](#modifiers)
1111
* [Component modifiers that affect sub-components](#component-modifiers-that-affect-sub-components)
1212
* [State](#state)
13+
* [Components That Style Components](#components-that-style-components)
1314
* [Class Prefix Conventions](#class-prefix-conventions)
1415
* [Us versus Them](#us-versus-them-aka-theres-an-x-ception-to-every-rule)
1516
* [When to use our selector naming scheme](#when-to-use-our-selector-naming-scheme)
@@ -292,6 +293,58 @@ Prefix | Purpose | Location |
292293
> \* The `m-` class prefix has an old, deprecated use: Mobify Modules. However, Mobify Modules have been replaced with third part plugins, and are treated as third party libraries with their own conventions.
293294
294295

296+
## Components That Style Components
297+
298+
Sometimes there are situations when a component makes use of other components, and in so doing needs to style them for within its context. Let's take a simple example of a button and icon component being tightly coupled in this manner:
299+
300+
> **Note:** the examples in this section will use React JSX syntax for sake of brevity.
301+
302+
```jsx
303+
<button className="c-button">
304+
<Icon name={icon} />
305+
{children}
306+
</button>
307+
```
308+
309+
In situations like this it is tempting to just style the icon's class inside of the button. However, this practice is poor and creates tight coupling between the Button and Icon components that shouldn't exist. As a rule of thumb, a component should only know about what it's responsible for; it should be unaware of anything external to itself. Continuing with this example, the Icon is an external component, therefore the Button component should be completely unaware there there is even such thing as icon classes, like `c-icon`.
310+
311+
The solution to this challenge is to instead give the external component a new class that our new component can know about, like `c-button__icon`. By doing it in this way the external Icon component is, for all intents and purposes, being treated as a sub-component of the Button component. This method also has the benefit of being free of any (tight) coupling between the components. Both components can change, be added or removed, without really effecting the other in an unpredictable way.
312+
313+
So, to summarize...
314+
315+
```jsx
316+
/* Bad! */
317+
<button className="c-button">
318+
<Icon name={icon} />
319+
{children}
320+
</button>
321+
```
322+
323+
```scss
324+
// Bad!
325+
.c-button .c-icon {
326+
// ...
327+
}
328+
```
329+
330+
### Good Practice
331+
332+
```jsx
333+
/* Good! */
334+
<button className="c-button">
335+
<Icon className="c-button__icon" name={icon} />
336+
{children}
337+
</button>
338+
```
339+
340+
```scss
341+
// Good!
342+
.c-button__icon {
343+
// ...
344+
}
345+
```
346+
347+
295348
## Parsing vs. Decorating
296349

297350
It's important to understand that we have a few different ways of authoring our CSS, and the way we do this is depends a lot on how we convert the desktop markup for mobile. Ideally, we parse the desktop markup and take full control of the final HTML. However, this isn't always possible, and sometimes we just output the desktop markup as is: untouched, or perhaps partially wrapped in order to control the appearance entirely through CSS.

0 commit comments

Comments
 (0)