Skip to content

Commit

Permalink
fix: feedback diglog position (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhyoungRyu authored Apr 22, 2024
1 parent 70221f5 commit 02e786b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/components/ChatAiWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ProviderContainer, {
} from './ProviderContainer';
import WidgetToggleButton from './WidgetToggleButton';
import WidgetWindow from './WidgetWindow';
import { MAX_Z_INDEX } from '../const';
import { MAX_Z_INDEX, elementIds } from '../const';
import { useWidgetOpen } from '../context/WidgetOpenContext';
import useMobileView from '../hooks/useMobileView';
import { isMobile } from '../utils';
Expand Down Expand Up @@ -44,7 +44,7 @@ const MobileComponent = () => {
<MobileContainer
style={{ display: isOpen ? 'block' : 'none' }}
width={mobileContainerWidth}
id="aichatbot-widget-window"
id={elementIds.widgetWindow}
>
<Chat />
</MobileContainer>
Expand Down
3 changes: 2 additions & 1 deletion src/components/CustomChannelHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import styled from 'styled-components';

import BetaLogo from './BetaLogo';
import BotProfileImage from './BotProfileImage';
import { elementIds } from '../const';
import { useConstantState } from '../context/ConstantContext';
import { useWidgetOpen } from '../context/WidgetOpenContext';
import { ReactComponent as CloseButton } from '../icons/ic-widget-close.svg';
Expand Down Expand Up @@ -114,7 +115,7 @@ export default function CustomChannelHeader({
}}
>
<customRefreshComponent.icon
id="aichatbot-widget-refresh-icon"
id={elementIds.refreshIcon}
width={customRefreshComponent.width}
height={customRefreshComponent.height}
style={
Expand Down
3 changes: 3 additions & 0 deletions src/components/ProviderContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { WidgetOpenProvider } from '../context/WidgetOpenContext';
import { useChannelStyle } from '../hooks/useChannelStyle';
// import useDatadogRum from '../hooks/useDatadog';
import useDynamicAttachModal from '../hooks/useDynamicAttachModal';
import useWidgetLocalStorage from '../hooks/useWidgetLocalStorage';
import { getTheme } from '../theme';
import { isMobile } from '../utils';
Expand All @@ -29,6 +30,8 @@ const SBComponent = ({ children }: { children: React.ReactElement }) => {
stringSet,
...restConstantProps
} = useConstantState();
useDynamicAttachModal();

// useDatadogRum();

const userAgentCustomParams = useRef({
Expand Down
4 changes: 2 additions & 2 deletions src/components/WidgetToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useRef } from 'react';
import styled, { css } from 'styled-components';

import { getColorBasedOnSaturation } from '../colors';
import { MAX_Z_INDEX } from '../const';
import { MAX_Z_INDEX, elementIds } from '../const';
import { useConstantState } from '../context/ConstantContext';
import { useWidgetOpen } from '../context/WidgetOpenContext';
import { useChannelStyle } from '../hooks/useChannelStyle';
Expand Down Expand Up @@ -97,7 +97,7 @@ interface ToggleButtonProps {
const StyledButton = ({ onClick, accentColor, isOpen }: ToggleButtonProps) => {
return (
<StyledWidgetButtonWrapper
id="aichatbot-widget-button"
id={elementIds.widgetToggleButton}
onClick={onClick}
accentColor={accentColor}
>
Expand Down
10 changes: 5 additions & 5 deletions src/components/WidgetWindow.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import styled, { css } from 'styled-components';

import { MAX_Z_INDEX } from '../const';
import { MAX_Z_INDEX, elementIds } from '../const';
import { useWidgetOpen } from '../context/WidgetOpenContext';
import { ReactComponent as CloseIcon } from '../icons/ic-widget-close.svg';
import { ReactComponent as CollapseIcon } from '../icons/icon-collapse.svg';
Expand Down Expand Up @@ -95,17 +95,17 @@ const WidgetWindow = ({ children }: { children: React.ReactNode }) => {
<StyledWidgetWindowWrapper
isOpen={isOpen}
isExpanded={isExpanded}
id="aichatbot-widget-window"
id={elementIds.widgetWindow}
>
<StyledExpandButton onClick={() => setIsExpanded((prev) => !prev)}>
{isExpanded ? (
<CollapseIcon id="aichatbot-widget-collapse-icon" />
<CollapseIcon id={elementIds.collapseIcon} />
) : (
<ExpandIcon id="aichatbot-widget-expand-icon" />
<ExpandIcon id={elementIds.expandIcon} />
)}
</StyledExpandButton>
<StyledCloseButton onClick={() => setIsOpen(false)}>
<CloseIcon id="aichatbot-widget-close-icon" />
<CloseIcon id={elementIds.closeIcon} />
</StyledCloseButton>
{children}
</StyledWidgetWindowWrapper>
Expand Down
10 changes: 10 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,13 @@ export interface CustomRefreshComponent {
onClick?: () => void;
style?: React.CSSProperties;
}

export const elementIds = {
widgetWindow: 'aichatbot-widget-window',
widgetToggleButton: 'aichatbot-widget-button',
collapseIcon: 'aichatbot-widget-collapse-icon',
expandIcon: 'aichatbot-widget-expand-icon',
closeIcon: 'aichatbot-widget-close-icon',
refreshIcon: 'aichatbot-widget-refresh-icon',
uikitModal: 'sendbird-modal-root',
};
39 changes: 39 additions & 0 deletions src/hooks/useDynamicAttachModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEffect } from 'react';

import { elementIds } from '../const';

const parentId = elementIds.widgetWindow;
const childId = elementIds.uikitModal;

function useDynamicAttachModal() {
useEffect(() => {
// Set up a MutationObserver to monitor changes in the parent element
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
// Check if the added nodes include the one with parentId
Array.from(mutation.addedNodes).forEach((node) => {
if (node?.id === parentId) {
const parent = document.getElementById(parentId);
const child = document.getElementById(childId);
if (parent && child) {
parent.appendChild(child);
// Disconnect the observer once the child is attached
observer.disconnect();
}
}
});
}
});
});

// Set the observer to monitor the entire document
observer.observe(document.body, { childList: true, subtree: true });

return () => {
observer?.disconnect();
};
}, []);
}

export default useDynamicAttachModal;

0 comments on commit 02e786b

Please sign in to comment.