diff --git a/contributor-docs/adrs/adr-026-parts-based-composition.md b/contributor-docs/adrs/adr-026-parts-based-composition.md new file mode 100644 index 00000000000..466079f6d2a --- /dev/null +++ b/contributor-docs/adrs/adr-026-parts-based-composition.md @@ -0,0 +1,316 @@ +# [ADR] Adopt a parts-based composition API inspired by Base UI + +Date: 2026-11-17 · Updated: 2026-11-23 + +## Status + +| Stage | State | +| -------------- | -------------- | +| Status | Proposed ❓ | +| Implementation | Not planned ⛔ | + +## Context + +GitHub teams tell us they're reaching for libraries like Base UI when a design is hard to build with Primer, often due to APIs that are inconsistent or too locked down to properly experiment with new combinations. This often leads to re-implementations of common patterns, and risks regressions for a11y or performance. + +With Base UI, teams get a consistent way to compose components and combine their behavior. We want teams to be able to do that in Primer. + +### What breaks today + +- **Small changes can break composition.** A Tooltip works on an `ActionMenu.Button`, but [extracting the pair into a reusable component can break the menu](https://github.com/primer/react/blob/59b51e1e33dbe6a40a90035a42b4e0e7c495051b/packages/react/src/ActionMenu/ActionMenu.tsx#L124-L139). In one product example, the team also has to remove the Tooltip from the React tree when the button is disabled to work around limitations in our APIs.  +- **Consumers have to wire components together themselves.** On a project card, the same button shows a Tooltip on hover and opens an overlay on click. The team has to connect both refs by hand. +- **Changing the layout takes more work than it should.** Moving a Dialog title or its actions means fitting the new layout back into the structure Dialog expects. +- **The same customization behaves differently across components.** One component might keep both class names or call both click handlers. Another might drop Primer's when the caller supplies one. Teams have to read the source to tell. +- **Callbacks don't give consumers enough information.** `ActionMenu` tells consumers that it closed. They can't tell from the callback whether someone pressed Escape or clicked outside, or cancel that change without taking over its state. + +## Decision + +We'll adopt Base UI's approach to composition as a starting point for Primer's parts-based API. All Primer components will follow the same API conventions. Components will expose parts where consumers need to arrange their own layouts or combine behavior. Simpler components may not need parts. + +Parts and configured components will share Primer's existing implementation. Teams can keep using configured components for standard patterns and use parts when they need more control. Primer will define and maintain the public API, even if we change the implementation later. + +### What changes + +- **Components are built from parts.** Every interactive component will expose named parts so consumers can move a trigger into their own component, wrap a title, or leave out an optional close button. +- **Every rendered part supports `as` and `render`.** Use `as` to change the element type. Use `render` to put the part's behavior on an element you supply, or pass a function that receives its props (including `ref`) and state. A Tooltip and a menu can share one button without consumers connecting refs by hand. Roots that only provide state and context don't take these props. +- **Parts follow Primer's prop-merging conventions.** Parts use [ADR-025](https://github.com/primer/react/blob/main/contributor-docs/adrs/adr-025-prop-merging.md) to combine Primer's props with the consumer's, including props on a `render` target. The merge order stays the same across components. +- **State-change callbacks use a consistent shape.** New callbacks take one object with the proposed value and information about what prompted the change. The same interaction uses the same `reason` across components. Callbacks that support cancellation include `cancel()` so consumers can stop a change without taking over the component's state. +- **The same prop means the same thing everywhere.** Shared props use the same names, values, and defaults across components. If two components offer the same sizes, callers shouldn't have to learn two different ways to refer to `medium`. +- **Component health scorecards track how components measure up to Primer's standards.** We'll start with [checks for API design and composition](#component-health-scorecards) and use them to review new APIs and recheck shipped components as our standards change. + +### What stays the same + +- **Existing component APIs keep working.** Teams can keep using `` with `ActionMenu.Button`, or `Dialog` with `title` and `onClose`. Existing subcomponents keep their prop handling, and existing callbacks keep their signatures, so adopting parts doesn't require rewriting existing usage. +- **Configured components keep their data APIs.** Teams can keep supplying data for common patterns and use parts when they need to arrange the content themselves. Consumers choose a configured prop or a part for a given piece of content. +- **Existing `as` props and slots stay supported.** Root `as` props and slots keep working where they're supported, so consumers can adopt parts without replacing customizations they already use. +- **Primer keeps its current implementation.** We'll reuse Primer's behavior code, types, CSS Modules, and accessibility defaults across parts and configured components as much as possible, and expose utilities to help teams adopt parts. + +### Example: a Tooltip on a menu trigger + +Today, simplified from the product menu above: + +```tsx + + + Medium + + {/* items */} + +``` + +This works while the Tooltip and button are inline. If a team moves them into an `` component, `ActionMenu` sees the wrapper and no longer recognizes its trigger. + +With the proposed API (the part names are examples): + +```tsx + + + Medium} />} /> + Reasoning effort + + {/* items */} + +``` + +The same `