Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 527
fix(ui): allow dismissing mobile search input#2021
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| <script setup lang="ts"> | ||
| import { onClickOutside } from '@vueuse/core' | ||
| import { LinkBase } from '#components' | ||
| import type { NavigationConfig, NavigationConfigWithGroups } from '~/types' | ||
| import { isEditableElement } from '~/utils/input' | ||
| @@ -143,6 +144,7 @@ const route = useRoute() | ||
| const isMobile = useIsMobile() | ||
| const isSearchExpandedManually = shallowRef(false) | ||
| const searchBoxRef = useTemplateRef('searchBoxRef') | ||
| const headerRef = useTemplateRef('headerRef') | ||
| // On search page, always show search expanded on mobile | ||
| const isOnHomePage = computed(() => route.name === 'index') | ||
| @@ -156,6 +158,21 @@ function expandMobileSearch() { | ||
| }) | ||
| } | ||
| function collapseMobileSearch() { | ||
| if (!isMobile.value) return | ||
| if (isOnSearchPage.value) return | ||
| isSearchExpandedManually.value = false | ||
| if (document.activeElement instanceof HTMLElement) { | ||
| document.activeElement.blur() | ||
| } | ||
| } | ||
| onClickOutside(headerRef, () => { | ||
| if (isMobile.value && isSearchExpanded.value && !isOnSearchPage.value) { | ||
| collapseMobileSearch() | ||
| } | ||
| }) | ||
| watch( | ||
| isOnSearchPage, | ||
| visible => { | ||
| @@ -171,13 +188,6 @@ watch( | ||
| function handleSearchBlur() { | ||
| showFullSearch.value = false | ||
| // Collapse expanded search on mobile after blur (with delay for click handling) | ||
| // But don't collapse if we're on the search page | ||
| if (isMobile.value && !isOnSearchPage.value) { | ||
| setTimeout(() => { | ||
| isSearchExpandedManually.value = false | ||
| }, 150) | ||
| } | ||
| } | ||
| function handleSearchFocus() { | ||
| @@ -200,10 +210,22 @@ onKeyStroke( | ||
| }, | ||
| { dedupe: true }, | ||
| ) | ||
| onKeyStroke( | ||
| e => | ||
| isKeyWithoutModifiers(e, 'Escape') && | ||
| isMobile.value && | ||
| isSearchExpanded.value && | ||
| !isOnSearchPage.value, | ||
| e => { | ||
| e.preventDefault() | ||
| collapseMobileSearch() | ||
| }, | ||
| ) | ||
| </script> | ||
| <template> | ||
| <header class="sticky top-0 z-50 border-b border-border"> | ||
| <header ref="headerRef" class="sticky top-0 z-50 border-b border-border"> | ||
| <div class="absolute inset-0 bg-bg/80 backdrop-blur-md" /> | ||
| <nav | ||
| :aria-label="$t('nav.main_navigation')" | ||
| @@ -301,15 +323,25 @@ onKeyStroke( | ||
| <!-- Mobile: Search button (expands search) --> | ||
| <ButtonBase | ||
| v-if="!isSearchExpanded && !isOnHomePage" | ||
| type="button" | ||
| class="sm:hidden ms-auto" | ||
| :aria-label="$t('nav.tap_to_search')" | ||
| :aria-expanded="showMobileMenu" | ||
| :aria-expanded="isSearchExpanded" | ||
| @click="expandMobileSearch" | ||
| v-if="!isSearchExpanded && !isOnHomePage" | ||
| classicon="i-lucide:search" | ||
| /> | ||
| <!-- Mobile: Close search button (collapses search) --> | ||
| <ButtonBase | ||
| v-if="isSearchExpanded && !isOnSearchPage" | ||
| type="button" | ||
| class="sm:hidden flex-shrink-0" | ||
| :aria-label="$t('nav.close_search')" | ||
| @click="collapseMobileSearch" | ||
| classicon="i-lucide:x" | ||
| /> | ||
Comment on lines
+335
to
+343
| ||
| <!-- Mobile: Menu button (always visible, click to open menu) --> | ||
| <ButtonBase | ||
| type="button" | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this could be extracted, since we duplicate this complex condition in a few places? it would also be more expressive by self-documenting with a name