[utils] Add opt-in DataAttributesOverrides augmentation for slot props#48554
Open
LukasTy wants to merge 1 commit into
Open
[utils] Add opt-in DataAttributesOverrides augmentation for slot props#48554LukasTy wants to merge 1 commit into
DataAttributesOverrides augmentation for slot props#48554LukasTy wants to merge 1 commit into
Conversation
Today, passing `data-testid` (or any `data-*` attribute) through `slotProps`
on a MUI component is a TypeScript error even though the attribute is
forwarded to the DOM at runtime. This adds a single, opt-in switch that
lets consumers declare exactly which `data-*` keys they want typed, at
whichever level of strictness they choose.
- `DataAttributesOverrides`: module-augmentable empty interface in
`@mui/utils/types`. The single sanctioned switch.
- `DataAttributes = DataAttributesOverrides`: dormant by default;
populated only when a consumer augments.
- `WithDataAttributes<T> = T | (T & DataAttributes)`: union form so the
original `T` stays assignable as-is (preserves backwards compatibility
with `x as CustomProps` style casts on slot values), while augmented
keys flow through the widened branch when consumers opt in.
- `SlotComponentProps` and `SlotComponentPropsWithSlotState` now wrap
their object and callback branches with `WithDataAttributes`. With an
empty default the wrapping is a no-op until a consumer augments — once
they do, every Material component that reaches slot props through these
helpers (or through `SlotProps` in `@mui/material`) picks up the new
keys automatically.
Consumers opt in with a single `declare module '@mui/utils/types' { ... }`
block. Examples are documented in the new `DataAttributes.ts` file and
exercised by a module-augmentation test using Backdrop's root slot.
This is the canonical place for the helper because every MUI slot prop
type ultimately flows through `@mui/utils/types`; downstream packages
(`@mui/x-*` and friends) get the augmentation transitively without
having to mirror the helper themselves.
Deploy previewhttps://deploy-preview-48554--material-ui.netlify.app/ Bundle size
Check out the code infra dashboard for more information about this PR. |
mj12albert
reviewed
May 20, 2026
| * augmentation is the single switch consumers can flip to choose their level | ||
| * of strictness. | ||
| * | ||
| * Examples: |
Member
There was a problem hiding this comment.
nit: could use JSdoc @example
| * } | ||
| * } | ||
| * | ||
| * // Loose: accept any `data-*` key on slots. |
Member
There was a problem hiding this comment.
Could the spec cover this as well?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a single opt-in switch —
DataAttributesOverrides— that lets consumers declare typed support fordata-*attributes on every MUI slot prop. Augmenting the interface is the one sanctioned way to flip the level of strictness; nothing is widened by default.Today, code like
is a TypeScript error even though the attribute is forwarded to the DOM at runtime. After this PR, consumers can opt in with a one-time augmentation:
Or, for the loose / "anything goes" form:
After augmentation, every Material component that wires slot props through
SlotComponentProps/SlotComponentPropsWithSlotState(or throughSlotPropsin@mui/material) picks up the augmented keys automatically.Changes
@mui/utils/typesDataAttributes.ts:DataAttributesOverrides— empty, module-augmentable interface. The single switch consumers flip.DataAttributes = DataAttributesOverrides— dormant by default; activates when augmented.WithDataAttributes<T> = T | (T & DataAttributes)— union form so the originalTstays assignable as-is (preserves backwards compatibility withx as CustomPropsstyle casts on slot values), while augmented keys flow through the widened branch when consumers opt in.index.tsre-exports the new symbols, and wraps bothSlotComponentPropsandSlotComponentPropsWithSlotState(object branch and callback branch) withWithDataAttributes.Module-augmentation test
packages/mui-material/test/typescript/moduleAugmentation/dataAttributesOverrides.{spec.tsx,tsconfig.json}augments@mui/utils/typesand uses Backdrop'srootslot to verify the augmentation flows throughSlotProps→SlotComponentProps→WithDataAttributes.Notes for reviewers
Why a union, not an intersection
Only the widened variant carries
DataAttributes. This preserves backwards compatibility:{ id: 'foo' } as CustomLabelPropsassigns to the narrow variant, soCustomLabelPropsdoes not need to declare adata-${string}index signature.{ 'data-testid': 'x' }(after augmentation) assigns to the widened variant.WithDataAttributes, so consumers returningsomeObj as CustomPropsfrom a slot callback stay assignable.Why opt-in (no default widening)
Slot prop types should not silently accept arbitrary
data-*keys — that hides typos, makes the surface less discoverable in hover/autocomplete, and disagrees with the principle that React's typed surface is what consumers see.The augmentation hook is a single, well-known module path (
@mui/utils/types) that consumers can shape to whichever level they want: strict (one named key), loose (fulldata-*template-literal index signature), or anywhere in between.Why
@mui/utils/typesis the right homeEvery MUI slot prop type ultimately flows through
SlotComponentProps/SlotComponentPropsWithSlotStatein@mui/utils/types. Putting the augmentation hook here means:@mui/x-data-grid,@mui/x-date-pickers,@mui/x-charts, ...) inherit the augmentation transitively — they don't need to mirror the helper themselves.Test plan
pnpm --filter "@mui/utils" run typescriptpasses.pnpm --filter "@mui/material" run typescriptpasses.pnpm typescript:module-augmentation— all 21+ existing tests pass, plus the new one.pnpm prettier --checkandpnpm eslintclean on the changed files.DataAttributesOverridesaugmentation above,<Backdrop slotProps={{ root: { 'data-testid': 'foo' } }} />type-checks and forwards to the DOM as expected. Without the augmentation, the same code is a TS error (matches today).