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 64
Add desktop content switcher to the home page #613
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/components/VContentSwitcher/VContentSwitcherButton.vue
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,86 @@ | ||
<template> | ||
<VButton | ||
class="flex flex-row font-semibold px-3 py-2 text-sr md:text-base" | ||
:class="{ 'w-12': isIconButton }" | ||
:variant="buttonVariant" | ||
size="disabled" | ||
:aria-label="buttonLabel" | ||
v-bind="a11yProps" | ||
@click="$emit('click')" | ||
> | ||
<VIcon :icon-path="icon" /> | ||
<span v-show="showLabel" :class="{ 'ms-2': showLabel }">{{ | ||
buttonLabel | ||
}}</span> | ||
<VIcon | ||
v-show="isMinScreenMd" | ||
class="text-dark-charcoal-40" | ||
:class="{ 'ms-2': isMinScreenMd }" | ||
:icon-path="caretDownIcon" | ||
/> | ||
</VButton> | ||
</template> | ||
<script> | ||
import { computed, inject, useContext } from '@nuxtjs/composition-api' | ||
import useContentType from '~/composables/use-content-type' | ||
import caretDownIcon from '~/assets/icons/caret-down.svg' | ||
|
||
import VButton from '~/components/VButton.vue' | ||
import VIcon from '~/components/VIcon/VIcon.vue' | ||
import { isMinScreen } from '@/composables/use-media-query' | ||
import { ALL_MEDIA, AUDIO, IMAGE } from '@/constants/media' | ||
|
||
export default { | ||
name: 'VContentSwitcherButton', | ||
components: { VButton, VIcon }, | ||
props: { | ||
a11yProps: { | ||
type: Object, | ||
required: true, | ||
}, | ||
activeItem: { | ||
type: String, | ||
default: ALL_MEDIA, | ||
validator: (val) => [ALL_MEDIA, IMAGE, AUDIO].includes(val), | ||
}, | ||
}, | ||
setup(props) { | ||
const { i18n } = useContext() | ||
const isHeaderScrolled = inject('isHeaderScrolled', null) | ||
const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: true }) | ||
|
||
const { icons, activeType: activeItem } = useContentType() | ||
const isIconButton = computed( | ||
() => isHeaderScrolled?.value && !isMinScreenMd.value | ||
) | ||
const buttonVariant = computed(() => { | ||
return isMinScreenMd.value && !isHeaderScrolled?.value | ||
? 'tertiary' | ||
: 'action-menu' | ||
}) | ||
const buttonLabel = computed(() => { | ||
const labelKey = { | ||
image: 'search-type.image', | ||
audio: 'search-type.audio', | ||
all: 'search-type.all', | ||
video: 'search-type.video', | ||
}[props.activeItem] | ||
return i18n.t(labelKey) | ||
}) | ||
const showLabel = computed( | ||
() => isMinScreenMd.value || !isHeaderScrolled.value | ||
) | ||
|
||
return { | ||
buttonVariant, | ||
isIconButton, | ||
buttonLabel, | ||
caretDownIcon, | ||
showLabel, | ||
isHeaderScrolled, | ||
isMinScreenMd, | ||
icon: computed(() => icons[activeItem.value]), | ||
} | ||
}, | ||
} | ||
</script> |
82 changes: 82 additions & 0 deletions
82
src/components/VContentSwitcher/VContentSwitcherPopover.vue
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,82 @@ | ||
<template> | ||
<VPopover | ||
ref="contentMenuPopover" | ||
class="flex mx-4 items-stretch" | ||
:label="$t('search-type.label')" | ||
> | ||
<template #trigger="{ a11yProps }"> | ||
<VContentSwitcherButton | ||
:a11y-props="a11yProps" | ||
:active-item="activeItem" | ||
/> | ||
</template> | ||
<VItemGroup | ||
direction="vertical" | ||
:bordered="false" | ||
type="radiogroup" | ||
class="z-10" | ||
> | ||
<VItem | ||
v-for="(item, idx) in content.types" | ||
:key="idx" | ||
:selected="item === activeItem" | ||
:is-first="idx === 0" | ||
@click.native="handleClick(item)" | ||
> | ||
<VIcon :icon-path="content.icons[item]" class="me-2 ms-4 my-4" /> | ||
<span class="pe-20 py-4 font-semibold">{{ | ||
$t(`search-type.${item}`) | ||
}}</span> | ||
</VItem> | ||
</VItemGroup> | ||
</VPopover> | ||
</template> | ||
|
||
<script> | ||
import { ref } from '@nuxtjs/composition-api' | ||
import useContentType from '~/composables/use-content-type' | ||
import checkIcon from '~/assets/icons/checkmark.svg' | ||
|
||
import VPopover from '~/components/VPopover/VPopover.vue' | ||
import VContentSwitcherButton from '~/components/VContentSwitcher/VContentSwitcherButton.vue' | ||
|
||
export default { | ||
name: 'VContentSwitcherPopover', | ||
components: { | ||
VContentSwitcherButton, | ||
VPopover, | ||
}, | ||
props: { | ||
activeItem: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
setup(props, { emit }) { | ||
const content = useContentType() | ||
|
||
const contentMenuPopover = ref(null) | ||
|
||
/** | ||
* Only the contentMenuPopover needs to be closed programmatically | ||
*/ | ||
const closeMenu = () => { | ||
if (contentMenuPopover.value) { | ||
contentMenuPopover.value.close() | ||
} | ||
} | ||
|
||
const handleClick = (item) => { | ||
emit('select', item) | ||
} | ||
|
||
return { | ||
content, | ||
checkIcon, | ||
handleClick, | ||
contentMenuPopover, | ||
closeMenu, | ||
} | ||
}, | ||
} | ||
</script> |
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,34 @@ | ||
import { computed, ref, useContext } from '@nuxtjs/composition-api' | ||
import { supportedContentTypes } from '~/constants/media' | ||
import { SEARCH } from '~/constants/store-modules' | ||
import { UPDATE_QUERY } from '~/constants/action-types' | ||
import allIcon from '~/assets/icons/all-content.svg' | ||
import audioIcon from '~/assets/icons/audio-content.svg' | ||
import imageIcon from '~/assets/icons/image-content.svg' | ||
|
||
const icons = { | ||
all: allIcon, | ||
audio: audioIcon, | ||
image: imageIcon, | ||
} | ||
export default function useContentType() { | ||
const { store } = useContext() | ||
|
||
const contentTypes = [...supportedContentTypes] | ||
|
||
const activeType = computed(() => store.state.search.searchType) | ||
const previousContentType = ref(activeType.value) | ||
const setActiveType = async (contentType) => { | ||
if (previousContentType.value === contentType) return | ||
await store.dispatch(`${SEARCH}/${UPDATE_QUERY}`, { | ||
searchType: contentType, | ||
}) | ||
previousContentType.value = contentType | ||
} | ||
return { | ||
setActiveType, | ||
activeType, | ||
types: contentTypes, | ||
icons, | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I always liked this approach to mapping variables to a different result (using an object inline). 🙂