Supply css class to 3rd party lib without using the name "className"
See original GitHub issueEnvironment
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
- Run the app, expect the P tag to be displayed as 60px, but it’s not.
- 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;
}
`;
- 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:
- Created 5 years ago
- Comments:19 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not sure why your above example doesn’t work:
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:
Is this fixed? I am using react-bootstrap and barely can integrate styled-components wince class names are not appended.