I want to make a photo slider with React.js - html

I want to make a photo slider like the following link by React.js.
https://youtu.be/Zv9KskTYTNQ
Each post contains a maximum of 5 photos When displaying, I want to display only one photo and switch photos with a slider instead of showing 5 photos.
Issue/error message
Currently, if there are two or more photos as shown below, they will be displayed vertically.
I tried the link below but I don't know how to apply it to my code. . https://www.npmjs.com/package/react-slideshow-image Or if there is another way that works, it will be great.
import React, { useState, useEffect, onClick } from "react";
import axios from "axios";
import Cookies from "universal-cookie";
import { apiURL } from "./Default";
import { useSelector } from "react-redux";
import { useParams, Link } from "react-router-dom";
import { useDispatch } from "react-redux";
import { setUserID, setUserName } from "../stores/user";
import { useForm } from "react-hook-form";
const cookies = new Cookies();
const Top = () => {
const { id } = useParams();
const [post, setPost] = useState();
const isLoggedIn = useSelector((state) => state.user.isLoggedIn);
const userID = useSelector((state) => state.user.userID);
const dispatch = useDispatch();
const {
register,
handleSubmit,
formState: { errors },
} = useForm();
const [len, setLen] = useState(0);
const [flug, setFlug] = useState(true);
const [initial_screen, setInitial] = useState(true);
const username = useSelector((state) => state.user.userName);
console.log(username);
let slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides((slideIndex += n));
}
function currentSlide(n) {
showSlides((slideIndex = n));
}
function showSlides(n) {
let i;
let slides = document.getElementsByClassName("mySlides");
let dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1;
}
if (n < 1) {
slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
}
const getPosts = async (data) => {
await axios
.get(apiURL + "posts/", {
headers: {
"Content-Type": "application/json",
},
})
.then((result) => {
setPost(result.data);
setLen(result.data.length);
setInitial(true);
})
.catch((err) => {
console.log(err);
});
};
const getLoginID = async (data) => {
await axios
.get(apiURL + "mypage/", {
headers: {
"Content-Type": "application/json",
Authorization: `JWT ${cookies.get("accesstoken")}`,
},
})
.then((get_user) => {
dispatch(setUserID(get_user.data.id));
dispatch(setUserName(get_user.data.username));
})
.catch((err) => {
console.log(err);
});
};
useEffect(() => {
getPosts();
getLoginID();
}, [flug]);
const getSearchResult = async (data) => {
console.log(data.search);
console.log(apiURL + "posts/?search=" + data.search);
await axios
.get(apiURL + "posts/?search=" + data.search, {
headers: {
"Content-Type": "application/json",
},
})
.then((result) => {
setLen(result.data.length);
setPost(result.data);。
setInitial(false);
})
.catch((err) => {
console.log(err);
});
};
return (
<div className="container">
<div className="row text-center justify-content-center">
{isLoggedIn ? (
<p className="top_hello">Hello {username}!</p>
) : (
<p className="top_hello">Hello Guest!</p>
)}
<div className="top_search col-10 col-lg-12">
<form
className="top_search_input form-inline col-12 col-lg-4"
onSubmit={handleSubmit(getSearchResult)}
>
<input
placeholder="Search Title or Maker"
className="form-control"
{...register("search", { required: true })}
/>
<input className="btn btn-secondary" type="submit" value="Search" />
</form>
</div>
{len >= 1 ? (
initial_screen ? (
<>
{post.map((item, i) => (
<div key={i} className="top_post col-6 col-lg-3">
<div className="slideshow-container">
{item.photo && (
<div className="mySlides fade">
<img className="top_post_photo" src={item.photo} />
</div>
)}
{item.photo2 && (
<div className="mySlides fade">
<img className="top_post_photo" src={item.photo2} />
</div>
)}
{item.photo3 && (
<div className="mySlides fade">
<img className="top_post_photo" src={item.photo3} />
</div>
)}
{item.photo4 && (
<div className="mySlides fade">
<img className="top_post_photo" src={item.photo4} />
</div>
)}
{item.photo5 && (
<div className="mySlides fade">
<img className="top_post_photo" src={item.photo5} />
</div>
)}
<a className="prev" onclick="plusSlides(-1)">
❮
</a>
<a className="next" onclick="plusSlides(1)">
❯
</a>
</div>
<p>Title: {item.title}</p>
<p>Condition: {item.condition_name}</p>
<Link to={`/post/${item.id}`} className="btn btn-secondary">
Detail
</Link>
</div>
))}
</>
) : (
<>
{post.map((item, i) => (
<div key={i} className="top_post col-6 col-lg-3">
{item.photo && (
<img className="top_post_photo" src={item.photo} />
)}
{item.photo2 && (
<img className="top_post_photo" src={item.photo2} />
)}
{item.photo3 && (
<img className="top_post_photo" src={item.photo3} />
)}
{item.photo4 && (
<img className="top_post_photo" src={item.photo4} />
)}
{item.photo5 && (
<img className="top_post_photo" src={item.photo5} />
)}
<p>Title: {item.title}</p>
<p>Condition: {item.condition_name}</p>
<Link to={`/post/${item.id}`} className="btn btn-secondary">
Detail
</Link>
</div>
))}
<button
onClick={() => setFlug(!flug)}
className="btn btn-secondary"
>
Back
</button>
</>
)
) : (
<div>
<p>{len}</p>
<p>not found!</p>
<button
onClick={() => setFlug(!flug)}
className="btn btn-secondary"
>
Back
</button>
</div>
)}
</div>
</div>
);
};
export default Top;
CSS
* Top.js */
.top_hello {
text-align: right;
margin: 20px 0 0 0;
}
.top_search {
margin: 20px 0 20px 0;
justify-content: right;
}
.top_search_input {
display: flex;
margin-right: 0;
margin-left: auto;
}
.top_post {
/* position: relative; */
background:rgb(209, 255, 209);
padding:15px;
border-radius: 10px;
margin-bottom: 30px;
width: 250px;
height: 400px;
/* margin: 10px; */
}
.top_post_photo {
object-fit: cover;
width: 200px;
height: 200px;
}
/* Slideshow container */
.slideshow-container {
max-width: 200px;
position: relative;
margin: auto;
}
/* Hide the images by default */
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Fading animation */
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
#keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

Related

How can I make a song progress bar for my audio player?

I've made a simple audio player, but now I want to add a song progress bar that fills up the timeline as the song plays, this is my html:
<div id="ap-timeline" onClick={this.mouseMove} ref={(timeline) => { this.timeline = timeline }}>
<div id="ap-handle" onMouseDown={this.mouseDown} ref={(handle) => { this.handle = handle }} />
<div id="ap-handle-circle" onMouseDown={this.mouseDown} ref={(handleCircle) => { this.handleCircle = handleCircle }} />
</div>
And this is my CSS:
#ap-timeline{
display: flex;
flex-direction: row;
align-items: center;
width: 550px;
height: 4px;
border-radius: 15px;
background: $audio-slider-gray;
margin: 0 10px;
#ap-handle {
background: $white;
height: 4px;
}
#ap-handle-circle {
width: 13px;
height: 13px;
border-radius: 50%;
background: $white;
transform: scale(0.25);
}
}
}
The ap-handle is the progress bar that I'm trying to use to fill up the timeline as the song plays.
I'm not much of a react person so excuse any poor practices here:
#progress-bar {
height: 20px;
background: red;
transform: scaleX(0);
transform-origin: center left;
}
class App extends Component {
constructor() {
super();
this.interval = null; // setInterval
this.audioEl = new Audio(
"https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3"
);
this.audioEl.addEventListener("canplaythrough", () => {
this.setState({
duration: this.audioEl.duration
});
});
this.state = {
currentTime: 0,
duration: 0
};
}
playAudio() {
this.audioEl.play();
// requestAnimationFrame would probably be better, but
// for example sake we'll use setInterval
this.interval = setInterval(() => {
this.setState({
currentTime: this.audioEl.currentTime,
progress: Math.ceil((this.audioEl.currentTime / this.audioEl.duration) * 100) / 100
});
}, 100);
}
stopAudio() {
clearInterval(this.interval);
this.audioEl.pause();
}
render() {
return (
<div>
<button onClick={() => this.playAudio()}>Play</button>
<button onClick={() => this.stopAudio()}>Stop</button>
<div
style={{ transform: `scaleX(${this.state.progress})` }}
id="progress-bar"
/>
</div>
);
}
}
Blitz

Make element's height adaptative to what's inside

I want to make a grey-colored background for my list in ReactJS.
The problem is that the background seems to have a fixed height which seems to be smaller than the inside's height...
I never met this problem before, and the only way I found to have the background size bigger than the element's is to put height: XXXpx, which is not suitable...
Can someone explain me what I did wrong ?
Here is my class file :
import React, { Component } from "react";
import { ListGroupItem } from "react-bootstrap";
import { LinkContainer } from "react-router-bootstrap";
import { API } from "aws-amplify";
import "./ProjectList.css";
export default class ProjectList extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
projects: [],
reducedView: true,
projectsNumber: 1000,
numberOfElementsToDisplay: 1000
};
}
async componentDidMount() {
try {
const projects = await this.projects();
console.log(projects)
this.setState({ projects });
} catch (e) {
alert(e);
}
this.setState({ isLoading: false });
}
projects() {
return API.get("economics-endpoint", "/list");
}
handleClick(e) {
this.setState({ reducedView: false });
e.preventDefault();
}
renderNotesList(projects) {
return [{}].concat(projects).map(
(note, i) => {
if(i !== 0){
if((this.state.reducedView && i <= this.state.numberOfElementsToDisplay) || !this.state.reducedView){
return(
<div className="element">
<ListGroupItem className="mainContainer">
<div>
ProjectName#{i}
</div>
</ListGroupItem>
</div>
);
}
}
else{
if(this.state.projectsNumber === 0){
return(
<div className="element">
<LinkContainer to={`/econx/new`}>
<ListGroupItem className="new">
<div>
+ Create New Project
</div>
</ListGroupItem>
</LinkContainer>
<div className="errorMessage">
You don't have any existing project yet.
</div>
</div>
);
}
else{
return(
<div className="element">
<LinkContainer to={`/econx/new`}>
<ListGroupItem className="new">
<div>
+ Create New Project
</div>
</ListGroupItem>
</LinkContainer>
</div>
);
}
}
}
);
}
render() {
return (
<div className="ProjectList">
<div className="projects">
{!this.state.isLoading &&
this.renderNotesList(this.state.projects)}
</div>
</div>
);
}
}
And my css file :
.ProjectList .notes h4 {
font-family: "Open Sans", sans-serif;
font-weight: 600;
overflow: hidden;
line-height: 1.5;
white-space: nowrap;
text-overflow: ellipsis;
}
.projects{
background-color: #E0E0E0;
border-radius: 10px;
padding: 1%;
}
.list-group-item{
margin-bottom: 3px;
padding: 0px;
line-height: 200px;
text-align: center;
vertical-align: middle;
border-radius: 6px;
padding-bottom: 50px;
}
.list-group-item:first-child{
padding: 0px;
border-radius: 6px;
}
.list-group-item:hover{
vertical-align: baseline;
}
.element{
width: 20%;
float: left;
padding: 0% 1% 1% 1%;
}
This doesn't answer your CSS question. I did have some thoughts on the JavaScript, though. I'll offer my refactor for your consideration. Hopefully I interpreted your original logic correctly:
import React, { Component } from "react";
import { ListGroupItem } from "react-bootstrap";
import { LinkContainer } from "react-router-bootstrap";
import { API } from "aws-amplify";
import "./ProjectList.css";
export default class ProjectList extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
projects: [],
reducedView: true,
projectsNumber: 1000,
numberOfElementsToDisplay: 1000
};
}
async componentDidMount() {
try {
const projects = await this.fetchProjects();
console.log(projects);
this.setState({ projects });
} catch (e) {
alert(e);
} finally {
this.setState({ isLoading: false });
}
}
fetchProjects = () => {
return API.get("economics-endpoint", "/list");
};
handleClick = e => {
e.preventDefault();
this.setState({ reducedView: false });
};
render() {
const {
isLoading,
numberOfElementsToDisplay,
projectsNumber,
reducedView
} = this.state;
const projects = reducedView
? this.state.projects.slice(0, numberOfElementsToDisplay)
: this.state.projects;
return (
<div className="ProjectList">
<div className="projects">
{!isLoading && (
<React.Fragment>
<div className="element">
<LinkContainer to={`/econx/new`}>
<ListGroupItem className="new">
<div>+ Create New Project</div>
</ListGroupItem>
</LinkContainer>
{projectsNumber === 0 && (
<div className="errorMessage">
You don't have any existing projects yet.
</div>
)}
</div>
{projects.map((project, index) => (
<div className="element">
<ListGroupItem className="mainContainer">
<div>ProjectName#{index}</div>
</ListGroupItem>
</div>;
))}
</React.Fragment>
)}
</div>
</div>
);
}
}
This fixes your off-by-one issue by hopefully capturing your intentions as I understand them:
If it's not loading...
Show a "Create New Project" item
If they don't have any projects yet, show them a message
If they do have projects, create a new element for each one
It's worth considering whether your projectsNumber could be replaced with projects.length. If so, you can probably combine (2) and (3) above to be an either-or scenario: If they don't have projects, show a message, if they do, show the projects.

I have a problem with overlapping elements in a JSX app

I created a button that adds emails inputs in a form (for invites), but at a certain number of inputs, they overlaps others elements on the page.
Here's some css :
.App {
height: 100%;
}
html,
body,
#root {
margin: 0;
height: 100%;
font-family: "Montserrat";
}
.page {
height: 80%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.content {
margin-top: 50px;
width: 400px;
}
(It's a JSX app)
Here's a screenshot of the page: https://paste.pics/ab860613d5fd6eb82dfe59894ee24bc0
Thanks for your help !
You can add a 'container' div around your input's.. From there you can use height and width props along with the overflow: 'scroll' attribute.
Something like this:
const { useState } = React;
// Create items
const NEW_INVITES = [...Array(10).keys()].map((item, index) => {
return {
id: index + 1,
value: index + 1
}
});
function InviteInput(props) {
const { value, onChange } = props;
const handleChange = e => {
onChange && onChange(e);
};
return (
<li>
<input
value={value}
onChange={handleChange}
className="form-input"
type="email"
placeholder="nom#exemple.com"
name="invites"
required
/>
</li>
);
}
function AddInviteButton(props) {
return (
<button onClick={props.onClick}>
Ajouter une autre personne // (Add another person)
</button>
);
}
function InviteForm({ onSubmit, height, width, addBorder }) {
const [nbInvites, setNbInvites] = useState(NEW_INVITES);
let containerRef;
const onAddInviteClick = () => {
let id = nbInvites.length + 1;
setNbInvites([
...nbInvites,
{
id,
value: id
}
]);
setTimeout((cr) => {
cr.scrollIntoView({ behavior: "smooth" });
}, 100, containerRef);
};
const handleChange = (event, index) => {
let newstate = [...nbInvites];
newstate[index].value = event.target.value;
setNbInvites(newstate);
};
const handleSubmit = event => {
onSubmit(event, nbInvites);
};
const invitesContainer = {
height,
width,
overflow: 'scroll',
border: addBorder === true ? '1px solid black' : '',
}
return (
<div>
<div style={invitesContainer}>
{nbInvites.map((item, index) => {
return (
<InviteInput
key={index}
value={item.value}
onChange={e => handleChange(e, index)}
/>
);
})}
<div ref={r => containerRef = r}/>
</div>
<AddInviteButton onClick={onAddInviteClick} />
<br />
<button onClick={handleSubmit}>Submit</button>
</div>
);
}
function App() {
const [formVals, setFormVals] = useState();
const doSubmit = (event, formValues) => {
setFormVals(formValues);
};
return (
<div className="page">
<h2 className="box-title">
Qui sont les eleves de cette classe ? // (Who are the students in this
class?)
</h2>
<p>
Vous pourrez toujours en ajouter par la suite // (You can always add
some later)
</p>
<InviteForm onSubmit={doSubmit} height="100px" width="200px" addBorder />
{formVals ? <pre>{JSON.stringify(formVals, null, 2)}</pre> : ""}
</div>
);
}
ReactDOM.render(<App />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>

How do I make a content div scrollable? React and CSS

Learning React.js framework and need some pointers on styling. CSS isn't my forte.
How do I style the static content div in the middle and make it scrollable only within the div?
No styling:
https://i.imgur.com/26wNAfH.jpg
How to style this?
https://i.imgur.com/c5nYCOz.jpg
Here's the scroll function:
https://storage.googleapis.com/hatchways-app.appspot.com/assessments/data/frontend/part%202.mp4
app.css
.name {
font-weight: bold;
font-size: 20px;
}
.centered {
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
.center {
position: fixed;
width: 500px;
height: 200px;
top: 50%;
left: 50%;
margin-top: -100px; /* Negative half of height. */
margin-left: -250px; /* Negative half of width. */
}
.content {
text-align: center;
border: 2px solid grey;
border-radius: 5px;
position: fixed;
/* center the div */
right: 0;
left: 0;
margin-right: auto;
margin-left: auto;
/* give it dimensions */
min-height: 10em;
width: 90%;
/* just for example presentation */
top: 5em;
background-color: white;
}
Output: https://i.imgur.com/Eyv6hab.png
HTML:
import React, { Component } from "react";
import "../App.css";
import "../../node_modules/bootstrap/dist/css/bootstrap.min.css";
const API = "https://www.hatchways.io/api/assessment/students";
class App extends Component {
constructor(props) {
super(props);
this.state = {
students: [],
isLoading: false,
error: null
};
}
componentDidMount() {
this.setState({ isLoading: true });
fetch(API)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Something went wrong ...");
}
})
.then(data =>
this.setState({ students: data.students, isLoading: false })
)
.catch(error => this.setState({ error, isLoading: false }));
}
render() {
const { students, isLoading, error } = this.state;
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return <p>Loading ...</p>;
}
return (
<body>
<div className="content">
<div>
{students.map(student => (
<div key={student.id}>
<p>
<img src={student.pic} />
</p>
<p className="name">
{student.firstName} {student.lastName}
</p>
<p>Email: {student.email}</p>
<p>Company: {student.company}</p>
<p> Skill: {student.skill}</p>
<p>Average: {student.grades}</p>
</div>
))}
</div>
</div>
{/* <div class="card mb-3">
{students.map(student => (
<div class="row no-gutters">
<div class="col-md-4">
<img src={student.pic} class="card-img" alt="..." />
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">
{student.firstName} {student.lastName}
</h5>
<p class="card-text">
<p>Email: {student.email}</p>
<p>Company: {student.company}</p>
<p> Skill: {student.skill}</p>
<p>Average: {student.grades}</p>
</p>
</div>
</div>
</div>
))}
</div> */}
</body>
);
}
}
export default App;
This might not help I am unfamiliar with that JS framework. I am only posting this because nobody has answered and maybe this can help.
<style>
scroll
{
max-height: 400px;
overflow-y: scroll;
}
</style>
<div class="scroll">

How to indent list and sublist menu?

I am trying to style the list and sublist menu. Some list items have name and some list items having sublist items having svg added infront of list item. in doing so, the list item without sublist item is not indented correctly. how can i achieve it. could somebody help me. Below is what i have tried.
return (
{this.props.expanded &&
<Drawer className="drawer">
<header>
<h4>List items</h4>
</header>
<ListItems listitemss={this.props.listitems} />
</Drawer>
}
</div>
);
};
}
class ListItemss extends React.PureComponent {
render() {
return <ul className="listitems_list">
<div className="lists">
{this.props.layers.map((list, index) => {
return <List key={index} list={list} />
})}
</div>
</ul>
}
}
class List extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
expanded: false,
hidden: false,
};
}
expand = () => {
this.setState({expanded: true});
};
collapse = () => {
this.setState({expanded: false});
};
hide_layer = () => {
this.setState({hidden: true});
};
show_layer = () => {
this.setState({hidden: false});
};
render() {
return <li>
<div className="list">
{this.props.list.children && !this.state.expanded &&
<SvgAccDown width="14" height="8" onClick={this.expand} />
}
{this.props.list.children && this.state.expanded &&
<SvgAccUp width="20" height="18" onClick={this.collapse} />
}
<span>{this.props.list.name}</span>
</div>
{this.props.list.children && this.state.expanded &&
<ListItems lists={this.props.list.children}/>
}
</li>;
}
}
.listitems_list {
height: 100%;
overflow: auto;
.lists {
height: 100%;
margin-left: 12px;
overflow: auto;
.list {
background-color: #fff;
display: flex;
flex-direction: row;
align-items: center;
cursor: pointer;
span {
padding-right: 12px;
padding-top: 12px;
padding-bottom: 12px;
padding-left: 2px;
font-size: 16px;
font-weight: bold;
}
}
}
}
If it is an external CSS file, and you are using something like Create React App, you can just import 'mystyle.css;`