Skip to content

Commit 586dc10

Browse files
committed
Make search result items links
1 parent 724a6ab commit 586dc10

File tree

7 files changed

+73
-65
lines changed

7 files changed

+73
-65
lines changed

src/components/link.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,19 @@ const Link = React.forwardRef(function Link({to, href, ...props}, ref) {
3838
return <PrimerLink ref={ref} href={href} {...props} />
3939
})
4040

41+
export const LinkNoUnderline = React.forwardRef(function LinkNoUnderline({sx, ...props}, ref) {
42+
return (
43+
<Link
44+
ref={ref}
45+
sx={{
46+
...sx,
47+
'&:hover, &:focus': {
48+
textDecoration: 'none',
49+
},
50+
}}
51+
{...props}
52+
/>
53+
)
54+
})
55+
4156
export default Link

src/components/search-results.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import {Box, Text} from '@primer/react'
3+
import {LinkNoUnderline} from './link'
34
import useSiteMetadata from '../hooks/use-site-metadata'
45
import * as getNav from '../util/get-nav'
56

@@ -31,6 +32,8 @@ function SearchResults({results, getItemProps, highlightedIndex}) {
3132

3233
return results.map((item, index) => (
3334
<Box
35+
as={LinkNoUnderline}
36+
to={item.path}
3437
key={item.path}
3538
style={{cursor: 'pointer'}}
3639
sx={{

src/components/table-of-contents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const Mobile = withTableOfContents(({items}) => {
3737
return (
3838
<Box sx={{display: ['block', null, 'none'], mb: 3}}>
3939
<Details {...getDetailsProps()}>
40-
<Button variant="invisible" as="summary" leadingIcon={open ? ChevronDownIcon : ChevronRightIcon}>
40+
<Button as="summary" sx={{display: 'inline-flex'}} leadingIcon={open ? ChevronDownIcon : ChevronRightIcon}>
4141
Table of contents
4242
</Button>
4343
<TableOfContents items={items} />

src/components/variant-select.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ import React from 'react'
22
import {ActionList, ActionMenu, Box} from '@primer/react'
33
import * as getNav from '../util/get-nav'
44
import usePage from '../hooks/use-page'
5-
import Link from './link'
5+
import {LinkNoUnderline} from './link'
66

77
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-
>
8+
<ActionList.Item as={LinkNoUnderline} to={url} id={shortName} active={active}>
179
{title}
1810
</ActionList.Item>
1911
)

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export const HEADER_BAR = 10
55
export const FULL_HEADER_HEIGHT = HEADER_HEIGHT + HEADER_BAR
66

77
export const SCROLL_MARGIN_TOP = FULL_HEADER_HEIGHT + 24
8+
9+
export const CLI_PATH = '/cli'

src/hooks/use-search.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import {navigate, graphql, useStaticQuery} from 'gatsby'
44
import {useIsMobile} from './use-breakpoint'
55
import usePage from './use-page'
66
import * as getNav from '../util/get-nav'
7-
8-
// This worker can live for the entire duraction of the site
9-
const WORKER = new Worker(new URL('../util/search.worker.js', import.meta.url))
10-
const CLI_ROOT = '/cli'
7+
import {CLI_PATH} from '../constants'
118

129
const useSearchData = () => {
1310
const data = useStaticQuery(graphql`
@@ -51,50 +48,13 @@ const useSearchData = () => {
5148

5249
const useCliVersion = () => {
5350
return getNav.getCurrentOrDefaultVariant(
54-
getNav.getItem(getNav.getVariantRoot(`${CLI_ROOT}/`, {stripTrailing: false})),
51+
getNav.getItem(getNav.getVariantRoot(`${CLI_PATH}/`, {stripTrailing: false})),
5552
usePage().location.pathname,
5653
)
5754
}
5855

59-
function useSearch() {
60-
const [query, setQuery] = React.useState()
61-
const [results, setResults] = React.useState(null)
62-
const queryRef = React.useRef()
63-
const items = useSearchData()
56+
const useSearchCombobox = (results, setQuery) => {
6457
const isMobile = useIsMobile()
65-
const {url: cliUrl} = useCliVersion()
66-
67-
const handleSearchResults = React.useCallback(({data}) => {
68-
if (data.debug) {
69-
console.log(data.debug)
70-
}
71-
if (data.query && data.results && data.query === queryRef.current) {
72-
setResults(data.results)
73-
}
74-
}, [])
75-
76-
React.useEffect(() => {
77-
WORKER.addEventListener('message', handleSearchResults)
78-
}, [handleSearchResults])
79-
80-
React.useEffect(() => {
81-
WORKER.postMessage({items})
82-
}, [items])
83-
84-
React.useEffect(() => {
85-
WORKER.postMessage({cli: {root: CLI_ROOT, current: cliUrl}})
86-
}, [cliUrl])
87-
88-
React.useEffect(() => {
89-
queryRef.current = query
90-
91-
if (query) {
92-
WORKER.postMessage({query})
93-
} else {
94-
setResults(null)
95-
}
96-
}, [query])
97-
9858
const combobox = useCombobox({
9959
id: 'search-box',
10060
items: results || [],
@@ -130,6 +90,50 @@ function useSearch() {
13090
return changes
13191
},
13292
})
93+
return combobox
94+
}
95+
96+
function useSearch() {
97+
const [query, setQuery] = React.useState()
98+
const [results, setResults] = React.useState(null)
99+
const queryRef = React.useRef()
100+
const items = useSearchData()
101+
const {url: cliUrl} = useCliVersion()
102+
const worker = React.useRef()
103+
104+
const handleSearchResults = React.useCallback(({data}) => {
105+
if (data.query && data.results && data.query === queryRef.current) {
106+
setResults(data.results)
107+
}
108+
}, [])
109+
110+
React.useEffect(() => {
111+
worker.current = new Worker(new URL('../util/search.worker.js', import.meta.url))
112+
}, [])
113+
114+
React.useEffect(() => {
115+
worker.current.addEventListener('message', handleSearchResults)
116+
}, [worker, handleSearchResults])
117+
118+
React.useEffect(() => {
119+
worker.current.postMessage({items})
120+
}, [worker, items])
121+
122+
React.useEffect(() => {
123+
worker.current.postMessage({cli: {root: CLI_PATH, current: cliUrl}})
124+
}, [worker, cliUrl])
125+
126+
React.useEffect(() => {
127+
queryRef.current = query
128+
129+
if (query) {
130+
worker.current.postMessage({query})
131+
} else {
132+
setResults(null)
133+
}
134+
}, [worker, query])
135+
136+
const combobox = useSearchCombobox(results, setQuery)
133137

134138
return {
135139
...combobox,

src/mdx/index.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {LinkIcon} from '@primer/octicons-react'
66
import textContent from 'react-addons-text-content'
77
import {SCROLL_MARGIN_TOP} from '../constants'
88
import usePage from '../hooks/use-page'
9-
import SiteLink from '../components/link'
9+
import SiteLink, {LinkNoUnderline} from '../components/link'
1010

1111
export {default as Code} from './code'
1212
export {default as Index} from './nav-hierarchy'
@@ -40,15 +40,7 @@ const StyledHeading = styled(Heading)`
4040

4141
const HeaderLink = ({autolink, children, ...props}) =>
4242
autolink ? (
43-
<SiteLink
44-
{...props}
45-
sx={{
46-
color: 'inherit',
47-
'&:hover, &:focus': {
48-
textDecoration: 'none',
49-
},
50-
}}
51-
>
43+
<LinkNoUnderline {...props} sx={{color: 'inherit'}}>
5244
{children}
5345
<Octicon
5446
icon={LinkIcon}
@@ -60,7 +52,7 @@ const HeaderLink = ({autolink, children, ...props}) =>
6052
verticalAlign: 'middle !important',
6153
}}
6254
/>
63-
</SiteLink>
55+
</LinkNoUnderline>
6456
) : (
6557
children
6658
)

0 commit comments

Comments
 (0)