-
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
140 additions
and
30 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 { Fragment } from 'preact'; | ||
import { Link as RouterLink } from 'wouter-preact'; | ||
|
||
type BreadcrumbLink = { | ||
title: string; | ||
href: string; | ||
}; | ||
|
||
export type DashboardBreadcrumbsProps = { | ||
links: BreadcrumbLink[]; | ||
}; | ||
|
||
function BreadcrumbLink({ | ||
title, | ||
href, | ||
classes, | ||
}: BreadcrumbLink & { classes?: string }) { | ||
return ( | ||
<RouterLink href={href} asChild> | ||
<Link | ||
underline="hover" | ||
variant="text-light" | ||
classes={classnames('truncate md:max-w-[350px]', classes)} | ||
> | ||
<ArrowLeftIcon className="md:hidden mr-2 inline" /> | ||
{title} | ||
</Link> | ||
</RouterLink> | ||
); | ||
} | ||
|
||
export default function DashboardBreadcrumbs({ | ||
links, | ||
}: DashboardBreadcrumbsProps) { | ||
return ( | ||
<div | ||
className="font-bold flex flex-col md:flex-row gap-0.5" | ||
data-testid="breadcrumbs-container" | ||
> | ||
{links.map(({ title, href }, index) => { | ||
const isLastLink = index === links.length - 1; | ||
|
||
return ( | ||
<Fragment key={`${index}${href}`}> | ||
<BreadcrumbLink | ||
href={href} | ||
title={title} | ||
classes={classnames('md:inline', { | ||
// In mobile devices, show only the last link | ||
hidden: !isLastLink, | ||
})} | ||
/> | ||
{!isLastLink && <CaretRightIcon />} | ||
</Fragment> | ||
); | ||
})} | ||
</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
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
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({ | ||
title: 'Hello world', | ||
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' }, | ||
], | ||
}), | ||
}), | ||
); | ||
}); |