Skip to content

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

2.1 KB
Raw Blame
<script setup lang="ts">
import { provide, ref, toRefs, useAttrs } from "vue"
import type { SetupContext } from "vue"

import type { ZIndex } from "#shared/constants/z-indices"
import { VPopoverContentContextKey } from "#shared/types/provides"
import { usePopoverContent } from "~/composables/use-popover-content"

import type { Placement, Strategy } from "@floating-ui/dom"

const props = withDefaults(
  defineProps<{
    id: string
    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: [] }>()

const attrs = useAttrs()

provide(VPopoverContentContextKey, true)

const propsRefs = toRefs(props)
const popoverRef = ref<HTMLElement | null>(null)

const { onKeyDown, onBlur, heightProperties, style } = usePopoverContent({
  popoverRef,
  popoverPropsRefs: propsRefs,
  emit: emit as SetupContext["emit"],
  attrs,
})
</script>

<template>
  <div
    v-show="visible"
    :id="id"
    ref="popoverRef"
    role="dialog"
    aria-modal="true"
    class="popover-content w-max-content absolute top-0 left-0 overflow-x-hidden overflow-y-auto rounded-xs border border-overlay bg-overlay shadow-sm"
    :class="`z-${zIndex}`"
    :style="{ ...heightProperties, ...style }"
    :tabindex="-1"
    :aria-hidden="!visible"
    @blur="onBlur"
    @keydown="onKeyDown"
  >
    <slot />
  </div>
</template>

<style>
.popover-content {
  height: var(--popover-height, auto);
}
</style>