Skip to content
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

🔎 Fix bugs in static search #475

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading