Skip to content

Commit feaf570

Browse files
author
Vamshi Maddur
committed
Added tabs to display different maps
1 parent 2dc808c commit feaf570

File tree

8 files changed

+106
-36
lines changed

8 files changed

+106
-36
lines changed

src/App.jsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
CssBaseline,
99
useMediaQuery,
1010
} from "@material-ui/core";
11+
import TabContext from "@material-ui/lab/TabContext";
1112
import Layout from "./components/Layout";
1213
import Landing from "./pages/Landing";
1314

@@ -18,29 +19,32 @@ function App() {
1819
]);
1920

2021
const [currentTheme, setCurrentTheme] = useState("light");
21-
const [currentMap, setCurrentMap] = useState("google");
22+
const [currentTab, setCurrentTab] = React.useState(1);
23+
24+
const handleTabChange = (event, newValue) => {
25+
setCurrentTab(newValue);
26+
};
2227

2328
const handleThemeChange = () => {
2429
if (currentTheme === "light") setCurrentTheme("dark");
2530
else setCurrentTheme("light");
2631
};
2732

28-
const handleMapsRendererChange = () => {
29-
setCurrentMap(currentMap === "google" ? "mapbox" : "google");
30-
};
31-
3233
theme = React.useMemo(() => myTheme(currentTheme), [currentTheme]);
3334
return (
3435
<ThemeProvider theme={theme}>
3536
<StylesProvider injectFirst>
3637
<MuiThemeProvider theme={theme}>
3738
<CssBaseline />
38-
<Layout
39-
handleThemeChange={handleThemeChange}
40-
handleMapsRendererChange={handleMapsRendererChange}
41-
>
42-
<Landing currentTheme={currentTheme} currentMap={currentMap} />
43-
</Layout>
39+
<TabContext value={currentTab}>
40+
<Layout
41+
currentTab={currentTab}
42+
handleThemeChange={handleThemeChange}
43+
handleTabChange={handleTabChange}
44+
>
45+
<Landing currentTheme={currentTheme} />
46+
</Layout>
47+
</TabContext>
4448
</MuiThemeProvider>
4549
</StylesProvider>
4650
</ThemeProvider>

src/components/GoogleMap.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import styled from "styled-components";
44

55
const MapContainer = styled.div`
66
position: absolute;
7-
top: 0;
7+
top: 64px;
88
bottom: 0;
99
left: 0;
1010
right: 0;
1111
font-family: Nunito;
12+
height: 75%;
13+
margin: 12px;
14+
border-radius: 8px;
15+
border: 2px solid #fff;
1216
`;
1317

1418
function GoogleMap({ options, onMount, className, onMountProps }) {

src/components/Layout/Header.jsx

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from "react";
22
import styled from "styled-components";
3-
import { AppBar, Toolbar } from "@material-ui/core";
3+
import { AppBar, Toolbar, withStyles, Tabs, Tab } from "@material-ui/core";
44
import ThemeSwitch from "components/ThemeSwitch";
5-
import MapSwitch from "components/MapSwitch";
65

76
const AppTitle = styled.h3`
87
font-weight: 700;
@@ -17,17 +16,55 @@ const ToolbarControls = styled.div`
1716
align-items: center;
1817
`;
1918

19+
const StyledTabs = withStyles({
20+
indicator: {
21+
display: "flex",
22+
justifyContent: "center",
23+
backgroundColor: "transparent",
24+
height: 4,
25+
"& > span": {
26+
maxWidth: 40,
27+
width: "100%",
28+
backgroundColor: "#fff",
29+
borderRadius: 8,
30+
},
31+
},
32+
})((props) => <Tabs {...props} TabIndicatorProps={{ children: <span /> }} />);
33+
34+
const StyledTab = withStyles((theme) => ({
35+
root: {
36+
textTransform: "none",
37+
color: "#fff",
38+
fontWeight: theme.typography.fontWeightRegular,
39+
fontSize: theme.typography.pxToRem(12),
40+
marginRight: theme.spacing(1),
41+
minWidth: 80,
42+
"&:focus": {
43+
opacity: 1,
44+
},
45+
},
46+
}))((props) => <Tab disableRipple {...props} />);
47+
2048
export default function Header({
49+
currentTab,
2150
handleThemeChange,
22-
handleMapsRendererChange,
51+
handleTabChange,
2352
}) {
2453
return (
2554
<AppBar position="fixed">
2655
<Toolbar>
27-
<AppTitle>Navigator</AppTitle>
56+
<AppTitle>Implement Maps in React</AppTitle>
2857
<Grow />
2958
<ToolbarControls>
30-
<MapSwitch handleMapsRendererChange={handleMapsRendererChange} />
59+
<StyledTabs
60+
value={currentTab}
61+
onChange={handleTabChange}
62+
aria-label="styled tabs example"
63+
>
64+
<StyledTab label="Google Maps API" />
65+
<StyledTab label="MapBox GL" />
66+
<StyledTab label="@react-google-maps" />
67+
</StyledTabs>
3168
<ThemeSwitch handleThemeChange={handleThemeChange} />
3269
</ToolbarControls>
3370
</Toolbar>

src/components/Layout/index.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ const Container = styled.div`
1111
`;
1212

1313
export default function Layout({
14+
currentTab,
1415
children,
1516
handleThemeChange,
16-
handleMapsRendererChange,
17+
handleTabChange,
1718
}) {
1819
return (
1920
<Container>
2021
<Header
22+
currentTab={currentTab}
2123
handleThemeChange={handleThemeChange}
22-
handleMapsRendererChange={handleMapsRendererChange}
24+
handleTabChange={handleTabChange}
2325
/>
2426
<Content>{children}</Content>
2527
</Container>

src/components/Map.jsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import React from "react";
2-
import {
3-
GoogleMap,
4-
useLoadScript
5-
} from "@react-google-maps/api";
2+
import { GoogleMap, useLoadScript } from "@react-google-maps/api";
3+
import styled from "styled-components";
64

75
const libraries = ["places"];
86

7+
const MapContainer = styled.div`
8+
position: absolute;
9+
top: 64px;
10+
bottom: 0;
11+
left: 0;
12+
right: 0;
13+
font-family: Nunito;
14+
height: 75%;
15+
margin: 12px;
16+
border-radius: 8px;
17+
border: 2px solid #fff;
18+
`;
19+
920
const mapContainerStyle = {
10-
width: "100vw",
11-
height: "100vh",
21+
width: "inherit",
22+
height: "100%",
23+
borderRadius: 8,
1224
};
1325

1426
const center = {
@@ -26,10 +38,12 @@ export default function Map() {
2638
if (!isLoaded) return <div>Loading...</div>;
2739

2840
return (
29-
<GoogleMap
30-
mapContainerStyle={mapContainerStyle}
31-
zoom={15}
32-
center={center}
33-
></GoogleMap>
41+
<MapContainer>
42+
<GoogleMap
43+
mapContainerStyle={mapContainerStyle}
44+
zoom={15}
45+
center={center}
46+
></GoogleMap>
47+
</MapContainer>
3448
);
3549
}

src/components/MapBox.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ const defaultLocation = {
1414

1515
const MapContainer = styled.div`
1616
position: absolute;
17-
top: 0;
17+
top: 64px;
1818
bottom: 0;
1919
left: 0;
2020
right: 0;
2121
font-family: Nunito;
22+
height: 75%;
23+
margin: 12px;
24+
border-radius: 8px;
25+
border: 2px solid #fff;
2226
`;
2327

2428
export default function MapBox({ currentTheme }) {

src/components/ThemeSwitch/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import "./style.scss";
33

44
export default function ThemeSwitch({ handleThemeChange }) {
55
return (
6-
<div style={{ display: "flex", alignItems: "center", marginRight: 12 }}>
6+
<div style={{ display: "flex", alignItems: "center", margin: '0 12px 0 24px' }}>
77
<input
88
className="switch-input"
99
id="theme-switch"

src/pages/Landing/index.jsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import React from "react";
2+
import TabPanel from "@material-ui/lab/TabPanel";
23
import Map from "components/Map";
34
import MapBox from "components/MapBox";
45
import GoogleMap from "components/GoogleMap";
56

6-
export default function Landing({ currentTheme, currentMap }) {
7+
export default function Landing({ currentTheme }) {
78
return (
89
<div>
9-
{currentMap === "google" ? (
10+
<TabPanel value={0}>
1011
<GoogleMap />
11-
) : (
12+
</TabPanel>
13+
<TabPanel value={1}>
1214
<MapBox currentTheme={currentTheme} />
13-
)}
15+
</TabPanel>
16+
<TabPanel value={2}>
17+
<Map />
18+
</TabPanel>
1419
</div>
1520
);
1621
}

0 commit comments

Comments
 (0)