Skip to content

Commit

Permalink
Extend types to cover className function.
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Mar 8, 2024
1 parent acaf4a7 commit 4b204d4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/wouter-preact/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ type AsChildProps<ComponentProps, DefaultElementProps> =
| ({ asChild?: false } & DefaultElementProps)
| ({ asChild: true } & ComponentProps);

type HTMLLinkAttributes = Omit<JSX.HTMLAttributes, "className"> & {
className?: string | undefined | ((isActive: boolean) => string | undefined);
};

export type LinkProps<H extends BaseLocationHook = BrowserLocationHook> =
NavigationalProps<H> &
AsChildProps<
{ children: ComponentChildren; onClick?: JSX.MouseEventHandler<Element> },
JSX.HTMLAttributes
HTMLLinkAttributes
>;

export type RedirectProps<H extends BaseLocationHook = BrowserLocationHook> =
Expand Down
12 changes: 12 additions & 0 deletions packages/wouter/test/link.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ describe("<Link /> types", () => {
</Link>;
});

it("can accept function as `className`", () => {
<Link
href="/"
className={(isActive) => (isActive ? "active" : "non-active")}
/>;

<Link
href="/"
className={(isActive) => (isActive ? "active" : undefined)}
/>;
});

it("should support other navigation params", () => {
<Link href="/" state={{ a: "foo" }}>
test
Expand Down
9 changes: 8 additions & 1 deletion packages/wouter/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,18 @@ type AsChildProps<ComponentProps, DefaultElementProps> =
| ({ asChild?: false } & DefaultElementProps)
| ({ asChild: true } & ComponentProps);

type HTMLLinkAttributes = Omit<
AnchorHTMLAttributes<HTMLAnchorElement>,
"className"
> & {
className?: string | undefined | ((isActive: boolean) => string | undefined);
};

export type LinkProps<H extends BaseLocationHook = BrowserLocationHook> =
NavigationalProps<H> &
AsChildProps<
{ children: ReactElement; onClick?: MouseEventHandler },
AnchorHTMLAttributes<HTMLAnchorElement> & RefAttributes<HTMLAnchorElement>
HTMLLinkAttributes & RefAttributes<HTMLAnchorElement>
>;

export function Link<H extends BaseLocationHook = BrowserLocationHook>(
Expand Down

0 comments on commit 4b204d4

Please sign in to comment.