Skip to content

Commit

Permalink
Merge pull request #33 from nickovchinnikov/login
Browse files Browse the repository at this point in the history
Fix layout and Themes
  • Loading branch information
nickovchinnikov authored May 2, 2022
2 parents 8099418 + 373ea9e commit 82d94d7
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 157 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,9 @@
### React hook form, validation and tests

[pull request](https://github.com/nickovchinnikov/coursesbox/pull/32)

### Fix styles, add login button

### Theme toggle flicker fix

[pull request](https://github.com/nickovchinnikov/coursesbox/pull/33)
20 changes: 19 additions & 1 deletion frontend/components/Icon/Icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,22 @@ const Search = (props: React.SVGProps<SVGSVGElement>) => (
</svg>
);

export const Icons = { User, Moon, Sun, Home, Settings, Search };
const Login = (props: React.SVGProps<SVGSVGElement>) => (
<svg
viewBox="64 64 896 896"
focusable="false"
data-icon="login"
width="1em"
height="1em"
fill="currentColor"
aria-hidden="true"
{...props}
>
<defs>
<style></style>
</defs>
<path d="M521.7 82c-152.5-.4-286.7 78.5-363.4 197.7-3.4 5.3.4 12.3 6.7 12.3h70.3c4.8 0 9.3-2.1 12.3-5.8 7-8.5 14.5-16.7 22.4-24.5 32.6-32.5 70.5-58.1 112.7-75.9 43.6-18.4 90-27.8 137.9-27.8 47.9 0 94.3 9.3 137.9 27.8 42.2 17.8 80.1 43.4 112.7 75.9 32.6 32.5 58.1 70.4 76 112.5C865.7 417.8 875 464.1 875 512c0 47.9-9.4 94.2-27.8 137.8-17.8 42.1-43.4 80-76 112.5s-70.5 58.1-112.7 75.9A352.8 352.8 0 01520.6 866c-47.9 0-94.3-9.4-137.9-27.8A353.84 353.84 0 01270 762.3c-7.9-7.9-15.3-16.1-22.4-24.5-3-3.7-7.6-5.8-12.3-5.8H165c-6.3 0-10.2 7-6.7 12.3C234.9 863.2 368.5 942 520.6 942c236.2 0 428-190.1 430.4-425.6C953.4 277.1 761.3 82.6 521.7 82zM395.02 624v-76h-314c-4.4 0-8-3.6-8-8v-56c0-4.4 3.6-8 8-8h314v-76c0-6.7 7.8-10.5 13-6.3l141.9 112a8 8 0 010 12.6l-141.9 112c-5.2 4.1-13 .4-13-6.3z"></path>
</svg>
);

export const Icons = { User, Moon, Sun, Home, Settings, Search, Login };
29 changes: 17 additions & 12 deletions frontend/components/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, MouseEvent } from "react";
import { forwardRef, ForwardedRef, FC, MouseEvent } from "react";
import styled from "@emotion/styled";
import { css } from "@emotion/react";

Expand Down Expand Up @@ -36,15 +36,20 @@ const Button = styled.button<ButtonProps>`

export type Props = {
/** onClick callback */
onClick: (event: MouseEvent<HTMLButtonElement>) => void;
} & IconProps;

export const IconButton: FC<Props> = ({ onClick, ...props }) => (
<Button
onClick={onClick}
size={`${(props.size || 2) * 2}rem`}
title={props.name}
>
<Icon {...props} />
</Button>
onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
} & Omit<IconProps, "ref">;

export const IconButton: FC<Props> = forwardRef(
({ onClick, ...props }, ref) => (
<Button
onClick={onClick}
size={`${(props.size || 2) * 2}rem`}
title={props.name}
ref={ref as ForwardedRef<HTMLButtonElement>}
>
<Icon {...props} />
</Button>
)
);

IconButton.displayName = "IconButton";
6 changes: 1 addition & 5 deletions frontend/components/Layout/Layout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ describe("Layout test cases", () => {
);

it("Render check", () => {
const { asFragment } = render(
<Layout isDark onThemeToggle={() => undefined}>
{child}
</Layout>
);
const { asFragment } = render(<Layout>{child}</Layout>);

expect(asFragment()).toMatchSnapshot();
});
Expand Down
169 changes: 60 additions & 109 deletions frontend/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,122 +1,73 @@
import { FC } from "react";
import { useState, useLayoutEffect, useEffect, FC } from "react";
import Link from "next/link";
import styled from "@emotion/styled";
import { ThemeProvider } from "@emotion/react";

import { Themes } from "@/styles/themes";

import { Logo } from "@/components/Logo";
import { Input } from "@/components/Input";
import { IconButton } from "@/components/IconButton";
import { StyledLink } from "@/components/StyledLink";

const Wrapper = styled.div`
display: grid;
gap: 0.1rem;
color: ${({ theme }) => theme.font.regular};
background-color: ${({ theme }) => theme.background};
padding: 0.5rem;
grid-template-areas:
"header nav"
"search search"
"content content"
"footer footer";
nav {
flex-direction: row;
justify-content: flex-end;
gap: 5vmin;
}
@media (min-width: 500px) {
grid-template-columns: 1fr 3fr;
}
@media (min-width: 960px) {
grid-template-columns: 1fr 4fr 2fr;
grid-template-areas:
"header search nav"
"content content content"
"footer footer footer";
}
`;
import {
Wrapper,
LogoLink,
StyledLogo,
MainNav,
SearchInput,
Content,
Footer,
} from "./components";

const LogoLink = styled(StyledLink)`
padding-right: 1vw;
`;
const useIsomorphicLayoutEffect =
typeof window !== "undefined" ? useLayoutEffect : useEffect;

const StyledLogo = styled(Logo)`
grid-area: header;
display: flex;
align-items: center;
justify-content: flex-start;
height: 4rem;
& .logo_full {
display: none;
}
@media (min-width: 560px) {
& .logo_short {
display: none;
}
& .logo_full {
display: inline;
}
}
`;
export const Layout: FC = ({ children }) => {
const [isDark, setIsDark] = useState(true);

const MainNav = styled.nav`
grid-area: nav;
display: flex;
justify-content: space-around;
align-items: center;
margin: 0 2vmin;
`;
const toggleDark = () => {
localStorage.setItem("theme", isDark ? "light" : "dark");
setIsDark(!isDark);
};

const SearchInput = styled(Input)`
grid-area: search;
width: 100%;
height: 4rem;
`;
useIsomorphicLayoutEffect(() => {
const isDark =
Boolean(localStorage.getItem("theme") === "dark") ||
window.matchMedia("prefers-color-scheme: dark").matches;

const Content = styled.main`
grid-area: content;
`;
setIsDark(isDark);
}, []);

const Footer = styled.footer`
grid-area: footer;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
height: 5rem;
`;
const theme = Themes[isDark ? "dark" : "light"];

type Props = {
isDark: boolean;
onThemeToggle: () => void;
return (
<ThemeProvider theme={theme}>
<Wrapper>
<Link href="/" passHref>
<LogoLink>
<StyledLogo size={3}>
<span className="logo_short">C8X</span>
<span className="logo_full">CoursesBox</span>
</StyledLogo>
</LogoLink>
</Link>
<MainNav>
<Link href="/all" passHref>
<StyledLink>All</StyledLink>
</Link>
<Link href="/login" passHref>
<IconButton name="Login" size={1} />
</Link>
<IconButton
name={isDark ? "Moon" : "Sun"}
size={1}
onClick={toggleDark}
/>
</MainNav>
<SearchInput icon="Search" placeholder="Search" onChange={() => null} />
<Content>{children}</Content>
<Footer>
© {new Date().getFullYear()} NickOvchinnikov. All rights reserved.
</Footer>
</Wrapper>
</ThemeProvider>
);
};

export const Layout: FC<Props> = ({ children, isDark, onThemeToggle }) => (
<Wrapper>
<Link href="/" passHref>
<LogoLink>
<StyledLogo size={3}>
<span className="logo_short">C8X</span>
<span className="logo_full">CoursesBox</span>
</StyledLogo>
</LogoLink>
</Link>
<MainNav>
<Link href="/all" passHref>
<StyledLink>All</StyledLink>
</Link>
<Link href="/news" passHref>
<StyledLink>News</StyledLink>
</Link>
<IconButton
name={isDark ? "Moon" : "Sun"}
size={1}
onClick={onThemeToggle}
/>
</MainNav>
<SearchInput icon="Search" placeholder="Search" onChange={() => null} />
<Content>{children}</Content>
<Footer>
© {new Date().getFullYear()} NickOvchinnikov. All rights reserved.
</Footer>
</Wrapper>
);
44 changes: 31 additions & 13 deletions frontend/components/Layout/__snapshots__/Layout.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,49 @@ exports[`Layout test cases Render check 1`] = `
>
All
</a>
<a
class="css-1sdexyg"
href="/news"
>
News
</a>
<button
class="css-15xqhy1"
title="Moon"
title="Login"
>
<svg
aria-label="Moon"
aria-hidden="true"
aria-label="Login"
class="css-z3g3v6"
data-icon="login"
fill="currentColor"
focusable="false"
height="1rem"
href="/login"
role="img"
viewBox="0 0 24 24"
viewBox="64 64 896 896"
width="1rem"
>
<defs>
<style />
</defs>
<path
d="M0 0h24v24H0z"
fill="none"
d="M521.7 82c-152.5-.4-286.7 78.5-363.4 197.7-3.4 5.3.4 12.3 6.7 12.3h70.3c4.8 0 9.3-2.1 12.3-5.8 7-8.5 14.5-16.7 22.4-24.5 32.6-32.5 70.5-58.1 112.7-75.9 43.6-18.4 90-27.8 137.9-27.8 47.9 0 94.3 9.3 137.9 27.8 42.2 17.8 80.1 43.4 112.7 75.9 32.6 32.5 58.1 70.4 76 112.5C865.7 417.8 875 464.1 875 512c0 47.9-9.4 94.2-27.8 137.8-17.8 42.1-43.4 80-76 112.5s-70.5 58.1-112.7 75.9A352.8 352.8 0 01520.6 866c-47.9 0-94.3-9.4-137.9-27.8A353.84 353.84 0 01270 762.3c-7.9-7.9-15.3-16.1-22.4-24.5-3-3.7-7.6-5.8-12.3-5.8H165c-6.3 0-10.2 7-6.7 12.3C234.9 863.2 368.5 942 520.6 942c236.2 0 428-190.1 430.4-425.6C953.4 277.1 761.3 82.6 521.7 82zM395.02 624v-76h-314c-4.4 0-8-3.6-8-8v-56c0-4.4 3.6-8 8-8h314v-76c0-6.7 7.8-10.5 13-6.3l141.9 112a8 8 0 010 12.6l-141.9 112c-5.2 4.1-13 .4-13-6.3z"
/>
</svg>
</button>
<button
class="css-15xqhy1"
title="Sun"
>
<svg
aria-label="Sun"
class="css-z3g3v6"
fill="none"
height="1rem"
role="img"
viewBox="0 0 24 24"
width="1rem"
>
<path
d="M10 6a8 8 0 0011.955 6.956C21.474 18.03 17.2 22 12 22 6.477 22 2 17.523 2 12c0-5.2 3.97-9.474 9.044-9.955A7.963 7.963 0 0010 6zm-6 6a8 8 0 008 8 8.006 8.006 0 006.957-4.045c-.316.03-.636.045-.957.045-5.523 0-10-4.477-10-10 0-.321.015-.64.045-.957A8.006 8.006 0 004 12zm14.164-9.709L19 2.5v1l-.836.209a2 2 0 00-1.455 1.455L16.5 6h-1l-.209-.836a2 2 0 00-1.455-1.455L13 3.5v-1l.836-.209A2 2 0 0015.29.836L15.5 0h1l.209.836a2 2 0 001.455 1.455zm5 5L24 7.5v1l-.836.209a2 2 0 00-1.455 1.455L21.5 11h-1l-.209-.836a2 2 0 00-1.455-1.455L18 8.5v-1l.836-.209a2 2 0 001.455-1.455L20.5 5h1l.209.836a2 2 0 001.455 1.455z"
clip-rule="evenodd"
d="M12 16a4 4 0 100-8 4 4 0 000 8zm0 2a6 6 0 100-12 6 6 0 000 12zM11 0h2v4.062a8.079 8.079 0 00-2 0V0zM7.094 5.68L4.222 2.808 2.808 4.222 5.68 7.094A8.048 8.048 0 017.094 5.68zM4.062 11H0v2h4.062a8.079 8.079 0 010-2zm1.618 5.906l-2.872 2.872 1.414 1.414 2.872-2.872a8.048 8.048 0 01-1.414-1.414zM11 19.938V24h2v-4.062a8.069 8.069 0 01-2 0zm5.906-1.618l2.872 2.872 1.414-1.414-2.872-2.872a8.048 8.048 0 01-1.414 1.414zM19.938 13H24v-2h-4.062a8.069 8.069 0 010 2zM18.32 7.094l2.872-2.872-1.414-1.414-2.872 2.872c.528.41 1.003.886 1.414 1.414z"
fill="currentColor"
fill-rule="evenodd"
/>
</svg>
</button>
Expand Down Expand Up @@ -101,7 +119,7 @@ exports[`Layout test cases Render check 1`] = `
/>
</label>
<main
class="css-1qc1fvq"
class="css-tee3o6"
>
<h1>
Main article area
Expand Down
Loading

1 comment on commit 82d94d7

@vercel
Copy link

@vercel vercel bot commented on 82d94d7 May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.