Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
hide home sidebar on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
ceolinwill committed Jun 22, 2020
1 parent 231711a commit a295ae8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
24 changes: 24 additions & 0 deletions src/components/HomeSidebar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { makeStyles } from '@material-ui/core';
import LanguageFilter from './LanguageFilter';
import MenuPages from './MenuPages';
import SocialLinks from './SocialLinks';

const useStyles = makeStyles((theme) => ({
filter: { width: 'auto', margin: theme.spacing(2, 0) },
}));

const HomeSidebar = () => {
const classes = useStyles();

return (
<aside>
<MenuPages />
<SocialLinks />
<div className={classes.filter}>
<LanguageFilter />
</div>
</aside>
);
};

export default HomeSidebar;
22 changes: 9 additions & 13 deletions src/components/SidebarPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Grid, Hidden, makeStyles } from '@material-ui/core';
import { Grid, makeStyles } from '@material-ui/core';
import { Post } from '@zoonk/models';
import LanguageFilter from './LanguageFilter';
import MenuPages from './MenuPages';
import HomeSidebar from './HomeSidebar';
import PostShare from './PostShare';
import SocialLinks from './SocialLinks';
import useIsMobile from './useIsMobile';

const useStyles = makeStyles((theme) => ({
container: {
Expand All @@ -28,18 +27,15 @@ interface SidebarPageProps {

const SidebarPage = ({ category, children, title }: SidebarPageProps) => {
const classes = useStyles();
const isMobile = useIsMobile();

return (
<Grid container spacing={2} className={classes.container}>
<Grid item sm={3} xs={12}>
<Hidden implementation="css" xsDown>
<MenuPages />
<SocialLinks />
<div className={classes.filter}>
<LanguageFilter />
</div>
</Hidden>
</Grid>
{!isMobile && (
<Grid item sm={3} xs={12}>
<HomeSidebar />
</Grid>
)}
<Grid item xs={12} sm={9} className={classes.column}>
<PostShare category={category} title={title} />
{children}
Expand Down
7 changes: 7 additions & 0 deletions src/components/useIsMobile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import useMediaQuery from './useMediaQuery';

const useIsMobile = () => {
return useMediaQuery(640);
};

export default useIsMobile;
33 changes: 33 additions & 0 deletions src/components/useMediaQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useCallback, useEffect } from 'react';

/**
* Check if a media query meets its target.
* Forked from https://github.com/vercel/next-site/blob/master/components/media-query.js
*/
const useMediaQuery = (width: string | number) => {
const [targetReached, setTargetReached] = useState(false);

const updateTarget = useCallback((e) => {
if (e.matches) {
setTargetReached(true);
} else {
setTargetReached(false);
}
}, []);

useEffect(() => {
const media = window.matchMedia(`(max-width: ${width}px)`);
media.addListener(updateTarget);

// Check on mount (callback is not called until a change occurs)
if (media.matches) {
setTargetReached(true);
}

return () => media.removeListener(updateTarget);
}, [updateTarget, width]);

return targetReached;
};

export default useMediaQuery;

0 comments on commit a295ae8

Please sign in to comment.