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

Ensure Buttons with a url prop output valid HTML #773

Merged
merged 1 commit into from
Jan 15, 2019
Merged

Ensure Buttons with a url prop output valid HTML #773

merged 1 commit into from
Jan 15, 2019

Conversation

BPScott
Copy link
Member

@BPScott BPScott commented Dec 17, 2018

WHY are these changes introduced?

Ensure disabled Buttons with a url prop output valid HTML

Fixes #757.

WHAT is this pull request doing?

When a button has a url it is rendered as an <a> tag, which does not
have a disabled attribute.

Stop adding the disabled attribute in that case.

As disabled is an invalid prop it doesn't do what we were hoping in this context

You can't click these <a> tags due to pointer-events:none being set on them, but you can still tab to them using the keyboard.

@dpersing / others: We should come up with what we want the expected behaviour to be when rendering a <Button url="foo" disabled>BAR</Button>

EDIT! Plan made: use an <a> without a href

How to 🎩

Copy-paste this code in playground/Playground.tsx:
import * as React from 'react';
import {Button, Page} from '@shopify/polaris';

interface State {}

export default class Playground extends React.Component<{}, State> {
  render() {
    return (
      <Page
        title="Playground"
        primaryAction={{content: 'View Examples', url: '/examples'}}
      >
        <h2>Normal</h2>
        <Button>ONE (rendered as &lt;button>)</Button>
        <Button url="/foo">TWO (rendered as &lt;a>)</Button>
        <h2>Disabled</h2>
        <Button disabled>THREE (rendered as &lt;button>)</Button>
        <Button disabled url="/foo">
          FOUR (rendered as &lt;a>)
        </Button>
        <h2>Loading</h2>
        <Button loading>FIVE (rendered as &lt;button>)</Button>
        <Button loading url="/foo">
          SIX (rendered as &lt;a>)
        </Button>

        <h2>Final</h2>
        <Button>FINAL BUTTON</Button>
      </Page>
    );
  }
}

Ensure that the <Button disabled url="/foo">TWO</Button> element does not render a disabled attribute in the generated HTML markup.

@BPScott BPScott requested a review from kaelig December 17, 2018 20:11
@BPScott BPScott temporarily deployed to polaris-react-pr-773 December 17, 2018 20:12 Inactive
@BPScott BPScott temporarily deployed to polaris-react-pr-773 December 17, 2018 21:12 Inactive
Copy link
Contributor

@dpersing dpersing left a comment

Choose a reason for hiding this comment

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

I think there are two things to consider: The disabled-related functionality and the styling.

Ensure that the TWO element does not render a disabled attribute in the generated HTML markup.

Thinking about this more, I think it would make sense to convert the "disabled" link to a non-interactive link by removing the href. That is, if someone produces a "disabled" link, it shouldn't have a valid href so it can't be accessed. This renders the <a> but makes it non-interactive.

Another alternative would be to add tabindex="-1" to the "disabled" link. However, this doesn't convey anything semantic, so I think it's not as clean a solution.

Whichever way we go, I think it would be good to also include a note about how the props can cancel each other out in the props explorer help text.

@kaelig
Copy link
Contributor

kaelig commented Dec 19, 2018

Another alternative would be to add tabindex="-1" to the "disabled" link. However, this doesn't convey anything semantic, so I think it's not as clean a solution.

Would that link still show up when someone using a screen reader pulls the list of all links in the page? If so, this may not be a good solution, as we want the link to be unreachable for everyone.

Copy link
Contributor

@kaelig kaelig left a comment

Choose a reason for hiding this comment

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

This PR fixes the issue, and the HTML is now valid.

Requires additional accessibility work for certain edge cases, but I think we can go ahead and merge.

@dpersing
Copy link
Contributor

dpersing commented Dec 20, 2018

Would that link still show up when someone using a screen reader pulls the list of all links in the page?

It would. The negative tabindex just makes it unreachable with the keyboard. To make it hidden to screen reader users, it would also need aria-hidden="true". @kaelig

@BPScott
Copy link
Member Author

BPScott commented Dec 20, 2018

Thinking about this more, I think it would make sense to convert the "disabled" link to a non-interactive link by removing the href. That is, if someone produces a "disabled" link, it shouldn't have a valid href so it can't be accessed. This renders the <a> but makes it non-interactive.

I think this is the preferable solution too. This will require a wee refactor as (sensibly) you can't omit the url property when calling <UnstyledLink> so we'd have to output a <span> or an <a> without a href within Button rather than deferring to UnstyledLink but I think that's fine.

@kaelig
Copy link
Contributor

kaelig commented Jan 10, 2019

Should we still merge this?

@BPScott BPScott temporarily deployed to polaris-react-pr-773 January 10, 2019 22:36 Inactive
@BPScott
Copy link
Member Author

BPScott commented Jan 10, 2019

Dragging it up to date now

@BPScott BPScott temporarily deployed to polaris-react-pr-773 January 10, 2019 23:46 Inactive
@BPScott
Copy link
Member Author

BPScott commented Jan 10, 2019

Daft realisation: why bother rendering span or a's without hrefs - just render <button disabled> instead!

EDIT: Per Devon on Slack: Because buttons get announced in a separate list and it might be odd for someone to hear a disabled button in that list then for it to disappear when it gets enabled (as it is no longer a button)

@BPScott BPScott temporarily deployed to polaris-react-pr-773 January 11, 2019 00:27 Inactive
@BPScott BPScott temporarily deployed to polaris-react-pr-773 January 11, 2019 00:35 Inactive
Copy link
Contributor

@dpersing dpersing left a comment

Choose a reason for hiding this comment

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

🎉

// This results in a minimal DOM change when the Button moves between
// enabled and disabled states.
// eslint-disable-next-line jsx-a11y/anchor-is-valid
<a id={id} className={className} aria-label={accessibilityLabel}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this still appear as a navigable anchor in the accessibility tree (just like a good old <a name="foo"></a>)?

Copy link
Contributor

Choose a reason for hiding this comment

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

would a span be a better idea @dpersing?

);
}

// UnstyledLink cant be disabled so always render a button when disabled
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with this one. The code is clear enough imo.

Always render a `<button disabled>` when rendering disabled Buttons
as `<a disabled>` is not valid html
@BPScott
Copy link
Member Author

BPScott commented Jan 15, 2019

Whoops, I thought I merged this last week!

@danrosenthal danrosenthal temporarily deployed to production January 16, 2019 18:10 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants