|
| 1 | +<template lang="pug"> |
| 2 | +div(ref="container") |
| 3 | + slot(name="activator") |
| 4 | + Portal(v-if="activatorNode && active", to="popover") |
| 5 | + div(:data-portal-id="portalId") |
| 6 | + PopoverOverlay( |
| 7 | + :activator="activatorNode", |
| 8 | + :fullWidth="fullWidth", |
| 9 | + :active="active", |
| 10 | + :preferInputActivator="preferInputActivator", |
| 11 | + :fixed="fixed", |
| 12 | + :preferredPosition="preferredPosition", |
| 13 | + :preferredAlignment="preferredAlignment", |
| 14 | + :zIndexOverride="zIndexOverride", |
| 15 | + @close="handleClose", |
| 16 | + @scrolled-to-bottom="$emit('scrolled-to-bottom')", |
| 17 | + ) |
| 18 | + template(v-slot:overlay="props") |
| 19 | + slot(name="content") |
| 20 | + PortalTarget(name="popover") |
| 21 | +</template> |
| 22 | + |
| 23 | +<script lang="ts"> |
| 24 | +import Vue from 'vue'; |
| 25 | +import { |
| 26 | + Component, Prop, Watch, Ref, |
| 27 | +} from 'vue-property-decorator'; |
| 28 | +import type { PreferredAlignment, PreferredPosition } from '../PositionedOverlay'; |
| 29 | +import { |
| 30 | + findFirstFocusableNodeIncludingDisabled, |
| 31 | + focusNextFocusableNode, |
| 32 | +} from '@/utilities/focus'; |
| 33 | +import { PopoverCloseSource, PopoverAutofocusTarget, setActivatorAttributes } from './utils'; |
| 34 | +import { useUniqueId } from '@/utilities/unique-id'; |
| 35 | +import { PopoverOverlay } from './components'; |
| 36 | +
|
| 37 | +@Component({ |
| 38 | + components: { |
| 39 | + PopoverOverlay, |
| 40 | + }, |
| 41 | +}) |
| 42 | +export default class Popover extends Vue { |
| 43 | + /** |
| 44 | + * The preferred direction to open the popover |
| 45 | + * @values above | below | mostSpace |
| 46 | + */ |
| 47 | + @Prop() |
| 48 | + public preferredPosition?: PreferredPosition; |
| 49 | +
|
| 50 | + /** |
| 51 | + * The preferred alignment of the popover relative to its activator |
| 52 | + * @values left | center | right |
| 53 | + */ |
| 54 | + @Prop() |
| 55 | + public preferredAlignment?: PreferredAlignment; |
| 56 | +
|
| 57 | + /** |
| 58 | + * Show or hide the Popover |
| 59 | + */ |
| 60 | + @Prop({ type: Boolean, required: true }) |
| 61 | + public active!: boolean; |
| 62 | +
|
| 63 | + /** |
| 64 | + * Use the activator's input element to calculate the Popover position |
| 65 | + * @default true |
| 66 | + */ |
| 67 | + @Prop({ type: Boolean, default: true }) |
| 68 | + public preferInputActivator?: boolean; |
| 69 | +
|
| 70 | + /** |
| 71 | + * Override on the default z-index of 400 |
| 72 | + */ |
| 73 | + @Prop({ type: Number }) |
| 74 | + public zIndexOverride?: number; |
| 75 | +
|
| 76 | + /** |
| 77 | + * Prevents focusing the activator or the next focusable element when the popover is deactivated |
| 78 | + */ |
| 79 | + @Prop({ type: Boolean }) |
| 80 | + public preventFocusOnClose?: boolean; |
| 81 | +
|
| 82 | + /** |
| 83 | + * Automatically add wrap content in a section |
| 84 | + */ |
| 85 | + @Prop({ type: Boolean }) |
| 86 | + public sectioned?: boolean; |
| 87 | +
|
| 88 | + /** |
| 89 | + * Allow popover to stretch to the full width of its activator |
| 90 | + */ |
| 91 | + @Prop({ type: Boolean }) |
| 92 | + public fullWidth?: boolean; |
| 93 | +
|
| 94 | + /** |
| 95 | + * Allow popover to stretch to fit content vertically |
| 96 | + */ |
| 97 | + @Prop({ type: Boolean }) |
| 98 | + public fullHeight?: boolean; |
| 99 | +
|
| 100 | + /** |
| 101 | + * Allow popover content to determine the overlay width and height |
| 102 | + */ |
| 103 | + @Prop({ type: Boolean }) |
| 104 | + public fluidContent?: boolean; |
| 105 | +
|
| 106 | + /** |
| 107 | + * Remains in a fixed position |
| 108 | + */ |
| 109 | + @Prop({ type: Boolean }) |
| 110 | + public fixed?: boolean; |
| 111 | +
|
| 112 | + /** |
| 113 | + /** Used to illustrate the type of popover element |
| 114 | + */ |
| 115 | + @Prop({ type: String }) |
| 116 | + public ariaHaspopup!: string; |
| 117 | +
|
| 118 | + /** |
| 119 | + * Allow the popover overlay to be hidden when printing |
| 120 | + */ |
| 121 | + @Prop({ type: Boolean }) |
| 122 | + public hideOnPrint?: boolean; |
| 123 | +
|
| 124 | + /** |
| 125 | + * The preferred auto focus target defaulting to the popover container |
| 126 | + * @default 'container' |
| 127 | + */ |
| 128 | + @Prop({ type: String, default: 'container' }) |
| 129 | + public autofocusTarget?: PopoverAutofocusTarget; |
| 130 | +
|
| 131 | + @Watch('active') |
| 132 | + onActiveChanged() { |
| 133 | + this.setAccessibilityAttributes(); |
| 134 | + } |
| 135 | +
|
| 136 | + @Ref('container') containerNode!: HTMLElement; |
| 137 | +
|
| 138 | + public isInPortal = (element: Element) => { |
| 139 | + let { parentElement } = element; |
| 140 | +
|
| 141 | + while (parentElement) { |
| 142 | + if (parentElement.matches('.vue-portal-target')) return false; |
| 143 | + parentElement = parentElement.parentElement; |
| 144 | + } |
| 145 | +
|
| 146 | + return true; |
| 147 | + } |
| 148 | +
|
| 149 | + public handleClose(source: PopoverCloseSource) { |
| 150 | + this.$emit('close', source); |
| 151 | + if (!this.containerNode || this.preventFocusOnClose) return; |
| 152 | +
|
| 153 | + if ((source === PopoverCloseSource.FocusOut || source === PopoverCloseSource.EscapeKeypress) |
| 154 | + && this.activatorNode |
| 155 | + ) { |
| 156 | + const focusableActivator = findFirstFocusableNodeIncludingDisabled( |
| 157 | + this.activatorNode as HTMLElement, |
| 158 | + ) |
| 159 | + || findFirstFocusableNodeIncludingDisabled(this.containerNode) |
| 160 | + || this.containerNode; |
| 161 | + if (!focusNextFocusableNode(focusableActivator, this.isInPortal)) { |
| 162 | + focusableActivator.focus(); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +
|
| 167 | + public activatorNode: HTMLElement | Element | null = null; |
| 168 | +
|
| 169 | + public id = useUniqueId('popover'); |
| 170 | +
|
| 171 | + public portalId = `popover-${useUniqueId('portal')}`; |
| 172 | +
|
| 173 | + public setAccessibilityAttributes() { |
| 174 | + if (!this.containerNode) { |
| 175 | + return; |
| 176 | + } |
| 177 | +
|
| 178 | + const firstFocusable = findFirstFocusableNodeIncludingDisabled(this.containerNode); |
| 179 | + const focusableActivator: HTMLElement & { disabled?: boolean; } = firstFocusable |
| 180 | + || this.containerNode; |
| 181 | + const activatorDisabled = 'disabled' in focusableActivator && Boolean(focusableActivator.disabled); |
| 182 | +
|
| 183 | + setActivatorAttributes(focusableActivator, { |
| 184 | + id: this.id, |
| 185 | + active: this.active, |
| 186 | + ariaHaspopup: this.ariaHaspopup, |
| 187 | + activatorDisabled, |
| 188 | + }); |
| 189 | + } |
| 190 | +
|
| 191 | + mounted(): void { |
| 192 | + if (this.containerNode) { |
| 193 | + const activatorNode = this.containerNode.firstElementChild; |
| 194 | + if (activatorNode) this.activatorNode = activatorNode; |
| 195 | + this.setAccessibilityAttributes(); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | +</script> |
| 200 | +<style lang="scss"> |
| 201 | +@import 'polaris-react/src/components/Popover/Popover.scss'; |
| 202 | +</style> |
0 commit comments