Skip to content

Commit 1b375ab

Browse files
committed
add tabs
1 parent 686205a commit 1b375ab

File tree

4 files changed

+96
-22
lines changed

4 files changed

+96
-22
lines changed

examples/lazy-injection/kitchen-sink/src/App.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
}
1515

1616
.App-header {
17-
min-height: 100vh;
17+
min-height: 50vh;
1818
display: flex;
1919
flex-direction: column;
2020
align-items: center;
2121
justify-content: center;
2222
font-size: calc(10px + 2vmin);
23+
margin-bottom: 10px;
2324
}
2425

2526
.App-link {
@@ -37,3 +38,11 @@
3738
transform: translateY(0px);
3839
}
3940
}
41+
42+
.tabs {
43+
display: flex;
44+
gap: 5px;
45+
justify-content: center;
46+
align-items: center;
47+
margin-bottom: 10px;
48+
}

examples/lazy-injection/kitchen-sink/src/App.tsx

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Suspense, useReducer, useTransition } from "react"
2-
import "./App.css"
1+
import { Suspense, useState, useTransition } from "react"
32
import logo from "./logo.svg"
43
import { Todos } from "./features/todos/Todos"
54
import { lazily } from "react-lazily"
5+
import { Tab } from "./components/Tab"
6+
import "./App.css"
67

78
// equivalent to
89
// const Counter = lazy(() => import("./features/counter/Counter").then(m => ({ default: m.Counter }))
@@ -11,33 +12,15 @@ const { Counter } = lazily(() => import("./features/counter/Counter"))
1112
const { Quotes } = lazily(() => import("./features/quotes/Quotes"))
1213

1314
const App = () => {
14-
const [counterOpen, toggleCounter] = useReducer(b => !b, false)
15-
const [quotesOpen, toggleQuotes] = useReducer(b => !b, false)
15+
const [tab, setTab] = useState("todos")
1616
const [, startTransition] = useTransition()
1717
return (
1818
<div className="App">
1919
<header className="App-header">
2020
<img src={logo} className="App-logo" alt="logo" />
21-
<Todos />
22-
<details open={counterOpen}>
23-
<summary onClick={() => startTransition(toggleCounter)}>
24-
Counter example (lazy)
25-
</summary>
26-
<Suspense fallback="Loading counter">
27-
{counterOpen && <Counter />}
28-
</Suspense>
29-
</details>
3021
<p>
3122
Edit <code>src/App.tsx</code> and save to reload.
3223
</p>
33-
<details open={quotesOpen}>
34-
<summary onClick={() => startTransition(toggleQuotes)}>
35-
Quotes example (lazy)
36-
</summary>
37-
<Suspense fallback="Loading quotes">
38-
{quotesOpen && <Quotes />}
39-
</Suspense>
40-
</details>
4124
<span>
4225
<span>Learn </span>
4326
<a
@@ -86,6 +69,37 @@ const App = () => {
8669
</a>
8770
</span>
8871
</header>
72+
<div className="tabs">
73+
<Tab
74+
selected={tab === "todos"}
75+
onClick={() => startTransition(() => setTab("todos"))}
76+
>
77+
Todos
78+
</Tab>
79+
<Tab
80+
selected={tab === "counter"}
81+
onClick={() => startTransition(() => setTab("counter"))}
82+
>
83+
Counter
84+
</Tab>
85+
<Tab
86+
selected={tab === "quotes"}
87+
onClick={() => startTransition(() => setTab("quotes"))}
88+
>
89+
Quotes
90+
</Tab>
91+
</div>
92+
{tab === "todos" && <Todos />}
93+
{tab === "counter" && (
94+
<Suspense fallback="Loading counter">
95+
<Counter />
96+
</Suspense>
97+
)}
98+
{tab === "quotes" && (
99+
<Suspense fallback="Loading quotes">
100+
<Quotes />
101+
</Suspense>
102+
)}
89103
</div>
90104
)
91105
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.tab {
2+
appearance: none;
3+
background: none;
4+
font-size: 32px;
5+
padding-left: 12px;
6+
padding-right: 12px;
7+
outline: none;
8+
border: 2px solid transparent;
9+
color: rgb(112, 76, 182);
10+
padding-bottom: 4px;
11+
cursor: pointer;
12+
background-color: rgba(112, 76, 182, 0.1);
13+
border-radius: 2px;
14+
transition: all 0.15s;
15+
}
16+
17+
.tab:hover,
18+
.tab:focus {
19+
border: 2px solid rgba(112, 76, 182, 0.4);
20+
}
21+
22+
.tab:active {
23+
background-color: rgba(112, 76, 182, 0.2);
24+
}
25+
26+
.selected {
27+
background-color: rgb(112, 76, 182);
28+
color: #f1edf8;
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { ReactNode } from "react"
2+
import styles from "./Tab.module.css"
3+
import clsx from "clsx"
4+
5+
export function Tab({
6+
children,
7+
selected,
8+
onClick,
9+
}: {
10+
children: ReactNode
11+
selected: boolean
12+
onClick: () => void
13+
}) {
14+
return (
15+
<button
16+
className={clsx(styles.tab, selected && styles.selected)}
17+
onClick={onClick}
18+
>
19+
{children}
20+
</button>
21+
)
22+
}

0 commit comments

Comments
 (0)