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

Rui/breadcrumbs #1505

Merged
merged 2 commits into from
Aug 4, 2023
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@polkadot/ui-keyring": "^2.9.7",
"@reach/tooltip": "^0.16.0",
"@react-aria/accordion": "^3.0.0-alpha.14",
"@react-aria/breadcrumbs": "^3.5.3",
"@react-aria/button": "^3.6.4",
"@react-aria/dialog": "^3.3.1",
"@react-aria/focus": "^3.6.1",
Expand Down
21 changes: 21 additions & 0 deletions src/assets/icons/ChevronRight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { forwardRef } from 'react';

import { Icon, IconProps } from '@/component-library/Icon';

const ChevronRight = forwardRef<SVGSVGElement, IconProps>((props, ref) => (
<Icon
{...props}
ref={ref}
xmlns='http://www.w3.org/2000/svg'
fill='none'
viewBox='0 0 24 24'
strokeWidth='1.5'
stroke='currentColor'
>
<path strokeLinecap='round' strokeLinejoin='round' d='M8.25 4.5l7.5 7.5-7.5 7.5' />
</Icon>
));

ChevronRight.displayName = 'ChevronRight';

export { ChevronRight };
1 change: 1 addition & 0 deletions src/assets/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { ArrowsUpDown } from './ArrowsUpDown';
export { ArrowTopRightOnSquare } from './ArrowTopRightOnSquare';
export { CheckCircle } from './CheckCircle';
export { ChevronDown } from './ChevronDown';
export { ChevronRight } from './ChevronRight';
export { Cog } from './Cog';
export { DocumentDuplicate } from './DocumentDuplicate';
export { InformationCircle } from './InformationCircle';
Expand Down
56 changes: 56 additions & 0 deletions src/component-library/Breadcrumbs/BreadcrumbItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { AriaBreadcrumbsProps, useBreadcrumbItem } from '@react-aria/breadcrumbs';
import { mergeProps } from '@react-aria/utils';
import { AnchorHTMLAttributes, Fragment, useRef } from 'react';

import { ChevronRight } from '@/assets/icons';

import { TextLinkProps } from '../TextLink';
import { StyledLinkBreadcrumb, StyledSpanBreadcrumb } from './Breadcrumbs.style';

type Props = {
isDisabled?: boolean;
isCurrent: boolean;
to: TextLinkProps['to'];
};

type InheritAttrs = Omit<AriaBreadcrumbsProps, keyof Props>;

type NativeAttrs = Omit<AnchorHTMLAttributes<unknown>, keyof (Props & InheritAttrs)>;

type BreadcrumbItemProps = Props & NativeAttrs & InheritAttrs;

const BreadcrumbItem = ({ children, isDisabled, isCurrent, to, ...props }: BreadcrumbItemProps): JSX.Element => {
const ref = useRef(null);
const { itemProps } = useBreadcrumbItem(
{
...props,
children,
isDisabled: isCurrent,
elementType: isCurrent ? 'span' : 'a'
},
ref
);

const commonProps: Pick<TextLinkProps, 'size' | 'color'> = {
size: 's',
color: isCurrent ? 'secondary' : 'tertiary'
};

return (
<Fragment>
{isCurrent ? (
<StyledSpanBreadcrumb ref={ref} {...mergeProps(commonProps, itemProps)}>
{children}
</StyledSpanBreadcrumb>
) : (
<StyledLinkBreadcrumb ref={ref} to={to} $isDisabled={isDisabled} {...mergeProps(commonProps, itemProps)}>
{children}
</StyledLinkBreadcrumb>
)}
{isCurrent === false && <ChevronRight size='xs' color='tertiary' />}
</Fragment>
);
};

export { BreadcrumbItem };
export type { BreadcrumbItemProps };
20 changes: 20 additions & 0 deletions src/component-library/Breadcrumbs/Breadcrumbs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Meta, Story } from '@storybook/react';

import { BreadcrumbItem, Breadcrumbs, BreadcrumbsProps } from '.';

const Template: Story<BreadcrumbsProps> = (args) => (
<Breadcrumbs {...args}>
<BreadcrumbItem to='#'>Strategies</BreadcrumbItem>
<BreadcrumbItem to='#'>BTC Passive Income</BreadcrumbItem>
</Breadcrumbs>
);

const Default = Template.bind({});
Default.args = {};

export { Default };

export default {
title: 'Forms/Breadcrumbs',
component: Breadcrumbs
} as Meta;
40 changes: 40 additions & 0 deletions src/component-library/Breadcrumbs/Breadcrumbs.style.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import styled from 'styled-components';

import { Span } from '../Text';
import { TextLink } from '../TextLink';
import { theme } from '../theme';

type StyledBreadcrumbProps = {
$isDisabled?: boolean;
};

const StyledNav = styled.nav``;

const StyledList = styled.ul`
flex-wrap: nowrap;
flex: 1 0;
justify-content: flex-start;
margin: 0;
padding: 0;
list-style-type: none;
display: flex;
`;

const StyledListItem = styled.li`
justify-content: flex-start;
align-items: center;
display: inline-flex;
position: relative;
`;

const StyledSpanBreadcrumb = styled(Span)<StyledBreadcrumbProps>`
padding: 0 ${theme.spacing.spacing2};
cursor: default;
`;

const StyledLinkBreadcrumb = styled(TextLink)<StyledBreadcrumbProps>`
padding: 0 ${theme.spacing.spacing2};
text-decoration: none;
`;

export { StyledLinkBreadcrumb, StyledList, StyledListItem, StyledNav, StyledSpanBreadcrumb };
55 changes: 55 additions & 0 deletions src/component-library/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { AriaBreadcrumbsProps, useBreadcrumbs } from '@react-aria/breadcrumbs';
import { Children, forwardRef, HTMLAttributes, isValidElement, ReactElement } from 'react';

import { useDOMRef } from '../utils/dom';
import { BreadcrumbItem } from './BreadcrumbItem';
import { StyledList, StyledListItem, StyledNav } from './Breadcrumbs.style';

type Props = {
onAction?: (key: React.Key) => void;
isDisabled?: boolean;
};

type NativeAttrs = Omit<HTMLAttributes<unknown>, keyof Props>;

type InheritAttrs = Omit<AriaBreadcrumbsProps, keyof Props>;

type BreadcrumbsProps = Props & NativeAttrs & InheritAttrs;

const Breadcrumbs = forwardRef<HTMLElement, BreadcrumbsProps>(
({ children, isDisabled, ...props }, ref): JSX.Element => {
const domRef = useDOMRef(ref);

const { navProps } = useBreadcrumbs(props);

const childArray: ReactElement[] = [];
Children.forEach(children, (child) => {
if (isValidElement(child)) {
childArray.push(child);
}
});

const lastIndex = childArray.length - 1;

const breadcrumbItems = childArray.map((child, index) => {
const isCurrent = index === lastIndex;

return (
<StyledListItem key={index}>
<BreadcrumbItem {...child.props} isCurrent={isCurrent} isDisabled={isDisabled} />
</StyledListItem>
);
});

return (
<StyledNav {...navProps} ref={domRef}>
<StyledList>{breadcrumbItems}</StyledList>
</StyledNav>
);
}
);

Breadcrumbs.displayName = 'Breadcrumbs';

export { Breadcrumbs };
export type { BreadcrumbsProps };
10 changes: 10 additions & 0 deletions src/component-library/Breadcrumbs/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BreadcrumbItem as LibBreadcrumbItem, BreadcrumbItemProps as LibBreadcrumbItemProps } from './BreadcrumbItem';

type BreadcrumbItemProps = Omit<LibBreadcrumbItemProps, 'isCurrent'>;

const BreadcrumbItem = (props: BreadcrumbItemProps): JSX.Element => <LibBreadcrumbItem isCurrent={false} {...props} />;
tomjeatt marked this conversation as resolved.
Show resolved Hide resolved

export type { BreadcrumbsProps } from './Breadcrumbs';
export { Breadcrumbs } from './Breadcrumbs';
export { BreadcrumbItem };
export type { BreadcrumbItemProps };
2 changes: 2 additions & 0 deletions src/component-library/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type { AccordionItemProps, AccordionProps } from './Accordion';
export { Accordion, AccordionItem } from './Accordion';
export type { AlertProps } from './Alert';
export { Alert } from './Alert';
export type { BreadcrumbItemProps, BreadcrumbsProps } from './Breadcrumbs';
export { BreadcrumbItem, Breadcrumbs } from './Breadcrumbs';
export type { CardProps } from './Card';
export { Card } from './Card';
export type { CoinIconProps } from './CoinIcon';
Expand Down
1 change: 1 addition & 0 deletions src/component-library/theme/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ const theme = {
},
icon: {
sizes: {
xs: 'var(--spacing-3)',
s: 'var(--spacing-4)',
md: 'var(--spacing-6)',
lg: 'var(--spacing-8)',
Expand Down
6 changes: 5 additions & 1 deletion src/pages/Strategies/Strategy/Strategy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useTranslation } from 'react-i18next';
import { useHistory, useParams } from 'react-router';

import { Card, CoinIcon, Flex, H1, H2, P, TextLink } from '@/component-library';
import { BreadcrumbItem, Breadcrumbs, Card, CoinIcon, Flex, H1, H2, P, TextLink } from '@/component-library';
import { MainContainer } from '@/components';
import FullLoadingSpinner from '@/legacy-components/FullLoadingSpinner';
import { PAGES, URL_PARAMETERS } from '@/utils/constants/links';
Expand Down Expand Up @@ -39,6 +39,10 @@ const Strategy = (): JSX.Element | null => {
return (
<MainContainer>
<Flex direction='column' gap='spacing6'>
<Breadcrumbs>
<BreadcrumbItem to={PAGES.STRATEGIES}>{t('navigation.strategies')}</BreadcrumbItem>
<BreadcrumbItem to='#'>{title}</BreadcrumbItem>
</Breadcrumbs>
<H1 size='lg' weight='bold'>
<Flex elementType='span' alignItems='center' gap='spacing2'>
<CoinIcon ticker={strategy.currency.ticker} /> {title}
Expand Down
Loading