-
-
Notifications
You must be signed in to change notification settings - Fork 246
[Docs]: Add version selector to VersionBadge #1308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,38 +1,103 @@ | ||||||||
| 'use client' | ||||||||
|
|
||||||||
| import { ChangeEvent } from 'react' | ||||||||
| import styled from 'styled-components' | ||||||||
| import { CARD_STYLES } from '@/utils/card' | ||||||||
| import { SPACINGS } from '@/utils/spacings' | ||||||||
| import { BORDER_RADIUSES } from '@/utils/border' | ||||||||
| import { FONT_SIZES, FONT_WEIGHTS } from '@/utils/font-sizes' | ||||||||
| import { COLORS } from '@/utils/theme' | ||||||||
| import { usePathname } from 'next/navigation' | ||||||||
| import { usePathname, useRouter } from 'next/navigation' | ||||||||
| import { getVersionFromPathname } from '@/utils/slug' | ||||||||
| import { DOCS_LATEST_VERSION, DOCS_VERSIONS } from '@/utils/global-data' | ||||||||
|
|
||||||||
| const VersionBadgeWrapper = styled.div` | ||||||||
| ${CARD_STYLES}; | ||||||||
| display: inline-flex; | ||||||||
| padding: 0.2rem ${SPACINGS.THREE}; | ||||||||
| align-items: center; | ||||||||
| padding: 0.6rem ${SPACINGS.THREE}; | ||||||||
| border-radius: ${BORDER_RADIUSES.SOFT}; | ||||||||
| font-size: ${FONT_SIZES.COMPLEMENTARY}; | ||||||||
| color: ${COLORS.TEXT_LOW_CONTRAST}; | ||||||||
| ` | ||||||||
|
|
||||||||
| const VersionBadgeKey = styled.span` | ||||||||
| const VersionBadgeKey = styled.label` | ||||||||
| font-weight: ${FONT_WEIGHTS.SEMI_BOLD}; | ||||||||
| margin-right: 0.4rem; | ||||||||
| ` | ||||||||
|
|
||||||||
| const VersionSelect = styled.select` | ||||||||
| appearance: none; | ||||||||
| background: transparent; | ||||||||
| border: none; | ||||||||
| padding: 0; | ||||||||
| width: 100%; | ||||||||
| cursor: pointer; | ||||||||
| font-weight: ${FONT_WEIGHTS.SEMI_BOLD}; | ||||||||
| outline: none; | ||||||||
|
|
||||||||
| &::-ms-expand { | ||||||||
| display: none; | ||||||||
| } | ||||||||
| ` | ||||||||
|
|
||||||||
| type PropType = {} | ||||||||
|
|
||||||||
| const DOCS_VERSION_SEGMENT_REGEX = /^v\d+$/ | ||||||||
|
|
||||||||
| function getDocsSubPath(pathname = ''): string[] { | ||||||||
| if (!pathname.startsWith('/docs')) return [] | ||||||||
| const [, ...docsSubPath] = pathname.split('/').filter(Boolean) | ||||||||
|
|
||||||||
| return DOCS_VERSION_SEGMENT_REGEX.test(docsSubPath[0] ?? '') | ||||||||
| ? docsSubPath.slice(1) | ||||||||
| : docsSubPath | ||||||||
| } | ||||||||
|
Comment on lines
+48
to
+55
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative approach is to use a regex like this. There’s no need to change it, though — feel free to choose whichever option you find more readable. const DOCS_WITH_VERSION_REGEX = /^\/docs(?:\/v\d+)?/
function getDocsSubPath(pathname = ''): string[] {
if (!pathname.startsWith('/docs')) return []
return pathname
.replace(DOCS_WITH_VERSION_REGEX, '')
.split('/')
.filter(Boolean)
} |
||||||||
|
|
||||||||
| function getVersionPathname(pathname = '', major: number): string { | ||||||||
| const versionPrefix = major === DOCS_LATEST_VERSION.MAJOR ? [] : [`v${major}`] | ||||||||
| return ['docs', ...versionPrefix, ...getDocsSubPath(pathname)].join('/') | ||||||||
|
Comment on lines
+58
to
+59
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could reuse existing helpers to achieve the same result—see function getVersionPathname(pathname = '', major: number): string {
const versionPrefix = major === DOCS_LATEST_VERSION.MAJOR ? '' : `v${major}`
const versionAndSubPath = joinSlugs(versionPrefix, ...getDocsSubPath(pathname))
return prefixSlugWithDocs(versionAndSubPath)
} |
||||||||
| } | ||||||||
|
|
||||||||
| export function VersionBadge(props: PropType) { | ||||||||
| const { ...restProps } = props | ||||||||
| const router = useRouter() | ||||||||
| const pathname = usePathname() | ||||||||
| const version = getVersionFromPathname(pathname) | ||||||||
|
|
||||||||
| function onVersionChange(event: ChangeEvent<HTMLSelectElement>) { | ||||||||
| const major = Number(event.target.value) | ||||||||
| const nextPathname = `/${getVersionPathname(pathname, major)}` | ||||||||
|
|
||||||||
| if (nextPathname === pathname) return | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s start loading the route here to maintain the strong INP score the website currently has 🙂:
Suggested change
|
||||||||
| router.push(nextPathname) | ||||||||
| } | ||||||||
|
|
||||||||
| return ( | ||||||||
| <VersionBadgeWrapper {...restProps}> | ||||||||
| <VersionBadgeKey>Version:</VersionBadgeKey> {version.NAME} | ||||||||
| <VersionBadgeKey htmlFor="version">Version:</VersionBadgeKey> | ||||||||
|
|
||||||||
| <VersionSelect | ||||||||
| aria-label="Select documentation version" | ||||||||
| value={String(version.MAJOR)} | ||||||||
| name="version" | ||||||||
| id="version" | ||||||||
| onChange={onVersionChange} | ||||||||
| > | ||||||||
| {DOCS_VERSIONS.map((docsVersion) => { | ||||||||
| const versionLabel = `v${docsVersion.MAJOR}` | ||||||||
| const versionSuffix = docsVersion.SUFFIX | ||||||||
| ? ` (${docsVersion.SUFFIX})` | ||||||||
| : '' | ||||||||
|
|
||||||||
| return ( | ||||||||
| <option key={versionLabel} value={docsVersion.MAJOR}> | ||||||||
| {versionLabel} | ||||||||
| {versionSuffix} | ||||||||
| </option> | ||||||||
| ) | ||||||||
| })} | ||||||||
| </VersionSelect> | ||||||||
| </VersionBadgeWrapper> | ||||||||
| ) | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.