File size: 3,442 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import { Gridicon } from '@automattic/components';
import styled from '@emotion/styled';
import { useTranslate } from 'i18n-calypso';
import { Key } from 'react';
import InfoPopover from 'calypso/components/info-popover';
const flexAligned = {
display: 'flex',
alignItems: 'center',
};
const StyledUl = styled.ul`
${ flexAligned };
list-style-type: none;
margin: 0;
`;
const StyledLi = styled.li`
${ flexAligned };
font-size: 13px;
font-weight: 400;
--color-link: var( --studio-gray-50 );
a {
text-decoration: none;
}
& .info-popover {
align-self: flex-start;
}
:last-of-type:not( :first-of-type ) {
--color-link: var( --studio-gray-80 );
font-weight: 500;
}
`;
const StyledBackLink = styled.a`
${ flexAligned };
font-size: 13px;
&,
&:link,
&:visited,
&:hover,
&:active {
color: var( --studio-gray-80 );
}
> svg {
margin-right: 5px;
}
`;
const StyledRootLabel = styled.span`
${ flexAligned };
font-size: 1rem;
font-weight: 600;
color: var( --studio-gray-80 );
`;
const StyledItem = styled.div`
display: flex;
`;
const StyledGridicon = styled( Gridicon )`
margin: 0 16px;
color: var( --color-neutral-10 );
`;
const HelpBubble = styled( InfoPopover )`
margin-left: 7px;
display: flex;
align-items: center;
& .gridicon {
color: var( --studio-gray-30 );
}
&:focus {
outline: thin dotted;
}
`;
const StyledIcon = styled.div`
margin-right: 10px;
`;
const renderHelpBubble = ( item: Item ) => {
if ( ! item.helpBubble ) {
return null;
}
return (
<HelpBubble icon="help-outline" position="right">
{ item.helpBubble }
</HelpBubble>
);
};
export type Item = {
id?: string;
label: string;
href?: string;
helpBubble?: React.ReactElement;
icon?: React.ReactElement;
onClick?: () => void;
className?: string;
};
interface Props {
items: Item[];
mobileItem?: Item;
compact?: boolean;
hideWhenOnlyOneLevel?: boolean;
}
const Breadcrumb: React.FunctionComponent< Props > = ( props ) => {
const translate = useTranslate();
const { items = [], mobileItem, compact = false, hideWhenOnlyOneLevel } = props;
if ( items.length === 0 ) {
return null;
}
if ( items.length === 1 ) {
if ( hideWhenOnlyOneLevel ) {
return null;
}
const [ item ] = items;
return (
<StyledItem>
{ item.icon && <StyledIcon>{ item.icon }</StyledIcon> }
<StyledRootLabel>{ item.label }</StyledRootLabel>
{ renderHelpBubble( item ) }
</StyledItem>
);
}
if ( compact ) {
const urlBack = mobileItem?.href ?? items[ items.length - 2 ].href;
const onClick = mobileItem?.onClick ?? items[ items.length - 2 ].onClick;
const label = mobileItem?.label ?? translate( 'Back' );
return (
<StyledBackLink className="breadcrumbs-back" href={ urlBack } onClick={ onClick }>
<Gridicon icon="chevron-left" size={ 18 } />
{ label }
</StyledBackLink>
);
}
return (
<StyledUl className="breadcrumbs">
{ items.map( ( item: Item, index: Key ) => (
<StyledLi key={ index } className={ item.className }>
{ item.icon && <StyledIcon>{ item.icon }</StyledIcon> }
{ index !== 0 && <StyledGridicon icon="chevron-right" size={ 14 } /> }
{ item.href && index !== items.length - 1 ? (
<a href={ item.href } onClick={ item.onClick }>
{ item.label }
</a>
) : (
<span>{ item.label }</span>
) }
{ renderHelpBubble( item ) }
</StyledLi>
) ) }
</StyledUl>
);
};
export default Breadcrumb;
|