Skip to content

Add type to all internal button elements #4912

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";functionhandleSubmit(event: React.FormEvent<HTMLFormElement>){event.preventDefault();console.log("form submitted");}exportdefaultfunctionApp(){return(<formonSubmit={handleSubmit}><TabPanelsaria-label="Select a tab"id="tab-panels"defaultTabIndex={0}><TabPanels.Tab>Tab1</TabPanels.Tab><TabPanels.Tab>Tab2</TabPanels.Tab><TabPanels.Tab>Tab3</TabPanels.Tab><TabPanels.Panel>Panel1</TabPanels.Panel><TabPanels.Panel>Panel2</TabPanels.Panel><TabPanels.Panel>Panel3</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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions