How to use button to pop up video window(ReactJs) - reactjs

i'm trying to make a button that use the same style but want to use it for two button (1) go to some page (2) to pop up a video trailer. but both of them do the action (1). I don't know how to make these two button do different job.
here is the code for Button,
import React from 'react';
import './Button.css';
import { Link } from 'react-router-dom';
const STYLES = ['btn--primary', 'btn--outline', 'btn--test'];
const SIZES = ['btn--medium', 'btn--large'];
export const Button = ({
children,
type,
onClick,
buttonStyle,
buttonSize
}) => {
const checkButtonStyle = STYLES.includes(buttonStyle)
? buttonStyle
: STYLES[0];
const checkButtonSize = SIZES.includes(buttonSize) ? buttonSize : SIZES[0];
return (
<Link to={Button.props.action} className='btn-mobile'>
<button
className={`btn ${checkButtonStyle} ${checkButtonSize}`}
onClick={onClick}
type={type}
>
{children}
</button>
</Link>
);
};
here is the code for two buttons
function HeroSection() {
return (
<div className='hero-container'>
<video src='/videos/animated-banner.mp4' autoPlay loop muted playsInline/>
<h1>TITLE</h1>
<p> simple text.</p>
<div className='hero-btns'>
<Button
className='btns'
buttonStyle='btn--outline'
buttonSize='btn--large'
>
GET STARTED
</Button>
<Button
className='btns'
buttonStyle='btn--primary'
buttonSize='btn--large'
onClick={console.log('hey')}
>
WATCH TRAILER <i className='far fa-play-circle' />
</Button>
</div>
</div>
);
}
export default HeroSection;

you can use onClick event for different jobs.
<Button
className='btns'
buttonStyle='btn--outline'
buttonSize='btn--large'
onClick={() => {
// ...go to some page...
}}
>
GET STARTED
</Button>
<Button
className='btns'
buttonStyle='btn--primary'
buttonSize='btn--large'
onClick={() => {
// ...open pop up...
}}
>
WATCH TRAILER <i className='far fa-play-circle' />
</Button>

IMO, I think you could add new props to and conditionally use tag either Link or button by checking that props. If you pass value to props to then will be rendered as Link, else as button
import React from 'react';
import './Button.css';
import { Link } from 'react-router-dom';
const STYLES = ['btn--primary', 'btn--outline', 'btn--test'];
const SIZES = ['btn--medium', 'btn--large']
export const Button = ({
children,
type,
onClick,
buttonStyle,
buttonSize,
to
}) => {
const checkButtonStyle = STYLES.includes(buttonStyle)
? buttonStyle
: STYLES[0];
const checkButtonSize = SIZES.includes(buttonSize) ? buttonSize : SIZES[0];
const ActionTag = to ? Link : 'button';
return (
<ActionTag
className={`btn ${checkButtonStyle} ${checkButtonSize}`}
onClick={onClick}
type={type}
to={to}
>
{children}
</ActionTag>
);
};
and you could use that component like this.
If it's used to navigate to other page, pass value to props to
For popping up a video trailer pass props onClick
<Button
className='btns'
buttonStyle='btn--outline'
buttonSize='btn--large'
onClick={
// do something
}
>
pop up
</Button>
<Button
className='btns'
buttonStyle='btn--primary'
buttonSize='btn--large'
to="/"
>
go to home
</Button>

Related

React - show two elelments on button click

I have a stackblitz here
I know I'm not meant to ask this but is there a better way of doing this.
I have two divs that are positioned on tpo of each other and two buttons to show each of the divs
I'm using useState and updating it on button click <button onClick={() => setBlock('One')}>Btn One</button>
and then show the div based on the useState value.
{block === 'One' && <div className="FlexContainerColOne"></div>}
I'm asking cos I've never seen like this before block === 'One' `and not sure if this the best way to do this
import * as React from 'react';
import './style.css';
import { FlexContainer } from './styled';
const App = () => {
const [block, setBlock] = React.useState('One');
return (
<div>
<button onClick={() => setBlock('One')}>Btn One</button>
<button onClick={() => setBlock('Two')}>Btn Two</button>
<FlexContainer>
{block === 'One' && <div className="FlexContainerColOne"></div>}
{block === 'Two' && <div className="FlexContainerColTwo"></div>}
</FlexContainer>
</div>
);
};
export default App;
If you want to switch between two divs, just create a state to toggle it
const App = () => {
const [toggleBlock, setToggleBlock] = React.useState(true);
return (
<div>
<button onClick={() => setToggleBlock(!toggleBlock)}>Toggle</button>
<FlexContainer>
{toggleBlock ? (
<div className="FlexContainerColOne"></div>
) : (
<div className="FlexContainerColTwo"></div>
)}
</FlexContainer>
</div>
);
};

React Pass the value input in the other Pages

I have a homepage where they can click the modal button. after the button click the modal will show. then in the modal they will input a text and when they click submit the text they input should be display in the homepage. Currently the one i did is not displaying in homepage when click the submit button. Please help. Thank you
This is the code:
https://codesandbox.io/s/competent-rgb-lk4ws
Modal
In your Modal component you can create a event when the Submit button is clicked:
import React from "react";
import "./modal.css";
// Create new event: onSubmitClick
function Modal({ setOpenModal, onSubmitClick }) {
// It`s function get name value and pass to onSubmitClick
function getData() {
const nameValue = document.getElementById("name").value;
onSubmitClick(nameValue);
setOpenModal(false);
}
return (
<div className="modalBackground">
<div className="modalContainer">
<div className="titleCloseBtn">
<button
onClick={() => {
setOpenModal(false);
}}
>
X
</button>
</div>
<div className="title">Forms</div>
<div className="body">
<input id="name" placeholder="Enter Your Name" />
</div>
<div className="footer">
<button
onClick={() => {
setOpenModal(false);
}}
id="cancelBtn"
>
Cancel
</button>
{/* The onclick should be onClick */}
<button onClick={getData}>Submit</button>
</div>
</div>
</div>
);
}
export default Modal;
Home
In home page you can get the event and use the new name value
import React, { useState } from "react";
import Modal from "../../src/components/Modal/modal.js";
const Home = () => {
const [modalOpen, setModalOpen] = useState(false);
const [name, setName] = useState("");
function handleModalSubmitClick(nameValue) {
// Receive new value, and set in a state
setName(nameValue);
}
return (
<div class="hello">
<h1>Hey, click on the button to open the modal.</h1>
<span>name: {name}</span>
<button
onClick={() => {
setModalOpen(true);
}}
class="button is-pulled-right"
>
Button
</button>
{modalOpen && (
<Modal
setOpenModal={setModalOpen}
onSubmitClick={handleModalSubmitClick} // Bind a function to handle a event
/>
)}
</div>
);
};
export default Home;

onClick React component

Basically I have created a button component that looks something like this:
import React from "react";
import styles from "./button.module.scss";
const Button = ({ icon, text, align, bgcolor }) => {
const btnStyle = {
justifySelf: align,
};
return (
<>
<div className={"btn " + bgcolor} style={btnStyle}>
<i className={icon}></i> {text}
</div>
</>
);
};
export default Button;
In my app I can then call it by:
<Button
icon="fa-light fa-trash"
text="Some text"
align="end"
bgcolor="yellow"
></Button>
However, at the same time I wish to have an onclick event similar to:
<div onClick={() => setState(!state)}>Click me</div>
How can that be achieved?
Write a function that you can pass as props to the Button component:
const function=()=>{
setState(!state)
}
<Button
icon="fa-light fa-trash"
text="Some text"
align="end"
bgcolor="yellow"
function={function}
></Button>
And use it inside like this:
const Button = ({ icon, text, align, bgcolor, function }) => {
const btnStyle = {
justifySelf: align,
};
return (
<>
<div onClick={function} className={"btn " + bgcolor} style={btnStyle}>
<i className={icon}></i> {text}
</div>
</>
);
};

Using state data as a props in other component conditionally

Here is my one component DISPLAY,
import React, { Component } from "react";
import ListGroup from "./listGroup";
class Display extends Component {
handleAllClick = (e) => {
e.preventDefault();
return (
<ListGroup
data={this.props.data}
onSubmitted={this.handleSubmit}
onClicked={this.handleAllCheck}
onChanged={this.handleChange}
onCheckChange={this.handleCheckChange}
onDeletion={this.handleDelete}
/>
);
};
handleCompleted = (e) => {
e.preventDefault();
return (
<ListGroup
data={this.props.data}
onSubmitted={this.handleSubmit}
onClicked={this.handleAllCheck}
onChanged={this.handleChange}
onCheckChange={this.handleCheckChange}
onDeletion={this.handleDelete}
/>
);
};
render() {
return (
<div>
<button
type="button"
className=" all btn btn-light p-1 mr-3"
onClick={this.handleAllClick}
>
All
</button>
<button
type="button"
className=" act btn btn-light p-1 mr-3"
onClick={this.handleActiveClick}
>
Active
</button>
<button
type="button"
className=" comp btn btn-light p-1"
onClick={this.handleCompleted}
>
Completed
</button>
</div>
);
}
}
export default Display;
Actually I want to use state which is named by data in the main component and that data contains a boolean variable called completed.
Now I wanted to conditionally use this completed as props with ListGroup component(Which I have imported above).
This should look like this:
handleCompleted = (e) => {
e.preventDefault();
(!this.props.completedItem &&
<ListGroup
data={this.props.data}
onSubmitted={this.handleSubmit}
onClicked={this.handleAllCheck}
onChanged={this.handleChange}
onCheckChange={this.handleCheckChange}
onDeletion={this.handleDelete}
/>
);
};
(I have passed this completed as completedItem in the main component).
But the problem is this is not working the right way!

onScroll when at the bottom of the div then disable button

Trying to achieve something similar to popular T&C's actions, when you scroll down to the very bottom of the div 'accept' button becomes enabled so you can click on it.
For this example, I am using material-UI components & button becomes disabled when I add 'disabled' within the component, that's all.
Here's the code:
import React from 'react';
import PropTypes from 'prop-types';
import Modal from '#material-ui/core/Modal';
import Button from '#material-ui/core/Button';
import './terms-popup.scss';
const handleScroll = (e) => {
const bottom = e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight;
if (bottom) { console.log('bottom is reached'); }
};
const TermsPopup= ({
isModalOpen,
isOpen,
title,
closeLabel,
showCloseButton,
}) => {
return (
<Modal
aria-labelledby="simple-modal-title"
aria-describedby="simple-modal-description"
open={isOpen}
onClose={() => isModalOpen(false)}
>
<div className="terms-container">
<div className="terms-header">
<h2>{title}</h2>
</div>
<div
className="terms-body"
onScroll={handleScroll}
>
<h3>Sample title</h3>
<p>Sample description would go here</p>
<div className="terms-footer">
{(showCloseButton) ? (
<Button
variant="contained"
type="button"
onClick={() => isModalOpen(false)}
>
{closeLabel}
</Button>
) : null}
</div>
</div>
</Modal>
);
};
TermsPopup.propTypes = {
isModalOpen: PropTypes.func.isRequired,
isOpen: PropTypes.bool.isRequired,
title: PropTypes.string.isRequired,
closeLabel: PropTypes.string,
showCloseButton: PropTypes.bool,
};
TermsPopup.defaultProps = {
closeLabel: 'accept',
showCloseButton: true,
};
export default TermsPopup;
And what I'd like to achieve is when the bottom is reached then Button should change to:
<Button
variant="contained"
type="button"
onClick={() => isModalOpen(false)}
disabled
/>
Move your onScroll event handler into the component itself, and use React state to update your component when the bottom is reached. You can use the state variable to then set the 'disabled' prop on your Button component.
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import Modal from '#material-ui/core/Modal';
import Button from '#material-ui/core/Button';
import './terms-popup.scss';
const TermsPopup= ({
isModalOpen,
isOpen,
title,
closeLabel,
showCloseButton,
}) => {
const [bottom, setBottom] = useState(false);
const handleScroll = (e) => {
const bottom = e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight;
setBottom(bottom)
};
return (
<Modal
aria-labelledby="simple-modal-title"
aria-describedby="simple-modal-description"
open={isOpen}
onClose={() => isModalOpen(false)}
>
<div className="terms-container">
<div className="terms-header">
<h2>{title}</h2>
</div>
<div
className="terms-body"
onScroll={handleScroll}
>
<h3>Sample title</h3>
<p>Sample description would go here</p>
<div className="terms-footer">
{(showCloseButton) ? (
<Button
variant="contained"
type="button"
onClick={() => isModalOpen(false)}
disabled={bottom}
>
{closeLabel}
</Button>
) : null}
</div>
</div>
</Modal>
);
};

Resources