Inline media queries using react - reactjs

I have this section of code I am trying to render. At different viewports, the button styles change from underline to background color using a themeColor I pull from a JSON file with acting as my database in a sense. How do you write an 'else if' inline in react so when my state is "active" and the media query matches, the styles update accordingly?
const btnMobileStyle = useMediaQuery('(min-width: 375px)')
const btnTabletStyle = useMediaQuery('(min-width: 768px)')
const styles = {
buttonMobile: (btnMobileStyle) => ({
borderBottom: `2px solid ${themeColor}`,
}),
buttonTablet: (btnTabletStyle) => ({
borderBottom: 'none',
backgroundColor: `${themeColor}`,
}),
}
return (
<main className="main">
<div className="main__btn-container">
<button
style={overviewActive ? styles.buttonMobile(btnMobileStyle) : {}}
onClick={updateOverview}
className="main__btn"
>
Overview
</button>
<button
style={structureActive ? styles.buttonMobile(btnMobileStyle) : {}}
onClick={updateStructure}
className="main__btn"
>
Structure
</button>
<button
style={surfaceActive ? styles.buttonMobile(btnMobileStyle) : {}}
onClick={updateSurface}
className="main__btn"
>
Surface
</button>
</div>

I was able to solve the issue using this code:
const btnMobileStyle = useMediaQuery('(max-width: 768px)')
const styles = {
btnMobile: (btnMobileStyle) => ({
borderBottom: btnMobileStyle ? `2px solid ${themeColor}` : {},
backgroundColor: btnMobileStyle ? 'transparent' : themeColor,
}),
}
return (
<main className="main">
<div className="main__btn-container">
<button
style={overviewActive ? styles.btnMobile(btnMobileStyle) : {}}
onClick={updateOverview}
className="main__btn"
>
Overview
</button>
<button
style={structureActive ? styles.btnMobile(btnMobileStyle) : {}}
onClick={updateStructure}
className="main__btn"
>
Structure
</button>
<button
style={surfaceActive ? styles.btnMobile(btnMobileStyle) : {}}
onClick={updateSurface}
className="main__btn"
>
Surface
</button>
</div>
<div className="main__img-container">
<img
className="main__planet-img"
src={state.planetImg}
alt={state.planetName}
/>
<img
className={
surfaceActive
? 'main__planet-overlay main__planet-overlay--active'
: 'main__planet-overlay'
}
src={state.planetImgOverlay}
alt=""
/>
</div>

Related

how to use compound pattern with dynamic imports nextjs

so i was trying to use dynamic imports in nextjs to import a component that uses compound pattern design(Dialog); but i get the following error while trying to use properties of Dialog:
TS2339: Property 'Title' does not exist on type 'ComponentType<{}>'.
  Property 'Title' does not exist on type 'ComponentClass<{}, any>'.
following is the Dialog component implementation that uses compound pattern:
const Dialog = ({children}) => (
<>
<NativeDialog
open={true}
PaperProps={{
sx: {
width: "700px",
borderRadius: "15px",
"#media only screen and (max-width: 720px)": {
width: "100vw",
height: "100vh",
},
},
}}
>
{children}
</NativeDialog>
</>
);
const DialogHeader = () => (
<div className={styles.headerContainer}>
<div style={{marginRight: "37%"}}>
<IconButton>
<Link href="/" passHref>
<CloseIcon/>
</Link>
</IconButton>
</div>
<div>
<TwitterLogo size="S"/>
</div>
</div>
);
const DialogTitle = ({children}) => (
<NativeDialogTitle>
<div className={styles.titleContainer}>{children}</div>
</NativeDialogTitle>
);
const DialogContent = ({children}) => (
<NativeDialogContent>
<div className={styles.formContainer}>{children}</div>
</NativeDialogContent>
);
const DialogActions = ({children}) => (
<NativeDialogActions>
<div className={styles.actionContainer}>{children}</div>
</NativeDialogActions>
);
Dialog.Header = DialogHeader;
Dialog.Title = DialogTitle;
Dialog.Content = DialogContent;
Dialog.Actions = DialogActions;
export default Dialog;
and here is the component that dynamically imports Dialog:
const Dialog = dynamic(
() => import('../../Dialog'),
{loading: () => <Spinner/>}
)
const Signin = ({signinState, setSigninState, handleSignin, setAuth}) =>
(
<>
<Dialog>
<Dialog.Title>
<Dialog.Header/>
<div className={`font font-eb ${styles.header}`}>
Sign in to Twitter
</div>
</Dialog.Title>
<Dialog.Content>
<div className={styles.contentContainer}>
<div className={styles.contentItem}>
<Button className={buttonStyles.whiteButton}>
<ThirdPartyAuth state="in" company="Google"/>
</Button>
</div>
<div className={styles.contentItem}>
<Button className={buttonStyles.whiteButton}>
<ThirdPartyAuth state="in" company="Apple"/>
</Button>
</div>
<div className={styles.contentItem}>
<Divider orientation="horizontal" text="or" width={300}/>
</div>
<div className={styles.contentItem}>
<TextField
sx={{width: 300}}
label={<div className="font">email</div>}
value={signinState.email}
onChange={e => setSigninState(prevState => {
return {...prevState, email: e.target.value}
})}
/>
</div>
<div className={styles.contentItem}>
<TextField
sx={{width: 300}}
label={<div className="font">password</div>}
value={signinState.password}
onChange={e => setSigninState(prevState => {
return {...prevState, password: e.target.value}
})}
/>
</div>
{(signinState.clientError || signinState.serverError) && <div className={styles.contentItem}>
<div className={styles.error + " font font-m"}>
{signinState.clientError ? signinState.clientError : signinState.serverError ? signinState.serverError : ""}
</div>
</div>}
<div className={styles.contentItem}
onClick={() => handleSignin(signinState, setSigninState, setAuth)}>
<Button
className={`
${buttonStyles.blackButton} font font-eb`}
>
Signin
</Button>
</div>
<div className={styles.contentItem}>
<Button
className={`
${buttonStyles.whiteButton} font font-b`}
>
Forgot password?
</Button>
</div>
<div
className={`${
styles.contentItem + " " + styles.help
} font font-m`}
>
Don&apos;t have an account?
<div style={{display: "inline-block"}}>
<Link href="/signup" passHref>
<div className={styles.signup}>Sign up</div>
</Link>
</div>
</div>
</div>
</Dialog.Content>
</Dialog>
</>
);
export default Signin;
using Dialog with regular import expression doesnt create this problem but dynamically importing it does. any thoughts is appreciated.

google react-places-autocomplete api restrict search suggestions to Australia / single country

Hello I want to limit my search suggestions (and therefore limit a user to choose an Australian address) with react places autocomplete to addresses from Australia, and to note I have checked stack overflow for similar questions and none of them seem to work, here is an example of the suggestions I am currently getting that i would like to be only Australian Suggestions.
screen shot
here is what i think is the relevant code
import PlacesAutocomplete, {
geocodeByAddress,
getLatLng,
} from 'react-places-autocomplete';
<PlacesAutocomplete
fullWidth
className="search-bar"
value={address}
onChange={setAddress}
onSelect={handleSelect}
>
{({ getInputProps, suggestions, getSuggestionItemProps, loading }) => (
<div >
<input
style={{width:"100%" }}
{...getInputProps({
placeholder: 'Enter Job Address',
className: 'location-search-input',
})}
/>
<div className="autocomplete-dropdown-container"
key={suggestions.description}
>
{loading && <div>Loading...</div>}
{suggestions.map((suggestion, index)=> {
const className = suggestion.active
? 'suggestion-item--active'
: 'suggestion-item';
// inline style for demonstration purpose
const style = suggestion.active
? { backgroundColor: '#fafafa', cursor: 'pointer' }
: { backgroundColor: '#ffffff', cursor: 'pointer' };
return (
<div
key={index}
{...getSuggestionItemProps(suggestion, {
className,
style,
})}
>
<span>{suggestion.description }</span>
</div>
);
})}
</div>
</div>
)}
</PlacesAutocomplete>
try with this prop
searchOptions={{componentRestrictions: { country: ['au'] }}}

How to make links work with card-img-overlay React

I'm having an issue on my project. I created a card-img-overlay to display icons over an image. If you click on the entire image you are redirected to a post. I would like to make the like and share icons clickable.
My project is in Reactjs. I am displaying images and videos from Reddit API.
Thank you for your help.
id,
slugTitle,
title,
url_overridden_by_dest,
author,
preview,
}) => {
const [isVideo, setIsVideo] = useState(false);
useEffect(() => {
if (preview) setIsVideo(preview.split('.').pop() === 'mp4');
}, [preview]);
const history = useHistory();
const goToPage = () => {
history.push(`/Post/${id}/${slugTitle}`);
};
return (
<Card
inverse
onClick={goToPage}
style={{
cursor: 'pointer',
}}
>
{isVideo && (
<video autoPlay="false" loop width="100%" src={preview}>
<track default kind="captions" />
</video>
)}
{!isVideo && (
<CardImg top width="100%" src={url_overridden_by_dest} alt={title} />
)}
<CardImgOverlay className="hideinfos">
<CardText className="w-100 d-flex justify-content-between">
<div>
<VscAccount className="mr-2" size={20} />
{author}
</div>
<div>
<LikeButtonhp
className="mr-2 card-link"
size={20}
style={{
position: 'relative',
}}
/>
<BiShareAlt size={20} />
</div>
</CardText>
</CardImgOverlay>
</Card>
);
};
You'll need to put onClick handlers on your LikeButtonhp and BiShareAlt components, and use event.stopPropagation() to stop the event from bubbling up to the <Card />:
<BiShareAlt
size={20}
onClick={event => {
event.stopPropagation();
// Do stuff for share click
}}
/>
You may need to alter the BiShareAlt and LikeButtonhp components to support an onClick prop also, for example if they render a <button> element it may look like this:
const BiShareAlt = ({ onClick }) => (
<button onClick={onClick}>
Share
</button>
);
export default BiShareAlt;
In my onClick, I added an e.stopPropagation(); and it solves my problem. Now I can click on the heart icon and it works. It stops the onClick set up on my image (parent).
function LikeButtonhp() {
const [liked, setLiked] = useState(false);
return (
<Button
outline
color="link"
className="likebutton"
onClick={(e) => {
e.stopPropagation();
setLiked(!liked);
}}
style={{ color: 'white' }}
>
{liked ? <BsHeartFill size={20} /> : <BsHeart size={20} />}
</Button>
);
}

Using react-multi-carousel but unable to use custom dots

How I can get clickable custom dots of this carousel.
I cannot bind a click event in the list item to move the carousel. I need a proper implementation between onClick and the li item to click on prev and next items in carousel
Here is the full code in the link codebox
const CustomDot = ({onMove,index,onClick,active}) => {
return (
<ol class="carousel-indicators">
<li data-target="#main-slide" data-slide-to="0" className={active ? "active" : "inactive"}
>How t bind the click event list item
onClick={() => onClick()}>{React.Children.slide1}</li>
<li data-target="#main-slide" data-slide-to="1" className={active ? "active" : "inactive"}
onClick={() => onClick()}>{React.Children.slide2} </li>
<li data-target="#main-slide" data-slide-to="2" className={active ? "active" : "inactive"}
onClick={() => onClick()}>{React.Children.slide3} </li>
</ol>
);
};
The problem is that the plugin expects to receive a single element (li for example) as CusomtDot but you pass a list (ol with some children).
The solution, pass a single element, like this:
const CustomDot = ({ onMove, index, onClick, active }) => {
// onMove means if dragging or swiping in progress.
// active is provided by this lib for checking if the item is active or not.
return (
<li
className={active ? "active" : "inactive"}
onClick={() => onClick()}
>
{index + 1}
</li>
);
};
Working demo: https://codesandbox.io/s/react-multi-carousel-customdot-jwkfo
const CustomDot = ({ onMove, index, onClick, active }) => {
return (
<li
className={active ? "active" : "inactive"}
onClick={() => onClick()}
>
<MaximizeIcon />
</li>
);
};
const arrowStyle = {
background: "transparent",
border: 0,
color: "#fff",
fontSize: "80px"
};
const CustomRight = ({ onClick }) => (
<button className="arrow right" onClick={onClick} style={arrowStyle}>
<ArrowForwardIcon style={{ fontSize: "50px" }} />
</button>
);
const CustomLeft = ({ onClick }) => (
<button className="arrow left" onClick={onClick} style={arrowStyle}>
<ArrowBackIcon style={{ fontSize: "50px" }} />
</button>
);
Working demo : https://codesandbox.io/s/react-multi-carousel-customdot-3q0f4?file=/src/App.js:683-1052

I want to add a "download button on modal carousel using react-images, the button should be available on modal popup

can i add a button as a parameter to the carousel,i tried all possible ways to add a button to this
<div>
<h2>Using with a Lightbox component</h2>
<Gallery photos={photos} onClick={openLightbox} />
<ModalGateway>
{viewerIsOpen ? (
<Modal onClose={closeLightbox}>
<Carousel
currentIndex={currentImage}
views={photos.map(x => ({
...x,
srcset: x.srcSet,
caption: x.title
}))}
/>
</Modal>
) : null}
</ModalGateway>
<div>
<Down />
</div>
</div>
You can create your own component:
const CustomFooter = ({ isModal, currentView }) => isModal && (
<div className="react-images__footer">
<button
className="btn some-stylin"
type="button"
onClick={() => { downloadImage(currentView.src); }}
>
Download
</button>
</div>
);
<ModalGateway>
{modalIsOpen ? (
<Modal onClose={this.toggleGallery}>
<Carousel
views={this.imgs}
currentIndex={currentIndex}
frameProps={{ autoSize: 'height' }}
components={{ Footer: CustomFooter }}
/>
</Modal>
) : null}
</ModalGateway>

Resources