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
fix: replace card button with link #2750
fix: replace card button with link #2750
Conversation
…ionally add a keyboard event listener to the <a> element
…ionally add a keyboard event listener to the <a> element
…barActivation is true
…yling to override default link color
A couple of comments/questions for @werdnanoslen / whoever else at Truss is reading this:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving for design, accessibility, and meeting the issue AC. I'll defer to someone else on code review and first two questions above.
I think the way you've done it maximizes user control and exposes underlying components in a clear way that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! Personally good to approve, but would love to see a default value for the new allowSpacebarActivation
prop, just to be super explicit.
Please ping me cause I'll need to re-review at that point anyways!
<Link | ||
className="usa-button" | ||
variant="unstyled" | ||
allowSpacebarActivation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As someone who hasn't tried using spacebar to activate an anchor tag (but have used it for buttons) this was a fun rabbit hole (w3c/aria#1701). I think this is a fair feature.
<a className={classes} {...linkProps}> | ||
<a | ||
className={classes} | ||
{...(allowSpacebarActivation && { onKeyDown: handleKeyDown })} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion, non blocking: You could instead use allowSpacebarActivation
inside the handleKeyDown method to decide whether or not to do anything. I think this is fine though, just conditionally spreading props like this looks a little wonky at first.
@@ -33,6 +34,13 @@ export function isCustomProps<T>( | |||
): props is CustomLinkProps<T> { | |||
return 'asCustom' in props | |||
} | |||
// keyboard handler for 'link as a button' | |||
const handleKeyDown = (e: React.KeyboardEvent) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping typing this as const handleKeyDown: React.KeyboardEventHandler<HTMLAnchorElement>
would maybe avoid the need to cast e.target, but alas, it does not. So IMO, this is fine.
Co-authored-by: Brandon Lenz <15805554+brandonlenz@users.noreply.github.com>
Summary
This PR replaces
<Button>
components with<Link>
components styled to look like buttons in the Storybook files for the Card component. Adds styling-related props from the original<Button>
components (outline
,secondary
) via the corresponding CSS classes from USWDS - ie.usa-button--secondary
This PR also adds an optional
allowSpacebarActivation
prop to the Link component. When this prop is set totrue
the link can be activated with the spacebar in addition to the Enter key.Related Issues or PRs
How To Test
Navigate to Components > Card > Card Examples, Components > Card > Card Tests, and Components > Typography > Link > Styled As Button in Storybook.
For each button:
<a>
element instead of a<button>
Note - visual regression testing will require review - this PR adds additional text to the story for Link Styled As A Button specifying that it can be activated with the spacebar.
Screenshots (optional)
Closes #2696