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

Can't style based on global parent class #702

Closed
gilmoreorless opened this issue Apr 17, 2019 · 2 comments · May be fixed by #923
Closed

Can't style based on global parent class #702

gilmoreorless opened this issue Apr 17, 2019 · 2 comments · May be fixed by #923

Comments

@gilmoreorless
Copy link

Type: Bug

Packages Version
fela 6.1.x & 10.2.x (tested both)
react-fela 7.3.x & 10.2.x

Description

I'm trying to integrate with a third-party library (Tether), which adds dynamic classes to a container component. These classes can be added and removed at any time, based on its position adapting to window size.

I'd like to style a component (entirely within the Tether container) based on the container's current classes. This doesn't seem to be possible with Fela.

The docs say:

While we only support the above selectors for a reason, we also understand that there might be some edge cases (mostly with third-party libraries) where you want to use other selectors as well. Therefore we provide the &-prefix for nested selectors.

From what I can tell, & only works when it's at the start of the selector (hence calling it "&-prefix"). But I need to use it after the Tether class, like .tether-element-attached-top &. This works in tools such as Sass and Styled Components.

This is not just a problem when using Tether, but affects integration with any library that adds CSS classes to the body element as part of its API (e.g. font preloading scripts which add a fonts-loaded class to body for conditional styling).

As a simplified example, I have a component with these basic styles:

{
  background: 'red',
  '.tether-element-attached-top &': {
    background: 'green',
  },
  '.tether-element-attached-bottom &': {
    background: 'blue',
  },
}

On its own the component should have a red background. If used within a Tether element, it should be either blue or green, depending on Tether's positioning calculations. When used within Tether, the markup is effectively this:

<div class="tether-enabled tether-element-attached-right tether-target-attached-right tether-element-attached-bottom tether-target-attached-top">
  <div class="a">This is my component element</div>
</div>

However when I do this, the element is always red, and Fela adds the following warning in the console:
The object key ".tether-element-attached-top &" is not a valid nested key in Fela.

@gilmoreorless
Copy link
Author

For the record, I ended up working around this by adding the conditional styles to the parent container. It's not great, because it breaks the component boundaries, but it was the neatest solution I could find.

For example:

const styles = {
  contents: {
    background: 'red',
  },
  tetherContainer: {
    '&.tether-target-attached-top > [data-tether-contents]': {
      background: 'green',
    },
  },
}

(Then I made sure that the contents always have the data-tether-contents attribute added, to avoid having to know anything about the element type.)

@robinweser
Copy link
Owner

@gilmoreorless Sorry for the delay. So, in general, the & is not a placeholder for the current class in Fela as this breaks with the idea of component-centric styles in my opinion. Sadly, there's no better way to work around this for now as we believe toggling classes is not the "right" way to do it at all. Knowing that many libs do this anyways, I think your solution is fine enough for this specific use case.

As there's not going to be a better solution anytime soon, I'd love to close this one. Feel free to reopen if there's anything left you'd like to discuss!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants