question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resolve ampersand to component-specific class, not instance-specific class

See original GitHub issue

In the bellow example, one would expect the margin-left rule to apply to the button themed with the admin palette. But this is not the case : the ampersand is resolved to the instance class.

import React from 'react';
import styled, { ThemeProvider } from 'styled-components';

const normalPalette = {
   background: 'black',
   foreground: 'white'
}

const adminPalette = {
   background: 'white',
   foreground: 'black'
}

// Create a <Button> react component which background and foreground rules
// are theme-dependant, but the sibling rule with left margin should be transversal
// Unfortunately, it doesn't work because the two sibling button instances have
// different class names.
const Button = styled.button`
  ${({ theme }) => `
    background-color: ${theme.background};
    color: ${theme.foreground};
  `}
  &+& {
    margin-left: 10px;
  }
`;

export default () => {
  return (
    <div>
      <ThemeProvider theme={normalPalette}>
        <Button> normal button </Button>
      </ThemeProvider>
      <ThemeProvider theme={adminPalette}>
        <Button> admin button </Button>
      </ThemeProvider>
    </div>
  );
}

The workaround now is to use a direct reference to the component, as in such example. However, this is counter-intuitive, cumbersome and ugly ! Plus, in my use-case, I declare components and their styles in two steps since I am implementing a rich featured theme engine. In such circumstances, I cannot refer to react components classes.

Feature request (breaking change)

& should now be resolved to the component static class selector.

Alternative (non-breaking)

Offer a specific pseudo class that will be resolved to the static class selector, such as :sc.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
kittencommented, Oct 29, 2017

We already had an issue here regarding this but I think we mistakenly closed it: https://github.com/styled-components/styled-components/issues/774

In v3 we might have more tools to solve this, since we’ll be able to reliable detect this selector and resolve it differently.

Edit: @mxstbr It’s not a good idea to resolve the parent (ampersand) selector to the component class by default, since then when interpolations are used inside the rule, this rule will apply to all components, which is even more confusing.

Instead all references with two styled components (& + &, & > &, & &) should resolve the second parent selector to the component specific class.

1reaction
probablyupcommented, Oct 21, 2018

This is done in v4

Read more comments on GitHub >

github_iconTop Results From Across the Web

LESS: Secrets of the Ampersand - SLaks.Blog
This allows you to add speciallize a class for a specific element type, while keeping the specializations nicely nested within the rest of ......
Read more >
What does &. (ampersand dot) mean in Ruby? - Stack Overflow
Introduced in Ruby 2.3.0, it lets you call methods on objects without worrying that the object may be nil (Avoiding an undefined method...
Read more >
Using Sass to Control Scope With BEM Naming - CSS-Tricks
I do like the BEM syntax (using it myself everyday), but I have to say “composing” class names using the ampersand directive and...
Read more >
Documentation - Ampersand.js
To create a State class of your own, you extend AmpersandState and provide instance properties and options for your class. Typically, this is...
Read more >
Basics - styled-components
No class name bugs: styled-components generates unique class names for your styles. ... as every bit of styling is tied to a specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found