-
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
135 additions
and
26 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
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
import { | ||
ArrowLeftIcon, | ||
CaretRightIcon, | ||
Link, | ||
} from '@hypothesis/frontend-shared'; | ||
import classnames from 'classnames'; | ||
import { Link as RouterLink } from 'wouter-preact'; | ||
|
||
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> | ||
); | ||
} | ||
|
||
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. | ||
// Setting explicit classes here is required for Tailwind to | ||
// properly detect they are being used. | ||
'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
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' }, | ||
], | ||
}), | ||
}), | ||
); | ||
}); |