Refactor frontend overlays
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<script setup>
|
||||
import OverlayShell from "./OverlayShell.vue";
|
||||
|
||||
const open = defineModel({ type: Boolean, default: false });
|
||||
|
||||
defineProps({
|
||||
title: { type: String, default: "Are you sure?" },
|
||||
message: { type: String, required: true },
|
||||
confirmLabel: { type: String, default: "Confirm" },
|
||||
});
|
||||
|
||||
const emit = defineEmits(["confirm", "cancel"]);
|
||||
|
||||
function cancel() {
|
||||
open.value = false;
|
||||
emit("cancel");
|
||||
}
|
||||
|
||||
function confirm() {
|
||||
open.value = false;
|
||||
emit("confirm");
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<OverlayShell v-model="open" :title="title" backdrop close-on-backdrop @close="cancel">
|
||||
<div class="confirm bdr-1">
|
||||
<h2>{{ title }}</h2>
|
||||
<p>{{ message }}</p>
|
||||
<div class="actions">
|
||||
<button type="button" class="confirm-button" @click="confirm">{{ confirmLabel }}</button>
|
||||
<button type="button" class="cancel-button" @click="cancel">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</OverlayShell>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.confirm {
|
||||
box-sizing: border-box;
|
||||
width: min(420px, calc(100vw - 24px));
|
||||
padding: 16px;
|
||||
color: var(--color-secondary);
|
||||
background: var(--color-surface);
|
||||
}
|
||||
.confirm h2 {
|
||||
margin-top: 0;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
.actions button {
|
||||
padding: 0.35rem 0.75rem;
|
||||
color: var(--color-primary);
|
||||
background: var(--color-link-bg);
|
||||
border: 1px solid var(--color-primary);
|
||||
cursor: pointer;
|
||||
}
|
||||
.confirm-button {
|
||||
color: var(--color-tertiary) !important;
|
||||
border-color: var(--color-tertiary) !important;
|
||||
}
|
||||
</style>
|
||||
@@ -1,62 +1,26 @@
|
||||
<script setup>
|
||||
import { watch, onUnmounted } from "vue";
|
||||
import OverlayShell from "./OverlayShell.vue";
|
||||
|
||||
// Popup modal: a dimmed backdrop over the page with the content floated on top.
|
||||
// Closes on backdrop click or Escape. Used by the home widgets so their create
|
||||
// forms pop up instead of replacing the widget's contents.
|
||||
const open = defineModel({ type: Boolean, default: false });
|
||||
|
||||
const props = defineProps({
|
||||
maxWidth: { type: String, default: "420px" },
|
||||
});
|
||||
|
||||
function onKeydown(e) {
|
||||
if (e.key === "Escape") open.value = false;
|
||||
}
|
||||
|
||||
// The Escape listener is attached only while open, so several modals on one
|
||||
// page can't all react to a single keypress. `immediate` covers a modal that
|
||||
// mounts already open.
|
||||
watch(
|
||||
open,
|
||||
(isOpen) => {
|
||||
if (isOpen) {
|
||||
window.addEventListener("keydown", onKeydown);
|
||||
} else {
|
||||
window.removeEventListener("keydown", onKeydown);
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
onUnmounted(() => window.removeEventListener("keydown", onKeydown));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- Teleported to <body> so the backdrop escapes any ancestor with
|
||||
overflow/transform/z-index that would otherwise clip or trap it. -->
|
||||
<Teleport to="body">
|
||||
<div v-if="open" class="modal-backdrop" @click.self="open = false">
|
||||
<div class="modal bdr-1" :style="{ maxWidth: props.maxWidth }">
|
||||
<slot />
|
||||
</div>
|
||||
<OverlayShell v-model="open" title="Dialog" backdrop close-on-backdrop>
|
||||
<div class="modal bdr-1" :style="{ maxWidth: props.maxWidth }">
|
||||
<slot />
|
||||
</div>
|
||||
</Teleport>
|
||||
</OverlayShell>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.modal-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background-color: rgba(0, 0, 0, 0.65);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 50;
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.modal {
|
||||
box-sizing: border-box;
|
||||
background-color: var(--color-surface);
|
||||
padding: 16px;
|
||||
width: 100%;
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
<script setup>
|
||||
import { nextTick, onUnmounted, ref, watch } from "vue";
|
||||
import { useOverlayStack } from "@/js/useOverlayStack";
|
||||
|
||||
const open = defineModel({ type: Boolean, default: false });
|
||||
const emit = defineEmits(["close"]);
|
||||
|
||||
const props = defineProps({
|
||||
role: { type: String, default: "dialog" },
|
||||
title: { type: String, default: "" },
|
||||
panelClass: { type: String, default: "" },
|
||||
backdrop: { type: Boolean, default: false },
|
||||
closeOnBackdrop: { type: Boolean, default: false },
|
||||
});
|
||||
|
||||
const panel = ref(null);
|
||||
let returnFocus = null;
|
||||
let bodyLocks = 0;
|
||||
let previousOverflow = "";
|
||||
let bodyLocked = false;
|
||||
|
||||
function lockBody() {
|
||||
if (!bodyLocks++) previousOverflow = document.body.style.overflow;
|
||||
document.body.style.overflow = "hidden";
|
||||
bodyLocked = true;
|
||||
}
|
||||
|
||||
function unlockBody() {
|
||||
if (!bodyLocked) return;
|
||||
bodyLocked = false;
|
||||
if (--bodyLocks === 0) document.body.style.overflow = previousOverflow;
|
||||
}
|
||||
|
||||
function close() {
|
||||
open.value = false;
|
||||
emit("close");
|
||||
}
|
||||
|
||||
useOverlayStack(open, close);
|
||||
|
||||
function focusPanel() {
|
||||
const target = panel.value?.querySelector("input, select, textarea, button, [tabindex]:not([tabindex='-1'])");
|
||||
(target ?? panel.value)?.focus();
|
||||
}
|
||||
|
||||
function trapFocus(event) {
|
||||
if (event.key !== "Tab" || !panel.value) return;
|
||||
const focusable = [...panel.value.querySelectorAll("input, select, textarea, button, [tabindex]:not([tabindex='-1'])")];
|
||||
if (!focusable.length) {
|
||||
event.preventDefault();
|
||||
panel.value.focus();
|
||||
return;
|
||||
}
|
||||
const first = focusable[0];
|
||||
const last = focusable.at(-1);
|
||||
if (event.shiftKey && document.activeElement === first) {
|
||||
event.preventDefault();
|
||||
last.focus();
|
||||
} else if (!event.shiftKey && document.activeElement === last) {
|
||||
event.preventDefault();
|
||||
first.focus();
|
||||
}
|
||||
}
|
||||
|
||||
watch(open, async (isOpen) => {
|
||||
if (isOpen) {
|
||||
lockBody();
|
||||
returnFocus = document.activeElement;
|
||||
await nextTick();
|
||||
focusPanel();
|
||||
} else if (returnFocus?.isConnected) {
|
||||
unlockBody();
|
||||
returnFocus.focus();
|
||||
returnFocus = null;
|
||||
} else {
|
||||
unlockBody();
|
||||
}
|
||||
}, { immediate: true });
|
||||
|
||||
onUnmounted(() => {
|
||||
unlockBody();
|
||||
if (returnFocus?.isConnected) returnFocus.focus();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Teleport to="body">
|
||||
<div
|
||||
v-if="open"
|
||||
class="overlay-shell"
|
||||
:class="{ 'has-backdrop': props.backdrop }"
|
||||
@click.self="props.closeOnBackdrop && close()"
|
||||
>
|
||||
<section
|
||||
ref="panel"
|
||||
class="overlay-panel"
|
||||
:class="props.panelClass"
|
||||
:role="props.role"
|
||||
:aria-modal="props.role === 'dialog' ? 'true' : undefined"
|
||||
:aria-label="props.title || undefined"
|
||||
tabindex="-1"
|
||||
@keydown="trapFocus"
|
||||
>
|
||||
<slot />
|
||||
</section>
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.overlay-shell {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 60;
|
||||
pointer-events: none;
|
||||
}
|
||||
.overlay-shell.has-backdrop {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
|
||||
max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
|
||||
background: rgb(0 0 0 / 65%);
|
||||
pointer-events: auto;
|
||||
}
|
||||
.overlay-panel {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
pointer-events: auto;
|
||||
outline: none;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user