|
| 1 | +import React, { ElementType, Fragment } from "react"; |
| 2 | +import styled from "styled-components"; |
| 3 | +import { IconAngleLeft, IconAngleRight } from "hds-react"; |
| 4 | + |
| 5 | +export type RouteItem = { |
| 6 | + title: string; |
| 7 | + slug: string; |
| 8 | +}; |
| 9 | + |
| 10 | +type Props = { |
| 11 | + routes: RouteItem[]; |
| 12 | + linkComponent?: ElementType; |
| 13 | + className?: string; |
| 14 | +}; |
| 15 | + |
| 16 | +const limits = { |
| 17 | + default: 25, |
| 18 | + current: 40, |
| 19 | +}; |
| 20 | + |
| 21 | +const MobileWrapper = styled.div` |
| 22 | + display: flex; |
| 23 | + align-items: center; |
| 24 | + max-width: 100%; |
| 25 | +`; |
| 26 | + |
| 27 | +const DesktopWrapper = styled.div` |
| 28 | + display: flex; |
| 29 | + align-items: center; |
| 30 | +`; |
| 31 | + |
| 32 | +const Wrapper = styled.nav` |
| 33 | + background-color: var(--color-white); |
| 34 | + font-size: var(--fontsize-body-m); |
| 35 | + display: flex; |
| 36 | + align-items: center; |
| 37 | + max-width: var(--container-width-xl); |
| 38 | + margin: 0 auto; |
| 39 | + line-height: var(--spacing-3-xl); |
| 40 | + color: var(--color-black); |
| 41 | + padding: 0 var(--spacing-m); |
| 42 | +
|
| 43 | + && > a { |
| 44 | + color: var(--color-black); |
| 45 | + text-decoration: underline; |
| 46 | + } |
| 47 | +
|
| 48 | + ${MobileWrapper} { |
| 49 | + display: none; |
| 50 | + } |
| 51 | +
|
| 52 | + &.isMobile { |
| 53 | + padding-left: var(--spacing-xs); |
| 54 | +
|
| 55 | + ${MobileWrapper} { |
| 56 | + display: flex; |
| 57 | + } |
| 58 | + ${DesktopWrapper} { |
| 59 | + display: none; |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + svg { |
| 64 | + margin: 0 var(--spacing-3-xs); |
| 65 | + } |
| 66 | +`; |
| 67 | + |
| 68 | +const Anchor = styled.a<{ $current?: boolean }>` |
| 69 | + &&& { |
| 70 | + ${({ $current }) => { |
| 71 | + switch ($current) { |
| 72 | + case true: |
| 73 | + return ` |
| 74 | + color: var(--color-black); |
| 75 | + font-family: HelsinkiGrotesk-Medium, var(--font-default); |
| 76 | + font-weight: 500; |
| 77 | + `; |
| 78 | + case false: |
| 79 | + default: |
| 80 | + return ` |
| 81 | + color: var(--color-black); |
| 82 | + text-decoration: underline; |
| 83 | + `; |
| 84 | + } |
| 85 | + }} |
| 86 | + } |
| 87 | +
|
| 88 | + ${MobileWrapper} & { |
| 89 | + overflow: hidden; |
| 90 | + text-overflow: ellipsis; |
| 91 | + } |
| 92 | +
|
| 93 | + white-space: nowrap; |
| 94 | +`; |
| 95 | + |
| 96 | +const Slug = styled.span<{ $current?: boolean }>` |
| 97 | + ${({ $current }) => { |
| 98 | + switch ($current) { |
| 99 | + case true: |
| 100 | + return ` |
| 101 | + color: var(--color-black); |
| 102 | + font-family: HelsinkiGrotesk-Medium, var(--font-default); |
| 103 | + font-weight: 500; |
| 104 | + `; |
| 105 | + case false: |
| 106 | + default: |
| 107 | + return ``; |
| 108 | + } |
| 109 | + }} |
| 110 | +
|
| 111 | + white-space: nowrap; |
| 112 | +`; |
| 113 | + |
| 114 | +const Breadcrumb = ({ |
| 115 | + routes = [], |
| 116 | + linkComponent, |
| 117 | + className, |
| 118 | +}: Props): JSX.Element => { |
| 119 | + const Link = linkComponent || Fragment; |
| 120 | + |
| 121 | + const routesWithSlug = routes?.filter((n) => n.slug); |
| 122 | + const lastRoute = routes[routes.length - 1]; |
| 123 | + const lastRouteWithSlug = routesWithSlug[routesWithSlug.length - 1]; |
| 124 | + |
| 125 | + return ( |
| 126 | + <Wrapper className={className}> |
| 127 | + {routesWithSlug.length > 1 && lastRoute.slug !== lastRouteWithSlug.slug && ( |
| 128 | + <MobileWrapper> |
| 129 | + <IconAngleLeft size="s" aria-hidden className="angleLeft" /> |
| 130 | + <Link |
| 131 | + {...(linkComponent && { |
| 132 | + href: lastRouteWithSlug?.slug, |
| 133 | + passHref: true, |
| 134 | + })} |
| 135 | + > |
| 136 | + <Anchor |
| 137 | + {...(!linkComponent && { href: lastRouteWithSlug?.slug })} |
| 138 | + title={lastRouteWithSlug.title} |
| 139 | + > |
| 140 | + {lastRouteWithSlug.title} |
| 141 | + </Anchor> |
| 142 | + </Link> |
| 143 | + </MobileWrapper> |
| 144 | + )} |
| 145 | + {routes?.map((item, index) => ( |
| 146 | + <DesktopWrapper key={`${item.title}${item.slug}`}> |
| 147 | + {index > 0 && ( |
| 148 | + <IconAngleRight size="s" aria-hidden className="angleRight" /> |
| 149 | + )} |
| 150 | + {item.slug ? ( |
| 151 | + <Link {...(linkComponent && { href: item.slug, passHref: true })}> |
| 152 | + <Anchor |
| 153 | + {...(!linkComponent && { href: item.slug })} |
| 154 | + title={item.title} |
| 155 | + $current={index === routes.length - 1} |
| 156 | + > |
| 157 | + {index === routes.length - 1 |
| 158 | + ? item.title.length > limits.current |
| 159 | + ? `${item.title.slice(0, limits.current)}...` |
| 160 | + : item.title |
| 161 | + : item.title.length > limits.default |
| 162 | + ? `${item.title.slice(0, limits.default)}...` |
| 163 | + : item.title} |
| 164 | + </Anchor> |
| 165 | + </Link> |
| 166 | + ) : ( |
| 167 | + <Slug $current={index === routes.length - 1} title={item.title}> |
| 168 | + {index === routes.length - 1 |
| 169 | + ? item.title.length > limits.current |
| 170 | + ? `${item.title.slice(0, limits.current)}...` |
| 171 | + : item.title |
| 172 | + : item.title.length > limits.default |
| 173 | + ? `${item.title.slice(0, limits.default)}...` |
| 174 | + : item.title} |
| 175 | + </Slug> |
| 176 | + )} |
| 177 | + </DesktopWrapper> |
| 178 | + ))} |
| 179 | + </Wrapper> |
| 180 | + ); |
| 181 | +}; |
| 182 | + |
| 183 | +export default Breadcrumb; |
0 commit comments