popup.js
113 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { store, getContext, getElement } from "@wordpress/interactivity"; |
| 5 | const { __ } = wp.i18n; |
| 6 | |
| 7 | const { state, actions } = store("presto-player/popup", { |
| 8 | state: { |
| 9 | activePopupId: null, |
| 10 | screenReaderText: "", |
| 11 | triggerRef: null, |
| 12 | inertElements: [], |
| 13 | get overlayEnabled() { |
| 14 | const ctx = getContext(); |
| 15 | return state?.activePopupId === ctx?.id; |
| 16 | }, |
| 17 | |
| 18 | /** |
| 19 | * Gets the ARIA role attribute for the popup. |
| 20 | * |
| 21 | * @return {string|null} 'dialog' if overlay is open, null otherwise. |
| 22 | */ |
| 23 | get roleAttribute() { |
| 24 | return state?.overlayEnabled ? "dialog" : null; |
| 25 | }, |
| 26 | |
| 27 | /** |
| 28 | * Gets the ARIA modal attribute for the popup. |
| 29 | * |
| 30 | * @return {string|null} 'true' if overlay is open, null otherwise. |
| 31 | */ |
| 32 | get ariaModal() { |
| 33 | return state?.overlayEnabled ? "true" : null; |
| 34 | }, |
| 35 | }, |
| 36 | |
| 37 | actions: { |
| 38 | showPopup() { |
| 39 | const ctx = getContext(); |
| 40 | const { ref } = getElement(); |
| 41 | |
| 42 | // Update the active popup id for the current popup. |
| 43 | state.activePopupId = ctx?.id; |
| 44 | |
| 45 | // make all children of the document inert exempt .presto-popup__overlay. |
| 46 | document |
| 47 | .querySelectorAll("body > :not(.presto-popup__overlay)") |
| 48 | .forEach((el) => { |
| 49 | if (!el?.hasAttribute("inert")) { |
| 50 | el?.setAttribute("inert", ""); |
| 51 | state?.inertElements?.push(el); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | // Set the trigger button ref. This is used to focus the trigger button when the popup is closed. |
| 56 | state.triggerRef = ref; |
| 57 | }, |
| 58 | hidePopup() { |
| 59 | // Clear the active popup id. |
| 60 | state.activePopupId = null; |
| 61 | |
| 62 | // remove inert attribute from all children of the document |
| 63 | (state?.inertElements || []).forEach((el) => { |
| 64 | el?.removeAttribute("inert"); |
| 65 | }); |
| 66 | state.inertElements = []; |
| 67 | |
| 68 | // Focus the trigger button when the popup is closed. |
| 69 | setTimeout(() => { |
| 70 | state?.triggerRef?.focus(); |
| 71 | }, 0); |
| 72 | |
| 73 | // Set the screen reader text to an empty string when the popup is closed. |
| 74 | state.screenReaderText = ""; |
| 75 | }, |
| 76 | }, |
| 77 | callbacks: { |
| 78 | setOverlayFocus() { |
| 79 | if (!state?.overlayEnabled) { |
| 80 | return; |
| 81 | } |
| 82 | // Moves the focus to the dialog when it opens. |
| 83 | const { ref } = getElement(); |
| 84 | |
| 85 | // Make sure the dialog is rendered and visible before focusing. |
| 86 | setTimeout(() => { |
| 87 | ref?.focus(); |
| 88 | }, 0); |
| 89 | |
| 90 | // If the popup is open, set the screen reader text. |
| 91 | state.screenReaderText = __( |
| 92 | "Presto Popup dialog opened.", |
| 93 | "presto-player" |
| 94 | ); |
| 95 | }, |
| 96 | handleKeydown(event) { |
| 97 | // Opens the popup when the user presses the enter key. |
| 98 | if (event?.key === "Enter" && !state?.overlayEnabled) { |
| 99 | actions.showPopup(); |
| 100 | } |
| 101 | // Closes the popup when the user presses the escape key. |
| 102 | if (event?.key === "Escape" && state?.overlayEnabled) { |
| 103 | actions.hidePopup(); |
| 104 | } |
| 105 | }, |
| 106 | updatePopupList() { |
| 107 | // Update the popup list for the current popup. |
| 108 | const ctx = getContext(); |
| 109 | ctx.popupList = state?.overlayEnabled ? [ctx?.id] : []; |
| 110 | }, |
| 111 | }, |
| 112 | }); |
| 113 |