forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Breadcrumbs.tsx
100 lines (87 loc) · 2.54 KB
/
Breadcrumbs.tsx
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
import classnames from 'classnames'
import {To} from 'history'
import React from 'react'
import styled from 'styled-components'
import Box from './Box'
import {get} from './constants'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'
const SELECTED_CLASS = 'selected'
const Wrapper = styled.li`
display: inline-block;
white-space: nowrap;
list-style: none;
&::after {
padding-right: 0.5em;
padding-left: 0.5em;
color: ${get('colors.fg.muted')};
font-size: ${get('fontSizes.1')};
content: '/';
}
&:first-child {
margin-left: 0;
}
&:last-child {
&::after {
content: none;
}
}
`
const BreadcrumbsBase = styled.nav<SxProp>`
display: flex;
justify-content: space-between;
${sx};
`
export type BreadcrumbsProps = React.PropsWithChildren<
{
className?: string
} & SxProp
>
function Breadcrumbs({className, children, sx: sxProp}: React.PropsWithChildren<BreadcrumbsProps>) {
const wrappedChildren = React.Children.map(children, child => <Wrapper>{child}</Wrapper>)
return (
<BreadcrumbsBase className={className} aria-label="Breadcrumbs" sx={sxProp}>
<Box as="ol" my={0} pl={0}>
{wrappedChildren}
</Box>
</BreadcrumbsBase>
)
}
type StyledBreadcrumbsItemProps = {
to?: To
selected?: boolean
} & SxProp
const BreadcrumbsItem = styled.a.attrs<StyledBreadcrumbsItemProps>(props => ({
activeClassName: typeof props.to === 'string' ? 'selected' : '',
className: classnames(props.selected && SELECTED_CLASS, props.className),
'aria-current': props.selected ? 'page' : null
}))<StyledBreadcrumbsItemProps>`
color: ${get('colors.accent.fg')};
display: inline-block;
font-size: ${get('fontSizes.1')};
text-decoration: none;
&:hover {
text-decoration: underline;
}
&.selected {
color: ${get('colors.fg.default')};
pointer-events: none;
}
${sx};
`
Breadcrumbs.displayName = 'Breadcrumbs'
BreadcrumbsItem.displayName = 'Breadcrumbs.Item'
export type BreadcrumbsItemProps = ComponentProps<typeof BreadcrumbsItem>
export default Object.assign(Breadcrumbs, {Item: BreadcrumbsItem})
/**
* @deprecated Use the `Breadcrumbs` component instead (i.e. `<Breadcrumb>` → `<Breadcrumbs>`)
*/
export const Breadcrumb = Object.assign(Breadcrumbs, {Item: BreadcrumbsItem})
/**
* @deprecated Use the `BreadcrumbsProps` type instead
*/
export type BreadcrumbProps = ComponentProps<typeof BreadcrumbsBase>
/**
* @deprecated Use the `BreadcrumbsItemProps` type instead
*/
export type BreadcrumbItemProps = ComponentProps<typeof BreadcrumbsItem>