I have an issue applying a custom css from an external css module to an Input from Reactstrap.
So I have the following code:
<Input
className={'border-0 p-0'}
style={{
width: '150px',
height: '100px',
fontSize: '18px',
overflow: 'hidden',
resize: 'none',
backgroundColor: 'ffffff',
boxShadow: 'none',
marginTop: '-10px',
marginBottom: '10px'
}}
innerRef={register}
type={'textarea'}
readOnly
/>
It is working now with inline styling, but there are too many properties and I want to put them in an scss external module, but it's not working.
I've tried style={styles.customCss} and it's not working, my custom styles are not applied. I've also tried to put className={'border-0 p-0' && styles.customCss} and still not working.
Any ideas?
In my Css external file I have
.customCss {
width: '150px';
height: '100px';
font-size: '18px';
overflow: 'hidden';
resize: 'none';
background-color: 'ffffff';
box-shadow: 'none';
margin-top: '-10px';
margin-bottom: '10px'
}
and in my component I have
import React from 'react';
import { useForm } from 'react-hook-form';
import {
Container,
Row,
Col,
Label,
Input,
Button,
FormText
} from 'reactstrap';
import classnames from 'classnames/bind';
import styles from './styles.module.scss';
const cx = classnames.bind(styles);
const customComponent = props => {
const { register, handleSubmit } = useForm();
return (
<div className={styles.container}>
<div className={styles.heading}>
<span className={styles.label}>
{heading}
</span>
</div>
<form>
<Col className={'p-0'}>
<Input
className={cx(styles.customCss, 'border-0 p-0')}
id="customer"
name="customer"
defaultValue={lot}
innerRef={register}
type={'textarea'}
readOnly
/>
</Col>
</form>
</div>
);
};
export default customComponent;
I'd assume that you're working with CSS modules when importing the styles, i.e. you have an import styles from './styles.module.scss' at the top of your file.
styles.customCss would contain the actual CSS class name, not the full style object itself. So addressing your attempt in using style={styles.customCss}, that won't work. See CRA's page on CSS modules.
Now for your other attempt in using className={'border-0 p-0' && styles.customCss}, I get the idea that you're trying to concatenate the class name from your custom CSS and Bootstrap's classes. What the code actually does is perform a short-circuit evaluation on 'border-0 p-0' && styles.customCss, which will only evaluate to styles.customCss. For combining CSS classes in the className prop, usually a library like clsx is helpful.
UPDATE
Looks like your CSS file is wrong. You're working with CSS, i.e. the values don't have quotes. That means, your styling would be something like this:
.customCss {
width: 150px;
height: 100px;
font-size: 18px;
overflow: hidden;
resize: none;
background-color: #ffffff;
box-shadow: none;
margin-top: -10px;
margin-bottom: 10px;
}
Working example in sandbox demo
Related
At the moment I'm using SCSS, as it is easy to use with NextJS. I really like how this system works, using the SCSS modules, and so I would also like to use it when using Material-UI. Material-UI uses JSS, which is a lot of boilerplate to write every time. Additionally, I prefer not to work with two ways of styling (SCSS modules and JSS). I already changed the order of CSS injection, so that I can use my SCSS modules on Material-UI components. However, I'm wondering if there is a way to overwrite styles of Material-UI components using SCSS modules? I have tried the following and a few similar things, but nothing seemed to work:
import styles from "./Login.module.scss";
import Button from "#material-ui/core/Button";
function Login() {
return (
<section>
<Button className={styles.button} variant="contained" color="primary">
Verify
</Button>
</section>
);
}
export default Login;
.button {
padding: 10px;
margin-bottom: 10px;
.MuiButton-containedPrimary {
border: 2px solid red;
background-color: red;
}
}
Below is the correct syntax:
.button {
padding: 10px;
margin-bottom: 10px;
&:global(.MuiButton-containedPrimary) {
border: 2px solid red;
background-color: red;
}
}
The example above has two key changes:
Using :global(.MuiButton-containedPrimary). Without using :global, CSS modules will transform the MuiButton-containedPrimary into a class name unique to this module and then it won't match what Material-UI puts on the button.
Adding the & in front effectively creates a selector like .button.MuiButton-containedPrimary matching an element with both classes on it. Your original syntax would treat .MuiButton-containedPrimary as a descendant selector and only match elements with that class that are a descendant of the button rather than the button itself.
You can use makeStyles of #material-ui. And pass to classes to override CSS default of material-ui.
import { makeStyles } from "#material-ui/styles";
const useStyle = makeStyles(() => ({
root: {
padding: "10px",
marginBottom: "10px",
},
containedPrimary: {
border: "2px solid red",
backgroundColor: "red"
}
}))
function Login() {
const classes = useStyle();
return (
<section>
<Button classes={classes} variant="contained" color="primary">
Verify
</Button>
</section>
);
}
Any one know where I am going wrong? I have the latest version of node-scss installed via npm.
I import my scss files as such import './loginform.module.scss';
This is an example component:
import React from 'react';
import './loginform.module.scss';
const LoginForm = (props) => {
return (
<div className='loginFormContainer'>
<form className='loginForm'>
<div className='loginFormUsermame'>
<label for='username'>Username</label>
<input
id='username'
type='text'
placeholder='Username'></input>
</div>
</form>
</div>
);
};
export default LoginForm;
It seems I am not getting any styles from my file. This is my .scss file
.loginFormContainer {
width: 100%;
max-width: 24rem;
.loginForm {
padding-left: 2rem;
padding-top: 1.5rem;
padding-right: 2rem;
padding-bottom: 2rem;
margin-bottom: 1rem;
background-color: #ffffff;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
border-radius: 0.375rem;
.loginFormUsername {
display: block;
color: #4a5568;
font-weight: bold;
margin-bottom: 0.5rem;
}
}
}
Any ideas? It seems to work in things with out classes such as p tag for example. I could make that massive via the scss file...but its my classes....
CodeSandBox: https://codesandbox.io/s/broken-shadow-n8517
The issue is that .module.scss naming convention is reserved for css modules that work with scss also in create-react-app. So the styles are being exported as an object here and you'll have to destructure the classnames (or use as styles.<className> if not destructuring ). You'll have to use them like this
import React from "react";
import {
loginFormContainer,
loginForm,
loginFormUsermame
} from "./login.module.scss";
/*
import styles from "./login.module.scss"
//to use as styles.loginFormContainer
*/
const LoginForm = props => {
return (
<div className={loginFormContainer}>
<form className={loginForm}>
<div className={loginFormUsermame}>
<label htmlFor="username">Username</label>
<input id="username" type="text" placeholder="Username" />
</div>
</form>
</div>
);
};
export default LoginForm;
OR
Change the naming of the scss file to not include .module.scss like simply to login.scss or login-module.scss.
Hope this helps !
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
hi I am using styled componant in react
const H4 = styled.h4`
font-size: 15px;
letter-spacing: 0.38px;
line-height: 1.33;
color: red;
padding: 20px;
`;
<H4>Small header</H4>
it will give exsact style in H4 tag. but how can I override this padding with utility classes like m-b-10 it will give margin-bottom:10px
something like this <H4 m-b-10>small header</H4>
same time I need to use this utility class whereever I want. in css I can simply write
m-b-10{margin-bottom:10px !important};
how can achieve this things on styled componenet?
One of the best solutions is to use https://styled-system.com/, it plays well with Styled Components and other libraries like Emotion and it offers what you need, to use utility-classes in the component definition.
Example:
import styled from 'styled-components'
import { color, space, fontSize } from 'styled-system'
// Set default styles and add styled-system functions to your component
const Box = styled.div`
/*defaut styles */
color: white;
font-size: 25px;
padding: 20px;
/* configurable properties */;
${color};
${space};
${fontSize};
`;
And to use it:
<Box bg="black" >
Lorem Ipsum
</Box>
<Box bg="black" color="green" fontSize="12px" p="10px">
Lorem Ipsum
</Box>
That code, will render this:
It also supports Media-Querys, Themes etc..
You can play with this CodeAndSandbox where it is been used with Styled Components https://codesandbox.io/s/styled-components-with-styled-system-njr17
You can use variables like
const m-b-10 = '20px';
const H4 = styled.h4`
padding: ${m-b-10};
`;
You can define such variables in a separate file and import them to styles components
You can define utility classes in the top component of your React tree. The example uses Gatsby.js, but you can easily adapt it for other React framework.
// src/components/layout.js
const GlobalStyles = createGlobalStyles`
html {
/* Put your CSS variables here*/
}
.m-b-10 {
margin-bottom: 10px;
}
`
Then make whatever defined in createGlobalStyles available for access in child components.
// src/components/layout.js
export default function Layout({ children }) {
return (
<React.Fragment>
<GlobalStyle />
{children}
</React.Fragment>
)
}
// src/pages.index.js
import Layout from "../components/layout"
const H4 = styled.h4`
font-size: 15px;
letter-spacing: 0.38px;
line-height: 1.33;
color: red;
padding: 20px;
`;
const IndexPage = () => {
return (
<Layout>
<H4 className="m-b-10" />
</Layout>
)
}
I want to add a Floating Action Button (FAB) on my app. Where I want to adjust its width and height to reduce its size using css. Also the button does not show the edit icon inside the button as expected.
Below are the code that I tried,
Code Sandbox
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import './profile/profile.css';
import ProfileInfo from './profile/profile';
ReactDOM.render(
<ProfileInfo />,
document.getElementById('root')
);
profile.js
import React from 'react';
import './profile.css';
import Fab from '#material-ui/core/Fab';
import Icon from '#material-ui/core/Icon';
class ProfileInfo extends React.Component{
render(){
return(
<div className='prof-main-container'>
<div className='prof-items-container'>
<div className='prof-pic-container'><img src="https://ik.imagekit.io/upgrad1/upgradlogo.png" className="prof-pic" alt="profile pic" /></div>
<div className='prof-info-data-container'>
<div className='user-name'>User Name</div>
<div className='posts-follows-container'>
{/* <span className='posts-follows-item-first'>Posts:6</span>
<span className='posts-follows-item'>Follows:4</span>
<span className='posts-follows-item'>Followed By:6</span> */}
<span>Posts:6</span>
<span className='posts-follows-item'>Follows:4</span>
<span>Followed By:6</span>
</div>
<div className='full-name-container'>
<div className='full-name'>UpGrad Education</div>
<div className='full-name-edit-btn'>
{/* <Button variant="fab" color="secondary" size='medium'>
Edit
</Button> */}
<Fab color="secondary" aria-label="Edit" className='edit-btn'>
<Icon>edit_icon</Icon>
</Fab>
</div>
</div>
</div>
</div>
</div>
)
}
}
export default ProfileInfo
profile.css
.prof-main-container {
display: flex;
background-color: #5995DA; /* Blue */
padding: 20px 0;
}
.prof-items-container {
border: 1px solid #fff;
width: 100%;
display: flex;
/* justify-content: space-around; */
padding: 0% 20%;
/* padding-left: 500px; */
}
.prof-info-data-container{
display: flex;
/* justify-content: space-between; */
flex-direction: column;
margin: 0px 40px;
}
.prof-pic-container{
padding: 15px 0px 0px 0px;
}
.prof-pic{
width: 50px;
height: 50px;
}
.posts-follows-item{
margin: 0px 60px;
}
.posts-follows-container{
font-size: 11px;
margin: 2px 0px;
}
.full-name-container{
display: flex;
flex-direction: row;
}
.full-name{
font-size: 12px;
margin: 10px 0px;
}
.user-name{
font-size: 12px;
font-weight: bold;
margin: 5px 0px;
}
.full-name-edit-btn{
margin: 5px 20px;
}
.edit-btn{
width: 20px;
height: 20px;
}
The expected edit icon is as below.
But actually it is rendering as below. Note that, no edit icon is displaying inside the button as expected instead it is showing "EC" inside the button.Please ignore the background color here.
Also, I should be able to adjust the width and height of the below edit button to reduce its size.But I am not able to do it using css. I tried adding a class named "edit-btn" inside profile.css but it does not work.
Did you try using classes prop instead of className on your Fab element.
According to the documentation you should use the former.
Refer this link for the same :
Material UI FAB props
I have updated my answer. Your .edit-btn class will work fine if you give it more specificity. Refer my code sandbox for the same.
Regarding your <Icon> can you help me understand what is edit_icon?
code sandbox
Do give it a try and see if it works. Also please share what you have tried so far.Thanks!