-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add component classes to Breadcrumbs (#23)
* apply component classes to breadcrumbs * return styled span elements with aria-current attr if children are strings
- Loading branch information
Showing
7 changed files
with
89 additions
and
81 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
import * as React from 'react'; | ||
import { classNames } from '@chbphone55/classnames'; | ||
import type { BreadcrumbsProps } from './props'; | ||
import { interleave } from '@warp-ds/core/breadcrumbs'; | ||
import { breadcrumbs as ccBreadcrumbs } from "@warp-ds/component-classes"; | ||
|
||
export const Breadcrumbs = (props: BreadcrumbsProps) => { | ||
const { children, className, ...rest } = props; | ||
const ariaLabel = props['aria-label'] || 'Her er du'; | ||
|
||
// Handles arrays of nodes passed as children | ||
const flattenedChildren = children.flat(Infinity); | ||
const styledChildren = flattenedChildren.map((child, index) => { | ||
if (React.isValidElement(child)) { | ||
const ccClasses = child.type === "a" ? ccBreadcrumbs.link : ccBreadcrumbs.text; | ||
const newClasses = child.props.className ? `${child.props.className} ${ccClasses}` : ccClasses; | ||
|
||
// To update a prop on React child element, we need to clone that Element first | ||
const styledChild = React.cloneElement(child as React.ReactElement, { className: newClasses }); | ||
return styledChild; | ||
} | ||
|
||
const isLastEl = index === flattenedChildren.length - 1; | ||
|
||
return <span className={ccBreadcrumbs.text} aria-current={isLastEl ? "page" : undefined}>{child}</span>; | ||
} | ||
) | ||
|
||
return ( | ||
<nav | ||
className={classNames('flex space-x-8 space-x-reverse', className)} | ||
className={className} | ||
aria-label={ariaLabel} | ||
{...rest} | ||
> | ||
<h2 className="sr-only">{ariaLabel}</h2> | ||
{interleave( | ||
flattenedChildren, | ||
<span aria-hidden className="select-none"> | ||
/ | ||
</span>, | ||
).map((element, index) => ( | ||
<React.Fragment key={index}>{element}</React.Fragment> | ||
))} | ||
<h2 className={ccBreadcrumbs.a11y}>{ariaLabel}</h2> | ||
<div className={ccBreadcrumbs.wrapper}> | ||
{interleave( | ||
styledChildren, | ||
<span aria-hidden className={ccBreadcrumbs.separator}> | ||
/ | ||
</span>, | ||
).map((element, index) => ( | ||
<React.Fragment key={index}>{element}</React.Fragment> | ||
))} | ||
</div> | ||
</nav> | ||
); | ||
}; |
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
Oops, something went wrong.