Reactjs Module CSS multiple class selector - reactjs

I'm trying to implement or add multiple class in out container when I click a button. But it seems that the styling is not being applied. Below are my code
Layout.module.css
.Content {
padding-left: 240px;
min-height: calc(100vh);
top: 0;
display: block;
position: relative;
padding-top: 80px;
background: #eee;
padding-bottom: 60px;
transition: padding-left 0.2s linear;
}
.Content.collapse {
padding-left: 100px;
display: block;
transition: padding-left 0.2s linear ;
}
Now I added the collapse class in my nav component like so
const layout = (props) => (
<Aux>
<Sidebar collapse={props.collapse} />
<div className={`${classes.Content} ${props.collapse ? 'collapse' : ''}`}>
<TopNavigation toggle={props.toggle}/>
{props.children}
<Footer />
</div>
</Aux>
);
So basically I'm just checking the props if it's collapse or not. If it is then I'll add a text collapse in the class.
Now when I click on a button it sets the state.collapse = true/false. It was able to do it's job. Now it seems that it's not reading my css style. Below is the generated class in my DOM
Notice the class .Content styling was detected. But as you can see here
Layout_Content__2WLOk collapse the div container has a class of collapse. So I was thinking it should read the .Content.collapse selector. Am I missing something here?

When using CSS modules, it creates a unique classname for each class for each instance of the component.
So you need to use the imported classes to have access to the generated classnames, just like you do for the .Content
So
<div className={`${classes.Content} ${props.collapse ? classes.collapse : ''}`}>

You are using a string not the generated hash
this part will not work
${props.collapse ? 'collapse' : ''}
Quick fix
Try not chaining it.
.collapse {
padding-left: 100px;
display: block;
transition: padding-left 0.2s linear ;
}
and add
classes.collapse instead of collapse
${props.collapse ? classes.collapse : ''}

In React you need to use the keyword 'className' instead of 'class'
https://reactjs.org/docs/dom-elements.html#classname
Also if you want to use CSS Modules you need to import your Layout.module.css file like this
import styles from './Layout.module.css';
And you can add CSS selector like this
<div className={styles.Content}></div>
you can study this here https://www.w3schools.com/react/react_css.asp

Related

How to add multiple classNames to an html element when using scss in React

I have a styles.module.scss file and a react component. How can add multiple classNames from the styles to ONE html element?
Below is my code:
/styles/styles.module.scss file
.gridContainer {
display: grid;
gap: 2em;
grid-template-columns: 1fr 25em;
grid-template-rows: auto;
}
.gridItem {
display: flex;
flex-flow: column;
}
.imageWithText {
border: 1px solid #4338ca;
border-radius: 1.6rem;
align-self: start;
}
/pages/infoPage.tsx file
import styles from "../styles/styles.module.scss";
export default function InfoPage({}: PageProps) {
<div className={styles.gridContainer}>
<div className={styles.gridItem}>
...
</div>
{/* I have added TWO style selectors here */}
<div className={(styles.gridItem, styles.imageWithText)}>
...
</div>
</div>
}
When I run this code, the div with TWO style selectors does not work as expected. The second classname={styles.gridItem} does not take the scss styles from the .gridItem selector. This div takes the styling only from the .imageWithText.
EXPECTED BEHAVIOR: I want the styles for both the .gridItem and the .imageWithText to apply to the second div.
I believe I am not writing the multiple selectors in the html element correctly and this has possibly got something to do with the way scss works. Any help would be appreciated.
You can try with backtic character (`), so your code :
<div className={`${styles.gridItem} ${styles.imageWithText}`}>

How to stop a css animation in react [duplicate]

This question already has answers here:
How to pause and resume CSS3 animation using JavaScript?
(5 answers)
Closed 2 years ago.
I have a problem where I have made my img rotate in a 360 continously but I want it to only do it when the music is playing. I am passing the value of isPlaying into the function but I do not know how to access the CSS value from React.
import React from "react";
const Song = ({ currentSong, isPlaying }: any) => {
return (
<div className="song-container">
<img id="image" alt={currentSong.name} src={currentSong.cover}></img>
<h2>{currentSong.name}</h2>
<h3>{currentSong.artist}</h3>
</div>
);
};
export default Song;
This is my CSS file:
.song-container {
min-height: 70vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
img {
width: 40%;
border-radius: 50%;
animation: spin 10s linear infinite;
animation-play-state: paused;
}
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
h2 {
padding: 3rem 1rem 1rem 1rem;
font-size: 3rem;
}
h3 {
font-size: 1.5rem;
}
}
I would just like to be able to stop it when the state is not playing and when it is playing I want the animation to play. I am also using typescript as I want to learn the language but if it is too hard to implement over I will just transition my website over to javascript.
There are two ways of doing this: inline styles and toggling some CSS class.
For inline styles method, you should set the style attribute for the img element, and the properties should be in camel case:
<img id="image" alt={currentSong.name} src={currentSong.cover} style={{ animationPlayState: isPlaying ? "running" : "paused" }} />
For the class toggling method, you should create a class in your CSS that changes the animation state.
For example, in the CSS:
img.animate {
animation-play-state: running;
}
Then, in the img element:
<img id="image" alt={currentSong.name} src={currentSong.cover} className={isPlaying ? "animate" : undefined} />
Toggling classes is a best practice, but both will work and inline styles are better when the styles are based on dynamic values you cannot predict.
Reference: https://reactjs.org/docs/dom-elements.html#style

how to change the antd carousel dot shape and color with styled component

tried to change the antd carousel dots styles, it can be able to implement with CSS but not with styled-components(CSS is not allowed in my project). as im new to front-end dont know the proper solution for this.
here is the code example https://stackblitz.com/edit/react-opnojd-rfagtz?file=index.js
thanks in advance :)
When using styled-component, the style would be applied with className sc-....
In your case, its style would be applied in div containing .slick-slider.
But, .ant-carousel is className for parent of that.
So, if it was included in the selector, style will be not applied.
Try this.
const CarouselWrapper = styled(Carousel)`
> .slick-dots li button {
width: 6px;
height: 6px;
border-radius: 100%;
}
> .slick-dots li.slick-active button {
width: 7px;
height: 7px;
border-radius: 100%;
background: red;
}
`;

Left-aligning content inside a button

I've been struggling for hours now trying to align an icon and text inside a button to the left. Currently the button is stretched and looks like this:
I want the chevron arrow and text to be left aligned inside the button. How to achieve this? (I'm using microsofts ui-fabric, don't think the css here interfere, i don't get any text aligning even if i remove them or change DefaultButton to a regular Button)
Render method:
public render(): JSX.Element {
const { isCollapsed } = this.state;
const { buttonText, collapsibleSectionText } = this.props;
return (
<div>
<DefaultButton
onClick={this._toggleCollapse} className="CollapsibleSection"
>
{isCollapsed ? <i className="ms-Icon ms-Icon--ChevronUp" aria-hidden="true" /> : <i className="ms-Icon ms-Icon--ChevronDown " aria-hidden="true" />}
<p>{buttonText}</p>
</DefaultButton>
{isCollapsed && collapsibleSectionText}
</div>
);
}
css:
.CollapsibleSection {
margin-top: 2%;
width: 100%;
}
DefaultButton component has property text-align: center;. You can check in their official docs. What I suggest you should do is try text-align: left; If that does not override fabric's css rule, then try to see if your rule is applied, since maybe fabric's rule have greater specificity then your css rule. If this does not work, then you need to override fabric's display property and say the following:
.CollapsibleSection {
margin-top: 2%;
width: 100%;
display: flex; //overriding fabric's display inline-block;
justify-content: flex-start:
}
Hi I just had to add text-align: left to make it work
.CollapsibleSection {
margin-top: 2%;
text-align: left;
width: 100%;
}
Quick answer:
justify-content: flex-start
If you'd like to learn more about it you can play this wonderful game which will give you a good knowledge about flexbox and positioning within it.
https://flexboxfroggy.com/

Styling React Bootstrap Component in Active State

I'm trying to style a React Bootstrap Navbar. I was able to successfully style the non-active tabs, however, I cannot seems to reach the active tab.
My styles live is a separate .scss file. Here's the relevant snippet:
.navitem {
a {
font-size: 12px;
color: #000;
background-color: #e7e7e7;
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-bottom-color: #357ebd;
border-bottom-style: solid;
border-radius: 0px !important;
display: inline-block;
border-bottom-width: 2px;
}
flex: 1;
}
.navitem.active {
background-color: #777777;
}
When an active Bootstrap component is created, its HTML looks like this:
<li role="presentation" class="Preview-navitem--19fA7 active"><a role="button" href="#">Preview</a></li>
Here's the HTML inside the react render() method:
<Nav
className={styles.navbar}
bsStyle="pills"
activeKey={this.state.value}
onSelect={this.handleSelect}
>
<NavItem className={styles.navitem} eventKey="1">Preview</NavItem>
{navItem}
</Nav>
So, basically, I can style the .navitem, but not the .active.
Any idea how I could accomplish this?
Assuming this is CSS Modules, which it looks like based on the pasted code, the solution is to use global selectors. CSS Modules briefly mentions this on the main README page under Exceptions. However, their webpack demo has a global selector example you can clone and play around with.
In short, you should change:
.navitem.active {
background-color: #777777;
}
to:
.navitem:global(.active) {
background-color: #777777;
}
in order for it to be processed properly by Webpack. This should be all that's needed!

Resources