Video pause on scroll (React.js | material UI) - reactjs

I have a video on my site that I want to be paused when I scroll it and play only when its visible. I have tried some solutions from internet but things are not working out.
This is my current code. I have tried out a solution from stackoverflow which shows me observe error. I would be glad if someone helps me out on this. Thank you.
Player.js
import { Box, Container } from "#mui/system";
import React, { useEffect, useRef, useState } from "react";
import ReactPlayer from "react-player";
import styled from "styled-components";
import videosample from "./homepagevideo.mp4";
const ResponsiveStyledPlayer = () => {
const Player = ({ className }) => (
<ReactPlayer
url={videosample}
className={className}
playing={true}
autoplay={true}
width="100%"
height="100%"
controls={false}
muted
loop={true}]
/>
);
const AbsolutelyPositionedPlayer = styled(Player)`
position: absolute;
top: 0;
left: 0;
`;
const RelativePositionWrapper = styled.div`
position: relative;
padding-top: 56.25%;
`;
return (
<Box>
<Container>
<RelativePositionWrapper>
<AbsolutelyPositionedPlayer />
</RelativePositionWrapper>
</Container>
</Box>
)
};
export default ResponsiveStyledPlayer;

you can you Waypoint for your requirement. I have made one demo for you, you can refer to that. Link Demo
import React, { useState } from 'react';
import ReactPlayer from 'react-player/youtube';
import { Waypoint } from 'react-waypoint';
import './style.css';
export default function App() {
let [shouldPlay, updatePlayState] = useState(false);
let handleEnterViewport = function () {
updatePlayState(true);
};
let handleExitViewport = function () {
updatePlayState(false);
};
return (
<div>
<Waypoint onEnter={handleEnterViewport} onLeave={handleExitViewport}>
<div>
<ReactPlayer
playing={shouldPlay}
url="https://www.youtube.com/watch?v=ysz5S6PUM-U"
/>
</div>
</Waypoint>
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
<h1>hii</h1>
<br />
</div>
);
}

Related

Kendo UI progress bar inside react-tooltip not working

I have a simple div which uses react-tooltip
Inside the tooltip I am trying to render kendo progress bar but it doesn't seem to be working, the bar is shown but the progress is not shown
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';
import 'react-tooltip/dist/react-tooltip.css'
import { Tooltip as ReactTooltip } from 'react-tooltip'
import { ProgressBar } from '#progress/kendo-react-progressbars';
const App = () => {
const TooltipContentTemplate = () => {
return <div>
<ProgressBar value={20} labelVisible={false}/>
</div>
};
return (
<div className="container">
<div className="row mb-4" id="1"
data-tooltip-html={ReactDOMServer.renderToString(<TooltipContentTemplate></TooltipContentTemplate>)}>
Hello World
</div>
<ReactTooltip style={{zIndex:"10", backgroundColor:"black",color:"black",width:"200px",height:"300px",border:'1px solid black'}} anchorId="1"/>
<div>
<ProgressBar value={20} labelVisible={false}/>
</div>
</div>
);
};
ReactDOM.render(
<App />,
document.querySelector('my-app')
);
Without hover on Hello World
On hover
I am trying with above code
This seems to be an issue related to the usage of ReactDOMServer.renderToString(), not sure what though.
You can try putting the tooltip content inside the tooltip component directly, instead of using data-tooltip-html on the anchor.
Your code updated:
import * as React from "react";
import * as ReactDOM from "react-dom";
import "react-tooltip/dist/react-tooltip.css";
import { Tooltip as ReactTooltip } from "react-tooltip";
import { ProgressBar } from "#progress/kendo-react-progressbars";
const App = () => {
const TooltipContentTemplate = () => {
return (
<div>
<ProgressBar value={20} labelVisible={false} />
</div>
);
};
return (
<div className="container">
<div className="row mb-4" id="1">
Hello World
</div>
<ReactTooltip
style={{
zIndex: "10",
backgroundColor: "black",
color: "black",
width: "200px",
height: "300px",
border: "1px solid black"
}}
anchorId="1"
>
{/* tooltip content here */}
<TooltipContentTemplate></TooltipContentTemplate>
</ReactTooltip>
<div>
<ProgressBar value={20} labelVisible={false} />
</div>
</div>
);
};
ReactDOM.render(<App />, document.querySelector("my-app"));

Why top navbar and sidebar not visible in grapejs?

Can you explain me the problem that I have in my code...I am trying to get grapesjs editor into my code...Can anyone help me out on this one??
import React, {useState, useEffect} from 'react';
import grapesjs from 'grapesjs';
import Basics from "grapesjs-blocks-basic"
import BaseReactComponent from "../../base-react-component";
import ReactComponents from "../../react-components";
import MuiComponents from "../../mui-components/index"
import Listing from '../../Listing';
import {Button, Slider, Snackbar} from "#material-ui/core"
import MuiButton from "mui-button";
const EditStepUI = (props) => {
const [editor, setEditor] = useState(true)
useEffect(() => {
const editor = grapesjs.init({
container: "#gjs",
height: "100%",
storageManager: false,
noticeOnUnload: false,
plugins: [Basics, BaseReactComponent, ReactComponents, MuiComponents]
});
console.log(editor);
}, [])
return (
<div id="editor">
editor.setComponents(`
<div>
<span>
Foo
</span>
<Listing>
<div>
Bar
</div>
<MuiButton variant='contained' color='primary'>
Click Me
</MuiButton>
<Slider />
</Listing>
<Snackbar>
<MuiButton variant='contained' color='secondary'>
Click Me
</MuiButton>
</Snackbar>
<Slider />
</div>
`);
</div>
)
}
export default EditStepUI;

issue with material ui components responsiveness

I am trying to use matrial ui elements,but my issue is that it is not responsive. I want to show 2
items on medium screen and just 1 on small ones, but it is 3 on all screens. thx
this is the parent component and I prop drill the array's elements to the child component
import * as React from "react";
import Box from "#mui/material/Box";
import ImageList from "#mui/material/ImageList";
import ImageListItem from "#mui/material/ImageListItem";
import ImageListItemBar from "#mui/material/ImageListItemBar";
import InfiniteScroll from "react-infinite-scroll-component";
import Grid from "#mui/material/Grid";
import ProductListItem from "./ProductListItem";
const ProductList = ({ products }) => {
return (
<>
<div className="mr-5 ml-5">
<Box>
<ImageList variant="masonry" cols={3} gap={18}>
{products.map((item) => (
<ProductListItem
item={item}
key={item.id}
/>
))}
</ImageList>
</Box>
</div>
</>
);
};
export default ProductList;
this is the child component for each element of array
import React, { useState, useEffect } from "react";
import Heart from "react-heart";
import { useLocalStorage } from "./storage.js";
import ImageListItem from "#mui/material/ImageListItem";
import ImageListItemBar from "#mui/material/ImageListItemBar";
import ImageList from "#mui/material/ImageList";
const ProductListItem = (props) => {
return (
<>
<ImageListItem>
<img
src={`${props.item.img_src}?w=248&fit=crop&auto=format`}
srcSet={`${props.item.img_src}?w=248&fit=crop&auto=format&dpr=2 2x`}
alt={props.item.title}
loading="lazy"
/>
<div style={{ width: "2rem" }} className="ml-2 mt-2">
<Heart />
</div>
<ImageListItemBar
subtitle={<span>by: aaaaaaaaaaaa</span>}
position="below"
align="right"
/>
</ImageListItem>
</>
);
};
export default ProductListItem;

Export variable - React

I have a component called Component1 in which I have the following code:
import React, { useState } from "react";
import Popover from "material-ui-popup-state/HoverPopover";
import Fab from "#material-ui/core/Fab";
import {
usePopupState,
bindHover,
bindPopover
} from "material-ui-popup-state/hooks";
import PaletteIcon from "#material-ui/icons/Palette";
import Colors from "./Colors";
const DEFAULT_COLOR = "red";
const COLORS = [/*list of colors*/];
const Component1 = ({ classes }) => {
const popupState = usePopupState({
variant: "popover",
popupId: "demoPopover"
});
const [selectedColor, setSelectedColor] = useState(DEFAULT_COLOR);
return (
<div className="box" style={{ backgroundColor: selectedColor }}>
<Fab variant="extended" {...bindHover(popupState)}>
<PaletteIcon />
</Fab>
<Popover
>
<div className="color-palette">
{COLORS.map((color) => (
<Colors
key={color}
selected={selectedColor === color}
onClick={setSelectedColor}
color={color}
/>
))}
</div>
</Popover>
</div>
);
};
export default Component1;
This component is imported in Component2 where the code is:
import React from "react";
import Component1 from "./Component1";
import Fab from "#material-ui/core/Fab";
import DeleteIcon from "#material-ui/icons/Delete";
function Component2(props) {
function handleClick() {
props.onDelete(props.id);
}
return (
<div className="note" style={{ backgroundColor: "selectedColor" }}>
<h1>{props.title}</h1>
<p>{props.content}</p>
<Fab onClick={handleClick}>
<DeleteIcon fontSize="small" />
</Fab>
<HoverPopover />
</div>
);
}
export default Component2;
In component2 I need to use the const selectedColor for styling purpose for div with class="note". However the issue is when I select colors from COLORS list the background-color of div with class="note" is not changing. I tried many options but I don't understand how to do it correctly. Please tell me how to do it right.
To share the "selectedColor" variable, which is actually a state, you would have to pass it through the props to the child component
Your "Component2" should declare the state "selectedColor", and this state and its function must be passed by the props to your "Component1".
https://reactjs.org/tutorial/tutorial.html#lifting-state-up

React routes not rendering the specific component

I am trying to make a WhatsApp Web clone using React and Firebase. Here I want to implement the functionality where clicking on a specific chat in the sidebar on left of window opens a chat body on right.
For this I am using Switch and Route in React. By default, I want to show the sidebar in all cases but display the Chat body only when the user clicks on one of the chats.
For this the code in my App.js is :
import React from "react";
import Chat from "./Chat";
import { BrowserRouter,Switch, Route } from "react-router-dom";
import "./App.css";
import Sidebar from "./Sidebar";
function App() {
return (
<div className="app">
<div className="app_body">
<BrowserRouter>
<Switch>
<Sidebar />
<Route exact path="/" component={Chat} />
<Route path="/rooms/:roomId" component={Chat} />
</Switch>
</BrowserRouter>
</div>
</div>
);
}
export default App;
However, only the sidebar gets displayed every time.The chat doesn't get displayed even when I have added the component Chat in my exact home route ("/")
as shown here
When I interchange the Chat and Sidebar components in the code, only the Chat component gets displayed like this
What is wrong in this code? I tried to search for a lot of answers but couldn't find any answers to this. I tried switching routes, components and everything
The codes for my specific components are :
Chat :
import React, { useState, useEffect } from "react";
import "./Chat.css";
import { Avatar, IconButton } from "#material-ui/core";
import {
Search,
AttachFile,
MoreVert,
InsertEmoticon,
Mic,
} from "#material-ui/icons";
import { useParams } from "react-router-dom";
import db from "./firebase";
function Chat() {
const [input, setInput] = useState("");
const { roomId } = useParams();
const [roomName, setRoomName] = useState("");
useEffect(() => {
if (roomId) {
db.collection("rooms")
.doc(roomId)
.onSnapshot((snapshot) => setRoomName(snapshot.data().name));
}
}, [roomId]);
const sendMessage = (e) => {
e.preventDefault();
console.log(input);
setInput("");
};
return (
<div className="chat">
<div className="chat_header">
<Avatar src="https://res.cloudinary.com/shatadrucld/image/upload/v1597305602/qpqnqwyuokbupddqwuwc.jpg" />
<div className="chat_headerInfo">
<h3>{roomName}</h3>
<p>Last seen at .....</p>
</div>
<div className="chat_headerRight">
<IconButton>
<Search />
</IconButton>
<IconButton>
<AttachFile />
</IconButton>
<IconButton>
<MoreVert />
</IconButton>
</div>
</div>
<div className="chat_body">
<p className={`chat_message ${true && "chat-receiver"}`}>
<span className="chat_name">Shatadru Roy</span>Hey guys
<span className="chat_timestamp">3:52pm</span>
</p>
</div>
<div className="chat_footer">
<InsertEmoticon />
<form>
<input
type="text"
placeholder="Type a message"
value={input}
onChange={(e) => {
setInput(e.target.value);
}}
></input>
<button type="submit" onClick={sendMessage}></button>
</form>
<Mic />
</div>
</div>
);
}
export default Chat;
Sidebar :
import SidebarChat from "./SidebarChat";
import db from "./firebase";
function Sidebar() {
const [rooms, setRooms] = useState([]);
useEffect(() => {
const unsubscribe=db.collection("rooms").onSnapshot((snapshot) => {
setRooms(
snapshot.docs.map((doc) => ({
id: doc.id,
data: doc.data(),
}))
);
});
return ()=>{
unsubscribe();
}
}, []);
return (
<div className="sidebar">
<div className="sidebar_header">
<Avatar />
<div className="sidebar_headerRight">
<IconButton>
<DonutLarge />
</IconButton>
<IconButton>
<Chat />
</IconButton>
<IconButton>
<MoreVert />
</IconButton>
</div>
</div>
<div className="sidebar_search">
<div className="sidebar_searchContainer">
<SearchOutlined />
<input placeholder="Seacrh chats" type="text"></input>
</div>
</div>
<div className="sidebar_chats">
<SidebarChat addNewChat />
{rooms.map(room=>{
return (<SidebarChat key={room.id} id={room.id} name={room.data.name} />)
})}
</div>
</div>
);
}
export default Sidebar;
Please help me with this problem. I am new to these concepts so I am completely stuck at this phase. I tried several solutions in the internet but none of them is working.
Edit :
I also tried to use withRouter but it is not working either
Try moving <Sidebar /> out of the <Switch> component. If is persistent then it should be outside of <Switch/>

Resources