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.

Supply css class to 3rd party lib without using the name "className"

See original GitHub issue

Environment

System:

  • OS: macOS High Sierra 10.13.6
  • CPU: x64 Intel® Core™ i7-4770HQ CPU @ 2.20GHz
  • Memory: 167.45 MB / 16.00 GB
  • Shell: 3.2.57 - /bin/bash

Binaries:

  • Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
  • Yarn: 1.10.1 - ~/.nvm/versions/node/v8.11.3/bin/yarn
  • npm: 6.4.1 - ~/.nvm/versions/node/v8.11.3/bin/npm

npmPackages:

  • styled-components: ^3.4.9 => 3.4.9

Preparation

Create an app with create-react-app,

Change the App.js to this:

import React, { Component } from "react";
import "./App.css";
import styled from "styled-components";

const MyPart = ({ pClassName }) => (
  <p className={pClassName}>this is the P tag</p>
);

const StyledMyPart = styled(MyPart).attrs({
  pClassName: "for-p-tag"
})`
  &.for-p-tag {
    font-size: 60px;
  }
`;

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <StyledMyPart />
        </header>
      </div>
    );
  }
}

export default App;

Steps to reproduce

  1. Run the app, expect the P tag to be displayed as 60px, but it’s not.
  2. Change the code to this:
const MyPart = ({ className }) => (
  <p className={className}>this is the P tag</p>
);

const StyledMyPart = styled(MyPart).attrs({
  className: "for-p-tag"
})`
  &.for-p-tag {
    font-size: 601px;
  }
`;
  1. Run the app, now it works.

What is the problem

How to make the styling work when the prop name is not className?

About it

Asked this previously on Stackoverflow.com without any luck. https://stackoverflow.com/questions/52378924/use-styled-components-to-supply-a-css-class/52441187#52441187

If the name of the prop is not className, it won’t work.

Don’t know if I did anything wrong, but I tried many ways. Is this just not supported or I miss something? Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
joewoodcommented, Oct 10, 2018

I’m not sure why your above example doesn’t work:

const StyledDatePicker = styled(DatePicker)`
    .my-calendar {
        color: black;
    }
`;

<StyledDatePicker
   calendarClassName="my-calendar"
/>

The same approach works in this trivial example: https://codesandbox.io/s/q3zp4v70m9. You should be able to default the prop too, to make it simpler:

const StyledDatePicker = styled(DatePicker).attrs({
    calendarClassName:"my-calendar"
})`{
    color:blue;
   }
    .my-calendar {
        color: black;
    }`;

<StyledDatePicker/>
1reaction
naoryecommented, May 28, 2019

Is this fixed? I am using react-bootstrap and barely can integrate styled-components wince class names are not appended.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use styled-components to supply a css class - Stack Overflow
It probably indicates that third party lib doesn't handle the additional className so that it's impossible for you to combine SC with the...
Read more >
Features In-Depth | Less.js
Less extends CSS with dynamic behavior such as variables, mixins, ... makes it possible to repeatedly refer to a parent selector without repeating...
Read more >
Code completion | WebStorm Documentation - JetBrains
These suggestions are based on the names of classes, types, and interfaces that are defined in your project, in the libraries you are...
Read more >
You don't know the classNames library - Arek Nawo
As the name implies, it's primarily meant for dealing with CSS classNames (very common in React and any other JSX-based UI framework), ...
Read more >
BlueJ FAQ
Installation and ConfigurationGeneral"BlueJ could not find any Java systems. ... Then type in (or select from the popup menu) the full class name...
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