Skip to content

Commit 60f9545

Browse files
feat: add search component and integrate into main application
- Introduced a new `Search` component to facilitate search functionality. - Integrated the `Search` component into the main application, replacing the existing search button with the new component. - Ensured the search button is rendered only if certain conditions are met, enhancing the application's dynamic behavior.
1 parent 4a013b9 commit 60f9545

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useEffect, useState } from 'react'
2+
3+
export const Search = () => {
4+
const isMac =
5+
typeof window !== 'undefined'
6+
? navigator.userAgent.toUpperCase().indexOf('MAC') >= 0
7+
: false
8+
9+
const [isClient, setIsClient] = useState<any>(false)
10+
useEffect(() => {
11+
setIsClient(true)
12+
}, [])
13+
14+
return (
15+
<button
16+
id="fern-search-button"
17+
className="fern-search-bar w-full"
18+
onClick={() => {
19+
if ((window as any).plugSDK) {
20+
(window as any).plugSDK.toggleSearchAgent()
21+
}
22+
}}>
23+
<span className="search-placeholder">
24+
<svg
25+
width="1.5em"
26+
height="1.5em"
27+
viewBox="0 0 24 24"
28+
strokeWidth="1.5"
29+
fill="none"
30+
xmlns="http://www.w3.org/2000/svg"
31+
color="currentColor"
32+
className="size-icon-md">
33+
<path
34+
d="M17 17L21 21"
35+
stroke="currentColor"
36+
strokeLinecap="round"
37+
strokeLinejoin="round"></path>
38+
<path
39+
d="M3 11C3 15.4183 6.58172 19 11 19C13.213 19 15.2161 18.1015 16.6644 16.6493C18.1077 15.2022 19 13.2053 19 11C19 6.58172 15.4183 3 11 3C6.58172 3 3 6.58172 3 11Z"
40+
stroke="currentColor"
41+
strokeLinecap="round"
42+
strokeLinejoin="round"></path>
43+
</svg>
44+
<span>Search...</span>
45+
</span>
46+
<kbd className="keyboard-shortcut-hint">
47+
{!isMac && isClient ? 'CTRL' : 'CMD'}&nbsp;+&nbsp; +
48+
</kbd>
49+
</button>
50+
)
51+
}

custom-implementation/src/main.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React from 'react'
88
import Header from './components/header'
99
import Footer from './components/footer'
1010
import { ThemeSwitch } from './components/theme-switch'
11+
import { Search } from './components/search'
1112

1213
import { getPageData } from './modules/sanity/utils'
1314

@@ -26,6 +27,7 @@ const render = async () => {
2627
const sidenav = document.querySelector('button.fern-search-bar')
2728
?.parentElement as HTMLElement
2829

30+
const searchButton = document.querySelector('button.fern-search-button')
2931

3032
const theme = document.getElementsByTagName('html')[0].getAttribute('class')
3133

@@ -36,6 +38,21 @@ const render = async () => {
3638
ReactDOM.render(React.createElement(ThemeSwitch), wrapper)
3739
}
3840

41+
// Replace search button with React Search component
42+
if (searchButton && !document.getElementById('search-component-wrapper') && !document.getElementById('theme-switch')) {
43+
const searchWrapper = document.createElement('div')
44+
searchWrapper.setAttribute('id', 'search-component-wrapper')
45+
searchWrapper.setAttribute('class', 'fern-sidebar-searchbar-container')
46+
searchButton.parentNode?.replaceChild(searchWrapper, searchButton)
47+
48+
const wrapper = document.createElement('div')
49+
wrapper.setAttribute('id', 'theme-switch')
50+
sidenav.appendChild(wrapper)
51+
52+
ReactDOM.render(React.createElement(Search), searchWrapper)
53+
ReactDOM.render(React.createElement(ThemeSwitch), wrapper)
54+
}
55+
3956
const fernHeaderId = document.getElementById(FERN_CONTENT_WRAPPER_ID)
4057
const devrevHeaderId = document.getElementById(DEVREV_CONTENT_WRAPPER_ID)
4158

0 commit comments

Comments
 (0)