so i created these 4 divs:
they are currently vertically aligned to the left side, i want them to be horizontally aligned next to each others and to the right side of the window. what i tried is adding inline flex but i want the width of the div to be static so it didn't work and also i tried float right but the order of the divs was messed up so i tried to reverse order but also didn't work.
card.js:
import React from 'react';
import '../Card/Card.css'
function card ({title,value,subValue,date}) {
return (
<div>
<div className="title">
{title}
</div>
<div className="value">
{value}
</div>
<div className="sub-value">
{subValue}
</div>
<div className="date">
{date}
</div>
</div>
)
}
export default card;
overview.js:
import React from 'react';
import Card from '../../components/widgets/Card/Card';
import '../../pages/Overview/Overview.css';
const Overview = (props) => {
const measurment = [{ title: "Last Blood Pressure", value: "90/60",subValue:"85 BPM",date:"05/14/2020 04:12"},
{ title: "Last Body Weight", value: "154",subValue:"13% Fat",date:"05/14/2020 04:12"},
{ title: "Last SpO2", value: "98%",subValue:"85 BPM",date:"05/14/2020 04:12"},
{ title: "Last Glucose", value: "200",subValue:" ",date:"05/14/2020 04:12"}
]
return (
measurment.map(measurment => {
return (
<div className="cards">
<Card
title={measurment.title}
value={measurment.value}
subValue={measurment.subValue}
date={measurment.date}
/>
</div>
)
}
)
)
}
export default Overview;
css code:
.cards{
margin:1em;
cursor:pointer;
height: 120px;
width:180px;
border-radius: 10px;
background-color:white;
border:1px solid #57c0e8;
padding-left: 0.4rem;
padding-top: 0.3rem;
}
.card-element{
color: lightslategray;
}
.title {
color:grey;
font-size: 12px;
text-align: left;
}
.value{
font-size: 32px;
text-align: center;
}
.sub-value {
text-align: right;
margin-right: 10px;
}
.date {
color:lightslategrey;
text-align: right;
margin-right: 10px;
font-size: 11px;
display: flexbox;
margin-bottom: 0px;
}
my code: https://codesandbox.io/live/wp9QOw
Try using this:
import React from "react";
import Card from "../Card/card";
import "../components/Overview.css";
const Overview = props => {
const measurment = [
{
title: "Last Blood Pressure",
value: "90/60",
subValue: "85 BPM",
date: "05/14/2020 04:12"
},
{
title: "Last Body Weight",
value: "154",
subValue: "13% Fat",
date: "05/14/2020 04:12"
},
{
title: "Last SpO2",
value: "98%",
subValue: "85 BPM",
date: "05/14/2020 04:12"
},
{
title: "Last Glucose",
value: "200",
subValue: " ",
date: "05/14/2020 04:12"
}
];
return(
<div style={{display: 'flex', justifyContent:'flex-end'}}>
{
measurment.map(measurment => {
return (
<div className="cards">
<Card
title={measurment.title}
value={measurment.value}
subValue={measurment.subValue}
date={measurment.date}
/>
</div>
);
})
}
</div>
)
};
export default Overview;
You can assign a className to your parent div and use flexbox.
Related
Using 'react-responsive' how can I make responsive for multiple resolution, I have tried below, but not getting desired output: Could someone suggest how to use it ?
1920px
1536px
import React from 'react';
import { useNavigate } from "react-router-dom";
import { useMediaQuery } from 'react-responsive';
const Admin = () => {
const navigate = useNavigate();
const isPcOrLaptop = useMediaQuery({
query: '(max-width: 1920px)'
});
const handleLogout = () => {
localStorage.removeItem('loginEmail');
navigate("/login");
};
return (
<div id="App">
{
isPcOrLaptop &&
<div className='adminSection'>
<div className='row'>
<h1>Hello Admin</h1>
<div className="logout">
<img src="/images/logout.png" alt="Logout" onClick={handleLogout}>
</img>
</div>
</div>
</div>
}
</div>
)
}
export default Admin;
// admin.css
.adminSection{
width: 100%;
}
h1{
margin: 25PX !important;
color: black !important;
}
.logout img {
height: 30px;
width: 30px;
margin: -70px 50px 0px 0px;
float: right;
}
I am trying to make my navbar and I want to display both react-selects in one single row so I did this
Everything was working with vanilla select but adding the react-select breaks my styling
Navbar.js
import React, { useState } from "react";
import classes from "./Navbar.module.css";
import { useDispatch } from "react-redux";
import { selectAction } from "../../../store/actions/actions";
import Logo from "../../../images/BB_S5_excl_namelab_c.jpg";
import Select from 'react-select';
const CharacterOptions = [
{ value: '-----------', label: '-----------' },
{ value: 'Get character by id', label: 'Get character by id' },
{ value: 'Get character by category', label: 'Get character by category' },
{ value: 'Get random character', label: 'Get random character' },
{ value: 'Search character by name', label: 'Search character by name' },
];
const QuoteOptions = [
{ value: '-----------', label: '-----------' },
{ value: 'Get quote by id', label: 'Get quote by id' },
{ value: 'Get quotes by series', label: 'Get quotes by series' },
{ value: 'Get a random quote', label: 'Get a random quote' },
{ value: 'Get quote by author', label: 'Get quote by author' },
{ value: 'Get a random quote by author', label: 'Get a random quote by author' },
];
const Navbar = (props) => {
const dispatch = useDispatch();
const [selectedCharacter, setSelectedCharacter] = useState(false);
const [selectedQuote, setSelectedQuote] = useState(false);
const dropDownCharacterSelected = (value) => {
if (value !== "-----------") {
setSelectedCharacter(true);
dispatch(selectAction(value.value));
} else {
dropDownCharacterDeSelected(value.value);
dispatch(selectAction(null));
}
};
const dropDownCharacterDeSelected = () => {
setSelectedCharacter(false);
};
const dropDownQuoteSelected = (value) => {
if (value !== "-----------") {
setSelectedQuote(true);
dispatch(selectAction(value.value));
} else {
dropDownQuoteDeSelected(value.value);
dispatch(selectAction(null));
}
};
const dropDownQuoteDeSelected = () => {
setSelectedQuote(false);
};
return (
<nav className={classes.Navbar}>
<img src={Logo} alt="Logo" width="100" height="60" />
<div>
<ul>
<li>
<label>Characters</label>
<Select
disabled={selectedQuote}
onChange={dropDownCharacterSelected}
options={CharacterOptions}
/>
</li>
<li>
<label>Quotes</label>
<Select
disabled={selectedCharacter}
onChange={dropDownQuoteSelected}
options={QuoteOptions}
/>
</li>
</ul>
</div>
</nav>
);
};
export default Navbar;
Navbar.module.css
.Navbar{
display: flex;
height: 52px;
box-sizing: content-box;
background-image: linear-gradient(60deg, #29323c 0%, #485563 100%);
padding: 10px;
justify-content: center;
align-items: center;
}
.Navbar div ul {
display: flex;
}
.Navbar ul li {
display: flex;
list-style: none;
}
.Navbar label {
width: 100px;
border-radius:8px;
margin:10px;
background-color: lightgrey;
color:darkgreen;
font-weight: bold;
font-size: 20px;
padding: 10px;
}
Edit
Cant increase width of react-select
Here is my Codesandbox URL for your reference
https://codesandbox.io/s/youthful-gauss-r1d8o
You can achieve that by using flexbox to align your items in a row:
.Navbar div ul {
width: 100%;
display: flex; /* use flexbox to align the inputs on same row */
}
.Navbar ul li {
display: flex; /* use flexbox to align the labels on same row than the inputs */
align-items: center; /* align the items vertically */
list-style: none;
}
.Navbar ul li input {
min-width: 100px; /* specify react-select component's width */
}
Adding display: flex, will show both component in the same row:
.Navbar div ul {
...
display: flex;
...
}
I am trying to create content slider in React, and to an extent I have succeeded, however I want the content to move horizontally instead of Vertically. Right now all the items are stacked on top of each other which I know is why they cannot work as a horizontal slider, initially I tried using flexbox in order to align the items next to each other but then they are unable to be moved using left or right in the CSS.
What I am currently doing is setting the top of the inner div (Which house the items) to a calculated number of pixels so that it will display the next "Slide". What I have been trying and failing is trying the get the items to be next to each other and the rather than top just change the left/ right of the slider to display the item.
(P.S. I have not added a way to stop the slider over the total number of items, will do that after)
Here is the React Code:
import React, { Component } from "react";
import "../App.css";
class Item extends Component {
constructor(props) {
super(props);
this.state = {
id: 1,
top: "0px",
};
}
onClickSlide = (_) => {
const item = this.state.id;
this.setState({ top: item * 200 * -1 + "px" });
this.setState({ id: item + 1 });
};
render() {
let dang = [
{ title: "Header 1", content: "Lorem ipsum proin gravida" },
{ title: "Header 2", content: "Lorem ipsum proin gravida" },
{ title: "Header 3", content: "Lorem ipsum proin gravida" },
{ title: "Header 4", content: "Lorem ipsum proin gravida" },
];
const item = this.state.id;
let innerStyle = {top: item * 200 * -1 + "px"};
return (
<div className="Outer">
<div className="inner" onClick={this.onClickSlide} style={{ top: this.state.top }}>
{dang.map((data, i) => {
return (
<div className="items">
<h3>{data.title}</h3>
<h3>{data.content}</h3>
</div>
);
})}
</div>
</div>
);
}
}
export default Item;
Here is the CSS:
.Outer {
display: block;
position: relative;
overflow: hidden;
width: 200px;
height: 200px;
}
.inner {
/* display: block; */
position: absolute;
transition: all 0.5s;
}
.items {
display: inline-block;
width: 200px;
height: 200px;
vertical-align: top;
}
Coolness, so with a bit of testing and some other carousel examples I have built my own version. There is still room for improvement but here is the base of it. the idea is to have a super long container were all of the images flex next to each other and by using the CSS property "translate" to move each image left or right depending on the direction clicked.
Below is the React JS example:
export default () => {
const [translate, setTranslate] = useState(0);
const handleClickBtn= (direction) => {
if (direction == "right") {
setTranslate({ translate: translate - 100 });
} else {
setTranslate({ translate: translate + 100 });
}
};
return (
<div className="banner-carousel">
<button
className="carousel-button btn-left"
onClick={(_) => handleClickBtn("left")}
>
Left
</button>
<div className="carousel-container">
<div
className="slide"
style={{ transform: `translateX(${translate}vw)`}}
>
<img className="carousel-image" src="https://www.publicdomainpictures.net/pictures/40000/nahled/random-texture.jpg" />
</div>
<div
className="slide"
style={{ transform: `translateX(${translate}vw)`}}
>
<img className="carousel-image" src="https://www.publicdomainpictures.net/pictures/40000/nahled/random-texture.jpg"/>
</div>
<div
className="slide"
style={{ transform: `translateX(${translate}vw)`}}
>
<img className="carousel-image" src="https://www.publicdomainpictures.net/pictures/40000/nahled/random-texture.jpg"/>
</div>
</div>
<button
className="carousel-button btn-right"
onClick={(_) => handleClickBtn("right")}
>
Right
</button>
</div>
);
};
Here is the SCSS:
.banner-carousel {
height: 720px;
.carousel-button {
cursor: pointer;
position: absolute;
top: 45%;
color: #fff;
z-index: 100;
&.btn-left {
left: 25px;
}
&.btn-right {
right: 25px;
}
}
.carousel-container {
display: flex;
width: 10000px;
.slide {
width: 100vw;
height: 720px;
transition: transform 0.25s linear;
.carousel-image {
width: 100%;
}
}
}
}
Again this still needs to be improved so might come back at a later date and update. If you have any other suggestions please feel free to post!
The code below displays result from reactjs arrays and everything is okay.
What Am now trying to achieve:
I am now trying to display reactjs results at the right hand side using css.
To this effect, I have created a Css class called Side-Bar within user.css files.
Here is my issue:
when I run the reactjs code with the css, the Css causes the reactjs result to be jammed with each other but
If I remove the css, everything will be displayed (but with no css to position at the right)
below is where and how am calling the css class className="sidebar"
<div className="sidebar">
<ul>
<div key={this.props.data.id}>
<button >{this.props.data.name}</button>
</div>
</ul>
</div>
user.css
.sidebar {
width: 20%;
position: fixed;
height: 100%;
right: 0px;
top: 0px;
padding-top: 50px;
padding-bottom: 10px;
border: 1px solid #b2b2b2;
text-align: bottom;
}
Reactjs code
import React, { Component, Fragment } from "react";
import { render } from "react-dom";
import './user.css';
class Person extends React.Component {
state = { open: false };
render() {
return (
<React.Fragment>
<div className="sidebar">
<ul>
<div key={this.props.data.id}>
<button >{this.props.data.name}</button>
</div>
</ul>
</div>
</React.Fragment>
);
}
}
class App extends React.Component {
constructor() {
super();
this.state = {
showBox: false,
data: [
{ id: "1", name: "user 1" },
{ id: "2", name: "user 2" },
{ id: "3", name: "user 3" },
{ id: "4", name: "user 4" },
{ id: "5", name: "user 5" }
]
};
}
render() {
return (
<div>
{this.state.data.map(person => (
<Person key={person.id} data={person} />
))}
</div>
);
}
}
Resolved using flex as per code below which i implement inside user.css files and then call it as class
.sidebar{
display: flex; justify-content: flex-end
}
I've been working on a section with expandable/collapsible sections. When I click on a section to expand or collapse it, a blue focus area shows up but it is placed on a weird angle. I don't know what is causing it and would like a solution to either get rid of it or place it back at the normal horizontal angle. Does anybody have any suggestions as to how to fix this?
I am using a Macbook and Chrome browser.
The entire grey block that this component appears in is placed at an angle as you can see from the top of the image attached below but in the reverse direction from the highlighted focus area.
My css:
#import '../../theme/variables.css';
.rotatedSection {
padding-bottom: 2rem;
}
.container {
max-width: 64rem;
margin: 0 auto;
display: flex;
padding: 2rem 0;
#media screen and (max-width: 68rem) {
margin: 0 3rem;
}
}
.accordianContainer {
flex: 1;
margin-right: 2rem;
min-width: 500px;
#media screen and (max-width: $tablet-lg-max-width) {
margin-right: 0;
}
#media screen and (max-width: 900px) {
min-width: 0;
}
}
.imageContainer {
flex: 1;
margin-left: 2rem;
max-height: 300px;
display: flex;
justify-content: center;
img {
flex: 1;
}
#media screen and (max-width: $tablet-lg-max-width) {
margin-left: 0;
}
}
.heading {
composes: h2 from 'theme/text';
margin-left: auto;
margin-right: auto;
}
My react code:
import React, {Component, PropTypes} from 'react';
import RotatedSection from 'components/RotatedSection';
import AccordionItem from './AccordionItem';
import css from './styles.css';
class AccordionSectionWithImage extends Component {
constructor (props) {
super(props);
this.state = {
activeIndex: null,
};
this.onOpen = this.onOpen.bind(this);
this.onClose = this.onClose.bind(this);
this.setActive = this.setActive.bind(this);
this.handleClickOutside = this.handleClickOutside.bind(this);
}
onOpen = (index) => {
this.setActive(index);
};
onClose = (callback = () => null) => {
this.setActive(null);
callback();
};
setActive = (activeIndex) => this.setState({activeIndex});
handleClickOutside = () => this.props.collapseOnBlur && this.onClose();
render () {
const {
entry: {
items,
heading,
image,
},
showIndex,
classNames,
meta = {},
} = this.props;
const {routeParams, toggleHamburger} = meta;
const {activeIndex} = this.state;
return (
<RotatedSection color='whisper' className={css.rotatedSection}>
<div className={css.container}>
<div className={css.accordianContainer}>
<h2 className={css.heading}>{heading}</h2>
{items && items.map((item, index) => (
<AccordionItem
key={index}
showIndex={showIndex}
entry={item}
meta={{
position: index,
isOpen: (index === activeIndex),
onOpen: () => this.onOpen(index),
onClose: () => this.onClose(),
onChildClick: () => this.onClose(toggleHamburger),
routeParams,
}}
classNames={classNames}
/>
))}
</div>
<div className={css.imageContainer}>
<img src={image && image.fields && image.fields.file.url} alt='Educational assessment' />
</div>
</div>
</RotatedSection>
);
}
}
AccordionSectionWithImage.propTypes = {
meta: PropTypes.object,
entry: PropTypes.object,
collapseOnBlur: PropTypes.bool,
showIndex: PropTypes.bool,
classNames: PropTypes.object,
};
export default AccordionSectionWithImage;
React component for individual section:
function AccordionItem (props) {
const {
meta: {
isOpen,
onOpen,
onClose,
},
entry: {
heading,
text,
},
} = props;
const handleClick = () => (isOpen ? onClose() : onOpen());
return (
<div className={css.itemContainer}>
<div className={css.innerContainer}>
<h3 className={css.heading} onClick={handleClick}>
<span className={css.titleText}>{heading}</span>
<i className={`zmdi zmdi-plus ${css.titleToggle}`} />
</h3>
{isOpen && (
<div className={css.contents}>
{text}
</div>
)}
</div>
</div>
);
}
For anybody else experiencing a similar problem:
Problem only appeared on mobile phones and the device mode of chrome inspector. It was due to the tap-highlight property.
Setting -webkit-tap-highlight-color to rgba(0,0,0,0) hid the problem but it's a non standard css property so the solution may not work for all devices/browsers/users.