Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Convert components for license explanation modal (#4801)

Olga Bulat967f885ea363
diff --git a/frontend/src/components/VContentSwitcher/VSearchTypePopover.vue b/frontend/src/components/VContentSwitcher/VSearchTypePopover.vue
index f9d914234..9698719aa 100644
--- a/frontend/src/components/VContentSwitcher/VSearchTypePopover.vue
+++ b/frontend/src/components/VContentSwitcher/VSearchTypePopover.vue
@@ -1,55 +1,39 @@
-<script lang="ts">
-import { computed, defineComponent, PropType, ref } from "vue"
+<script setup lang="ts">
+import { computed, ref } from "vue"
import useSearchType from "~/composables/use-search-type"
import type { SearchType } from "~/constants/media"
-import { defineEvent } from "~/types/emits"
-
import VPopover from "~/components/VPopover/VPopover.vue"
import VSearchTypeButton from "~/components/VContentSwitcher/VSearchTypeButton.vue"
import VSearchTypes from "~/components/VContentSwitcher/VSearchTypes.vue"
-export default defineComponent({
- name: "VSearchTypePopover",
- components: {
- VPopover,
- VSearchTypeButton,
- VSearchTypes,
- },
- props: {
- showLabel: {
- type: Boolean,
- default: false,
- },
- placement: {
- type: String as PropType<"header" | "searchbar">,
- default: "header",
- },
- },
- emits: {
- select: defineEvent<[SearchType]>(),
- },
- setup(_, { emit }) {
- const contentMenuPopover = ref<InstanceType<typeof VPopover> | null>(null)
+withDefaults(
+ defineProps<{
+ showLabel?: boolean
+ placement?: "header" | "searchbar"
+ }>(),
+ {
+ showLabel: false,
+ placement: "header",
+ }
+)
+
+const emit = defineEmits<{
+ select: [SearchType]
+}>()
- const { getSearchTypeProps } = useSearchType()
+const contentMenuPopover = ref<InstanceType<typeof VPopover> | null>(null)
- const searchTypeProps = computed(() => getSearchTypeProps())
+const { getSearchTypeProps } = useSearchType()
- const handleSelect = (searchType: SearchType) => {
- emit("select", searchType)
- contentMenuPopover.value?.close()
- }
+const searchTypeProps = computed(() => getSearchTypeProps())
- return {
- handleSelect,
- contentMenuPopover,
- searchTypeProps,
- }
- },
-})
+const handleSelect = (searchType: SearchType) => {
+ emit("select", searchType)
+ contentMenuPopover.value?.close()
+}
</script>
<template>
diff --git a/frontend/src/components/VItemGroup/VItem.vue b/frontend/src/components/VItemGroup/VItem.vue
index 89bb6911d..c2d88c361 100644
--- a/frontend/src/components/VItemGroup/VItem.vue
+++ b/frontend/src/components/VItemGroup/VItem.vue
@@ -7,10 +7,10 @@ import {
VItemGroupContextKey,
VItemGroupFocusContextKey,
} from "~/types/item-group"
+import { VPopoverContentContextKey } from "~/types/provides"
import VButton from "~/components/VButton.vue"
import VIcon from "~/components/VIcon/VIcon.vue"
-import { VPopoverContentContextKey } from "~/components/VPopover/VPopoverContent.vue"
export default defineComponent({
name: "VItem",
diff --git a/frontend/src/components/VLicense/VLicenseElements.vue b/frontend/src/components/VLicense/VLicenseElements.vue
index e88a9d756..623402f19 100644
--- a/frontend/src/components/VLicense/VLicenseElements.vue
+++ b/frontend/src/components/VLicense/VLicenseElements.vue
@@ -1,7 +1,7 @@
-<script lang="ts">
+<script setup lang="ts">
import { useI18n } from "#imports"
-import { computed, defineComponent, PropType } from "vue"
+import { computed } from "vue"
import type { License } from "~/constants/license"
import { useUiStore } from "~/stores/ui"
@@ -10,49 +10,35 @@ import { getElements } from "~/utils/license"
import VIcon from "~/components/VIcon/VIcon.vue"
-export default defineComponent({
- name: "VLicenseElements",
- components: { VIcon },
- props: {
+const props = withDefaults(
+ defineProps<{
/**
* the slug of the license
* @values ALL_LICENSES
*/
- license: {
- type: String as PropType<License>,
- required: true,
- },
+ license: License
/**
* the size of the icons and text
*/
- size: {
- type: String as PropType<"big" | "small">,
- default: "big",
- },
- },
- setup(props) {
- const i18n = useI18n({ useScope: "global" })
- const elementNames = computed(() =>
- getElements(props.license).filter((icon) => icon !== "cc")
- )
+ size?: "big" | "small"
+ }>(),
+ {
+ size: "big",
+ }
+)
- const isSmall = computed(() => props.size === "small")
- const uiStore = useUiStore()
- const isMobile = computed(() => !uiStore.isDesktopLayout)
+const i18n = useI18n({ useScope: "global" })
+const elementNames = computed(() =>
+ getElements(props.license).filter((icon) => icon !== "cc")
+)
- const getLicenseDescription = (element: string) => {
- return i18n.t(`browsePage.licenseDescription.${camelCase(element)}`)
- }
+const isSmall = computed(() => props.size === "small")
+const uiStore = useUiStore()
+const isMobile = computed(() => !uiStore.isDesktopLayout)
- return {
- elementNames,
- isSmall,
- isMobile,
-
- getLicenseDescription,
- }
- },
-})
+const getLicenseDescription = (element: string) => {
+ return i18n.t(`browsePage.licenseDescription.${camelCase(element)}`)
+}
</script>
<template>
diff --git a/frontend/src/components/VPopover/VPopover.vue b/frontend/src/components/VPopover/VPopover.vue
index 72a447df5..d1b87223c 100644
--- a/frontend/src/components/VPopover/VPopover.vue
+++ b/frontend/src/components/VPopover/VPopover.vue
@@ -5,7 +5,7 @@
*/
import { ref, computed, SetupContext } from "vue"
-import { zIndexValidator } from "~/constants/z-indices"
+import { type ZIndex, zIndexValidator } from "~/constants/z-indices"
import { useDialogControl } from "~/composables/use-dialog-control"
@@ -78,7 +78,7 @@ const props = withDefaults(
/**
* the z-index to apply to the popover content
*/
- zIndex?: number | string
+ zIndex?: ZIndex
/**
* Whether the popover height should be clipped and made scrollable
* if the window height is too small.
diff --git a/frontend/src/components/VPopover/VPopoverContent.vue b/frontend/src/components/VPopover/VPopoverContent.vue
index 2956309df..cefc7392a 100644
--- a/frontend/src/components/VPopover/VPopoverContent.vue
+++ b/frontend/src/components/VPopover/VPopoverContent.vue
@@ -1,102 +1,61 @@
-<script lang="ts">
-import {
- defineComponent,
- toRefs,
- ref,
- provide,
- InjectionKey,
- PropType,
-} from "vue"
+<script setup lang="ts">
+import { provide, ref, toRefs, useAttrs } from "vue"
import { usePopoverContent } from "~/composables/use-popover-content"
-import { defineEvent } from "~/types/emits"
-import { zIndexValidator } from "~/constants/z-indices"
+import type { ZIndex } from "~/constants/z-indices"
-import type { Placement, Strategy } from "@floating-ui/dom"
+import { VPopoverContentContextKey } from "~/types/provides"
+import type { Placement, Strategy } from "@floating-ui/dom"
import type { SetupContext } from "vue"
-export const VPopoverContentContextKey = Symbol(
- "VPopoverContentContextKey"
-) as InjectionKey<boolean>
+const props = withDefaults(
+ defineProps<{
+ visible: boolean
+ hide: () => void
+ hideOnEsc?: boolean
+ hideOnClickOutside?: boolean
+ autoFocusOnShow?: boolean
+ autoFocusOnHide?: boolean
+ triggerElement: HTMLElement | null
+ placement?: Placement
+ strategy?: Strategy
+ zIndex: ZIndex
+ clippable?: boolean
+ trapFocus?: boolean
+ }>(),
+ {
+ hideOnEsc: true,
+ hideOnClickOutside: true,
+ autoFocusOnShow: true,
+ autoFocusOnHide: true,
+ placement: "bottom-end",
+ strategy: "absolute",
+ clippable: false,
+ trapFocus: true,
+ }
+)
+
+/**
+ * This is the only documented emitted event but in reality we pass through `$listeners`
+ * to the underlying element so anything and everything is emitted. `@keydown` is the
+ * only one this component overrides and controls (but ultimately still emits).
+ */
+const emit = defineEmits<{ keydown: []; blur: [] }>()
-export default defineComponent({
- name: "VPopoverContent",
- props: {
- visible: {
- type: Boolean,
- required: true,
- },
- hide: {
- type: Function as PropType<() => void>,
- required: true,
- },
- hideOnEsc: {
- type: Boolean,
- default: true,
- },
- hideOnClickOutside: {
- type: Boolean,
- default: true,
- },
- autoFocusOnShow: {
- type: Boolean,
- default: true,
- },
- autoFocusOnHide: {
- type: Boolean,
- default: true,
- },
- triggerElement: {
- type: (import.meta.server
- ? Object
- : HTMLElement) as PropType<HTMLElement | null>,
- required: true,
- },
- placement: {
- type: String as PropType<Placement>,
- default: "bottom-end",
- },
- strategy: {
- type: String as PropType<Strategy>,
- default: "absolute",
- },
- zIndex: {
- type: [String, Number],
- required: true,
- validator: zIndexValidator,
- },
- clippable: {
- type: Boolean,
- default: false,
- },
- trapFocus: {
- type: Boolean,
- default: true,
- },
- },
- /**
- * This is the only documented emitted event but in reality we pass through `$listeners`
- * to the underlying element so anything and everything is emitted. `@keydown` is the
- * only one this component overrides and controls (but ultimately still emits).
- */
- emits: { keydown: defineEvent(), blur: defineEvent() },
- setup(props, { emit, attrs }) {
- provide(VPopoverContentContextKey, true)
+const attrs = useAttrs()
- const propsRefs = toRefs(props)
- const popoverRef = ref<HTMLElement | null>(null)
+provide(VPopoverContentContextKey, true)
- const { onKeyDown, onBlur, heightProperties, style } = usePopoverContent({
- popoverRef,
- popoverPropsRefs: propsRefs,
- emit: emit as SetupContext["emit"],
- attrs,
- })
+const propsRefs = toRefs(props)
+const popoverRef = ref<HTMLElement | null>(null)
- return { popoverRef, onKeyDown, onBlur, heightProperties, style }
- },
+const { onKeyDown, onBlur, heightProperties, style } = usePopoverContent({
+ popoverRef,
+ popoverPropsRefs: propsRefs,
+ emit: emit as SetupContext["emit"],
+ attrs,
})
</script>
diff --git a/frontend/src/constants/z-indices.ts b/frontend/src/constants/z-indices.ts
index d1151605f..e3b36cba4 100644
--- a/frontend/src/constants/z-indices.ts
+++ b/frontend/src/constants/z-indices.ts
@@ -23,3 +23,5 @@ export const Z_INDICES = Object.freeze({
*/
export const zIndexValidator = (value: string | number): boolean =>
Object.keys(Z_INDICES).includes(value.toString())
+
+export type ZIndex = keyof typeof Z_INDICES
diff --git a/frontend/src/types/provides.ts b/frontend/src/types/provides.ts
index 54c664e02..4eb9da951 100644
--- a/frontend/src/types/provides.ts
+++ b/frontend/src/types/provides.ts
@@ -1,5 +1,8 @@
-import type { InjectionKey, Ref } from "vue"
+import { InjectionKey, Ref } from "vue"
export const IsHeaderScrolledKey = Symbol() as InjectionKey<Ref<boolean>>
export const IsSidebarVisibleKey = Symbol() as InjectionKey<Ref<boolean>>
export const ShowScrollButtonKey = Symbol() as InjectionKey<Ref<boolean>>
+export const VPopoverContentContextKey = Symbol(
+ "VPopoverContentContextKey"
+) as InjectionKey<boolean>