This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
145 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { computed, watch } from '@nuxtjs/composition-api' | ||
|
||
import { useUiStore } from '~/stores/ui' | ||
import { useFeatureFlagStore } from '~/stores/feature-flag' | ||
import { useCookies } from '~/composables/use-cookies' | ||
import { isMinScreen } from '~/composables/use-media-query' | ||
|
||
import type { NuxtAppOptions } from '@nuxt/types' | ||
|
||
/** | ||
* This composable updates the UI store when the screen width changes or | ||
* when the SSR layout settings are different from the cookie settings. | ||
* | ||
* The threshold for switching between mobile and desktop layout is | ||
* `lg` for the `new_header` and `md` for the `old_header`. | ||
*/ | ||
export function useLayout({ app }: { app: NuxtAppOptions }) { | ||
const uiStore = useUiStore() | ||
const featureFlagStore = useFeatureFlagStore() | ||
|
||
const cookies = useCookies(app) | ||
|
||
const isNewHeaderEnabled = computed(() => featureFlagStore.isOn('new_header')) | ||
|
||
// `isMobile` is set in the middleware for each server request. | ||
const shouldPassInSSR = uiStore.isDesktopLayout | ||
const desktopBreakpoint = computed(() => | ||
isNewHeaderEnabled.value ? 'lg' : 'md' | ||
) | ||
|
||
const isDesktopLayout = isMinScreen(desktopBreakpoint, { shouldPassInSSR }) | ||
|
||
watch(isDesktopLayout, (isDesktop) => { | ||
updateLayout(isDesktop) | ||
}) | ||
|
||
const updateLayout = (isDesktop: boolean) => { | ||
if (isDesktop !== uiStore.isDesktopLayout) { | ||
uiStore.updateBreakpoint(isDesktop, cookies.set) | ||
} | ||
} | ||
|
||
return { | ||
isDesktopLayout, | ||
updateLayout, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { FeatureState } from '~/constants/feature-flag' | ||
|
||
export type SnackbarState = 'not_shown' | 'visible' | 'dismissed' | ||
|
||
/** | ||
* The cookies that Openverse uses to store the UI state. | ||
*/ | ||
export interface OpenverseCookies { | ||
/** | ||
* The state of the instructions snackbar for audio component. | ||
*/ | ||
uiInstructionsSnackbarState?: SnackbarState | ||
/** | ||
* Whether the filters were dismissed on desktop layout. | ||
*/ | ||
uiIsFilterDismissed?: boolean | ||
/** | ||
* Whether the site layout is desktop (or mobile). | ||
*/ | ||
uiIsDesktopLayout?: boolean | ||
/** | ||
* Whether the request user agent is mobile or not. | ||
*/ | ||
uiIsMobileUa?: boolean | ||
/** | ||
* The state of the feature flags. | ||
*/ | ||
features?: Record<string, FeatureState> | ||
} |
Oops, something went wrong.