Skip to content

Commit

Permalink
fix: lets screenreaders know if links are external
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarni committed Dec 9, 2024
1 parent d5c09e4 commit e7b3e83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 9 additions & 1 deletion packages/core/src/components/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cn from "classnames";
import React from "react";
import React, { useId } from "react";
import { PolymorphicPropsWithRef, PolymorphicRef } from "../polymorphism";

export type LinkProps<ElementType extends React.ElementType> =
Expand All @@ -26,15 +26,23 @@ export const Link = React.forwardRef(function Link<
} = props;
const Component = as;

const srId = useId();

return (
<Component
ref={ref}
className={cn("jkl-link", className, {
"jkl-link--external": external,
})}
aria-describedby={external ? srId : undefined}
{...rest}
>
{children}
{external && (
<span hidden={true} id={srId}>
Ekstern lenke
</span>
)}
</Component>
);
});
10 changes: 9 additions & 1 deletion packages/jokul/src/components/link/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clsx } from "clsx";
import React from "react";
import React, { useId } from "react";
import {
PolymorphicPropsWithRef,
PolymorphicRef,
Expand Down Expand Up @@ -29,15 +29,23 @@ export const Link = React.forwardRef(function Link<
} = props;
const Component = as;

const srId = useId();

return (
<Component
ref={ref}
className={clsx("jkl-link", className, {
"jkl-link--external": external,
})}
aria-describedby={external ? srId : undefined}
{...rest}
>
{children}
{external && (
<span hidden={true} id={srId}>
Ekstern lenke
</span>
)}
</Component>
);
});
11 changes: 8 additions & 3 deletions packages/jokul/src/components/link/documentation/LinkExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { ExampleComponentProps, ExampleKnobsProps } from "doc-utils/index.js";
import React, { FC } from "react";
import { Link } from "../Link.js";

export const knobs: ExampleKnobsProps = {};
export const knobs: ExampleKnobsProps = {
boolProps: ["Ekstern"],
};

export const LinkExample: FC<ExampleComponentProps> = () => {
export const LinkExample: FC<ExampleComponentProps> = ({ boolValues }) => {
const external = boolValues?.["Ekstern"];
/* -- EXAMPLE CODE START -- */
return (
<div>
<Link href="#">Lenke</Link>
<Link href="#" external={external}>
Lenke
</Link>
</div>
);
/* -- EXAMPLE CODE END -- */
Expand Down

0 comments on commit e7b3e83

Please sign in to comment.