|
1 | 1 | import React from 'react' |
2 | 2 | import {ActionList, ActionMenu, Box} from '@primer/react' |
3 | | -import {navigate} from 'gatsby' |
4 | 3 | import * as getNav from '../util/get-nav' |
5 | 4 | import usePage from '../hooks/use-page' |
| 5 | +import Link from './link' |
6 | 6 |
|
7 | | -const VariantItem = ({match, active}) => { |
8 | | - const {variant, page} = match |
| 7 | +const VariantItem = ({title, shortName, url, active}) => ( |
| 8 | + <ActionList.Item |
| 9 | + as={Link} |
| 10 | + to={url} |
| 11 | + id={shortName} |
| 12 | + active={active} |
| 13 | + sx={{ |
| 14 | + ':hover': {textDecoration: 'none'}, |
| 15 | + }} |
| 16 | + > |
| 17 | + {title} |
| 18 | + </ActionList.Item> |
| 19 | +) |
9 | 20 |
|
10 | | - const navigateToPage = React.useCallback(() => navigate(`${page.url}?v=true`), [page.url]) |
| 21 | +const useVariantFocus = path => { |
| 22 | + const anchorRef = React.useRef(null) |
| 23 | + const pathRef = React.useRef(null) |
11 | 24 |
|
12 | | - const handleClick = React.useCallback( |
13 | | - event => { |
14 | | - event.preventDefault() |
15 | | - navigateToPage() |
16 | | - }, |
17 | | - [navigateToPage], |
18 | | - ) |
| 25 | + React.useEffect(() => { |
| 26 | + const previousPath = pathRef.current |
| 27 | + pathRef.current = path |
19 | 28 |
|
20 | | - const handleKey = React.useCallback( |
21 | | - event => { |
22 | | - if (event.key === 'Enter') { |
23 | | - navigateToPage() |
| 29 | + if (getNav.didVariantChange(previousPath, path)) { |
| 30 | + const anchor = anchorRef.current |
| 31 | + const onBlur = () => { |
| 32 | + anchor.removeEventListener('blur', onBlur) |
| 33 | + anchor.focus() |
24 | 34 | } |
25 | | - }, |
26 | | - [navigateToPage], |
27 | | - ) |
| 35 | + anchor.addEventListener('blur', onBlur) |
| 36 | + return () => anchor.removeEventListener('blur', onBlur) |
| 37 | + } |
| 38 | + }, [path]) |
28 | 39 |
|
29 | | - return ( |
30 | | - <ActionList.Item onKeyDown={handleKey} onClick={handleClick} id={variant.shortName} active={active}> |
31 | | - {variant.title} |
32 | | - </ActionList.Item> |
33 | | - ) |
| 40 | + return anchorRef |
34 | 41 | } |
35 | 42 |
|
36 | | -const VariantMenu = ({variants, path}) => { |
| 43 | +const VariantMenu = ({title, latest, current, prerelease, legacy, path}) => { |
37 | 44 | const [open, setOpen] = React.useState(false) |
38 | | - |
39 | | - const {selected, items} = variants.reduce( |
40 | | - (acc, match, key) => { |
41 | | - const active = match.page.url === path |
42 | | - if (active) { |
43 | | - acc.selected = match |
44 | | - } |
45 | | - acc.items.push({match, key, active}) |
46 | | - return acc |
47 | | - }, |
48 | | - {selected: variants[0], items: []}, |
49 | | - ) |
| 45 | + const anchorRef = useVariantFocus(path) |
| 46 | + const labelId = 'label-versions-list-item' |
50 | 47 |
|
51 | 48 | return ( |
52 | 49 | <> |
53 | | - <Box as="p" sx={{m: 0}} id="label-versions-list-item"> |
| 50 | + <Box as="p" sx={{m: 0}} id={labelId}> |
54 | 51 | Select CLI Version: |
55 | 52 | </Box> |
56 | | - <ActionMenu open={open} onOpenChange={setOpen}> |
57 | | - {/* Disabling to remove lint warnings. This property was added as "autofocus" |
58 | | - in a previous accessibility audit which did not trigger the lint warning. */ |
59 | | - /* eslint-disable-next-line jsx-a11y/no-autofocus */} |
60 | | - <ActionMenu.Button autoFocus aria-describedby="label-versions-list-item"> |
61 | | - {selected.variant.title} |
62 | | - </ActionMenu.Button> |
63 | | - <ActionMenu.Overlay width="medium" onEscape={() => setOpen(false)}> |
64 | | - <ActionList id="versions-list-item" aria-labelledby="label-versions-list-item"> |
65 | | - {items.map(item => ( |
66 | | - <VariantItem key={item.key} {...item} /> |
67 | | - ))} |
| 53 | + <ActionMenu anchorRef={anchorRef} open={open} onOpenChange={setOpen}> |
| 54 | + <ActionMenu.Button aria-describedby={labelId}>{title}</ActionMenu.Button> |
| 55 | + <ActionMenu.Overlay width="auto" onEscape={() => setOpen(false)}> |
| 56 | + <ActionList aria-labelledby={labelId}> |
| 57 | + <ActionList.Group title="Current"> |
| 58 | + <VariantItem {...latest} /> |
| 59 | + {current && <VariantItem {...current} />} |
| 60 | + {prerelease && <VariantItem {...prerelease} />} |
| 61 | + </ActionList.Group> |
| 62 | + {legacy && ( |
| 63 | + <ActionList.Group title="Legacy"> |
| 64 | + {legacy.map(item => ( |
| 65 | + <VariantItem key={item.title} {...item} /> |
| 66 | + ))} |
| 67 | + </ActionList.Group> |
| 68 | + )} |
68 | 69 | </ActionList> |
69 | 70 | </ActionMenu.Overlay> |
70 | 71 | </ActionMenu> |
71 | 72 | </> |
72 | 73 | ) |
73 | 74 | } |
74 | 75 |
|
75 | | -const VariantSelect = () => { |
76 | | - const {location} = usePage() |
77 | | - const root = getNav.getVariantRoot(location.pathname) |
78 | | - const path = getNav.getPath(location.pathname) |
79 | | - const vp = getNav.getVariantAndPage(root, path) |
80 | | - const variants = vp ? getNav.getVariantsForPage(root, vp.page) : [] |
| 76 | +const useVariants = () => { |
| 77 | + const {pathname} = usePage().location |
81 | 78 |
|
82 | | - if (!variants.length) { |
83 | | - return null |
84 | | - } |
| 79 | + return React.useMemo(() => { |
| 80 | + const root = getNav.getVariantRoot(pathname) |
| 81 | + const path = getNav.getPath(pathname) |
| 82 | + const vp = getNav.getVariantAndPage(root, path) |
| 83 | + const variantPages = vp ? getNav.getVariantsForPage(root, vp.page) : [] |
85 | 84 |
|
86 | | - return ( |
| 85 | + if (!variantPages.length) { |
| 86 | + return null |
| 87 | + } |
| 88 | + |
| 89 | + const result = {path, latest: null, current: null, prerelease: null, legacy: []} |
| 90 | + |
| 91 | + for (const {variant, page} of variantPages) { |
| 92 | + const item = {...variant, url: page.url, active: page.url === path} |
| 93 | + let typeDesc = '' |
| 94 | + switch (variant.type) { |
| 95 | + case 'latest': |
| 96 | + result.latest = item |
| 97 | + typeDesc = ' (Latest)' |
| 98 | + break |
| 99 | + case 'current': |
| 100 | + result.current = item |
| 101 | + typeDesc = ' (Current)' |
| 102 | + break |
| 103 | + case 'prerelease': |
| 104 | + result.prerelease = item |
| 105 | + typeDesc = ' (Prerelease)' |
| 106 | + break |
| 107 | + default: |
| 108 | + result.legacy.push(item) |
| 109 | + typeDesc = ' Legacy' |
| 110 | + } |
| 111 | + if (item.active) { |
| 112 | + result.title = `${item.title}${typeDesc}` |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + result.legacy.sort((a, b) => parseInt(b.shortName.slice(1)) - parseInt(a.shortName.slice(1))) |
| 117 | + |
| 118 | + return result |
| 119 | + }, [pathname]) |
| 120 | +} |
| 121 | + |
| 122 | +const VariantSelect = () => { |
| 123 | + const variants = useVariants() |
| 124 | + return variants ? ( |
87 | 125 | <Box sx={{mt: 2, mb: 3}}> |
88 | | - <VariantMenu variants={variants} path={path} /> |
| 126 | + <VariantMenu {...variants} /> |
89 | 127 | </Box> |
90 | | - ) |
| 128 | + ) : null |
91 | 129 | } |
92 | 130 |
|
93 | 131 | export default VariantSelect |
0 commit comments