Skip to content

Commit

Permalink
Merge branch 'search-form' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Oct 31, 2024
2 parents 81e104d + 8298087 commit 99a3d69
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 42 deletions.
5 changes: 4 additions & 1 deletion src/components/advanced-search-form/advanced-search-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ export function AdvancedSearchForm() {
</Section>
{resultingQuery ? (
<p>
Search query: <code>{resultingQuery}</code>
Search query:{' '}
<ButtonLink tag="code" onClick={onSearch}>
{resultingQuery}
</ButtonLink>
</p>
) : null}
{showFullDocs ? (
Expand Down
2 changes: 2 additions & 0 deletions src/components/advanced-search-form/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const usernameFilters = new Set([
'by:',
'from:',
'commented-by:',
'liked-by:',
'cliked-by:',
'in:',
'-in:',
'-from:',
Expand Down
43 changes: 7 additions & 36 deletions src/components/layout-header-search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@ const autocompleteAnchor = /(^|[^a-z\d])@|((from|to|author|by|in|commented-?by|l
export const HeaderSearchForm = withRouter(function HeaderSearchForm({ router, closeSearchForm }) {
const isWideScreen = useMediaQuery('(min-width: 700px)');
const isNarrowScreen = useMediaQuery('(max-width: 549px)');
const onSearchPage = router.routes[router.routes.length - 1].name === 'search';

const [searchExpanded, setSearchExpanded] = useState(false);
const [query, setQuery] = useState('');
const onQueryChange = useEvent(({ target }) => setQuery(target.value));

const fullSearchForm = isWideScreen;
const compactSearchForm = !fullSearchForm;
const collapsibleSearchForm = isNarrowScreen && (!onSearchPage || searchExpanded);

useEffect(() => {
if (!collapsibleSearchForm) {
setSearchExpanded(false);
}
}, [collapsibleSearchForm]);
const collapsibleSearchForm = isNarrowScreen;

const initialQuery = useInitialQuery(router);
const input = useRef(null);
Expand All @@ -49,13 +41,14 @@ export const HeaderSearchForm = withRouter(function HeaderSearchForm({ router, c
input.current.blur();
});

const onFocus = useEvent(() => isNarrowScreen && onSearchPage && setSearchExpanded(true));
const onKeyDown = useEvent((e) => e.keyCode === KEY_ESCAPE && input.current.blur());
const clearSearchForm = useEvent(() => (setQuery(''), input.current.focus()));

const focusHandlers = useDebouncedFocus({
onFocus,
onBlur: closeSearchForm,
const onBlur = useEvent((e) => {
// When the new focus is outside the search form
if (!e.currentTarget.contains(e.relatedTarget)) {
closeSearchForm();
}
});

useEffect(() => {
Expand All @@ -82,7 +75,7 @@ export const HeaderSearchForm = withRouter(function HeaderSearchForm({ router, c

return (
<form className={styles.searchForm} action="/search" onSubmit={onSubmit}>
<span className={styles.searchInputContainer} {...focusHandlers} tabIndex={0}>
<span className={styles.searchInputContainer} onBlur={onBlur} tabIndex={0}>
<span className={styles.searchInputBox}>
<input
className={styles.searchInput}
Expand Down Expand Up @@ -149,25 +142,3 @@ function useInitialQuery(router) {
}
}, [router.routes, router.params, router.location]);
}

function useDebouncedFocus({ onFocus: onFocusOrig, onBlur: onBlurOrig }, interval = 100) {
const focusTimer = useRef(0);
const blurTimer = useRef(0);

const cleanup = useEvent(() => {
window.clearTimeout(blurTimer.current);
window.clearTimeout(focusTimer.current);
});
useEffect(() => () => cleanup(), [cleanup]);

const onFocus = useEvent(() => {
cleanup();
focusTimer.current = window.setTimeout(onFocusOrig, interval);
});
const onBlur = useEvent(() => {
cleanup();
blurTimer.current = window.setTimeout(onBlurOrig, interval);
});

return { onFocus, onBlur };
}
9 changes: 4 additions & 5 deletions src/components/layout-header.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CONFIG */
import { IndexLink, Link, withRouter } from 'react-router';
import { IndexLink, Link } from 'react-router';
import { faBars, faSearch, faSignInAlt } from '@fortawesome/free-solid-svg-icons';
import { useCallback, useState } from 'react';
import cn from 'classnames';
Expand All @@ -12,9 +12,8 @@ import styles from './layout-header.module.scss';
import { SignInLink } from './sign-in-link';
import { HeaderSearchForm } from './layout-header-search';

export const LayoutHeader = withRouter(function LayoutHeader({ router }) {
export function LayoutHeader() {
const dispatch = useDispatch();
const onSearchPage = router.routes[router.routes.length - 1].name === 'search';
const isLayoutWithSidebar = useMediaQuery('(min-width: 992px)');
const isWideScreen = useMediaQuery('(min-width: 700px)');
const isNarrowScreen = useMediaQuery('(max-width: 549px)');
Expand All @@ -25,7 +24,7 @@ export const LayoutHeader = withRouter(function LayoutHeader({ router }) {

const fullSearchForm = isWideScreen;
const compactSearchForm = !fullSearchForm;
const collapsibleSearchForm = isNarrowScreen && (!onSearchPage || searchExpanded);
const collapsibleSearchForm = isNarrowScreen;

const openSearchForm = useCallback(() => setSearchExpanded(true), []);
const closeSearchForm = useCallback(() => setSearchExpanded(false), []);
Expand Down Expand Up @@ -106,4 +105,4 @@ export const LayoutHeader = withRouter(function LayoutHeader({ router }) {
)}
</header>
);
});
}
1 change: 1 addition & 0 deletions styles/helvetica/dark-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ body,
}

body,
legend,
.post .post-body .post-text,
.post .post-body,
.box-header,
Expand Down

0 comments on commit 99a3d69

Please sign in to comment.