-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversions-dropdown.tsx
More file actions
55 lines (49 loc) · 1.89 KB
/
versions-dropdown.tsx
File metadata and controls
55 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useState } from "react"
import { useNavigate } from "react-router"
import { Icon } from "~/ui/icon/icon"
import { getCurrentVersion, homepageUrlWithVersion, isKnownVersion } from "~/utils/version-resolvers"
import { versions } from "~/utils/versions"
export function VersionDropdown() {
const navigate = useNavigate()
const { version: currentVersion } = getCurrentVersion()
const [selectedVersion, setSelectedVersion] = useState(currentVersion)
function onChange(e: React.ChangeEvent<HTMLSelectElement>) {
const next = e.target.value
if (next === currentVersion) return
setSelectedVersion(isKnownVersion(next) ? next : currentVersion)
const to = homepageUrlWithVersion(next)
const nav = () => {
navigate(to)
e.target.blur()
}
if (document.startViewTransition) document.startViewTransition(nav)
else nav()
}
return (
<div className="relative inline-block text-[var(--color-text-normal)]">
<select
id="version"
name="version"
className="cursor-pointer appearance-none rounded-lg border border-[var(--color-border)] bg-[var(--color-background)] py-2.5 pr-10 pl-4 font-medium text-sm shadow-sm transition-all duration-200 hover:bg-[var(--color-border)] focus:border-transparent focus:bg-[var(--color-border)] focus:outline-none focus:ring-none"
value={selectedVersion}
onChange={onChange}
aria-label="Select documentation version"
>
{versions.map((v) => (
<option
key={v}
value={v}
className={`bg-[var(--color-background)] text-[var(--color-text-normal)] hover:bg-[var(--color-background-hover)]${selectedVersion === v ? "font-semibold text-[var(--color-text-active)]" : ""}
`}
>
{v}
</option>
))}
</select>
<Icon
name="ChevronDown"
className="-translate-y-1/2 pointer-events-none absolute top-1/2 right-3 h-4 w-4 text-[var(--color-text-muted)] transition-colors duration-200"
/>
</div>
)
}