Spoiler: Auto-hide in 5 seconds, better background

This commit is contained in:
Alexander Zinchuk 2022-02-01 04:36:17 +01:00
parent b1036d2d62
commit 43adf819ae
4 changed files with 20 additions and 10 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -27,7 +27,7 @@
}
&--concealed &__content {
visibility: hidden;
user-select: none;
opacity: 0;
}
}

View File

@ -13,7 +13,12 @@ type OwnProps = {
messageId?: number;
};
const spoilersByMessageId: Map<number, VoidFunction[]> = new Map();
const AUTO_HIDE_TIMEOUT = 5000; // 5s
const actionsByMessageId: Map<number, {
reveal: VoidFunction;
conceal: VoidFunction;
}[]> = new Map();
const buildClassName = createClassNameBuilder('Spoiler');
@ -21,30 +26,35 @@ const Spoiler: FC<OwnProps> = ({
children,
messageId,
}) => {
const [isRevealed, reveal] = useFlag();
const [isRevealed, reveal, conceal] = useFlag();
const handleClick = useCallback((e: ReactMouseEvent<HTMLDivElement, MouseEvent>) => {
e.preventDefault();
e.stopPropagation();
spoilersByMessageId.get(messageId!)?.forEach((_reveal) => _reveal());
}, [messageId]);
actionsByMessageId.get(messageId!)?.forEach((actions) => actions.reveal());
setTimeout(() => {
actionsByMessageId.get(messageId!)?.forEach((actions) => actions.conceal());
conceal();
}, AUTO_HIDE_TIMEOUT);
}, [conceal, messageId]);
useEffect(() => {
if (!messageId) {
return undefined;
}
if (spoilersByMessageId.has(messageId)) {
spoilersByMessageId.get(messageId)!.push(reveal);
if (actionsByMessageId.has(messageId)) {
actionsByMessageId.get(messageId)!.push({ reveal, conceal });
} else {
spoilersByMessageId.set(messageId, [reveal]);
actionsByMessageId.set(messageId, [{ reveal, conceal }]);
}
return () => {
spoilersByMessageId.delete(messageId);
actionsByMessageId.delete(messageId);
};
}, [handleClick, isRevealed, messageId, reveal]);
}, [conceal, handleClick, isRevealed, messageId, reveal]);
return (
<span