Skip to content

Add type to all internal button elements #4912

@lseppala

Description

@lseppala

Description

Several Primer UI elements use the <button> element as part of rendering, such as Tab nav. Buttons without a type tag automatically default to type="submit" (MDN docs). If there's a form tag containing any Primer components that render buttons, interacting with the element will trigger the form submission action. This is surprising and undesirable, especially when the component doesn't make sense as a submission button.

(xref: https://github.com/github/internal-statuspage/pull/4091, https://github.com/github/incident-lifecycle/issues/737)

Steps to reproduce

I had some trouble setting up the latest version of primer on CodeSandbox (it complains about missing files from @oddbird/popover-polyfill, despite adding it as a dependency). Here's a minimum example that demonstrates the issue:

import { TabPanels } from "@primer/react";

function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
  event.preventDefault();
  console.log("form submitted");
}

export default function App() {
  return (
    <form onSubmit={handleSubmit}>
      <TabPanels aria-label="Select a tab" id="tab-panels" defaultTabIndex={0}>
        <TabPanels.Tab>Tab 1</TabPanels.Tab>
        <TabPanels.Tab>Tab 2</TabPanels.Tab>
        <TabPanels.Tab>Tab 3</TabPanels.Tab>
        <TabPanels.Panel>Panel 1</TabPanels.Panel>
        <TabPanels.Panel>Panel 2</TabPanels.Panel>
        <TabPanels.Panel>Panel 3</TabPanels.Panel>
      </TabPanels>
    </form>
  );
}

Clicking each of the tabs will cause form submitted to be printed to the console, because each tab renders a button without a type attribute.

My opinion (not that anyone asked for it): it would be best to avoid unspecified type attributes for buttons with a linting check, like with https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/button-has-type.md

Version

v36.27.0

Browser

Chrome, Safari, Firefox, Edge, iOS Safari

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions