Reactjs: css causes reactjs result to jam together - css

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
}

Related

align divs to the right side of the window in react

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.

Can't get buttons to wrap to new line instead of overflowing container

I couldn't get a JSFiddle to work properly with React and some other dependencies, so I hope the link to this Github repo is sufficient for demonstrating the issue:
https://github.com/ishraqiyun77/button-issues/
Basically, a group of buttons is rendered and they should be auto-widened to fill white space and take up the whole row. This works in Chrome, Edge, Safari, and Firefox. It looks like this:
This isn't happening in IE. I've been messing with it for hours and haven't made much progress:
Here is the code, although could clone the repo I posted above:
// component.jsx
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import {
Button,
Col,
Modal,
ModalBody,
ModalHeader,
Row
} from 'reactstrap';
import styles from '../assets/scss/app.scss';
class TestPrint extends Component {
constructor(props) {
super(props);
this.state = {
modal: false,
}
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({
modal: !this.state.modal
})
}
renderContent() {
let buttons = [];
for (let i = 1; i < 50; i++) {
buttons.push(
<Col key={i}>
<Button
key={i}
className='cuts-btn'
>
{i} - Test
</Button>
</Col>
);
};
return buttons;
}
render() {
return (
<div>
<Button
style={
{
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)'
}
}
onClick={this.toggle}
>
Open Modal for Buttons
</Button>
<Modal
size='lg'
isOpen={this.state.modal}
toggle={this.toggle}
className='results-modal'
>
<ModalHeader toggle={this.toggle}>
Button Issues
</ModalHeader>
<ModalBody>
<div className='results-bq-cuts'>
<Row>
{this.renderContent()}
</Row>
</div>
</ModalBody>
</Modal>
</div>
)
}
}
ReactDOM.render(<TestPrint />, document.getElementById('app'));
.results-modal {
max-width: 1200px;
.modal-content {
.modal-body {
margin-left: 13px;
margin-right: 13px;
.results-bq-cuts {
width: 100%;
.col {
padding:2px;
}
.cuts-btn {
font-size: 11px;
padding: 3px;
width: 100%;
box-shadow: none;
}
// .col {
// padding: 2px;
// display: table-cell;
// flex-basis: 100%;
// flex: 1;
// }
// .cuts-btn {
// font-size: 11px;
// padding: 3px;
// width: 100%;
// box-shadow: none;
// }
}
}
}
}
I have all of the <Button> wrapped in <Col> because that should be what is filling the white space by increasing the size of the button.
Thanks for the help!
IE11 doesn't like working out the width of flex items. If you add flex-basis: calc( 100% / 24 ); to .col it works :) Obviously use any width you want, but what I've given replicates the 21 boxes on one line. But essentially flex-basis needs a defined width to work.
​
Or add an extra class to each element (such as col-1 ) This'll also achieve the same thing.

CSS Help - Remove blank space between Images (<img>)

Right now I am trying to make images stack side by side, vertically and horizontally, so that there is no white spaces between them. This is what my view currently looks like.
Ideally, there would be none of that blank, white space. I have inserted the ReactJS code below with my GifViewer component that holds the . Any ideas on how I can get this to work? And the code will not run due to the axios request, so it's just for visual effect right now.
img {
display: inline !important;
width: 20%;
vertical-align: top;
height: auto;
top: 0;
left: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
class GifViewer extends Component {
render() {
return (
<img
key={this.props.keyyer}
src={`https://i.giphy.com/${this.props.id}.gif`}
/>
);
}
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
searchTerm: "",
counter: 0,
pastSearches: [],
data: null
};
this.handleChange = this.handleChange.bind(this);
}
componentDidUpdate() {
console.log("this.state.data", this.state.data);
}
componentDidMount() {
console.log("Mounted");
}
handleChange(event) {
this.setState({ searchTerm: event.target.value });
axios
.get(
`${BASE_URL}${config.apiKey}${QUERY}${this.state.searchTerm}${LIMITS}`
)
.then(result => {
this.setState({ data: result.data.data });
});
}
render() {
if (this.state.data) {
var GIFS = this.state.data.map(d => (
<GifViewer keyyer={d.embed_url} id={d.id} />
));
}
return (
<div className="container-fluid">
<div className="row py-5 bg-success">
<div className="col-8 offset-2">
<h3 className="text-center">LET'S BREAK THE INTER-WEBZ</h3>
<input
type="text"
className="form-control"
value={this.state.searchTerm}
onChange={this.handleChange}
placeholder="Search"
/>
</div>
</div>
{this.state.data ? GIFS : "Nothing yet"}
</div>
);
}
}
export default App;
My personal favorite way to resolve this is the css property object-fit: cover; (applied to your img rule).
There's a great article on the property here, but the gist (for your purpose) is that you can zoom the image to cover the whole space without stretching or distorting the image.

React style by class name

I'm implementing a sidebar following this example but can't figure out how to apply the styles for each className...
This is what I've tried, but there are no styles applied...
Component:
import React from 'react';
import styles from '../stylesheets/menu.css';
var Parent = React.createClass({
getInitialState(){
return {sidebarOpen: false};
},
handleViewSidebar(){
this.setState({sidebarOpen: !this.state.sidebarOpen});
},
render() {
return (
<div>
<Header onClick={this.handleViewSidebar} />
<SideBar isOpen={this.state.sidebarOpen} toggleSidebar={this.handleViewSidebar} />
<Content isOpen={this.state.sidebarOpen} />
</div>
);
}
});
var SideBar = React.createClass({
render() {
var sidebarClass = this.props.isOpen ? 'sidebar open' : 'sidebar';
return (
<div className={sidebarClass}>
<div>I slide into view</div>
<div>Me too!</div>
<div>Meee Threeeee!</div>
<button onClick={this.props.toggleSidebar} className="sidebar-toggle">Toggle Sidebar</button>
</div>
);
}
});
export default Parent;
Styles:
.sidebar {
position: absolute;
top: 60px;
}
.sidebar-toggle {
position: absolute;
top: 20%;
}
.sidebar.open {
left: 0;
}
webpack.config :
module: {
loaders: [
{
test: /\.css$/,
loader: 'css-loader'
}
]
}
Partial folder structure:
client
--stylesheets
----menu.css
--components
----Sidebar.js
You are importing the styles as an object:
import styles from '../stylesheets/menu.css';
This can be used for CSS-in-JS, but in your case it will not work.
Try changing it to:
import '../stylesheets/menu.css';
And it should correctly apply the styles.

How to dynamically change CSS in :host in angular 2?

How do I dynamically change CSS properties of a component host?
I have a component and in it's CSS I have given it a stlye:
:host {
overflow-x: hidden
}
On a button click from child component, I need to add overflow-y: hidden to the host component.
How do I achieve this behavior?
Here is a working example.
Use the following HostBinding:
#HostBinding('style.overflow-y') overflowY = 'scroll';
This would give the following component:
#Component({
selector: 'my-app',
template: `
<div>
<button (click)="addStyle()">Add Style</button>
<h2>Hello</h2>
</div>`, styles: [
`
:host {
overflow-x: hidden;
height: 50px;
width: 200px;
display: block;
}
`,
],
})
export class App {
name: string;
#HostBinding('style.overflow-y')
overflowY = 'scroll';
constructor() {
}
addStyle() {
this.overflowY = 'hidden';
}
}

Resources