Skip to content

Commit 87bca53

Browse files
Updates
1 parent fe542d0 commit 87bca53

File tree

6 files changed

+93
-7
lines changed

6 files changed

+93
-7
lines changed

components/button.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import styles from "./button.module.css";
22

33
enum Color {
4+
grey = "#cccccc",
45
blue = "dodgerblue",
56
yellow = "#e3b307",
67
green = "#57b004",
8+
red = "#db2e02",
79
}
810

911
type Props = {
1012
children: string;
1113
color?: keyof typeof Color;
14+
onClick?(): void;
1215
};
1316

1417
export function Button(props: Props) {
15-
const { children, color = "blue" } = props;
18+
const { children, color = "grey", onClick } = props;
1619

1720
return (
18-
<button className={styles.button} style={{ color: Color[color] }}>
21+
<button
22+
className={styles.button}
23+
style={{ color: Color[color] }}
24+
onClick={onClick}
25+
>
1926
<span>{children}</span>
2027
</button>
2128
);

components/input.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type Props = {
2+
value: number;
3+
onChange(value: number): void;
4+
};
5+
6+
export function Input({ value, onChange }: Props) {
7+
return (
8+
<>
9+
<label>
10+
User input styles:{" "}
11+
<input
12+
style={{
13+
width: `${value}px`,
14+
15+
// this can be moved as a static style, in the .css file
16+
padding: "0.5em",
17+
}}
18+
type="number"
19+
value={value}
20+
onChange={(e) => onChange(+e.target.value)}
21+
/>
22+
</label>
23+
</>
24+
);
25+
}

pages/index.tsx

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1+
import { useState } from "react";
2+
import Link from "next/link";
13
import { Button } from "../components/button";
4+
import { Input } from "../components/input";
25

36
export default function Home() {
7+
const [showButtons, toggleButtons] = useState(false);
8+
const [width, setWidth] = useState(100);
9+
410
return (
511
<main>
6-
<Button>Default color</Button>
7-
<Button color="yellow">Yellow color</Button>
8-
<Button color="green">Green color</Button>
12+
<Link href="/other">
13+
<a>Other page</a>
14+
</Link>
15+
16+
<Button color="blue" onClick={() => toggleButtons(true)}>
17+
Show other buttons
18+
</Button>
19+
20+
{showButtons && (
21+
<>
22+
<Button color="yellow">Yellow button</Button>
23+
<Button color="green">Green button</Button>
24+
<Button color="red">Red button</Button>
25+
</>
26+
)}
27+
28+
<Input value={width} onChange={(val) => setWidth(val)} />
929
</main>
1030
);
1131
}

pages/other.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useRouter } from "next/router";
2+
import { Button } from "../components/button";
3+
4+
import styles from "../styles/other.module.css";
5+
6+
export default function Home() {
7+
const router = useRouter();
8+
9+
return (
10+
<main>
11+
<Button onClick={() => router.back()}>Go Back</Button>
12+
13+
<h1 className={styles.heading}>Another route</h1>
14+
15+
<p>
16+
Styles should be code-splitted, loaded only when the route is loaded.
17+
</p>
18+
19+
<p>
20+
The button is loaded on the main route also, so it should not be fetched
21+
twice.
22+
</p>
23+
</main>
24+
);
25+
}

styles/globals.css

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ button,
1515
input {
1616
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
1717
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
18+
font-size: 16px;
1819
}
1920

20-
a {
21-
color: inherit;
21+
a:link,
22+
a:visited {
23+
color: dodgerblue;
2224
text-decoration: none;
2325
}
26+
a:hover {
27+
text-decoration: underline;
28+
}
2429

2530
* {
2631
box-sizing: border-box;

styles/other.module.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.heading {
2+
max-width: 75vw;
3+
font-size: 1.5rem;
4+
}

0 commit comments

Comments
 (0)