Spoiler: Auto-hide in 5 seconds, better background
This commit is contained in:
parent
b1036d2d62
commit
43adf819ae
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 |
@ -27,7 +27,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
&--concealed &__content {
|
&--concealed &__content {
|
||||||
visibility: hidden;
|
user-select: none;
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,12 @@ type OwnProps = {
|
|||||||
messageId?: number;
|
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');
|
const buildClassName = createClassNameBuilder('Spoiler');
|
||||||
|
|
||||||
@ -21,30 +26,35 @@ const Spoiler: FC<OwnProps> = ({
|
|||||||
children,
|
children,
|
||||||
messageId,
|
messageId,
|
||||||
}) => {
|
}) => {
|
||||||
const [isRevealed, reveal] = useFlag();
|
const [isRevealed, reveal, conceal] = useFlag();
|
||||||
|
|
||||||
const handleClick = useCallback((e: ReactMouseEvent<HTMLDivElement, MouseEvent>) => {
|
const handleClick = useCallback((e: ReactMouseEvent<HTMLDivElement, MouseEvent>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
||||||
spoilersByMessageId.get(messageId!)?.forEach((_reveal) => _reveal());
|
actionsByMessageId.get(messageId!)?.forEach((actions) => actions.reveal());
|
||||||
}, [messageId]);
|
|
||||||
|
setTimeout(() => {
|
||||||
|
actionsByMessageId.get(messageId!)?.forEach((actions) => actions.conceal());
|
||||||
|
conceal();
|
||||||
|
}, AUTO_HIDE_TIMEOUT);
|
||||||
|
}, [conceal, messageId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!messageId) {
|
if (!messageId) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spoilersByMessageId.has(messageId)) {
|
if (actionsByMessageId.has(messageId)) {
|
||||||
spoilersByMessageId.get(messageId)!.push(reveal);
|
actionsByMessageId.get(messageId)!.push({ reveal, conceal });
|
||||||
} else {
|
} else {
|
||||||
spoilersByMessageId.set(messageId, [reveal]);
|
actionsByMessageId.set(messageId, [{ reveal, conceal }]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
spoilersByMessageId.delete(messageId);
|
actionsByMessageId.delete(messageId);
|
||||||
};
|
};
|
||||||
}, [handleClick, isRevealed, messageId, reveal]);
|
}, [conceal, handleClick, isRevealed, messageId, reveal]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span
|
<span
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user