Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for descendant selectors. #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,45 @@ const styles = StyleSheet.create({

Aphrodite will ensure that the global `@font-face` rule for this font is only inserted once, no matter how many times it's referenced.

# Descendant selectors

Descendant rules (i.e. a rule like `.parent:hover .child { ... }` in CSS) are created by adding values to your styles indicating the styles to apply to the child. To distinguish the styles from normal CSS keys, the keys start with a `>>`, like `'>>child'`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On initial read of this, I assumed that child would be a literal class name and it took me a couple read throughs to figure out that child would be a classname generated by Aphrodite.

It might be worth explaining here that this is really only for cases that can't be expressed directly through others mean (which, I believe, is only styling children of pseudo-selector-modified parents).


```js
const styles = StyleSheet.create({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great that we could accomplish this without having to have a special API for child selectors 👍

someContainer: {
":hover": {
">>myChild": {
// Styles to apply to the child when the parent is
// hovered over.
color: "red",
fontWeight: "bold"
}
}
}
});
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add the example css output of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Then, apply the `someContainer` style using `css(styles.someContainer)`, and add the child's style to a descendant of the parent using `css(styles.someContainer.myChild)`, where `myChild` is the name of the key you used, without the `>>`.

For example, using React's jsx syntax:

```jsx
<div className={css(styles.someContainer, ...)}>
<div className={css(styles.someContainer.myChild, ...)}>
This turns red and bold when the parent is hovered over
</div>
</div>
```

This will generate CSS that looks like:
```css
.someContainer_xxx:hover .myChild_xxx {
color: red;
font-weight: bold;
}
```

# Caveats

## Assigning a string to a content property for a pseudo-element
Expand Down
Loading