Skip to content

Commit

Permalink
🔎 Fix bugs in static search (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Sep 24, 2024
1 parent 4447e95 commit 23e9f9a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/odd-queens-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@myst-theme/providers': patch
'@myst-theme/site': patch
---

add useNavigateProvider
20 changes: 17 additions & 3 deletions packages/providers/src/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type ThemeContextType = {
top?: number;
Link?: Link;
NavLink?: NavLink;
navigate?: (to: string) => void;
};

const ThemeContext = React.createContext<ThemeContextType | undefined>(undefined);
Expand All @@ -64,22 +65,24 @@ export function ThemeProvider({
children,
renderers,
Link,
top,
NavLink,
navigate,
top,
}: {
theme: Theme | null;
setTheme: SetThemeType;
children: React.ReactNode;
renderers?: NodeRenderers;
Link?: Link;
top?: number;
NavLink?: NavLink;
navigate?: (to: string) => void;
top?: number;
}) {
const validatedRenderers = validateRenderers(renderers);

return (
<ThemeContext.Provider
value={{ theme, setTheme, renderers: validatedRenderers, Link, NavLink, top }}
value={{ theme, setTheme, renderers: validatedRenderers, Link, NavLink, navigate, top }}
>
{children}
</ThemeContext.Provider>
Expand Down Expand Up @@ -131,6 +134,17 @@ export function useNavLinkProvider(): NavLink {
return NavLink ?? HtmlNavLink;
}

export function useNavigateProvider(): (to: string) => void {
const context = React.useContext(ThemeContext);
const { navigate } = context ?? {};
return (
navigate ??
((to: string) => {
window.location.href = to;
})
);
}

export function useThemeTop(): number {
const context = React.useContext(ThemeContext);
const { top } = context ?? {};
Expand Down
11 changes: 7 additions & 4 deletions packages/site/src/components/Navigation/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState, useMemo, useCallback, useRef, forwardRef } from 'react';
import type { KeyboardEventHandler, Dispatch, SetStateAction, FormEvent, MouseEvent } from 'react';
import { useNavigate, useFetcher } from '@remix-run/react';
import { useFetcher } from '@remix-run/react';
import {
ArrowTurnDownLeftIcon,
MagnifyingGlassIcon,
Expand All @@ -20,6 +20,7 @@ import {
useLinkProvider,
withBaseurl,
useBaseurl,
useNavigateProvider,
} from '@myst-theme/providers';

/**
Expand Down Expand Up @@ -182,6 +183,7 @@ function SearchResultItem({
closeSearch?: () => void;
}) {
const { hierarchy, type, url, queries } = result;
const baseurl = useBaseurl();
const Link = useLinkProvider();

// Render the icon
Expand Down Expand Up @@ -219,7 +221,7 @@ function SearchResultItem({
return (
<Link
className="block px-1 py-2 text-gray-700 rounded shadow-md dark:text-white group-aria-selected:bg-blue-600 group-aria-selected:text-white dark:shadow-none dark:bg-stone-800"
to={url}
to={withBaseurl(url, baseurl)}
// Close the main search on click
onClick={closeSearch}
>
Expand Down Expand Up @@ -415,7 +417,8 @@ function SearchForm({
setQuery(event.target.value);
}, []);
// Handle item selection
const navigate = useNavigate();
const navigate = useNavigateProvider();
const baseurl = useBaseurl();

// Handle item selection and navigation
const handleSearchKeyPress = useCallback<KeyboardEventHandler<HTMLInputElement>>(
Expand All @@ -434,7 +437,7 @@ function SearchForm({

const url = searchResults[selectedIndex]?.url;
if (url) {
navigate(url);
navigate(withBaseurl(url, baseurl));
closeSearch?.();
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/site/src/pages/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
NavLink,
useRouteError,
isRouteErrorResponse,
useNavigate,
} from '@remix-run/react';
import {
DEFAULT_NAV_HEIGHT,
Expand Down Expand Up @@ -53,6 +54,7 @@ export function Document({
top?: number;
renderers?: NodeRenderers;
}) {
const navigate = useNavigate();
const links = staticBuild
? {
Link: (props: any) => <Link {...{ ...props, reloadDocument: true }} />,
Expand All @@ -61,6 +63,7 @@ export function Document({
: {
Link: Link as any,
NavLink: NavLink as any,
navigate,
};

// (Local) theme state driven by SSR and cookie/localStorage
Expand Down

0 comments on commit 23e9f9a

Please sign in to comment.