Skip to content

Conversation

@pedrodurek
Copy link
Member

WHY are these changes introduced?

I'm working on a task in Shopify/web where I call the preventDefault function (event handler), but it's not returning the appropriate types for Button callbacks.

WHAT is this pull request doing?

Fixing button types (callbacks)

How to 🎩

🖥 Local development instructions
🗒 General tophatting guidelines
📄 Changelog guidelines

Copy-paste this code in playground/Playground.tsx:
import React from 'react';
import {Page} from '../src';

export function Playground() {
  return (
    <Page title="Playground">
      {/* Add the code you want to test in here */}
    </Page>
  );
}

🎩 checklist

  • Tested on mobile
  • Tested on multiple browsers
  • Tested for accessibility
  • Updated the component's README.md with documentation changes
  • Tophatted documentation changes in the style guide
  • For visual design changes, pinged one of @ HYPD, @ mirualves, @ sarahill, or @ ry5n to update the Polaris UI kit

@ghost
Copy link

ghost commented Apr 8, 2021

👋 Thanks for opening your first pull request. A contributor should give feedback soon. If you haven’t already, please check out the contributing guidelines.

@pedrodurek pedrodurek self-assigned this Apr 8, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Apr 8, 2021

🔴 This pull request modifies 1 files and might impact 115 other files. Because this is a larger than average splash zone for a change, remember to tophat areas that could be affected.

Details:
All files potentially affected (total: 115)
🧩 src/types.ts (total: 115)

Files potentially affected (total: 115)

@BPScott
Copy link
Member

BPScott commented Apr 9, 2021

Hi. Have an old issue: #521

In particular note @lemonmade's comment that points out that while this is good for use on web it is fundamentally incompatible in other worlds like Argo and exposing these dom events, and potentially relying on them is going to be a big problem for Argo.

@clauderic
Copy link
Member

Hey @BPScott,

I think it'd be worthwhile to re-visit this discussion. @lemonmade's comments around how this is fundamentally incompatible with Argo make sense and I generally share those concerns. I also agree we should be discouraging consumers from relying on these events and opening issues instead for the things that are currently hard to implement.

With that said, I fundamentally disagree with the current strategy of type obfuscation. It's one thing not to document the fact that events are available in order to discourage use; on the other hand, type obfuscation has real consequences with regards to the type safety of consuming applications.

This recently caused a bug in production for us on the Online Store because we passed a handler that accepted optional arguments to a Button handler, which you would assume is safe to do based on the type declaration of the Button props, but is not safe in practice.

function foo(optionalArg?: boolean) {
  if (optionalArg) {
    throw new Error();
  }
}

<Button onClick={foo} /> // Typed as () => void, but is in fact (event: React.SyntheticEvent) => void

// On click, error is thrown because `foo` is called with `event`

Were these handlers properly typed, TypeScript would have caught this issue. If the event is there, the types should reflect this, regardless of whether or not we want to discourage use.

@BPScott
Copy link
Member

BPScott commented May 25, 2021

I think it'd be worthwhile to re-visit this discussion

Happy to rediscuss and provide context, though I'm not on the polaris team these days so the final decision will fall to @Shopify/polaris-team. :)

@alex-page alex-page requested a review from lemonmade May 26, 2021 02:16
@lemonmade
Copy link
Member

@alex-page not sure if you had a particular thing in mind from my review, but my opinion on this is pretty much unchanged. I agree with @clauderic that the current lie the API tells you needs to be resolved, but I would resolve it by changing the implementation of Button to no longer implicitly pass along the event argument (along with any other event handlers that do something similar), rather than adding all these event objects as actual, guaranteed APIs.

@alex-page alex-page force-pushed the main branch 7 times, most recently from 2c6e842 to d2611d6 Compare June 11, 2021 16:24
@pedrodurek pedrodurek closed this Jun 25, 2021
@alex-page alex-page deleted the button-types branch March 22, 2022 04:17
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