Skip to content

Commit

Permalink
perf(theme): appHeader navigation improvement
Browse files Browse the repository at this point in the history
- add @nuxtjs/device package to recognize used device
- remove mobile observer from header and replave it with device recognition tool

- navigation is now lazy loaded, categories required for a navigation are not loaded at all on
mobile devices
  • Loading branch information
bartoszherba committed Feb 18, 2022
1 parent ac62c4a commit ebf0188
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 30 deletions.
46 changes: 17 additions & 29 deletions packages/theme/components/AppHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
/>
</nuxt-link>
</template>
<template #navigation>
<SfHeaderNavigationItem

<template
v-if="$device.isDesktop"
#navigation
>
<HeaderNavigationItem
v-for="(category, index) in categoryTree"
:key="index"
v-e2e="'app-header-url_women'"
Expand Down Expand Up @@ -120,7 +124,7 @@
v-if="!!term"
class="sf-search-bar__button sf-button--pure"
aria-label="Close search"
@click="closeOrFocusSearchBar"
@click="closeSearch"
>
<span class="sf-search-bar__icon">
<SfIcon
Expand Down Expand Up @@ -170,6 +174,7 @@ import {
SfSearchBar,
SfOverlay,
} from '@storefront-ui/vue';
import {
categoryGetters,
useCart,
Expand All @@ -186,14 +191,15 @@ import {
defineComponent,
useRouter,
useContext,
useAsync,
useFetch,
} from '@nuxtjs/composition-api';
import { clickOutside } from '@storefront-ui/vue/src/utilities/directives/click-outside/click-outside-directive.js';
import {
mapMobileObserver,
unMapMobileObserver,
} from '@storefront-ui/vue/src/utilities/mobile-observer.js';
import debounce from 'lodash.debounce';
import HeaderNavigationItem from './navigation/HeaderNavigationItem';
import {
useUiHelpers,
useUiState,
Expand All @@ -203,6 +209,7 @@ import CurrencySelector from '~/components/CurrencySelector.vue';
export default defineComponent({
components: {
HeaderNavigationItem,
SfHeader,
CurrencySelector,
StoreSwitcher,
Expand Down Expand Up @@ -254,10 +261,6 @@ export default defineComponent({
}
};
useAsync(async () => {
await Promise.all([loadCartTotalQty(), loadWishlistItemsCount(), categoriesListSearch({ pageSize: 20 })]);
});
const showSearch = () => {
if (!isSearchOpen.value) {
isSearchOpen.value = true;
Expand Down Expand Up @@ -293,29 +296,16 @@ export default defineComponent({
};
}, 1000);
const isMobile = computed(() => mapMobileObserver().isMobile.get());
const closeOrFocusSearchBar = () => {
if (isMobile.value) {
return closeSearch();
}
term.value = '';
return searchBarRef.value.$el.children[0].focus();
};
watch(() => term.value, (newVal, oldVal) => {
const shouldSearchBeOpened = (!isMobile.value && term.value.length > 0)
&& ((!oldVal && newVal)
|| (newVal.length !== oldVal.length
&& isSearchOpen.value === false));
if (shouldSearchBeOpened) isSearchOpen.value = true;
});
const removeSearchResults = () => {
result.value = null;
};
useFetch(async () => {
if (app.$device.isDesktop) {
await Promise.all([loadCartTotalQty(), loadWishlistItemsCount(), categoriesListSearch({ pageSize: 20 })]);
}
});
onBeforeUnmount(() => {
unMapMobileObserver();
});
Expand All @@ -324,15 +314,13 @@ export default defineComponent({
accountIcon,
cartTotalItems,
categoryTree,
closeOrFocusSearchBar,
closeSearch,
showSearch,
hideSearch,
getAgnosticCatLink,
handleAccountClick,
handleSearch,
isAuthenticated,
isMobile,
isSearchOpen,
removeSearchResults,
result,
Expand Down
37 changes: 37 additions & 0 deletions packages/theme/components/Navigation/HeaderNavigationItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<template>
<div class="sf-header-navigation-item">
<div class="sf-header-navigation-item__item sf-header-navigation-item__item--desktop">
<slot name="desktop-navigation-item">
<SfLink
class="sf-header-navigation-item__link"
:link="link"
>
{{
label
}}
</SfLink>
</slot>
<slot />
</div>
</div>
</template>
<script>
import { SfLink } from '@storefront-ui/vue';
export default {
name: 'HeaderNavigationItem',
components: {
SfLink,
},
props: {
label: {
type: String,
default: '',
},
link: {
type: [String, Object],
default: '',
},
},
};
</script>
6 changes: 5 additions & 1 deletion packages/theme/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ export default () => {
],
},
loading: { color: '#fff' },
device: {
refreshOnResize: true,
},
buildModules: [
// to core
'@nuxtjs/composition-api/module',
'@nuxt/typescript-build',
'@nuxtjs/google-fonts',
'@nuxtjs/pwa',
'@nuxtjs/style-resources',
'@nuxtjs/device',
['@vue-storefront/nuxt', {
// @core-development-only-start
coreDevelopment: true,
Expand Down Expand Up @@ -90,7 +94,7 @@ export default () => {
magentoBaseUrl,
imageProvider,
magentoApiEndpoint,
customApolloHttpLinkOptions
customApolloHttpLinkOptions,
}],
'@nuxt/image',
],
Expand Down
1 change: 1 addition & 0 deletions packages/theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@babel/core": "^7.16.12",
"@nuxt/types": "latest",
"@nuxt/typescript-build": "latest",
"@nuxtjs/device": "^2.1.0",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/user-event": "^13.5.0",
"@testing-library/vue": "^5.8.2",
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3190,6 +3190,13 @@
unplugin-vue2-script-setup "^0.7.3"
upath "^2.0.1"

"@nuxtjs/device@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@nuxtjs/device/-/device-2.1.0.tgz#552c668b2d82dc982d18429845777c19d48d87d6"
integrity sha512-TYBdt1w2bmCCWp+MhgcBATZtqyUBi3nSdNpcLGILw5USLwCsC/yZtIkq9YisuEzuRnod9w/RZpoE80MxLftEuA==
dependencies:
defu "^3.2.2"

"@nuxtjs/google-fonts@^1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@nuxtjs/google-fonts/-/google-fonts-1.3.0.tgz#36cefee4aef4f8b772180c7ed7cc902f02f585c4"
Expand Down

0 comments on commit ebf0188

Please sign in to comment.