ReactJs using styles css module on react-burger-menu component - reactjs

I am working with react-burger-menu and I am having trouble setting the styles={styles} tag on the menu component, from a CSS module.
Given :
./App.js
import Page from './components/Page/Page';
import SideMenu from './components/sideMenu/SideMenu.js';
import styles from './components/sideMenu/SideMenu.module.css';
function App() {
return (
<div className="app" id="app">
<SideMenu styles={ styles } pageWrapId={ "page-wrap" } outerContainerId={ "app" }/>
<div id="page-wrap">
<Page />
</div>
</div>
);
}
export default App;
./sideMenu/SideMenu.js
import React from "react";
import { scaleRotate as Menu } from "react-burger-menu";
export default props => {
return (
<Menu {...props} >
<a className="menu-item" href="/">
Home
</a>
</Menu>
);
};
./sideMenu/SideMenu.module.css
.bm-burger-button {
position: fixed;
width: 36px;
height: 30px;
right: 36px;
top: 36px;
}
.bm-burger-bars {
background: #373a47;
}
I am unable to pass the prop styles to SideMenu menu bar. The stylesheet is just not read. I have also tried style={styles} instead. If I omit the styles={styles} tag and just put the CSS styling in ./index.css then the styling is rendered correctly but I want to separate the menu styling as a module.
I have also tried to write the
import styles from '../../components/sideMenu/SideMenu.module.css'
inside the sideMenu/SideMenu.js file instead, and return (<Menu {...props} styles={styles}> (...) but still with no success.
Finally, I have also tried to set
<div className="app" id="app" styles={styles}> (...) </div>
in ./App.js as it was suggested in this post but that doesn't work either.
Does anyone know what I am doing wrong?

First, it’s recommend to use camelCase for classes with more than one word. The reason for this is that you’ll access these classes later as properties of a JS object, hyphens are not syntactically allowed. So you must first of all change your SideMenu.module.css for example like this:
.bm_burger_button {
position: fixed;
width: 36px;
height: 30px;
right: 36px;
top: 36px;
}
.bm_burger_bars {
background: #373a47;
}
Import the module you’ve just created from within your component, like you did:
import styles from './components/sideMenu/SideMenu.module.css';
To use a class defined in your module, just refer to it as a normal property from the styles object, like:
<div className={styles.bm_burger_button}> (...) </div>

Related

How to reduce the number of product visible on my react carousel?

I have created a product carousel with react, it has 10 products, but I only want a max of 4 products to display at any one time, I tried reducing the width but that didn't work. Also had a look at the documentation for the carousel but couldn't find a solution for this https://www.npmjs.com/package/pure-react-carousel#component-properties-props
This is the git repo for the carousel https://github.com/RMP1992/react-product-carousel
react Carousel component
import React from 'react';
import {data} from'../data/data';
import { CarouselProvider, Slider, Slide, ButtonBack, ButtonNext, Image } from 'pure-react-carousel';
import './Carousel.css';
import Card from './Card';
export default class extends React.Component {
render() {
return (
<CarouselProvider
naturalSlideWidth={100}
naturalSlideHeight={125}
currentSlide={4}
totalSlides={10}
visibleSlides={4}
>
<Slider>
{data.map(item =>(
item.carouselData.map(product => (
<Slide >
<Image><img src={product.productImageUrl}/></Image>
<p>{product.name}</p>
<p>£{product.price.formattedValue}</p>
</Slide>
))
))}
</Slider>
<ButtonBack>Back</ButtonBack>
<ButtonNext>Next</ButtonNext>
</CarouselProvider>
);
}
}
CSS
.image___xtQGH {
display: block;
}
.carousel__slide {
list-style-type: none;
padding-bottom: unset !important;
width: 306px !important;
border: black solid 1px;
margin: 0 10px !important;
}
.carousel__slider-tray {
display: flex;
flex-direction: row;
}
I see you haven't imported their css yet.
Add this line to your App component and delete all your custom css as well. I think everything will work just fine.
import "pure-react-carousel/dist/react-carousel.es.css";
You can use Array#filter method to take first n elements from array.
{data.filter((item, index) => index < 4).map(item =>(
item.carouselData.map(product => (
<Slide >
<Image><img src={product.productImageUrl}/></Image>
<p>{product.name}</p>
<p>£{product.price.formattedValue}</p>
</Slide>
))
))}
Also, you can use lodash take method
https://lodash.com/docs/4.17.15#take

How to customize toolbar in primereact?

I am new to primereact. I wanted a custom toolbar like in material-ui.
<AppBar>
<Toolbar>
<h4> App Header </h4>
</Toolbar>
</AppBar>
But primereact's toolbar isn't accepting children. Any way to fix?
Based on PrimeReact's Documentation, Toolbar provides left and right templates to place content at these sections. But if you would like to achieve a Toolbar same as material-ui you should do something like this:
import "./styles.css";
import { Toolbar } from "primereact/toolbar";
import "primereact/resources/themes/saga-blue/theme.css";
import "primereact/resources/primereact.min.css";
import "primeicons/primeicons.css";
export default function App() {
return (
<>
<Toolbar left={<h4> App Header </h4>} />
</>
);
}
But after rendering the component, seems it doesn't the thing you would like to achieve and its in the consequence of toolbar style. Thus for changing style of toolbar you need to add this styles:
.p-toolbar {
position: fixed;
left: auto;
top: 0;
right: 0;
width: 100%;
padding: 0 1rem !important;
}

Is there a way to override style from Semantic-UI with style from Styled-Components in ReactJS?

I'm building a small project in ReactJS and I would like to use Semantic-UI (as a main theme of my website) and override some parts of it with Styled Components (like if it was some custom CSS).
When I'm trying to import them on the same component, I've noticed that Styled-Components will use some of the style I wrote but not all of it (like text-size or some margin).
EDIT
Here's the way I've tried to implement them in my component:
import "semantic-ui-css/semantic.min.css";
import { Grid, Form, Segment, Button, Header, Message, Icon } from 'semantic-ui-react';
import { StyledRegister } from '../styles/StyledRegister';
class Register extends React.Component {
...
render() {
return (
<StyledRegister>
<Grid textAlign="center" verticalAlign="middle">
<Grid.Column style={{ maxWidth: 450 }}>
// Here's continue the long code of my register page including form ect...
</Grid.Column>
</Grid>
</StyledRegister>
Do any of you have an idea if it's possible, and if so, is there proper way to do it ?
An example with the custom adapted statistics component of semantic ui:
import "semantic-ui-css/semantic.min.css";
import { StyledStatistic } from "./styledStatistic";
function App() {
return (
<div className="App">
<StyledStatistic>
<StyledStatistic.Value>5,550</StyledStatistic.Value>
<StyledStatistic.Label>Downloads</StyledStatistic.Label>
</StyledStatistic>
</div>
);
}
import styled from "styled-components";
import { Statistic } from "semantic-ui-react";
export const StyledStatistic = styled(Statistic)`
padding: 2rem;
display: flex !important;
justify-content: center;
&&& > .value {
color: red;
}
&&& > .label {
color: green;
}
`;
See: https://codesandbox.io/s/goofy-mestorf-go410

Unexpected white area below html in Reactjs

I use Reactjs and encounter an unexpected white area below the html tag:
I've checked all my .css files and cannot find any margin, padding or position attributes that might cause that problem. When I use Google Chrome inspector, I cannot inspect that white space since it's below my html tag (image above). There's no error messages indicate the cause.
My react app structure:
Here's the code:
App.js:
import React, {Component} from 'react';
import Wrapper from './components/Wrapper';
import SongCard from './components/SongCard';
import songs from './songs.json';
class App extends Component {
state = {
songs
};
render() {
return ([
<div key={0}>
<Wrapper>
{this.state.songs.map(song => (
<SongCard
key={song.id}
id={song.id}
name={song.name}
artist={song.artist}
src={song.src}
/>
))}
</Wrapper>
</div>
]);
}
}
export default App;
Wrapper.js:
import React from 'react';
import './Wrapper.css';
const Wrapper = props => (
<div className="row justify-content-center">
<div className="col-lg-8">{props.children}</div>
</div>
);
export default Wrapper;
Wrapper.css:
.row, .col-lg-8 {
margin: 0;
padding: 0;
color: white;
font-family: 'Amaranth', sans-serif;
}
SongCard.js:
import React from 'react';
import './SongCard.css';
const SongCard = props => (
<div className="song-card">
<div className="row">
<div className="col-auto spotify-player">
<iframe title={props.id} src={props.src} frameBorder="0" allowtransparency="true"></iframe>
</div>
<div className="col text-center song-info">
<div>{props.name}</div>
<div>{props.artist}</div>
</div>
</div>
<div className="song-card-divider"></div>
</div>
);
export default SongCard;
SongCard.css:
iframe {
width: 100%;
}
.col, .col-auto, .song-card {
margin: 0;
padding: 0;
background-color: #343a40;
}
.spotify-player {
width: 80px;
height: 80px;
}
.song-card-divider {
height: 0;
border-top: 1px solid #e9ecef;
}
I've looked for answers but couldn't find any answer.
I've found the cause of the problem:
It's the iframe height that causes the extra white area on the bottom of the page. I post my answer here so this question can be marked as answered and hopefully it helps people who have the same problem.

styled-components - how to set styles on html or body tag?

Ordinarily, when using pure CSS, I have a style sheet that contains:
html {
height: 100%;
}
body {
font-size: 14px;
}
When using styled-components in my React project, how do I set styles for the html or body tags? Do I maintain a separate CSS file just for this purpose?
You can, of course, maintain a separate CSS file that you include in your HTML via a <link> tag.
For v4:
Use createGlobalStyle from Styled-components.
import { createGlobalStyle } from 'styled-components'
const GlobalStyle = createGlobalStyle`
body {
color: ${props => (props.whiteColor ? 'white' : 'black')};
}
`
<React.Fragment>
<GlobalStyle whiteColor />
<Navigation /> {/* example of other top-level stuff */}
</React.Fragment>
Pre v4:
Styled-components also exports an injectGlobal helper to inject global CSS from JavaScript:
import { injectGlobal } from 'styled-components';
injectGlobal`
html {
height: 100%;
width: 100%;
}
`
See the API documentation for more information!
As of Oct, 2018.
NOTE
The injectGlobal API was removed and replaced by createGlobalStyle in
styled-components v4.
According to docs createGlobalStyle is
A helper function to generate a special StyledComponent that handles global styles. Normally, styled components are automatically scoped to a local CSS class and therefore isolated from other components. In the case of createGlobalStyle, this limitation is removed and things like CSS resets or base stylesheets can be applied.
Example
import { createGlobalStyle } from 'styled-components'
const GlobalStyle = createGlobalStyle`
body {
color: ${props => (props.whiteColor ? 'white' : 'black')};
}
`
// later in your app
<React.Fragment>
<Navigation /> {/* example of other top-level stuff */}
<GlobalStyle whiteColor />
</React.Fragment>
Read more on official docs.

Resources