Skip to content

Commit

Permalink
fix: add id prefix to all ids in case of multiple instances of the wi…
Browse files Browse the repository at this point in the history
…dget in a page
  • Loading branch information
chybisov committed Jan 24, 2023
1 parent 520c76b commit 684f6d8
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 31 deletions.
6 changes: 3 additions & 3 deletions packages/widget/src/components/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { styled } from '@mui/material/styles';
import type { PropsWithChildren } from 'react';
import { useWidgetConfig } from '../providers';
import type { WidgetVariant } from '../types';
import { ElementId } from '../utils';
import { createElementId, ElementId } from '../utils';

export const maxHeight = 680;

Expand Down Expand Up @@ -54,11 +54,11 @@ export const FlexContainer = styled(Container)({

export const AppContainer: React.FC<PropsWithChildren<{}>> = ({ children }) => {
// const ref = useRef<HTMLDivElement>(null);
const { containerStyle, variant } = useWidgetConfig();
const { containerStyle, variant, elementId } = useWidgetConfig();
return (
<RelativeContainer sx={containerStyle} variant={variant}>
<CssBaselineContainer
id={ElementId.ScrollableContainer}
id={createElementId(ElementId.ScrollableContainer, elementId)}
variant={variant}
enableColorScheme
// ref={ref}
Expand Down
5 changes: 3 additions & 2 deletions packages/widget/src/components/BottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
useRef,
useState,
} from 'react';
import { getScrollableContainer } from '../../hooks';
import { useGetScrollableContainer } from '../../hooks';
import { backdropProps, modalProps, paperProps } from '../Dialog';
import type { BottomSheetBase, BottomSheetProps } from './types';

export const BottomSheet = forwardRef<BottomSheetBase, BottomSheetProps>(
({ elementRef, children, open, onClose }, ref) => {
const getContainer = useGetScrollableContainer();
const openRef = useRef(open);
const [drawerOpen, setDrawerOpen] = useState(open);

Expand All @@ -36,7 +37,7 @@ export const BottomSheet = forwardRef<BottomSheetBase, BottomSheetProps>(

return (
<Drawer
container={getScrollableContainer}
container={getContainer}
ref={elementRef}
anchor="bottom"
open={drawerOpen}
Expand Down
5 changes: 3 additions & 2 deletions packages/widget/src/components/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DialogProps, Theme } from '@mui/material';
import { Dialog as MuiDialog } from '@mui/material';
import type { PropsWithChildren } from 'react';
import { getScrollableContainer } from '../hooks';
import { useGetScrollableContainer } from '../hooks';

export const modalProps = {
sx: {
Expand Down Expand Up @@ -32,9 +32,10 @@ export const Dialog: React.FC<PropsWithChildren<DialogProps>> = ({
open,
onClose,
}) => {
const getContainer = useGetScrollableContainer();
return (
<MuiDialog
container={getScrollableContainer}
container={getContainer}
open={open}
onClose={onClose}
sx={modalProps.sx}
Expand Down
6 changes: 4 additions & 2 deletions packages/widget/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { FC, PropsWithChildren } from 'react';
import { useLocation } from 'react-router-dom';
import { useDefaultElementId } from '../../hooks';
import { useWidgetConfig } from '../../providers';
import { ElementId, stickyHeaderRoutes } from '../../utils';
import { createElementId, ElementId, stickyHeaderRoutes } from '../../utils';
import { Container } from './Header.style';
import { NavigationHeader } from './NavigationHeader';
import { WalletHeader } from './WalletHeader';

export const HeaderContainer: FC<PropsWithChildren<{}>> = ({ children }) => {
const { pathname } = useLocation();
const elementId = useDefaultElementId();
return (
<Container
id={ElementId.Header}
id={createElementId(ElementId.Header, elementId)}
sticky={stickyHeaderRoutes.some((route) => pathname.includes(route))}
>
{children}
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './useChain';
export * from './useChains';
export * from './useContentHeight';
export * from './useDebouncedWatch';
export * from './useDefaultElementId';
export * from './useExpandableVariant';
export * from './useFeaturedTokens';
export * from './useGasSufficiency';
Expand Down
23 changes: 15 additions & 8 deletions packages/widget/src/hooks/useContentHeight.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* eslint-disable consistent-return */
import type { MutableRefObject } from 'react';
import { useLayoutEffect, useState } from 'react';
import { ElementId } from '../utils';
import { createElementId, ElementId } from '../utils';
import { useDefaultElementId } from './useDefaultElementId';
import { getScrollableContainer } from './useScrollableContainer';

const getContentHeight = () => {
const headerElement = document.getElementById(ElementId.Header);
const getContentHeight = (elementId: string) => {
const headerElement = document.getElementById(
createElementId(ElementId.Header, elementId),
);
const containerElement = document.getElementById(
ElementId.ScrollableContainer,
createElementId(ElementId.ScrollableContainer, elementId),
);
if (!containerElement || !headerElement) {
console.warn(
Expand All @@ -21,10 +24,13 @@ const getContentHeight = () => {
};

export const useContentHeight = () => {
const [contentHeight, setContentHeight] = useState<number>(getContentHeight);
const elementId = useDefaultElementId();
const [contentHeight, setContentHeight] = useState<number>(() =>
getContentHeight(elementId),
);
useLayoutEffect(() => {
if (!contentHeight) {
setContentHeight(getContentHeight());
setContentHeight(getContentHeight(elementId));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand All @@ -34,8 +40,9 @@ export const useContentHeight = () => {
export const useSetContentHeight = (
ref: MutableRefObject<HTMLElement | undefined>,
) => {
const elementId = useDefaultElementId();
useLayoutEffect(() => {
const scrollableContainer = getScrollableContainer();
const scrollableContainer = getScrollableContainer(elementId);
if (
!scrollableContainer ||
!ref.current ||
Expand All @@ -47,5 +54,5 @@ export const useSetContentHeight = (
return () => {
scrollableContainer.style.removeProperty('height');
};
}, [ref]);
}, [elementId, ref]);
};
6 changes: 6 additions & 0 deletions packages/widget/src/hooks/useDefaultElementId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useWidgetConfig } from '../providers';

export const useDefaultElementId = () => {
const { elementId } = useWidgetConfig();
return elementId;
};
36 changes: 25 additions & 11 deletions packages/widget/src/hooks/useScrollableContainer.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
import { useLayoutEffect, useState } from 'react';
import { ElementId } from '../utils';
import { useCallback, useLayoutEffect, useState } from 'react';
import { createElementId, ElementId } from '../utils';
import { useDefaultElementId } from './useDefaultElementId';

export const getScrollableContainer = () =>
document.getElementById(ElementId.ScrollableContainer);
export const getScrollableContainer = (elementId: string) =>
document.getElementById(
createElementId(ElementId.ScrollableContainer, elementId),
);

export const useGetScrollableContainer = () => {
const elementId = useDefaultElementId();
const getContainer = useCallback(
() => getScrollableContainer(elementId),
[elementId],
);

return getContainer;
};

export const useScrollableContainer = () => {
const [containerElement, setContainerElement] = useState(
getScrollableContainer,
export const useScrollableContainer = (elementId: string) => {
const [containerElement, setContainerElement] = useState(() =>
getScrollableContainer(elementId),
);

useLayoutEffect(() => {
if (!containerElement) {
setContainerElement(getScrollableContainer());
setContainerElement(getScrollableContainer(elementId));
}
}, [containerElement]);
}, [containerElement, elementId]);

return containerElement;
};

export const useScrollableOverflowHidden = () => {
const elementId = useDefaultElementId();
useLayoutEffect(() => {
const element = getScrollableContainer();
const element = getScrollableContainer(elementId);
if (element) {
element.style.overflowY = 'hidden';
}
Expand All @@ -29,5 +43,5 @@ export const useScrollableOverflowHidden = () => {
element.style.overflowY = 'auto';
}
};
}, []);
}, [elementId]);
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { ChainKey } from '@lifi/sdk';
import { getChainByKey } from '@lifi/sdk';
import { createContext, useContext, useEffect, useMemo } from 'react';
import { createContext, useContext, useEffect, useId, useMemo } from 'react';
import { setDefaultSettings, useSettingsStoreContext } from '../../stores';
import { formatAmount } from '../../utils';
import type { WidgetContextProps, WidgetProviderProps } from './types';

const initialContext: WidgetContextProps = {
disabledChains: [],
elementId: '',
};

const WidgetContext = createContext<WidgetContextProps>(initialContext);
Expand All @@ -28,6 +29,7 @@ export const WidgetProvider: React.FC<
} = {},
}) => {
const settingsStoreContext = useSettingsStoreContext();
const elementId = useId();
const value = useMemo((): WidgetContextProps => {
try {
const searchParams = Object.fromEntries(
Expand Down Expand Up @@ -76,12 +78,13 @@ export const WidgetProvider: React.FC<
!isNaN(parseFloat(searchParams.fromAmount))
? formatAmount(searchParams.fromAmount)
: fromAmount,
elementId,
} as WidgetContextProps;
} catch (e) {
console.warn(e);
return config;
return { ...config, elementId };
}
}, [config, fromAmount, fromChain, fromToken, toChain, toToken]);
}, [config, elementId, fromAmount, fromChain, fromToken, toChain, toToken]);

useEffect(() => {
setDefaultSettings(settingsStoreContext, value);
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/providers/WidgetProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { WidgetConfig } from '../../types';
export type WidgetContextProps = WidgetConfig & {
fromChain?: number;
toChain?: number;
elementId: string;
};

export interface WidgetProviderProps {
Expand Down
3 changes: 3 additions & 0 deletions packages/widget/src/utils/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export enum ElementId {
ScrollableContainer = 'widget-scrollable-container',
Header = 'widget-header',
}

export const createElementId = (ElementId: ElementId, elementId: string) =>
`${ElementId}-${elementId}`;

0 comments on commit 684f6d8

Please sign in to comment.