-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add breadcrumbs to the dashboard views
- Loading branch information
Showing
5 changed files
with
145 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lms/static/scripts/frontend_apps/components/dashboard/DashboardBreadcrumbs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
ArrowLeftIcon, | ||
CaretRightIcon, | ||
Link, | ||
} from '@hypothesis/frontend-shared'; | ||
import classnames from 'classnames'; | ||
import { Link as RouterLink } from 'wouter-preact'; | ||
|
||
export type BreadcrumbLink = { | ||
title: string; | ||
href: string; | ||
}; | ||
|
||
export type DashboardBreadcrumbsProps = { | ||
links: BreadcrumbLink[]; | ||
}; | ||
|
||
function BreadcrumbLink({ title, href }: BreadcrumbLink) { | ||
return ( | ||
<RouterLink href={href} asChild> | ||
<Link underline="hover" variant="text-light" classes="truncate"> | ||
<ArrowLeftIcon className="inline-block md:hidden mr-1 align-sub" /> | ||
{title} | ||
</Link> | ||
</RouterLink> | ||
); | ||
} | ||
|
||
/** | ||
* Navigation breadcrumbs showing a list of links | ||
*/ | ||
export default function DashboardBreadcrumbs({ | ||
links, | ||
}: DashboardBreadcrumbsProps) { | ||
return ( | ||
<div | ||
className="flex flex-row gap-0.5 w-full font-semibold" | ||
data-testid="breadcrumbs-container" | ||
> | ||
{links.map(({ title, href }, index) => { | ||
const isLastLink = index === links.length - 1; | ||
return ( | ||
<span | ||
key={`${index}${href}`} | ||
className={classnames('gap-0.5', { | ||
// In mobile devices, show only the last link | ||
'md:flex hidden': !isLastLink, | ||
'flex max-w-full': isLastLink, | ||
// Distribute max width for every link as evenly as possible. | ||
// These must be static values for Tailwind to detect them. | ||
// See https://tailwindcss.com/docs/content-configuration#dynamic-class-names | ||
'md:max-w-[50%]': links.length === 2, | ||
'md:max-w-[33.333333%]': links.length === 3, | ||
'md:max-w-[25%]': links.length === 4, | ||
'md:max-w-[230px]': links.length > 4, | ||
})} | ||
> | ||
<BreadcrumbLink href={href} title={title} /> | ||
{!isLastLink && <CaretRightIcon />} | ||
</span> | ||
); | ||
})} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lms/static/scripts/frontend_apps/components/dashboard/test/DashboardBreadcrumbs-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { checkAccessibility } from '@hypothesis/frontend-testing'; | ||
import { mount } from 'enzyme'; | ||
|
||
import DashboardBreadcrumbs from '../DashboardBreadcrumbs'; | ||
|
||
describe('DashboardBreadcrumbs', () => { | ||
function createComponent(props = {}) { | ||
return mount(<DashboardBreadcrumbs {...props} />); | ||
} | ||
|
||
[['foo', 'bar'], [], ['one', 'two', 'three']].forEach(links => { | ||
it('shows expected amount of links', () => { | ||
const wrapper = createComponent({ | ||
links: links.map(title => ({ title, href: `/${title}` })), | ||
}); | ||
|
||
assert.equal(wrapper.find('BreadcrumbLink').length, links.length); | ||
}); | ||
}); | ||
|
||
it( | ||
'should pass a11y checks', | ||
checkAccessibility({ | ||
content: () => | ||
createComponent({ | ||
links: [ | ||
{ title: 'Foo', href: '/foo' }, | ||
{ title: 'Bar', href: '/bar' }, | ||
], | ||
}), | ||
}), | ||
); | ||
}); |