From 9a42eda579d9c9efd8fc6840be2a9272990c605e Mon Sep 17 00:00:00 2001 From: jean-philippe martel Date: Thu, 10 Oct 2024 20:29:16 -0400 Subject: [PATCH 1/3] initial code to add the search functionnality onto the a small viewport --- .../src/components/docsearch/doc-search.tsx | 54 +++++++++++++------ .../docs/src/components/header/header.tsx | 9 +++- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/packages/docs/src/components/docsearch/doc-search.tsx b/packages/docs/src/components/docsearch/doc-search.tsx index 4a809788590..01edc6e5bee 100644 --- a/packages/docs/src/components/docsearch/doc-search.tsx +++ b/packages/docs/src/components/docsearch/doc-search.tsx @@ -9,9 +9,11 @@ import { type Signal, $, sync$, + useTask$, } from '@builder.io/qwik'; +import { Modal } from '@qwik-ui/headless'; import type { DocSearchHit, InternalDocSearchHit } from './types'; -import { type ButtonTranslations, DocSearchButton } from './doc-search-button'; +import { type ButtonTranslations } from './doc-search-button'; import { DocSearchModal, type ModalTranslations } from './doc-search-modal'; import styles from './doc-search.css?inline'; @@ -71,8 +73,16 @@ export const DocSearch = component$((props: DocSearchProps) => { status: 'idle', snippetLength: 10, }); - const searchButtonRef = useSignal(); + const isOpen = useSignal(false); + + useTask$((ctx) => { + ctx.track(() => { + if (state.isOpen != isOpen.value) { + isOpen.value = state.isOpen; + } + }); + }); return (
{ // another one. if (!document.body.classList.contains('DocSearch--active')) { state.isOpen = true; + isOpen.value = true; } } if ( @@ -102,6 +113,7 @@ export const DocSearch = component$((props: DocSearchProps) => { event.preventDefault(); if (state.isOpen) { state.isOpen = false; + isOpen.value = false; } else if (!document.body.classList.contains('DocSearch--active')) { open(); } @@ -110,27 +122,35 @@ export const DocSearch = component$((props: DocSearchProps) => { if (searchButtonRef && searchButtonRef.value === document.activeElement) { if (/[a-zA-Z0-9]/.test(String.fromCharCode(event.keyCode))) { state.isOpen = true; + isOpen.value = true; state.initialQuery = event.key; } } }), ]} > - { - state.isOpen = true; - }} - /> - {state.isOpen && ( - - )} + + { + state.isOpen = true; + isOpen.value = true; + }} + > + Search + + + {state.isOpen && ( + + )} + +
); }); diff --git a/packages/docs/src/components/header/header.tsx b/packages/docs/src/components/header/header.tsx index a9aa3a20816..5dad5cc0392 100644 --- a/packages/docs/src/components/header/header.tsx +++ b/packages/docs/src/components/header/header.tsx @@ -44,6 +44,13 @@ export const Header = component$(() => { +
+ +
+ ); +}); export const Header = component$(() => { useStyles$(styles); + const shouldActivate = useSignal(false); const globalStore = useContext(GlobalStore); const pathname = useLocation().url.pathname; @@ -44,11 +71,12 @@ export const Header = component$(() => { -
- + { + shouldActivate.value = true; + }} + className=" absolute right-10 lg:hidden" />
); @@ -134,15 +126,13 @@ export const Header = component$(() => { Shop -
  • +
  • -