Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: token list and height related improvements #289

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defaultMaxHeight } from '@lifi/widget';
import { MenuItem, type SelectChangeEvent } from '@mui/material';
import type { CSSProperties } from 'react';
import type { CSSProperties, FocusEventHandler } from 'react';
import { type ChangeEventHandler, useEffect, useId, useState } from 'react';
import {
type Layout,
Expand Down Expand Up @@ -96,10 +96,8 @@ export const LayoutControls = () => {
setSelectedLayoutId(getLayoutMode(config?.theme?.container));
}, [config?.theme?.container, setSelectedLayoutId]);

const handleSelectChange = (event: SelectChangeEvent<any>) => {
const newLayoutId = event.target.value;

switch (newLayoutId) {
const setInitialLayout = (layoutId: Layout) => {
switch (layoutId) {
case 'restricted-height':
setHeader();

Expand Down Expand Up @@ -166,6 +164,21 @@ export const LayoutControls = () => {
}
};

const handleSelectChange = (event: SelectChangeEvent<any>) => {
setHeightValue(undefined);
const newLayoutId = event.target.value;
setInitialLayout(newLayoutId);
};

const handleInputBlur: FocusEventHandler<HTMLInputElement> = (e) => {
const valueConvertedToNumber = parseInt(e.target.value, 10);

if (valueConvertedToNumber < defaultMaxHeight) {
setHeightValue(undefined);
setInitialLayout(selectedLayoutId);
}
};

const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
const valueConvertedToNumber = parseInt(e.target.value, 10);
const height = Number.isFinite(valueConvertedToNumber)
Expand Down Expand Up @@ -260,6 +273,7 @@ export const LayoutControls = () => {
value={heightValue ?? ''}
placeholder={`${defaultMaxHeight}`}
onChange={handleInputChange}
onBlur={handleInputBlur}
/>
</CardRowContainer>
) : null}
Expand Down
49 changes: 31 additions & 18 deletions packages/widget/src/components/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,40 @@ export const RelativeContainer = styled(Box, {
interface CssBaselineContainerProps {
variant?: WidgetVariant;
paddingTopAdjustment: number;
elementId: string;
}

const CssBaselineContainer = styled(ScopedCssBaseline, {
shouldForwardProp: (prop) =>
!['variant', 'paddingTopAdjustment'].includes(prop as string),
})<CssBaselineContainerProps>(({ theme, variant, paddingTopAdjustment }) => ({
display: 'flex',
flex: 1,
flexDirection: 'column',
overflowX: 'clip',
margin: 0,
width: '100%',
maxHeight:
variant === 'drawer' || theme.container?.display === 'flex'
? 'none'
: theme.container?.maxHeight
? theme.container?.maxHeight
: theme.container?.height || defaultMaxHeight,
overflowY: 'auto',
height: theme.container?.display === 'flex' ? 'auto' : '100%',
paddingTop: paddingTopAdjustment,
}));
!['variant', 'paddingTopAdjustment', 'elementId'].includes(prop as string),
})<CssBaselineContainerProps>(
({ theme, variant, paddingTopAdjustment, elementId }) => ({
display: 'flex',
flex: 1,
flexDirection: 'column',
overflowX: 'clip',
margin: 0,
width: '100%',
maxHeight:
variant === 'drawer' || theme.container?.display === 'flex'
? 'none'
: theme.container?.maxHeight
? theme.container?.maxHeight
: theme.container?.height || defaultMaxHeight,
overflowY: 'auto',
height: theme.container?.display === 'flex' ? 'auto' : '100%',
paddingTop: paddingTopAdjustment,
// The following allows for the token list to always fill the available height
// use of CSS.escape here is to deal with characters such as ':' returned by reacts useId hook
// related issue - https://github.com/facebook/react/issues/26839
[`&:has(#${CSS.escape(createElementId(ElementId.TokenList, elementId))})`]:
{
height: theme.container?.maxHeight
? theme.container?.maxHeight
: theme.container?.height || defaultMaxHeight,
},
}),
);

export const FlexContainer = styled(Container)(({ theme }) => ({
display: 'flex',
Expand Down Expand Up @@ -110,6 +122,7 @@ export const AppContainer: React.FC<PropsWithChildren<{}>> = ({ children }) => {
variant={variant}
enableColorScheme
paddingTopAdjustment={positionFixedAdjustment}
elementId={elementId}
// ref={ref}
>
<FlexContainer disableGutters>{children}</FlexContainer>
Expand Down
9 changes: 8 additions & 1 deletion packages/widget/src/components/TokenList/TokenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { useChain } from '../../hooks/useChain.js';
import { useDebouncedWatch } from '../../hooks/useDebouncedWatch.js';
import { useTokenBalances } from '../../hooks/useTokenBalances.js';
import { useTokenSearch } from '../../hooks/useTokenSearch.js';
import { useWidgetConfig } from '../../providers/WidgetProvider/WidgetProvider.js';
import { FormKeyHelper } from '../../stores/form/types.js';
import { useFieldValues } from '../../stores/form/useFieldValues.js';
import type { TokenAmount } from '../../types/token.js';
import { createElementId, ElementId } from '../../utils/elements.js';
import { TokenNotFound } from './TokenNotFound.js';
import { VirtualizedTokenList } from './VirtualizedTokenList.js';
import type { TokenListProps } from './types.js';
Expand All @@ -25,6 +27,7 @@ export const TokenList: FC<TokenListProps> = ({
320,
'tokenSearchFilter',
);
const { elementId } = useWidgetConfig();

const { chain, isLoading: isChainLoading } = useChain(selectedChainId);
const { account } = useAccount({ chainType: chain?.chainType });
Expand Down Expand Up @@ -81,7 +84,11 @@ export const TokenList: FC<TokenListProps> = ({
!tokenSearchFilter;

return (
<Box ref={parentRef} style={{ height, overflow: 'auto' }}>
<Box
ref={parentRef}
style={{ height, overflow: 'auto' }}
id={createElementId(ElementId.TokenList, elementId)}
>
{!tokens.length && !isLoading ? (
<TokenNotFound formType={formType} />
) : null}
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/config/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const defaultMaxHeight = 682;
export const defaultMaxHeight = 686;
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export const useTokenListHeight = ({

const processResize = debounce(() => handleResize(), 40);

// calling this on initial mount prevents the lists resizing appearing glitchy
handleResize();

const appContainer = document.getElementById(
createElementId(ElementId.AppExpandedContainer, elementId),
);
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/utils/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum ElementId {
Header = 'widget-header',
RelativeContainer = 'widget-relative-container',
ScrollableContainer = 'widget-scrollable-container',
TokenList = 'token-list',
}

export const createElementId = (ElementId: ElementId, elementId: string) =>
Expand Down