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

Improve keyboard accessibility of the codecard component #10405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 22 additions & 6 deletions webapp/src/codecard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export class CodeCardView extends data.Component<CodeCardProps, CodeCardState> {
card.onClick(e);
} : undefined;

const keydownHandler = (e: React.KeyboardEvent) => {
const charCode = (typeof e.which == "number") ? e.which : e.keyCode;
if (charCode === /*enter*/13 || charCode === /*space*/32) {
clickHandler(e);
}
}

const imageUrl = card.imageUrl || (card.youTubeId ? `https://img.youtube.com/vi/${card.youTubeId}/0.jpg` : undefined);

// these header-derived properties must be taken from the virtual API system, not the props. Otherwise
Expand All @@ -95,9 +102,18 @@ export class CodeCardView extends data.Component<CodeCardProps, CodeCardState> {
const ariaLabel = card.ariaLabel || card.title || card.shortName || name;

const style = card.style || "card"
const cardDiv = <div className={`ui ${style} ${color} ${card.onClick ? "link" : ''} ${className ? className : ''}`}
role={card.role} aria-selected={card.role === "option" ? "true" : undefined} aria-label={ariaLabel} title={card.title}
onClick={clickHandler} tabIndex={card.onClick ? card.tabIndex || 0 : null} onKeyDown={card.onClick ? fireClickOnEnter : null}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fireClickOnEnter does the wrong thing here when using "Enter" or "Space" while the "Feedback" anchor tag is focussed. It prevents the default behaviour, then triggers click on this element instead of the anchor tag from which the original event was triggered, causing the experiment to toggle on/off.


const renderButton = (content: JSX.Element) => {
return (<div className={`ui ${style} ${color} ${card.onClick ? "link" : ''} ${className ? className : ''}`}
role={card.role} aria-selected={card.role === "option" ? "true" : undefined} aria-label={ariaLabel} title={card.title}
onClick={clickHandler} tabIndex={card.onClick ? card.tabIndex || 0 : null} onKeyDown={keydownHandler}>{content}</div>)
}
const renderLink = (content: JSX.Element) => {
return (<a href={url} className={`ui ${style} ${color} link ${className ? className : ''}`}
aria-label={ariaLabel} title={card.title}>{content}</a>)
}

const cardContent = <>
{card.header ?
<div key="header" className={"ui content " + (card.responsive ? " tall desktop only" : "")}>
{card.header}
Expand Down Expand Up @@ -169,12 +185,12 @@ export class CodeCardView extends data.Component<CodeCardProps, CodeCardState> {
{lf("Feedback")}
</a> : undefined}
</div> : undefined}
</div>;
</>;

if (!card.onClick && url) {
return <a href={url}>{cardDiv}</a>;
return (renderLink(cardContent))
} else {
return (cardDiv)
return (renderButton(cardContent))
}
}
}
2 changes: 1 addition & 1 deletion webapp/src/scriptsearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ export class ScriptSearch extends data.Component<ISettingsProps, ScriptSearchSta
/>}
{showOpenBeta && <codecard.CodeCardView
ariaLabel={lf("Open the next version of the editor")}
role="button"
role="link"
key={'beta'}
className="beta"
icon="lab ui cardimage"
Expand Down