Fix: share popup not closing when clicking outside menu
All checks were successful
Automated Container Build / build-and-push (push) Successful in 57s
All checks were successful
Automated Container Build / build-and-push (push) Successful in 57s
This commit is contained in:
parent
061d13f421
commit
f0548bcc6e
1 changed files with 21 additions and 2 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useRef } from "react";
|
||||||
|
|
||||||
interface ShareMenuProps {
|
interface ShareMenuProps {
|
||||||
targetType: "DECK" | "QUIZ" | "GROUP";
|
targetType: "DECK" | "QUIZ" | "GROUP";
|
||||||
|
|
@ -16,6 +16,24 @@ export function ShareMenu({ targetType, contentId, classSlug, compact = false }:
|
||||||
const [copied, setCopied] = useState(false);
|
const [copied, setCopied] = useState(false);
|
||||||
const [isGroupShared, setIsGroupShared] = useState(false);
|
const [isGroupShared, setIsGroupShared] = useState(false);
|
||||||
const [groupName, setGroupName] = useState<string | null>(null);
|
const [groupName, setGroupName] = useState<string | null>(null);
|
||||||
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
||||||
|
const menuRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return;
|
||||||
|
|
||||||
|
function handlePointerDown(event: PointerEvent) {
|
||||||
|
const target = event.target as Node;
|
||||||
|
if (buttonRef.current?.contains(target) || menuRef.current?.contains(target)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setOpen(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("pointerdown", handlePointerDown);
|
||||||
|
return () => document.removeEventListener("pointerdown", handlePointerDown);
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open) return;
|
if (!open) return;
|
||||||
|
|
@ -63,6 +81,7 @@ export function ShareMenu({ targetType, contentId, classSlug, compact = false }:
|
||||||
return (
|
return (
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<button
|
<button
|
||||||
|
ref={buttonRef}
|
||||||
onClick={() => setOpen(!open)}
|
onClick={() => setOpen(!open)}
|
||||||
className={`${compact ? "h-8 w-8 rounded-lg" : "h-10 w-10 rounded-xl"} grid place-items-center text-text-muted transition-colors hover:bg-bg-surface-alt hover:text-primary`}
|
className={`${compact ? "h-8 w-8 rounded-lg" : "h-10 w-10 rounded-xl"} grid place-items-center text-text-muted transition-colors hover:bg-bg-surface-alt hover:text-primary`}
|
||||||
title="Share"
|
title="Share"
|
||||||
|
|
@ -75,7 +94,7 @@ export function ShareMenu({ targetType, contentId, classSlug, compact = false }:
|
||||||
{open && (
|
{open && (
|
||||||
<>
|
<>
|
||||||
<div className="fixed inset-0 z-40" onClick={() => setOpen(false)} />
|
<div className="fixed inset-0 z-40" onClick={() => setOpen(false)} />
|
||||||
<div className="absolute right-0 z-50 mt-2 w-72 origin-top-right animate-slide-up rounded-2xl border border-border-light bg-bg-surface p-5 shadow-[var(--shadow-modal)]">
|
<div ref={menuRef} className="absolute right-0 z-50 mt-2 w-72 origin-top-right animate-slide-up rounded-2xl border border-border-light bg-bg-surface p-5 shadow-[var(--shadow-modal)]">
|
||||||
<h4 className="editorial-title mb-3 text-xl text-text-heading">Public sharing</h4>
|
<h4 className="editorial-title mb-3 text-xl text-text-heading">Public sharing</h4>
|
||||||
|
|
||||||
{loading ? (
|
{loading ? (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue