How to load a <div> on top of React google map component? - css

I am trying to display a small card basically over the google maps component that this https://www.npmjs.com/package/#react-google-maps/api package provides. I set the zIndex to 2 but the <div> still shows below this map component.
The card element is not supposed to respond to any click it's just displayed on first load with the Map component
return (
<div>
<GoogleMap
mapContainerStyle={mapContainerStyle}
zoom={8}
center={center}
></GoogleMap>
<Tracking style={{zIndex: 2}}/>
</div>
);
Tracking is the div that contains information.
Map container style is:
const mapContainerStyle = {
width: "100vw",
height: "100vh",
};

Actually, there are a lot of ways to do it.
Briefly:
You have to add position: 'relative' to the parent component.
You have to add position: 'absolute' and zIndex: 0 to the child component that you want to display as the background.
You have to add position: 'absolute' and zIndex: 1 (or more) to the child component that you want to display as the foreground.
If you use inline styling, you could do this:
Adding top: some_number, left: some_number, right: some_number, or bottom: some_number is optional (depends on what position you want)
Here is the working code sandbox https://codesandbox.io/s/compassionate-rhodes-qbm43
App.js
import "./styles.css";
import Map from "./Map.js";
import Tracking from "./Tracking";
export default function App() {
return (
<div
style={{
position: "absolute",
zIndex: 0,
width: "100%", // or you can use width: '100vw'
height: "100%" // or you can use height: '100vh'
}}
>
<Map />
<div
style={{
zIndex: 1,
position: "absolute",
top: 10,
left: 10,
backgroundColor: "white", // you can use any color value
width: "10%", // or you can use width: any_number
height: "90%" // or you can use height: any_number
}}
>
<Tracking />
</div>
</div>
);
}
Map.js
import React from "react";
import {
GoogleMap,
useLoadScript,
Marker,
InfoWindow
} from "#react-google-maps/api";
import Tracking from "./Tracking";
const libraries = ["places"];
const mapContainerStyle = {
position: "relative",
width: "100%",
height: "100%"
};
const center = {
lat: -33.86882,
lng: 151.20929
};
function Map() {
const { isLoaded, loadError } = useLoadScript({
googleMapsApiKey: process.env.REACT_APP_GOOGLE_MAPS_API_KEY,
libraries
});
if (loadError) return "Error loading maps";
if (!isLoaded) return "Loading Maps";
return (
<div
style={{
position: "absolute",
zIndex: 0,
width: "100%", // or you can use width: '100vw'
height: "100%" // or you can use height: '100vh'
}}
>
<GoogleMap
mapContainerStyle={mapContainerStyle}
zoom={8}
center={center}
></GoogleMap>
</div>
);
}
export default Map;
Tracking.js
import React from "react";
const Tracking = () => {
return (
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
);
};
export default Tracking;

Related

Square popup is coming while refreshing the page or switching the router with loading gif

Whenever we are loading the page or switching the router then we are
showing the loader(loading gif ) in web page application so loading
gif is coming with shadow popup . Not able to understand how to remove
it and how to handle it?
Chrome Version 89.0.4389.114 (Official Build)(64-bit)
Loading Gif Details:
Dimension: 193X 192
Width/Height: 193/192 pixels
Bit Depth: 8
Item Type: GIF
File Size: 170KB
import React from 'react';
import loadingGif from '../../img/loader.gif';
const Loader = (props) => {
return (
props.showLoader ? <div className="modal-backdrop modal-backdrop-loader" style={{ paddingTop: '0'}}>
<img src={loadingGif} alt="Loading. Please wait!" style={{ top: '50%', left: '50%', transform: 'translate(-50%, -50%)', position: 'fixed', height: '75px', width: '75px'}} />
</div> : ""
);
}
export default Loader;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
The modal is coming from the div around the img:
<div className="modal-backdrop modal-backdrop-loader" style={{ paddingTop: '0'}}>
If you have the Bootstrap CSS file loaded then the modal-backdrop class has some styles attached to it:
.modal-backdrop {
position: fixed;
top: 0;
left: 0;
z-index: 1040;
width: 100vw;
height: 100vh;
background-color: #000;
}
That creates a black background that appears on top of other content and is the full width and full height of the window.
Bootstrap Modal Docs
It's up to you how you want to handle this. Do you sometimes want to show the backdrop? Should showBackdrop be a prop? How do you want to style the container when there is no backdrop?
// some random gif to use as a placeholder
const loadingGif =
"https://icon-library.net/images/spinner-icon-gif/spinner-icon-gif-28.jpg";
// just the spinner
const Loader = ({ showLoader = true }) => {
return showLoader ? (
<img
src={loadingGif}
alt="Loading. Please wait!"
style={{
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
position: "fixed",
height: "75px",
width: "75px"
}}
/>
) : null;
};
ReactDOM.render(<Loader showLoader={true} />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

Material-UI style buttons on the right

How do you align buttons on the right using Material-UI's makeStyles function?
I have tried using CSS's margin-right: 0 tag, but there is an error using '-' with makeStyles.
I renamed it as 'marginRight' and it still does not work. Also mr: 0 is not valid either. (Using Material-UI's spacing).
The code is trying to make the UI similar to stackOverflow's title layout.
import React from 'react';
import { makeStyles } from "#material-ui/core/styles";
import { Box, Button } from "#material-ui/core";
const style = makeStyles({
titleItemRight: {
color: 'white',
backgroundColor: 'blue',
top: '50%',
height: 30,
align: 'right',
position: 'relative',
transform: 'translateY(-50%)',
}
});
const App = () => {
const classes = style();
return (
<div>
<Box className={classes.titleBar}>
<Button variant='text' className={classes.titleItemRight}>Sign In</Button>
</Box>
</div>
);
};
Change,
align: 'right'
To,
float: 'right'
So the code would look like,
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import { Box, Button } from "#material-ui/core";
const style = makeStyles({
titleItemRight: {
color: "white",
backgroundColor: "blue",
top: "50%",
height: 30,
float: "right",
position: "relative",
transform: "translateY(-50%)"
}
});
const App = () => {
const classes = style();
return (
<div>
<Box className={classes.titleBar}>
<Button variant="text" className={classes.titleItemRight}>
Sign In
</Button>
</Box>
</div>
);
};
Working Codesandbox
I'd suggest using a flexbox for this or just using the AppBar provided already by material ui
https://material-ui.com/components/app-bar/#app-bar
if you'd still like to use Box, just edit the titleBar styles this way and add a spacer element to seperate elements to far right or far left
const style = makeStyles({
titleBar: {
display: 'flex',
width:'100%',
flexFlow: 'row',
},
spacer: {
flex: '1 1 auto'
}
});
and then your component
<Box className={classes.titleBar}>
<LogoHere/>
<div className={classes.spacer}/>
<Button variant="text">
Sign In
</Button>
</Box>

How To Transition React Elements That Already Have An Animation

I need some help merging an animation and a transition that occur at different times using react-spring.
When the component is mounted using useSpring I make the component appear, but I would like it so that when the button child is removed the container transitions smoothly upwards. Theres more details in the example.
https://codesandbox.io/s/naughty-dawn-r0e5x
PLEASE HELP :)
I created a solution. There is a new Spring animation mainly for the button, but we can use part of it for the original div. In this case the maxHeight property. So this way you can combine the animations.
import ReactDOM from "react-dom";
import React, { useState } from "react";
import { useSpring, animated } from "react-spring";
function WelcomeMessage() {
const [isStarted, setIsStarted] = useState(false);
const animation1 = useSpring({
right: "1%",
opacity: 1,
from: { right: "-20%", opacity: 0 },
delay: 300
});
const animation2 = useSpring({
opacity: isStarted ? 0 : 1,
maxHeight: isStarted ? 150 : 250,
transform: `translateY(${isStarted ? "-50px" : "0px"})`
});
return (
<animated.div
style={{
backgroundColor: "pink",
position: "fixed",
top: "2%",
right: "1%",
cursor: "default",
borderRadius: 5,
width: "90%",
maxWidth: 400,
...animation1,
maxHeight: animation2.maxHeight
}}
>
<div style={{ padding: "15px 25px" }}>
<span>
This is a toaster notification component. When you click the following
button it should disappear and the pink container's bottom section
should transition upwards when it decreases in height.
</span>
<br />
<br />
{
<animated.button
style={{ width: "100%", fontSize: 48, ...animation2 }}
onClick={() => {
setIsStarted(true);
}}
>
Get Started
</animated.button>
}
</div>
</animated.div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<WelcomeMessage />, rootElement);
Here is the example: https://codesandbox.io/s/green-star-nzfj8
Is this answer your question?

Create a fixed footer in Ant Design

I want to accomplish something like this with a fixed footer.
--------------------------------
| HEADER |
--------------------------------
| | |
| | |
| SIDE | CONTENT |
| BAR | |
| | |
--------------------------------
| FOOTER |
--------------------------------
I'm using Ant Design layout and I added a position fixed to the footer but I'm getting a weird bug when the sidebar groups are expanded that block some of the sidebar links.
Here is an example:
https://codepen.io/HugoLiconV/pen/jONBLJQ?editors=0010
EDIT:
The main problem is that if I expand the sidebar group that says "expand me" because there is a lot of links, the footer will hide some of the links. This happens especially a 1366 x 768 resolution
Here is a codepen link https://codepen.io/sniperdumbooo/pen/pozeZJb?editors=0010 for how you wanted to achieve with a fixed footer and centered content and sidebar big fixed
The changes made are removed display: 'flex' from footer and added textAlign: 'center' to the header part and added textAlign: 'justify' to the center body as:
<Header style={{ background: '#DDD', padding: 0, textAlign: 'center' }} >Header</Header>
<Footer style={{
borderTop: '1px solid #e8e8e8',
position: 'fixed',
left: 0,
bottom: 0,
width: '100%',
backgroundColor: 'white',
textAlign: 'center'}} >
textAlign: 'justify' ### to the div inside the <Content />
And the blocked sidebar links when expanded is due to the position: 'fixed' in the
<Layout style={{
overflow: 'auto',
minHeight: '100vw', ## make it minHeight: '100vw'
width: '100%',
left: 0 ### Removed the position: 'fixed'
}}>
Also remove style="padding: 24px" from div in html id="container"
<div id="container"></div>
will give you what you want to achieve with the bug fixed as in this link of codepen : Desired solution
You can add margin bottom same as footer height in px , please add below css and check,
.ant-layout-sider-children {
height: 100%;
margin-top: -0.1px;
padding-top: 0.1px;
margin-bottom: 95px;
}
hi i red all answers and i think there is an easier way for only using antd instead of using extra css
just use this kind of affix around your footer :
<Affix offsetBottom={0}>
and the document if you need :
https://ant.design/components/affix/
Unfortunately this can not (easily) be solved with CSS, and your best bet is to simply move the footer out one level, to the bottom of the parent <Layout>:
<Layout> <!-- Core layout -->
<Layout> <!-- Layout with Header and Content -->
</Layout>
<Footer style={{
borderTop: '1px solid #e8e8e8',
position: 'fixed',
left: 0,
bottom: 0,
width: '100%',
backgroundColor: 'white',
textAlign: 'center',
display: 'flex',}}
>
Ant Design ©2018 Created by Ant UED
</Footer>
</Layout>
This can be seen here.
Here, i have made a little change in your footer css and it looks fine.
<Footer style={{
borderTop: '1px solid #e8e8e8',
position: 'fixed',
left: 0,
bottom: 0,
width: '100%',
backgroundColor: 'white',
textAlign: 'center',}}
>
Hope this helps !
I had figured out a hack solution
import React, { FC, useEffect, useMemo } from 'react';
import { Table } from 'antd';
import { useMeasure } from 'react-use';
...
export const App: FC = () => {
const dataSource = ...
const columns = ...
...
/** Render */
const [ref, { height }] = useMeasure<HTMLDivElement>();
const tableTitleRef = useRef<Element>();
const tableHeaderRef = useRef<Element>();
const tableBodyRef = useRef<Element>();
const tableFooterRef = useRef<Element>();
const refs = (e: HTMLDivElement) => {
ref(e);
// 攔截 ref 呼叫,找到 title, header, body, footer element
tableTitleRef.current = e?.getElementsByClassName('ant-table-title')?.[0];
tableHeaderRef.current = e?.getElementsByClassName('ant-table-header')?.[0];
tableBodyRef.current = e?.getElementsByClassName('ant-table-body')?.[0];
tableFooterRef.current = e?.getElementsByClassName('ant-table-footer')?.[0];
};
/** 38、48 分別是沒有換行時的基本高度,當作 element 找不到時的 fallback default 值 */
const scroll = useMemo(() => {
return {
x: 1200,
y:
height -
(tableTitleRef.current?.clientHeight || 38) -
(tableHeaderRef.current?.clientHeight || 38) -
(tableFooterRef.current?.clientHeight || 48)
}
}, [height]);
useEffect(() => {
if (scroll) {
// 直接指定高度
if (tableBodyRef.current) tableBodyRef.current.style = `overflow: auto scroll; height: ${scroll.y}px;`;
}
}, [height, scroll]);
return (
<div ref={refs} style={{ height: '100%' }}>
<Table
scroll={scroll}
sticky={true}
columns={columns}
dataSource={dataSource}
pagination={false}
showHeader={true}
...
title={() => (
<TaskTableTitle
...
/>
)}
footer={() => (
<TaskTableFooter
...
/>
)}
/>
</div>
);
}

Make Images Fit and Resize properly

Been trying to fix this myself for awhile and a bit stuck. I can't make these images display properly - Keeping there position in mobile and desktop view.
Probably easy to solve for most people. I am trying to re-make this website, http://www.milkbardigital.com.au/ , as a lesson in web page design .
here's my code
/**
* Created by Hewlbern on 21-Jan-17.
*/
import React from 'react';
import {Image} from 'react-bootstrap';
export default React.createClass ( {
render() {
var background = {
backgroundSize : 'cover'
};
var heroStyle = {
width:'auto',
overflow : 'hidden',
};
var ontop = {
position: 'relative',
display: 'block',
margin: 'auto',
width: '50%',
bottom: '30%',
left: '0',
right: '0',
};
return (
<div style={heroStyle}>
<Image
style={background} responsive
src="http://www.milkbardigital.com.au/wp-content/uploads/2015/11/Milkbar-Home-Background.jpg">
</Image>
<Image
style={ontop} responsive
src="http://www.milkbardigital.com.au/wp-content/uploads/2015/11/Milkbar-Digital-Media.png">
</Image>
</div>
);
}
});

Resources