Skip to content

Commit

Permalink
fix: Fix tooltip modes not working
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Mar 30, 2024
1 parent 3d3008a commit cc81f1d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/components/ChatBotTooltip/ChatBotTooltip.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
white-space: nowrap;
cursor: pointer;
font-size: 20px;
transition: transform 0.3s ease;
}

.rcb-chat-tooltip-tail {
Expand Down
62 changes: 39 additions & 23 deletions src/components/ChatBotTooltip/ChatBotTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";

import { isDesktop } from "../../services/Utils";
import { useBotOptions } from "../../context/BotOptionsContext";

import "./ChatBotTooltip.css";
Expand All @@ -15,37 +16,52 @@ const ChatBotTooltip = () => {
// tracks whether to show tooltip
const [showTooltip, setShowTooltip] = useState<boolean>(false);

// checks if tooltip should be shown on load
useEffect(() => {
if (botOptions.tooltip?.mode === "ALWAYS") {
return;
}
if (botOptions.isOpen) {
setShowTooltip(false);
}
}, [botOptions.isOpen]);
// tracks if tooltip was shown on start
const [shownTooltipOnStart, setShownTooltipOnStart] = useState<boolean>(false);

// tooltip offset
const [tooltipOffset, setTooltipOffset] = useState<number>(0);

/**
* Checks if tooltip should be shown.
*/
const shouldShowTooltip = () => {
// checks if tooltip should be shown
useEffect(() => {
const mode = botOptions.tooltip?.mode;
if (mode === "ALWAYS") {
return true;
if (isDesktop) {
let offset;
if (botOptions.isOpen) {
offset = (botOptions.chatWindowStyle?.width as number || 375) -
(botOptions.chatButtonStyle?.width as number || 75)
} else {
offset = 0;
}
setTooltipOffset(offset);
setShowTooltip(true);
} else {
if (botOptions.isOpen) {
setShowTooltip(false);
} else {
setShowTooltip(true);
}
}
} else if (mode === "NEVER") {
setShowTooltip(false);
} else if (mode === "START") {
return showTooltip;
if (!shownTooltipOnStart) {
setShownTooltipOnStart(true);
setShowTooltip(true);
} else {
setShowTooltip(false);
}
} else if (mode === "CLOSE") {
return !botOptions.isOpen;
} else {
return false;
setShowTooltip(!botOptions.isOpen);
}
};

}, [botOptions.isOpen]);

// styles for tooltip
const tooltipStyle = {
display: shouldShowTooltip() ? "visible" : "hidden",
opacity: shouldShowTooltip() ? 1 : 0,
right: `calc(${botOptions.chatButtonStyle?.width || 75}px + 40px)`,
transform: `translateX(-${tooltipOffset}px)`,
right: (botOptions.chatButtonStyle?.width as number || 75) + 40,
bottom: 30,
backgroundColor: botOptions.theme?.secondaryColor,
color: botOptions.theme?.secondaryColor,
Expand All @@ -62,7 +78,7 @@ const ChatBotTooltip = () => {
{!botOptions.theme?.embedded &&
<div
style={tooltipStyle}
className={`rcb-chat-tooltip ${botOptions.isOpen ? "rcb-tooltip-hide" : "rcb-tooltip-show"}`}
className={`rcb-chat-tooltip ${showTooltip ? "rcb-tooltip-show" : "rcb-tooltip-hide"}`}
onClick={() => setBotOptions({...botOptions, isOpen: true})}
>
<span style={{ color: "#fff" }}>{botOptions.tooltip?.text}</span>
Expand Down

0 comments on commit cc81f1d

Please sign in to comment.