Currently styled-components uses MurmurHash algorithm to create a unique identifier and then converts the hash number to alphabetic name.
Each component instance with unique props has it’s own CSS class name which is generated by means of the MurmurHash algorithm, the componentId and the evaluatedStyles string:
const className = hash(componentId + evaluatedStyles);
Then this class name is stored in the component state as generatedClassName.
Answer from RPG on Stack OverflowAdd prefix to class of all the styled components
More meaningful/semantic class names during development
Random classes getting displayed when we use styled components?
Prefix for CSS class names in Component styles?
Currently styled-components uses MurmurHash algorithm to create a unique identifier and then converts the hash number to alphabetic name.
Each component instance with unique props has it’s own CSS class name which is generated by means of the MurmurHash algorithm, the componentId and the evaluatedStyles string:
const className = hash(componentId + evaluatedStyles);
Then this class name is stored in the component state as generatedClassName.
This was about all I could find in the styled-components FAQ
Each node actually has two classes connected to it: one is static per component, meaning each element of a styled component has this class. It hasn't any style attached to it. Instead, it's used to quickly identify which styled component a DOM objects belongs to or to make minor changes in the DevTools. It's also used for component selectors. The static class probably will look something like:
.sc-fVOeaW.The other is dynamic, meaning it will be different for every element of your styled component with different props, based on what the interpolations result in. It will probably look like
.fVOeaW(note the lack of "sc" prefix.)For example, the styled component
<Button />would render with the same static class every time. If the styles are changed using interpolations, like<Button secondary />, then the dynamic class will be a different one, while the static class would remain the same.
Also, Motivation
No class name bugs: styled-components generates unique class names for your styles. You never have to worry about duplication, overlap or misspellings.
TL;DR They are automagically generated and maintained by styled-components.
I know you can easily do this for a static stylesheet with something like PostCSS but Is it possible to prefix class names for component styles since they are embedded in the JS files when the project is built?
The environment that we deploy our application to is a hellscape of global CSS and we have had a lot of clashes between global styles and our component class names.
You need to provide className for the wrapper/container as styled-component injecting the styles through it:
const SignupForm = ({ className }) => (
<form className={className}>
<input className="input" />
<button className="button">Button</button>
</form>
);
const Form = styled(SignupForm)`
.input {
background-color: palegreen;
}
.button {
background-color: palevioletred;
}
`;
Just add extra atrribute className by using attrs to existing styled component.
const FormWrapper = styled.div.attrs({
className: 'SignupForm',
})`
.input {
/* Custom Styles */
}
.button {
/* Custom Styles */
}
`