How can I change Checkbox's Icon using Ant Design? - css

As you can see, I have a Checkbox element, I have an svg file and I want to place it in this checkbox. I don't know how to do this, I searched the internet but couldn't find it. It is not mentioned in the document, unfortunately, can you help?
https://codesandbox.io/s/h4eb1
As an example, I can show the required field at the bottom.

Following example shows how to customize and replace check box with SVG. Please note that this example is not using Antd check box, still you can get the same results
index.css
.my-image:hover {
cursor: pointer;
}
input[type="checkbox"]:checked +.customcheckbox {
fill: red;
}
Demo.js
import React from 'react';
import 'antd/dist/antd.css';
import './index.css';
const Demo = () => {
function onChange(e) {
console.log(`checked = ${e.target.checked}`);
}
return (
<>
<label>
<input
type="checkbox"
onChange={onChange}
name=""
value=""
style={{ display: 'none' }}
/>
<svg
className="customcheckbox"
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink"
preserveAspectRatio="xMidYMid"
width="20"
height="20"
viewBox="0 0 25 28"
>
<path
d="M8 0v2h8v-2h-8zm0 24v-2h8v2h-8zm10-24h6v6h-2v-4h-4v-2zm-18 8h2v8h-2v-8zm0-2v-6h6v2h-4v4h-2zm24 10h-2v-8h2v8zm0 2v6h-6v-2h4v-4h2zm-18 6h-6v-6h2v4h4v2zm12-18h-12v12h12v-12z"
class="cls-1"
/>
</svg>
</label>
</>
);};
export default Demo;
If you want to customize antd checkbox icon (not SVG), use can customise the antd css classes (inspect the checkbox in browser to check the classes).

Related

semantic-ui-react Make input use full width available

I have the following Segment
<Segment className='AddAPIKey'>
<Form>
<Form.Group>
<Form.Field>
<Input placeholder='XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' />
</Form.Field>
<Form.Button color='green' floated='right' content='Add new API key' />
</Form.Group>
</Form>
</Segment>
With the style:
.ui.AddAPIKey {
display: flex;
justify-content: right;
}
Resulting in:
Is there a way I can make the input field take the entire width like in the example below? I've tried with width: 100% and auto and no luck.
import React from "react";
import { Input, Form, Segment } from "semantic-ui-react";
import "./style.css";
const InputExampleFocus = () => (
<Segment>
<Form>
<Form.Group style={{ display: "flex" }}>
<Form.Field style={{ flexGrow: "1" }}>
<Input placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" />
</Form.Field>
<Form.Button color="green" floated="right" content="Add new API key" />
</Form.Group>
</Form>
</Segment>
);
export default InputExampleFocus;
Try this I have removed the .ui.AddAPIKey class and used Inline css to style Form.Group and Form.Field
It produces an Input field that covers all the spaces available.
I hope it solves the issue.
https://codesandbox.io/embed/semantic-ui-example-forked-x5d4qm?fontsize=14&hidenavigation=1&theme=dark
Open the codesandbox and go to example.js

How can I put an icon inside a Input in ReactJS?

I'm new in reactJS, but I want to know how I can put icon inside input element?
I have a FormItem and inside of this an Input, and inside of this input i want to put an icon.
<Col xs={24} sm={24} md={24} lg={12}>
<FormItem
{...formItemLayout}
label={intl.formatMessage({ id: `maintenace.link.linkOrigin` })}
>
{getFieldDecorator(`officePhone`, {
initialValue: selectedData_.officePhone
? selectedData_.officePhone
: ""
})(
<Input
name="officePhone"
placeholder={intl.formatMessage({
id: `maintenance.officePhone`
})}
/>
)}
</FormItem>
</Col>
If anyone knows, please, I will be very grateful.
If you use some lib related to the Ant Design. You can add prefix prop
<Input prefix={<SmileOutlined />} />
You could use a background on the input, something like this :
input: {
background: url(icon.gif) no-repeat scroll 7px 7px;
padding-left: 20px;
}

How to change the size of svg icon in React?

I wanted to display an svg icon and a text in a same line in React and Next.js. Now I have found how to do it. Though I still don't know how to control the size of the svg icon in the context. I want the svg icon to be same size as each character of the text.
I put the browser display here.
And I put my code bellow, please point out what is wrong about my code.
//mysvg.js
import styles from '../styles/svg.module.css'
export function CircleBottomIcon(props) {
return (<span class={styles['svg-image']}>
<svg
width="30"
height="30"
viewBox="0 0 30 30"
xmlns="http://www.w3.org/2000/svg"
{...props}
>
<title>circle-bottom</title>
<path
d="M27.5 15c0 6.893-5.608 12.5-12.5 12.5-6.893 0-12.5-5.608-12.5-12.5C2.5 8.107 8.108 2.5 15 2.5c6.893 0 12.5 5.608 12.5 12.5zm2.5 0c0-8.284-6.716-15-15-15C6.716 0 0 6.716 0 15c0 8.284 6.716 15 15 15 8.284 0 15-6.716 15-15zm-15 2.5l-5.625-5.625-1.875 1.91L15 21.25l7.5-7.466-1.875-1.91L15 17.5z"
fill-rule="evenodd"
/>
</svg></span>
);
}
//index.js
import {CircleBottomIcon} from '../components/mysvg'
export default function Home() {
return (
<span>
1234<CircleBottomIcon></CircleBottomIcon>567890
</span>
)
}
//svg.module.css
.svg-image {
display: inline-block;
width: 16px;
height: 16px;
}
I'd suggest you keep constant values for viewbox param and accept props for height and width. Add a default values for these as 30 and 30. Part of code looks like:
const { height, width } = props;
<svg
width={width}
height={height}
viewBox="0 0 30 30"
...
Then you can use any values for width and height when you use the svg component.
your .svg-image selector should target the svg element:
.svg-image {
display: inline-block;
svg {
width: 16px;
height: 16px;
}
}

Change background-color of an unchecked Material-UI checkbox

I am working with React-Redux and Material-UI for styling. I have a < Checkbox /> that is on a toolbar. The toolbar's background-color is blue (#295ED9).
I've managed to change the color of the checkbox, when it is checked. I have also changed the color of the outline of the checkbox to white, when it is unchecked.
Problem: I cannot change the background-color / fill-color of the checkbox when it is unchecked. It is always the same color as the toolbar it is on (blue - #295ED9).
I have tried changing the background-color, color, and fill attributes of the < svg > and the < path > to white.
The best result I can get is when I change the < svg > fill attribute to white. Unfortunately, this does not make the checkbox fill with a white background when unchecked.
JSX
<div className="hasBalance">
<FormControlLabel
control={<Checkbox color="primary"/>}
label="Has Balance"
/>
</div>
SCSS
.hasBalance {
grid-area: balance;
.MuiSvgIcon-root {
fill: white;
}
}
Elements in Chrome Inspect Tool
<div class="hasBalance">
<label class="MuiFormControlLabel-root">
<span class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-143 MuiCheckbox-root MuiCheckbox-colorPrimary MuiIconButton-colorPrimary" aria-disabled="false">
<span class="MuiIconButton-label">
<input class="PrivateSwitchBase-input-146" type="checkbox" data-indeterminate="false" value="">
<svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation">
<path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"></path>
</svg>
</span>
<span class="MuiTouchRipple-root"></span>
</span>
<span class="MuiTypography-root MuiFormControlLabel-label MuiTypography-body1">Has Balance</span>
</label>
</div>
The goal is to change the background-color / fill-color of the unchecked Material-UI checkbox to white. Thank you.
Below is an approach that seems to work. This uses the ":after" pseudo-element to place a white background behind the SVG.
import React from "react";
import ReactDOM from "react-dom";
import { withStyles } from "#material-ui/core/styles";
import Checkbox from "#material-ui/core/Checkbox";
const WhiteBackgroundCheckbox = withStyles(theme => ({
root: {
color: "red",
"& .MuiIconButton-label": {
position: "relative",
zIndex: 0
},
"&:not($checked) .MuiIconButton-label:after": {
content: '""',
left: 4,
top: 4,
height: 15,
width: 15,
position: "absolute",
backgroundColor: "white",
zIndex: -1
}
},
checked: {}
}))(Checkbox);
function App() {
return (
<div style={{ backgroundColor: "blue" }}>
<WhiteBackgroundCheckbox />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Related question: Change the tick color in MuiCheckbox material UI
MUIv5
Accepted answer doesn't work in MUIv5. MuiIconButton-label doesn't exist in Checkbox anymore and it changed the structure of the component.
For anyone struggling with v5, here's my kinda-hacky solution :
import { Components } from '#mui/material'
const MuiCheckbox: Components['MuiCheckbox'] = {
styleOverrides: {
root: {
'& .MuiSvgIcon-root': {
zIndex: 1,
},
'& .PrivateSwitchBase-input': {
width: 'auto',
height: 'auto',
top: 'auto',
left: 'auto',
opacity: '1',
visibility: 'hidden', // optional
'&::before': {
content: '""',
position: 'absolute',
background: 'white',
height: "100%",
width: "100%",
visibility: "visible" // optional
},
}
},
},
}
It relies on the fact that the <input> element assumes just the right dimensions and position if we reset its size and positioning attributes. That way the solution is resistant to various paddings and size props applied to the root element of the component. AFAIK it shouldn't break anything, but my experience with MUI is not that long.
Note that I have this solution defined as a global styleOverride in my theme's config and I want this custom background to affect checked tick too - mix and match with Ryan's answer for your scenario.
Visibility attributes are optional, they just make sure the input element stays hidden (I set opacity 1 on it, default is 0). Just make sure you remove both if you don't want them.

React: SVG hover color doesn't disappear when setting active unless mouse touches screen

I'm use Create React App and have svgs for my social icons.
After clicking an icon, a new tab opens. When the user goes back to the tab with the website, the hover color is still there until the mouse moves on the screen. So if the user goes back to the tab but keeps the mouse in the browser's nav bar you will see the hover color remain. This remains when i use active focus or visited to reset the color before hovering or clicking.
If the user only hovers and stays on the page, then it is fine. It's after the user clicks and returns to the site where the hover color remains.
I'm not sure what causes this. Maybe its because I'm changing the styles in CSS rather than using mouseMove events in react? However, I'm not sure how to do what I'm doing in CSS with React events because I have to set the event on the a tag and see the color change reflected on the path tag.
Can some one please explain why this happens and the best practice around this?
css
a:hover path {
fill: #f40ef0;
}
a:active path {
fill: #fff;
}
or instead of active use:
a:focus path {
fill: #fff;
}
js
const Github = (
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
x="0px"
y="0px"
width="30px"
height="30px"
viewBox="0 0 438.549 438.549"
xmlSpace="preserve"
>
<g>
<path
id="Github"
d="M409.132,114.573c-19.608-33.596-46.205-60.194-79.798-79.8C295.736,15.166,259.057,5.365,219.271,5.365 c-39.781,0-76.472,9.804-110.063,29.408c-33.596,19.605-60.192,46.204-79.8,79.8C9.803,148.168,0,184.854,0,224.63 c0,47.78,13.94,90.745,41.827,128.906c27.884,38.164,63.906,64.572,108.063,79.227c5.14,0.954,8.945,0.283,11.419-1.996 c2.475-2.282,3.711-5.14,3.711-8.562c0-0.571-0.049-5.708-0.144-15.417c-0.098-9.709-0.144-18.179-0.144-25.406l-6.567,1.136 c-4.187,0.767-9.469,1.092-15.846,1c-6.374-0.089-12.991-0.757-19.842-1.999c-6.854-1.231-13.229-4.086-19.13-8.559 c-5.898-4.473-10.085-10.328-12.56-17.556l-2.855-6.57c-1.903-4.374-4.899-9.233-8.992-14.559 c-4.093-5.331-8.232-8.945-12.419-10.848l-1.999-1.431c-1.332-0.951-2.568-2.098-3.711-3.429c-1.142-1.331-1.997-2.663-2.568-3.997 c-0.572-1.335-0.098-2.43,1.427-3.289c1.525-0.859,4.281-1.276,8.28-1.276l5.708,0.853c3.807,0.763,8.516,3.042,14.133,6.851 c5.614,3.806,10.229,8.754,13.846,14.842c4.38,7.806,9.657,13.754,15.846,17.847c6.184,4.093,12.419,6.136,18.699,6.136 c6.28,0,11.704-0.476,16.274-1.423c4.565-0.952,8.848-2.383,12.847-4.285c1.713-12.758,6.377-22.559,13.988-29.41 c-10.848-1.14-20.601-2.857-29.264-5.14c-8.658-2.286-17.605-5.996-26.835-11.14c-9.235-5.137-16.896-11.516-22.985-19.126 c-6.09-7.614-11.088-17.61-14.987-29.979c-3.901-12.374-5.852-26.648-5.852-42.826c0-23.035,7.52-42.637,22.557-58.817 c-7.044-17.318-6.379-36.732,1.997-58.24c5.52-1.715,13.706-0.428,24.554,3.853c10.85,4.283,18.794,7.952,23.84,10.994 c5.046,3.041,9.089,5.618,12.135,7.708c17.705-4.947,35.976-7.421,54.818-7.421s37.117,2.474,54.823,7.421l10.849-6.849 c7.419-4.57,16.18-8.758,26.262-12.565c10.088-3.805,17.802-4.853,23.134-3.138c8.562,21.509,9.325,40.922,2.279,58.24 c15.036,16.18,22.559,35.787,22.559,58.817c0,16.178-1.958,30.497-5.853,42.966c-3.9,12.471-8.941,22.457-15.125,29.979 c-6.191,7.521-13.901,13.85-23.131,18.986c-9.232,5.14-18.182,8.85-26.84,11.136c-8.662,2.286-18.415,4.004-29.263,5.146 c9.894,8.562,14.842,22.077,14.842,40.539v60.237c0,3.422,1.19,6.279,3.572,8.562c2.379,2.279,6.136,2.95,11.276,1.995 c44.163-14.653,80.185-41.062,108.068-79.226c27.88-38.161,41.825-81.126,41.825-128.906 C438.536,184.851,428.728,148.168,409.132,114.573z"
fill="#FFFFFF"
/>
</g>
</svg>
);
const socialList = [
{
href: 'https://github.com/turnipdabeets',
title: 'See my Github projects',
icon: Github
},
...
];
const Icon = ({ icon, href, title }) => (
<a className="social" href={href} title={title} target="_blank">
{icon}
</a>
);
const SocialGroup = () => (
<ul className="socialNav">
{socialList.map(social => (
<li key={social.href}>
<Icon href={social.href} title={social.title} icon={social.icon} />
</li>
))}
</ul>
);
UPDATE: This is only happening on chrome web browsers and safari on iOS 10 (not web)
Maybe try this:
a:visited path{
fill: #FFFFFF;
}
a:hover path {
fill: #f40ef0;
}
a:active path {
fill: #fff;
}
So that your visited tags will stay the same color.
Because right now your hover color is the same as the default visited color for most browsers.

Resources