Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linkProps prop to NavigationItem component #3280

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/orange-countries-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@salt-ds/core": minor
---

`NavigationItem`: add `linkProps` prop to allow extending `HTMLAnchorElement`'s functionality (eg. when using `NavigationItem` with other third-party navigation libraries).
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ describe("GIVEN a NavItem", () => {
});
});

describe("AND `href` is NOT passed", () => {
describe("AND `href` is NOT passed AND `linkProps` is passed", () => {
it("should render a link using linkProps", () => {
cy.mount(
<NavigationItem
linkProps={{ href: "https://www.saltdesignsystem.com" }}
>
Navigation Item
</NavigationItem>
);
cy.findByRole("link").should(
"have.attr",
"href",
"https://www.saltdesignsystem.com"
);
});
});

describe("AND `href` is NOT passed AND `linkProps` is NOT passed", () => {
it("should render a button", () => {
cy.mount(<NavigationItem>Navigation Item</NavigationItem>);
cy.findByRole("button").should("exist");
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/navigation-item/ConditionalWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { NavigationItemProps } from "./NavigationItem";
interface ConditionalWrapperProps
extends Pick<
NavigationItemProps,
"parent" | "expanded" | "onExpand" | "active" | "href" | "onClick"
| "parent"
| "expanded"
| "onExpand"
| "active"
| "href"
| "onClick"
| "linkProps"
> {
children: ReactNode;
className: string;
Expand All @@ -18,13 +24,14 @@ export const ConditionalWrapper = ({
onExpand,
active,
href,
linkProps,
}: ConditionalWrapperProps) => {
const handleExpand = (event: MouseEvent<HTMLButtonElement>) => {
event.stopPropagation();
onExpand?.(event);
};

return parent || href === undefined ? (
return parent || (href === undefined && linkProps === undefined) ? (
<button
aria-label="expand"
aria-expanded={expanded}
Expand All @@ -38,6 +45,7 @@ export const ConditionalWrapper = ({
aria-current={active ? "page" : undefined}
href={href}
className={className}
{...linkProps}
>
{children}
</a>
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/navigation-item/NavigationItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export interface NavigationItemProps extends ComponentPropsWithoutRef<"div"> {
* Href to be passed to the Link element.
*/
href?: string;
/**
* Additional properties passed to the Link element.
*/
linkProps?: ComponentPropsWithoutRef<"a">;
}

const withBaseName = makePrefixer("saltNavigationItem");
Expand All @@ -60,6 +64,7 @@ export const NavigationItem = forwardRef<HTMLDivElement, NavigationItemProps>(
onExpand,
href,
style: styleProp,
linkProps = {},
...rest
} = props;

Expand Down Expand Up @@ -97,6 +102,7 @@ export const NavigationItem = forwardRef<HTMLDivElement, NavigationItemProps>(
onExpand={onExpand}
active={active}
href={href}
linkProps={linkProps}
>
<span className={withBaseName("label")}>{children}</span>
{parent && (
Expand Down