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

fix: update mobile web trade form for better ux experience on isolated trades #1108

Merged
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
1 change: 1 addition & 0 deletions src/components/Collapsible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const $Trigger = styled(Trigger)`
--trigger-textColor: inherit;
--trigger-icon-width: 0.75em;
--trigger-icon-color: inherit;
--icon-size: var(--trigger-icon-width);
`;
const $TriggerIcon = styled.span`
width: var(--trigger-icon-width);
Expand Down
9 changes: 5 additions & 4 deletions src/components/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,9 @@ export const Icon = styled(
}: ElementProps & StyleProps & { size?: string }) =>
Component ? <Component className={className} {...props} /> : null
)`
width: ${({ size }) => size ?? '1em'};
height: ${({ size }) => size ?? '1em'};
min-width: ${({ size }) => size ?? '1em'};
min-height: ${({ size }) => size ?? '1em'};
--icon-size: ${({ size }) => size ?? ''};
width: var(--icon-size, 1em);
height: var(--icon-size, 1em);
min-width: var(--icon-size, 1em);
min-height: var(--icon-size, 1em);
`;
46 changes: 44 additions & 2 deletions src/components/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { type ReactNode, type Ref } from 'react';
import { useRef, type ReactNode, type Ref } from 'react';

import { Content, List, Root, Trigger } from '@radix-ui/react-tabs';
import styled, { css, keyframes } from 'styled-components';

import { type MenuItem } from '@/constants/menus';

import { useFadeOnHorizontalScrollContainer } from '@/hooks/useFadeOnHorizontalScrollContainer';

import breakpoints from '@/styles/breakpoints';
import { layoutMixins } from '@/styles/layoutMixins';
import { tabMixins } from '@/styles/tabMixins';
Expand Down Expand Up @@ -71,6 +73,11 @@ export const Tabs = <TabItemsValue extends string>({
const withBorders = dividerStyle === 'border';
const withUnderline = dividerStyle === 'underline';

const headerRef = useRef<HTMLDivElement>(null);
const { showFadeStart, showFadeEnd } = useFadeOnHorizontalScrollContainer({
scrollRef: headerRef,
});

const triggers = (
<>
<$List $fullWidthTabs={fullWidthTabs} $withBorders={withBorders}>
Expand Down Expand Up @@ -128,7 +135,17 @@ export const Tabs = <TabItemsValue extends string>({
$withInnerBorder={withBorders || withUnderline}
$uiRefreshEnabled={uiRefresh}
>
<$Header $side={side}>{triggers}</$Header>
{showFadeStart || showFadeEnd ? (
<$HorizontalScrollContainer showFadeStart={showFadeStart} showFadeEnd={showFadeEnd}>
<$Header $side={side} ref={headerRef}>
{triggers}
</$Header>
</$HorizontalScrollContainer>
) : (
<$Header $side={side} ref={headerRef}>
{triggers}
</$Header>
)}

{sharedContent ?? (
<div tw="stack shadow-none">
Expand Down Expand Up @@ -212,8 +229,33 @@ const $Root = styled(Root)<{
}
`;

const $HorizontalScrollContainer = styled.div<{
showFadeStart: boolean;
showFadeEnd: boolean;
}>`
${layoutMixins.horizontalFadeScrollArea}
--scrollArea-fade-zIndex: calc(var(--stickyHeader-zIndex) + 1);

${({ showFadeStart }) =>
!showFadeStart &&
css`
&:before {
opacity: 0;
}
`}

${({ showFadeEnd }) =>
!showFadeEnd &&
css`
&:after {
opacity: 0;
}
`};
`;

const $Header = styled.header<{ $side: 'top' | 'bottom' }>`
${layoutMixins.contentSectionDetachedScrollable}
flex: 1;

${({ $side }) =>
({
Expand Down
99 changes: 67 additions & 32 deletions src/components/ToggleGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Ref } from 'react';
import { useImperativeHandle, useRef, type Ref } from 'react';

import { Item, Root } from '@radix-ui/react-toggle-group';
import styled, { css } from 'styled-components';
Expand All @@ -7,6 +7,7 @@ import { ButtonShape, ButtonSize } from '@/constants/buttons';
import { type MenuItem } from '@/constants/menus';

import { useBreakpoints } from '@/hooks/useBreakpoints';
import { useFadeOnHorizontalScrollContainer } from '@/hooks/useFadeOnHorizontalScrollContainer';

import { layoutMixins } from '@/styles/layoutMixins';

Expand Down Expand Up @@ -48,42 +49,53 @@ export const ToggleGroup = forwardRefFn(
) => {
const { isTablet } = useBreakpoints();

const innerRef = useRef<HTMLInputElement>(null);
useImperativeHandle(ref, () => innerRef.current!, []);

const { showFadeStart, showFadeEnd } = useFadeOnHorizontalScrollContainer({
scrollRef: innerRef,
});

return (
<$Root
ref={ref}
type="single"
value={value}
onValueChange={(newValue: MenuItemValue) => {
if ((ensureSelected && newValue) || !ensureSelected) {
onValueChange(newValue);
}
onInteraction?.();
}}
className={className}
loop
overflow={overflow}
tw="row gap-[0.33em]"
>
{items.map((item) => (
<Item key={item.value} value={item.value} disabled={item.disabled} asChild>
<ToggleButton
size={size ?? (isTablet ? ButtonSize.Small : ButtonSize.XSmall)}
shape={shape}
disabled={item.disabled}
{...buttonProps}
>
{item.slotBefore}
<$Label>{item.label}</$Label>
{item.slotAfter}
</ToggleButton>
</Item>
))}
</$Root>
<$HorizontalScrollContainer showFadeStart={showFadeStart} showFadeEnd={showFadeEnd}>
<$Root
ref={innerRef}
type="single"
value={value}
onValueChange={(newValue: MenuItemValue) => {
if ((ensureSelected && newValue) || !ensureSelected) {
onValueChange(newValue);
}
onInteraction?.();
}}
className={className}
loop
overflow={overflow}
tw="row gap-[0.33em]"
>
{items.map((item) => (
<Item key={item.value} value={item.value} disabled={item.disabled} asChild>
<ToggleButton
size={size ?? (isTablet ? ButtonSize.Small : ButtonSize.XSmall)}
shape={shape}
disabled={item.disabled}
{...buttonProps}
>
{item.slotBefore}
<$Label>{item.label}</$Label>
{item.slotAfter}
</ToggleButton>
</Item>
))}
</$Root>
</$HorizontalScrollContainer>
);
}
);

const $Root = styled(Root)<{ overflow: 'scroll' | 'wrap' }>`
const $Root = styled(Root)<{
overflow: 'scroll' | 'wrap';
}>`
${({ overflow }) =>
({
scroll: css`
Expand All @@ -101,3 +113,26 @@ const $Label = styled.div`
// don't truncate 2 characters
min-width: 1rem;
`;

const $HorizontalScrollContainer = styled.div<{
showFadeStart: boolean;
showFadeEnd: boolean;
}>`
${layoutMixins.horizontalFadeScrollArea}

${({ showFadeStart }) =>
!showFadeStart &&
css`
&:before {
opacity: 0;
}
`}

${({ showFadeEnd }) =>
!showFadeEnd &&
css`
&:after {
opacity: 0;
}
`};
`;
54 changes: 54 additions & 0 deletions src/hooks/useFadeOnHorizontalScrollContainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { RefObject, useCallback, useEffect, useState } from 'react';

/**
* @description Apply horizontal fade to scroll container when scrollable
* Assumed that the two sides are the same height
*/
export const useFadeOnHorizontalScrollContainer = ({
scrollRef,
}: {
scrollRef?: RefObject<HTMLDivElement>;
}) => {
const [showFadeStart, setShowFadeStart] = useState(false);
const [showFadeEnd, setShowFadeEnd] = useState(false);

const scrollContainer = scrollRef?.current;

const onScroll = useCallback(() => {
if (scrollRef?.current) {
const { clientWidth, scrollWidth, scrollLeft } = scrollRef.current;
const scrollStart =
clientWidth != null &&
scrollWidth != null &&
scrollLeft != null &&
scrollWidth > clientWidth &&
scrollLeft > 0;
const scrollEnd =
clientWidth != null &&
scrollWidth != null &&
scrollLeft != null &&
scrollWidth > clientWidth + scrollLeft;

setShowFadeStart(scrollStart);
setShowFadeEnd(scrollEnd);
}
}, [scrollRef]);

// Set fade on initial mount of container
useEffect(() => {
onScroll();
});

// Adjust fade on scroll of container
useEffect(() => {
scrollContainer?.addEventListener('scroll', onScroll, false);
return () => {
scrollContainer?.removeEventListener('scroll', onScroll, false);
};
}, [onScroll, scrollContainer]);

return {
showFadeStart,
showFadeEnd,
};
};
2 changes: 1 addition & 1 deletion src/pages/trade/MarketSelectorAndStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const MarketSelectorAndStats = ({
const $Container = styled.div`
${layoutMixins.container}
${layoutMixins.scrollAreaFadeEnd}

display: grid;

grid-template:
Expand Down
17 changes: 15 additions & 2 deletions src/styles/layoutMixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const scrollSnapItem = css`
// Applies a fade to beginning of a scrollable container. Apply to the parent of layoutMixins.scrollArea
const scrollAreaFadeStart = css`
/* Params */
--scrollArea-fade-zIndex: 1;
--scrollArea-fadeWidth: 1.5rem;

/* Rules */
Expand All @@ -53,13 +54,14 @@ const scrollAreaFadeStart = css`
height: 100%;
width: var(--scrollArea-fadeWidth);
background: linear-gradient(to left, transparent 10%, var(--color-layer-2));
z-index: 1;
z-index: var(--scrollArea-fade-zIndex);
}
`;

// Applies a fade to end of a scrollable container. Apply to the parent of layoutMixins.scrollArea
const scrollAreaFadeEnd = css`
/* Params */
--scrollArea-fade-zIndex: 1;
--scrollArea-fadeWidth: 1.5rem;

/* Rules */
Expand All @@ -73,7 +75,7 @@ const scrollAreaFadeEnd = css`
height: 100%;
width: var(--scrollArea-fadeWidth);
background: linear-gradient(to right, transparent 10%, var(--color-layer-2));
z-index: 1;
z-index: var(--scrollArea-fade-zIndex);
}
`;

Expand All @@ -83,6 +85,16 @@ const scrollAreaFade = css`
${scrollAreaFadeEnd}
`;

const horizontalFadeScrollArea = css`
${scrollAreaFade}

display: flex;
align-items: center;
overflow: hidden;

transition: opacity 0.3s var(--ease-out-expo);
`;

// Creates a scrollable container that can contain sticky and/or scroll-snapped descendants.
const scrollArea = css`
/* Params */
Expand Down Expand Up @@ -523,6 +535,7 @@ export const layoutMixins = {
scrollAreaFadeStart,
scrollAreaFadeEnd,
scrollAreaFade,
horizontalFadeScrollArea,

scrollArea,

Expand Down
Loading
Loading