position in reactjs with css - css

everyone, I am working on Reactjs project, I want to add submit button inside the input field, but I am not able to add, can anyone help me to place submit button inside input field, thanks in advance, my demo code, expected and actual outcome is attached
Reactjs code
<div className={styles.subscribeInputContainer}>
{I18n.getComponent(
(formattedValue) =>
<input
id="subscribeInput"
required={true}
value={this.state.value}
className={styles.subscribeInput}
placeholder={formattedValue}
onKeyPress={(e) => this.onKeyPress(e)}
onChange={(e) => this.onEmailChange(e.target.value)}
/>,
'footer.your-email-address',
{},
'Enter Your email address'
)}
<p className={styles.subscribeButton} id="btnId">
<Button variant="primary" className={styles.subscribeIcon} onClick={() => this.onSubscribe()}>Button</Button>
</p>
</div>
CSS code
.inputContainer {
position: relative;
}
.subscribeIcon {
position: absolute;
right: 10px;
}
.subscribeInputContainer {
display: flex;
flex-flow: row;
flex: 1;
font-size: $fontSize-lg;
color: $color-body;
}
Actual outcome
Expected Outcome

First, don't use a p as a wrapper, use a div
When you use position: absolute you take that element out of flow, and as such its parent will not take it into account when sizing itself.
If you remove position: absolute from the .subscribeIcon and add margin-left: auto to the .subscribeButton, your button should be properly right aligned within its parent, the .subscribeInputContainer.
.subscribeInputContainer {
display: flex;
flex-flow: row;
flex: 1;
border: 1px solid gray;
padding: 5px;
}
.subscribeButton {
margin-left: auto;
}
.subscribeIcon {}
<div class='subscribeInputContainer'>
<input id="subscribeInput" required={true} value={this.state.value} className={styles.subscribeInput} placeholder={formattedValue} onKeyPress={(e) />
<div class='subscribeButton' id="btnId">
<Button variant="primary" class='subscribeIcon' onClick={()=> this.onSubscribe()}>Button</Button>
</div>
</div>

Related

How to give animation on width change with different classname but for same component

what is the best practice to show/hide the side menu with animation? The way I implement works fine but I can't implement the animation. I also shared images so everyone can have some idea.
The answer might be easy but I couldn't fully understand the toggle class efficiently. If I would I should be able to implement transition just by changing the width from the same classname.
Also I am getting this error on console: Received false for a non-boolean attribute className.
React code ;
const handleSize = () => {
setOpen(!open); }; return (
<div
className={"sideBar" + `${open ? " sideBar__show" : " sideBar__hide"}`}
>
<div className="sideBar__header">
<div className="menu_buttons" onClick={handleSize}>
<MenuIcon className="sideBar_icon" />
<p className={!open && "sideBar_hide__content"}>Menu</p>
</div>
</div>
<hr className="sideBar__divider" />
<div className="sideBar__content">
{sideBarContent.map((item) => {
return (
<div className="menu_buttons" key={item.name}>
<item.icon />
<p className={!open && "sideBar_hide__content"}>{item.name}</p>
</div>
);
})}
</div>
<hr className="sideBar__divider" />
<div className="sideBar__footer">
<Avatar>{firstLetter}</Avatar>
<p className={!open && "sideBar_hide__content"}>{adminName}</p>
</div>
</div> ); };
CSS;
.sideBar {
position: fixed;
left: var(--sidebar-left-margin);
height: max-content;
top: calc(var(--header-height) + 10px);
bottom: 10px;
border-radius: 10px;
background-color: var(--second-color);
display: flex;
flex-direction: column;
align-items: center;
padding: 10px 5px;
border: 1px solid var(--main-color);
/* transition: all ease-in 1s; */
}
.sideBar__show {
width: var(--sidebarOpen-width);
}
.sideBar_hide {
width: var(--sidebarClose-width);
}
.sideBar_hide__content {
display: none;
}
Not sure what is best practice, this is my currently approach:
const handleSize = () => setOpen(!open);
const sideBarClasses = open ? "sideBar sideBar_show : "sideBar sideBar_hide;
<div className="sideBar__header">
<div className={sideBarClasses}>

How to restrict the number of items in flexbox according to screen size

I am new to grid designing on website design
In my react project, I have a row showing 3 components. One is Leverage type dot selection, Two of them are input box, Loan amount and Loan Rate.
Right now, every things seems fine in full screen, but when I shrink it, you can see that Loan Rate is placed on the next line while Loan Amount was still on above line
But what I want is, when I shrink it, it is either like this(1), or like (2) when it is super small
In(2), basically its been achieved with my existing css, when the screen is super small
Q: How could I either showing everything in the same line or showing one line with leverage type and one line with Loan Amount and Loan Rate?
Here is my code
.setting {
display: flex;
justify-content: space-between;
margin-top: 50px;
}
There are many ways we can break flex item in flexbox layout the one I have used here is flex-direction:column, this property sets every flexitem to vertically..
#media(max-width:576px) {
.flexBox {
flex-direction: column;
/*set all flexItem to vertically*/
align-items: stretch;
/* stretch will foce to take full width*/
}
}
Meaning of media(max-width:979px) means, CSS inside of that code block will only apply to screen size lessthan or equal to 979px. Means the CSS will not affect for screensize bigger than 979px.
Speaking about below code,
.flexBox .flexItem {
flex: 1 0 calc(100%/var(--number-of-items-for-midium-screen) - var(--gap-val)*2)
}
calc(100%/var(--number-of-items-for-midium-screen) - var(--gap-val)*2) here we divide 100% width with the number of items we want per row and we minus var(--gap-val) twice because we have applied gap:10px, which applied both side of .flexItem so we minus it 2 times.
And just to make CSS more friendly I have created CSS variables for
--number-of-items-for-midium-screen <= Number of items in a row
--gap-val <= gap between flex items in a row
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--number-of-items-for-midium-screen: 2;
/*Number of items you want in medium screen(below 979px)*/
--gap-val: 10px;
/*Gap you want between flex items*/
}
.flexBox {
display: flex;
align-items: center;
gap: var(--gap-val);
margin: 10px;
}
.flexBox .flexItem {
flex: 1;
border: 2px solid #d6d6d6;
padding: 10px;
}
#media(max-width:979px) {
.flexBox {
flex-wrap: wrap;
}
.flexBox .flexItem {
flex: 1 0 calc(100%/var(--number-of-items-for-midium-screen) - var(--gap-val)*2)
}
}
#media(max-width:576px) {
.flexBox {
flex-direction: column;
/*set all flexItem to vertically*/
align-items: stretch;
/* stretch will foce to take full width*/
}
}
<div class="flexBox">
<div class="flexItem">
<p>Leverage type</p>
<label for="static">static</label>
<input type="radio" value="static" id="static" name="gp1" />
<label for="dynamic">dynamic</label>
<input type="radio" value="static" id="dynamic" name="gp1" />
</div>
<div class="flexItem">
<label for="loanRate">Loan Rate</label>
<input id="loanRate" type="text" />
</div>
<div class="flexItem">
<label for="loanAmount">Loan Amount</label>
<input id="loanAmount" type="text" />
</div>
</div>
Understanding how to break layouts in flexbox
const btn1 = document.querySelector("#btn1"),
btn3 = document.querySelector("#btn3"),
btn4 = document.querySelector("#btn4");
const flexBox = document.querySelectorAll(".flexBox")[0];
const flexItems = document.querySelectorAll(".flexBox .flexItem");
const preview = document.querySelectorAll(".preview")[0];
btn1.onclick = () => {
flexBox.style.flexDirection = "column";
preview.style.display = "block";
preview.innerHTML = `.flexBox{flex-direction:column;}`;
}
btn3.onclick = () => {
flexItems.forEach(flexItem => {
flexItem.style.flex = "1";
})
preview.style.display = "block";
preview.innerHTML = `.flexItems{flex:1;}`;
}
btn4.onclick = () => {
const numberofItems = +document.querySelector("#numberofItems").value;
if (numberofItems && numberofItems >= 1 && numberofItems <= 6) {
flexBox.style.flexFlow = "initial";
flexBox.style.flexWrap = "wrap";
flexItems.forEach(flexItem => {
flexItem.style.flex = `1 0 calc(100%/${numberofItems})`;
})
preview.style.display = "block";
preview.innerHTML = `.flex{flex-wrap:wrap;}</br>.flexItems{flex:1 0 calc(100%/${numberofItems});}`;
} else {
alert("Please enter number of items you need from 1 to 6");
}
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.flexBox {
display: flex;
}
.flexItem {
display: inline-block;
height: 100px;
}
.bg-red {
background-color: red;
}
.bg-blue {
background-color: blue;
}
.bg-green {
background-color: green;
}
/*Just for styling*/
.buttons {
margin-top: 10px;
}
.buttons button {
padding: 10px;
border: 2px solid #000;
background: #fff;
text-transform: capitalize;
margin: 10px;
}
.textInput {
display: flex;
gap: 5px;
margin: 10px;
}
.textInput input {
flex: 1 0 75%;
}
.textInput button {
padding: 5px;
margin: 0;
}
.preview {
width: 100%;
border: 2px solid #000;
display: none;
padding: 30px;
}
<div class="flexBox">
<div class="flexItem bg-red">
<h5>This is flex box item</h5>
</div>
<div class="flexItem bg-blue">
<h5>This is flex box item</h5>
</div>
<div class="flexItem bg-green">
<h5>This is flex box item</h5>
</div>
<div class="flexItem bg-red">
<h5>This is flex box item</h5>
</div>
<div class="flexItem bg-blue">
<h5>This is flex box item</h5>
</div>
<div class="flexItem bg-green">
<h5>This is flex box item</h5>
</div>
</div>
<div class="buttons">
<button id="btn1">Apply flex-direction to column</button>
<button id="btn3">set all box equal size</button>
<div class="textInput">
<input type="number" id="numberofItems" placeholder="Enter the number of items you want in a row" min="1" max="6">
<button id="btn4">set items in a row</button>
</div>
</div>
<div class="preview">
<h2>Applied css</h2>
</div>

Position element in relation to a list element

I'm trying to make a pop-up options box appear next to each one of the elements of a file list. This window show the options available for that files.
I'm using an onClick function that toggle classes between one hidden and the other visible. My problem is that the box is placed always in the same place, despite the file you click, so it seems like you are always opening the options from the first file.
The React code:
return (
<div className='file-row'>
<div className='file-row-1'>
<div className='checkbox-iconFile-container'>
<input className='checkbox-folder' type="checkbox"/>
<p id={props.id} className='file-name'><img className='resourceIcon'alt='tipo de documento' src={resourceIcon}></img>{newStr}</p>
</div>
<p className='file-date'>{date}</p>
<p className='groups-file-text'>UBIP,...</p>
</div>
<div className='div-hidden-options'>
<ModalResources></ModalResources>
<div className='moreInfo-dots-container' onClick={showFileOptions}>
<img className='moreInfo-dots' alt='Opciones' src={dots}></img>
</div>
<div id='file-options' className='div-file-options-hidden'>
<p>Modificar título</p>
<p>Modificar archivo</p>
<p>Mover recurso</p>
<p>Eliminar recurso</p>
</div>
</div>
</div>
)
The showFileOptions function :
function showFileOptions () {
let optionsDiv = document.getElementById('file-options');
optionsDiv.classList.toggle('div-file-options-show');
}
And the CSS:
.div-hidden-options {
width: 15%;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.div-file-options-hidden {
visibility: hidden;
position: absolute;
}
.div-file-options-show {
visibility: visible;
position: absolute;
margin-left: 15%;
background-color: white;
border: 1px solid #DDDDDD;
padding: 5px;
border-radius: 5px;
margin-top: -36px;
}
And an image of the position that the box takes regardless of the file we click:
And this is the desirable behaviour of the box when the second file is clicked:

react-bootstrap form margin causing overflow

I just got into react-bootstrap today. I want the form to be in a div with margins on left and right side. My jsx looks like this:
const Settings = () =>{
return(
<div className="content">
<div className="box">
{/* Actual form with course info */}
<Form className="course-info">
<Form.Group as={Row} controlId="formHorizontalEmail">
<Form.Label variant="settingsLabel" column sm={3}>
Course Title
</Form.Label>
<Col sm={5}>
<Form.Control/>
</Col>
</Form.Group>
</Form>
</div>
</div>
);
}
CSS:
/* A big wrapper for everything on this page */
.content{
display: flex;
align-items: center;
justify-content: center;
}
/* The box containing the class settings */
.box {
width: 600px;
height: 600px;
border: 30px solid #A6D1DF;
margin-top:50px;
}
.center-items{
margin-top: 1em;
text-align: center;
}
/* Gives the form left/right margin and sets text color */
.course-info {
padding-left: 20px;
color: #9E7772;
}
Form.Label-settingsLabel {
font-size: 1em;
color: red;
}
And the form content overflows the right-hand side of the box, and removing the margin fixes the problem.
However, I still want that margin for styling purposes. Any suggestions? Thanks

Flexbox justify-content depending on number of children

Having this:
<label>
<button>Create</button>
</label>
I want button to be aligned to the right like this
----------------------------
| [create]|
----------------------------
while having this:
<label>
<button>Delete</button>
<button>Update</button>
</label>
I want buttons to be in the corners
----------------------------
|[delete] [update]|
----------------------------
Without adding additional classes to the label.
You can just use margin-left: auto on last-child and that will produce desired result.
label {
display: flex;
border-bottom: 1px solid #aaa;
margin: 20px 0;
}
label button:last-child {
margin-left: auto;
}
<label>
<button>Create</button>
</label>
<label>
<button>Delete</button>
<button>Update</button>
</label>
You can do this using nothing but standard flexbox properties:
flex-direction: row-reverse - put the items in a row, start from "the end" (depends on reading direction)
justify-content: space-between - put the items as far away from each other as possible
label {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
}
<label>
<button>Create</button>
</label>
<label>
<button>Delete</button>
<button>Update</button>
</label>
You can use CSS Flexbox justify-content property to align your elements.
Have a look at this Codepen.
Or look at the code below for reference, also I've used <div>s in place of <label> as in the .two they are acting on both the buttons.
div {
display: flex;
background: #ddd;
padding: 10px 20px;
margin-bottom: 10px;
}
div:first-child {
justify-content: flex-end;
}
div:nth-child(2) {
justify-content: space-between;
}
<div>
<button>Create</button>
</div>
<div>
<button>Delete</button>
<button>Update</button>
</div>
Learn more about CSS Flexbox
Hope this helps!

Resources