Skip to content

Commit 00e33d1

Browse files
committed
Mobile friendly
1 parent 50d8783 commit 00e33d1

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

articles/Custom-input.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ If the default input types (FormInput, FormSelect ...) do not provide enough fun
44

55
![custom input](https://raw.githubusercontent.com/wiki/CodeStix/typed-react-form/images/custominput.gif)
66

7-
Custom input that shows an error when needed, paints gray background when modified, gets disabled when submitting and shows its defaultValue as a placeholder. You can tweak this custom input further by implementing transformations for different input types, allowing `HTMLInputAttributes` etc.
7+
The following code resembles a custom input component that shows an error when needed, paints gray background when modified, gets disabled when submitting and shows its defaultValue as a placeholder. You can tweak this custom input further by implementing transformations for different input types, allowing `HTMLInputAttributes` etc.
88

99
```jsx
1010
function CustomInput<T>(props: { form: FormState<T>; name: keyof T; children?: React.ReactNode }) {

components/NavBar.tsx

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import styled from "styled-components";
33
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
44
import { faGithub, faGithubAlt, faGithubSquare } from "@fortawesome/free-brands-svg-icons";
55
import Link from "next/link";
6-
import { faBook } from "@fortawesome/free-solid-svg-icons";
6+
import { faBars, faBook } from "@fortawesome/free-solid-svg-icons";
7+
import { useEffect, useState } from "react";
78

89
const Name = styled.span`
910
cursor: pointer;
@@ -12,6 +13,10 @@ const Name = styled.span`
1213
font-size: 1.5rem;
1314
color: #2065af;
1415
margin-right: auto;
16+
17+
@media only screen and (max-width: 600px) {
18+
font-size: 1.2rem;
19+
}
1520
`;
1621

1722
const NavItem = styled.a`
@@ -37,7 +42,16 @@ const Icon = styled(FontAwesomeIcon)`
3742
/* color: #195daa; */
3843
`;
3944

40-
export function NavBar() {
45+
function useWindowWidth() {
46+
const [windowWidth, setWindowWidth] = useState(1200);
47+
useEffect(() => {
48+
setWindowWidth(window.innerWidth);
49+
}, []);
50+
return windowWidth;
51+
}
52+
53+
export function NavBar(props: { onMenu: () => void }) {
54+
const windowWidth = useWindowWidth();
4155
return (
4256
<CenterContainer>
4357
<Container>
@@ -47,14 +61,26 @@ export function NavBar() {
4761
&nbsp;Typed React Form
4862
</Name>
4963
</Link>
50-
<Link href="https://github.com/codestix/typed-react-form" passHref>
64+
65+
{/* <Link href="https://github.com/codestix/typed-react-form" passHref>
5166
<NavItem>GitHub</NavItem>
52-
</Link>
67+
</Link> */}
68+
5369
<Link href="https://github.com/codestix/typed-react-form" passHref>
5470
<NavItem>
5571
<Icon icon={faGithub} />
5672
</NavItem>
5773
</Link>
74+
75+
{windowWidth < 600 && (
76+
<NavItem
77+
onClick={(ev) => {
78+
ev.stopPropagation();
79+
props.onMenu();
80+
}}>
81+
<Icon icon={faBars} />
82+
</NavItem>
83+
)}
5884
</Container>
5985
</CenterContainer>
6086
);

pages/docs/[doc].tsx

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { GetStaticProps, GetServerSideProps, GetStaticPaths } from "next";
22
import fs from "fs/promises";
33
import path from "path";
44
import ReactMarkdown from "react-markdown";
5-
import React from "react";
5+
import React, { useEffect, useState } from "react";
66
import { CenterContainer } from "../../components/CenterContainer";
77
import { NavBar } from "../../components/NavBar";
88
import { SideBar } from "../../components/SideBar";
@@ -11,6 +11,7 @@ import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
1111
import { materialOceanic } from "react-syntax-highlighter/dist/cjs/styles/prism";
1212
import Link from "next/link";
1313
import Head from "next/head";
14+
import { useRouter } from "next/router";
1415

1516
const ARTICLES_PATH = path.join(process.cwd(), "articles");
1617

@@ -22,16 +23,29 @@ const Container = styled.div`
2223
display: grid;
2324
gap: 3em;
2425
grid-template-columns: 200px 1fr;
26+
27+
@media only screen and (max-width: 600px) {
28+
grid-template-columns: 1fr;
29+
/* display: block; */
30+
/* max-width: 100vw; */
31+
}
2532
`;
2633

2734
const ReactMarkdownContainer = styled.div`
35+
display: block;
2836
overflow: hidden;
2937
margin-bottom: 30em;
3038
3139
code {
3240
font-size: 1.3em;
3341
}
3442
43+
/* @media only screen and (max-width: 600px) {
44+
pre {
45+
margin: 0 -2em !important;
46+
}
47+
} */
48+
3549
a {
3650
padding: 0.3em 0;
3751
color: #186eee;
@@ -59,19 +73,39 @@ const ReactMarkdownContainer = styled.div`
5973
}
6074
`;
6175

76+
const SidebarHolder = styled.div`
77+
@media only screen and (max-width: 600px) {
78+
top: 0em;
79+
left: 0em;
80+
width: 100%;
81+
z-index: 100;
82+
backdrop-filter: blur(50px);
83+
height: 100%;
84+
position: fixed;
85+
padding: 1em;
86+
transform: translateX(0);
87+
transition: 100ms ease-in;
88+
&.hidden {
89+
transition: 100ms ease-in;
90+
transform: translateX(-100vw);
91+
}
92+
}
93+
`;
94+
6295
export default function DocPage(props: Props) {
96+
const [showMobileMenu, setShowMobileMenu] = useState(false);
6397
return (
64-
<>
98+
<main onClick={() => setShowMobileMenu(false)}>
6599
<Head>
66100
<link rel="preconnect" href="https://fonts.gstatic.com" />
67101
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&family=Roboto&display=swap" rel="stylesheet" />
68102
</Head>
69-
<NavBar />
70-
<CenterContainer style={{ margin: "2em 0.5em" }}>
103+
<NavBar onMenu={() => setShowMobileMenu(true)} />
104+
<CenterContainer style={{ margin: "0 0.5em" }}>
71105
<Container>
72-
<div>
106+
<SidebarHolder className={!showMobileMenu && "hidden"}>
73107
<SideBar />
74-
</div>
108+
</SidebarHolder>
75109
<ReactMarkdownContainer>
76110
<ReactMarkdown
77111
renderers={{
@@ -91,7 +125,7 @@ export default function DocPage(props: Props) {
91125
</ReactMarkdownContainer>
92126
</Container>
93127
</CenterContainer>
94-
</>
128+
</main>
95129
);
96130
}
97131

0 commit comments

Comments
 (0)