How to style Material UI TextField component with Modular CSS? - css

I am trying to change the border color of the Material UI TextField component using Modular CSS, but when I try to, it does not work.
CSS (In Styles.module.css):
.formInput {
font-size: 18px !important;
font-family: Roboto, serif !important;
color: #057FA8 !important;
border-color: #057FA8 !important;
}
.formInput:hover {
border-color: #057FA8 !important;
}
JS:
import styles from './Styles.module.css';
...
<TextField id="name" name="name" label="Name" className={styles.formInput} value={formik.values.name} onChange={formik.handleChange}
error={Boolean(formik.errors.name)} helperText={formik.errors.name}
onBlur={(e) => {
e.preventDefault();
formik.validateField('name');
formik.setFieldTouched('name');
}}
margin='normal'
fullWidth
variant="outlined"
/>
Any help would be greatly appreciated.

Material UI V4 provides a classes property on TextField that lets you style the internal components.
Based on this example you should be able to try:
/* LABEL STYLES */
.formInput label.Mui-focused
{
color: #057FA8 !important;
font-size: 18px;
font-family: Roboto, serif
}
/* INPUT FONT STYLES (on wrapper element not input directly) */
.formInput .MuiOutlinedInput-root
{
font-size: 18px;
font-family: Roboto, serif
}
/* BORDER COLOR */
.formInput .MuiOutlinedInput-root fieldset
{
border-color: #057FA8;
}
/* BORDER COLOR ON HOVER */
.formInput .MuiOutlinedInput-root:hover fieldset
{
border-color: #057FA8
}
/* BORDER COLOR ON FOCUS*/
.formInput .MuiOutlinedInput-root.Mui-focused fieldset
{
border-color: #057FA8
}
and
import styles from './Styles.module.css';
const classes = { root: styles.formInput };
const ExampleComponent = () => {
return (
<TextField
id="name"
name="name"
label="Name"
classes={classes}
margin='normal'
fullWidth
variant="outlined"
/>
)
}

Related

Access other className on hover in emotion css

I use EmotionCSS. I want to get an access to other className while catching focus on another. For example I have a component like that:
<div className={styles.root}>
<input className={styles.input} ... />
<div/>
The style file looks like that:
import { css } from '#emotion/css';
export const styles = {
root: css`
border: 1px solid black;
`
input: css`
...
&:focus {
// Here I want to access 'root' className and change its colour. Is it possible?
}
`,
}
You can try to use the :has() pseudo-class on the root to change the focus styles in input tag.
import { css } from "#emotion/css";
export const styles = {
root: css`
border: 3px solid black;
&:has(input:focus) {
border-color: red;
}
`,
input: css`
font-size: 1.5rem;
&:focus {
text-transform: uppercase;
}
`
};
export default function App() {
return (
<div className="App">
<div className={styles.root}>
<input type="text" className={styles.input} />
</div>
</div>
);
}

material ui, how to change background color, font color, border color of autocomplete textfield

How can I change the background color, font color, and border color of this material-ui autocomplete textfield (combobox)? I would like to use css. this is what I have tried so far.
<Autocomplete
disablePortal
id="combo-box-demo"
options={clients}
className="test"
renderInput={(params) => <TextField {...params} label="Movie" />}
/>
css class
.test {
.MuiAutocomplete-listbox {
color: red;
}
}
Since you would like to use css, you can change colors like this:
.test fieldset {
border-color: red;
}
.test .MuiInputBase-root:hover fieldset {
border-color: blue;
}
.test input {
color: red;
}
.test label {
color: red;
}
.test {
background-color: yellow;
}
You can take a look at this sandbox for a live working example.

Why is this CSS selector taking precedence over more specific selector?

I have this in my React app:
<Nav>
<BasicBtn>Basic button</BasicBtn>
<MainBtn>Main button</MainBtn>
</Nav>
The font size for buttons is set globally:
// globals.css
button {
font-size: 24px;
}
What I want to do is to reduce BasicBtn's font size via the Nav component to 20px, meaning I want it to be smaller when wrapped inside Nav; however, I do NOT want to affect MainBtn, whose font size is set to 14px in its own module. What happens, instead, is that the font size set in Nav overrides the font size set in MainBtn, so MainBtn is 20px as well instead of 14px.
This is my Nav component:
// Nav.js
import styles from "./Nav.module.css"
const Nav = ( { children } ) => <nav className={ styles.Nav }>{ children }</nav>
export default Nav
// Nav.module.css
.Nav button {
font-size: 20px;
}
This is my BasicBtn component:
// BasicBtn.js
import styles from "./BasicBtn.module.css"
import cn from "classnames"
const BasicBtn = ( { children, extraStyles } ) => {
return <button className={ cn( styles.BasicBtn, extraStyles ) }>{ children }</button>
}
export default BasicBtn
// BasicBtn.module.css
.BasicBtn {
padding: 10px;
background: green;
color: white;
}
This is my MainBtn component:
// MainBtn.js
import styles from "./MainBtn.module.css"
import BasicBtn from "../BasicBtn"
const MainBtn = ( { children } ) => <BasicBtn extraStyles={ styles.MainBtn }>{ children }</BasicBtn>
export default MainBtn
// MainBtn.module.css
.MainBtn {
font-size: 14px;
}
This is the generated HTML:
<nav class="Nav_Nav__WC_B6">
<button class="BasicBtn_BasicBtn__q_G1X">Basic button</button>
<button class="BasicBtn_BasicBtn__q_G1X MainBtn_MainBtn__92Bu4">Main button</button>
</nav>
And these, in this order, are the CSS rules that I get when clicking on the generated MainBTN (copied from DevTools):
.Nav_Nav__WC_B6 button {
font-size: 20px;
}
.MainBtn_MainBtn__92Bu4 {
font-size: 14px;
}
.BasicBtn_BasicBtn__q_G1X {
padding: 10px;
background: green;
color: white;
}
Shouldn't the .MainBtn_MainBtn__92Bu4 selector take precedence over .Nav_Nav__WC_B6 button, seeing as it is more specific?

Vue button toggling CSS using checkbox

I am working on VueJS below is my css
.button-css {
align-items: center;
background-color: var(--azure-radiance);
border-radius: 30px;
display: flex;
height: 50px;
min-width: 200px;
padding: 0 60px;
}
.opensans-bold-white-18px {
color: var(--white);
font-family: var(--font-family-open_sans);
font-size: var(--font-size-xxxl);
font-style: normal;
font-weight: 700;
align-self: center;
}
and followed by Vue script
new Vue({
el: "#app",
data: {
termsState: false,
validated: false
},
computed: {
termsError() {
return this.validated && !this.termsState
}
},
methods: {
handleTermsState() {
this.validated = false
},
handleSubmit() {
this.validated = true
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id='app'>
<label for="terms">
Terms and Privacy Policy
<input type="checkbox" id="terms" name="terms" v-model="termsState" #change="handleTermsState">
{{ termsState }}
</label>
<div><button class="button-css opensans-bold-white-18px" type="submit" :disabled="!termsState" #click="handleSubmit">Submit</button></div>
</div>
The button retains the CSS only when it is enabled i.e 'oval shape button' when checkbox is ticked, when it is disabled it takes a gray rectangular shape button. I want to retain the shape of button as gray oval shape button disabled mode how to achieve it?
Below is the before and after images
I want both before and after images to be the oval shape
Actually it has nothing to do with Vue. All you have to do is to modify your CSS like this:
First remove the color property:
.opensans-bold-white-18px {
/* color: var(--white); */
font-family: var(--font-family-open_sans);
font-size: var(--font-size-xxxl);
font-style: normal;
font-weight: 700;
align-self: center;
}
Second, change your button css class to bind a computed property like this:
<button :class="cssClass" type="submit" :disabled="!termsState" #click="handleSubmit">Submit</button>
And add your computed property:
computed: {
cssClass() {
return "overlap-group-23 opensans-bold-white-18px button-css " + (this.termsState ? "button-enabled" : "");
}
}
Tested on both Safari and Chrome.
Is this what you want?

Material-ui show pointer cursor when hovering over TextField

Material-ui/ReactJS newbie question. I'm trying to show a pointer cursor when hovering over a Material-ui TextField but having a difficult time doing so. It makes use of 'cursor: text' by default. I've been able to successfully change the textfield background color on hover but adding "cursor: pointer !important" does no good. I've tried making use of className, class, style (inline), but I'm certain I'm not doing something correctly. Material-ui has a demo illustrating how to change textfield styling on hover and focused at [https://codesandbox.io/s/p7uwn?file=/demo.js][1] where I have also tried changing the cursor to a pointer on hover but still no luck. Any assistance would be greatly appreciated.
import React from 'react';
import styled from 'styled-components';
import { TextField, NoSsr } from '#material-ui/core';
const StyledTextField = styled(TextField)`
label.Mui-focused {
color: green;
}
.MuiOutlinedInput-root {
fieldset {
border-color: red;
}
&:hover fieldset {
border-color: yellow;
cursor: pointer !important;
}
&.Mui-focused fieldset {
border-color: green;
}
}
`;
export default function GlobalClassName() {
return (
<NoSsr>
<StyledTextField label="Deterministic" variant="outlined" id="deterministic-outlined-input" />
</NoSsr>
);
}
Just a quick browser inspection gave the CSS component we need to target. It's
.MuiOutlinedInput-input
Just giving it a
cursor: pointer;
property will solve your problem.
Here is the code:
import React from 'react';
import styled from 'styled-components';
import { TextField, NoSsr } from '#material-ui/core';
const StyledTextField = styled(TextField)`
label.Mui-focused {
color: green;
}
.MuiOutlinedInput-input {
cursor: pointer;
}
.MuiOutlinedInput-root {
fieldset {
border-color: red;
}
&:hover fieldset {
border-color: blue;
cursor: pointer;
}
&.Mui-focused fieldset {
border-color: green;
}
}
`;
export default function GlobalClassName() {
return (
<NoSsr>
<StyledTextField label="Deterministic" variant="outlined" id="deterministic-outlined-input" />
</NoSsr>
);
}
Or just literally put cursor:pointer into its css, either in-line as <Component style={{cursor: 'pointer'}}> or <Component sx={{cursor: 'pointer'}}> or in its styled component css. This will automatically change your mouse onHover, and the top answer here is way over the top. Just add cursor: 'pointer' to the component's css.
Using
<TextField sx={{ cursor: 'pointer' }} />
did not work for me, instead, I needed to specify it as
<TextField sx={{ input: { cursor: 'pointer' } }}
which did affect the desired change.

Resources