feat(chip): modernize MD3 API#5002
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the Chip component toward the v6/MD3 API direction by introducing an explicit label prop, extracting Chip-specific MD3 tokens, and simplifying layout/color/state-layer handling to better match Material Design 3 behavior.
Changes:
- Added
labelprop (with deprecatedchildrenfallback) and updated examples/tests to use the new API. - Introduced
ChipTokensand refactored chip color/state-layer logic (getChipColors) to be token-driven. - Simplified Chip layout (leading/avatar/close handling) and removed press-time elevation animation overhead.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/components/Chip/tokens.ts | Adds Chip-specific MD3 dimension/color/opacity/elevation tokens used by the new implementation. |
| src/components/Chip/helpers.tsx | Refactors color calculations (container/border/label/icons/ripple) to use ChipTokens and MD3 state layer behavior. |
| src/components/Chip/Chip.tsx | Updates public API (label), simplifies layout/press handling, and adopts tokenized sizing/padding and ripple/state layer behavior. |
| src/components/tests/ListItem.test.tsx | Updates Chip usage to the new label prop. |
| src/components/tests/Chip.test.tsx | Updates Chip tests for label, adds coverage for label-vs-children precedence, and adds ripple/platform-color fallback test. |
| src/components/tests/snapshots/ListItem.test.tsx.snap | Snapshot updates reflecting new Chip layout/styling. |
| src/components/tests/snapshots/Chip.test.tsx.snap | Snapshot updates reflecting new Chip layout/styling and close button structure. |
| example/src/Examples/TooltipExample.tsx | Updates Chip usage to label. |
| example/src/Examples/TeamDetails.tsx | Updates Chip usage to label and refreshes chip row examples. |
| example/src/Examples/ListSectionExample.tsx | Updates Chip usage to label. |
| example/src/Examples/FABExample.tsx | Updates Chip usage to label. |
| example/src/Examples/ChipExample.tsx | Reworks Chip example screen to showcase MD3-oriented chip types/states using the new API. |
| example/src/Examples/CardExample.tsx | Updates Chip usage to label. |
| const Chip = ({ | ||
| mode = 'flat', | ||
| mode = 'outlined', | ||
| label, |
| touchable: { | ||
| height: '100%', | ||
| flexShrink: 1, | ||
| }, |
| if (disabled) { | ||
| return colors.onSurface; | ||
| return isOutlined | ||
| ? 'transparent' | ||
| : withAlpha( | ||
| theme.colors[ChipTokens.disabledColor], | ||
| ChipTokens.disabledContainer | ||
| ); |
| if (disabled) { | ||
| return withAlpha( | ||
| theme.colors[ChipTokens.disabledColor], | ||
| ChipTokens.disabledOutline | ||
| ); | ||
| } |
| avatarOverlayColor: withAlpha( | ||
| theme.colors.scrim, | ||
| ChipTokens.selectedAvatarOverlay | ||
| ), |
| container: { | ||
| borderWidth: StyleSheet.hairlineWidth, | ||
| height: ChipTokens.containerHeight, | ||
| borderWidth: ChipTokens.outlineWidth, | ||
| borderStyle: 'solid', | ||
| flexDirection: Platform.select({ default: 'column', web: 'row' }), | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| alignSelf: 'flex-start', | ||
| overflow: 'hidden', |
| style={({ pressed }) => [ | ||
| styles.closeButton, | ||
| Platform.OS === 'web' && pressed | ||
| ? { backgroundColor: rippleColor } | ||
| : null, | ||
| ]} |
satya164
left a comment
There was a problem hiding this comment.
Please remove old props and code entirely instead of deprecating them.
| /** | ||
| * Text content of the `Chip`. | ||
| * Text label of the `Chip`. | ||
| */ | ||
| label?: string; | ||
| /** | ||
| * @deprecated Use `label` instead. Children are kept as a compatibility | ||
| * fallback and should be plain text. | ||
| */ | ||
| children: React.ReactNode; | ||
| children?: React.ReactNode; |
There was a problem hiding this comment.
Revert this. There shouldn't be unnecessary breaking changes.
| /** | ||
| * @supported Available in v5.x with theme version 3 | ||
| * Whether to display overlay on selected chip | ||
| * @deprecated Selected chips use the MD3 selected container and state layer. | ||
| * This prop is kept as a compatibility overlay. | ||
| */ | ||
| showSelectedOverlay?: boolean; |
| /** | ||
| * @supported Available in v5.x with theme version 3 | ||
| * Sets smaller horizontal paddings `12dp` around label, when there is only label. | ||
| * @deprecated MD3 chips have a fixed density. This only reduces label-only | ||
| * horizontal padding for compatibility. | ||
| */ | ||
| compact?: boolean; |
| export const ChipTokens = { | ||
| ...dimensions, | ||
| ...colors, | ||
| ...opacity, | ||
| ...elevation, | ||
| }; |
There was a problem hiding this comment.
Export the constants directly instead of creating an additional object just to export.
| if (disabled) { | ||
| return withAlpha( | ||
| theme.colors[ChipTokens.disabledColor], | ||
| ChipTokens.disabledOutline | ||
| ); | ||
| } | ||
|
|
||
| return colors.secondaryContainer; | ||
| if (selectedColor !== undefined) { | ||
| return withAlpha(selectedColor, 0.29); | ||
| } |
There was a problem hiding this comment.
Lets avoid color manipulation and use tokens directly. If opacity etc. is needed, consider applying it to the view if possible.
Motivation
Refactors
Chiptoward the v6/MD3 API and implementation direction.This change introduces an explicit
labelprop, extracts Chip-specific tokens, simplifies the internal layout, and removes press-time elevationanimation overhead. It also updates selected, outlined, disabled, icon/avatar, close icon, touch target, and state layer/ripple handling to better align
with Material Design 3.
The example app has been updated to showcase the new API and MD3-oriented Chip states.
Related issue
Closes #4931
Test plan
cd example/android./gradlew app:assembleDebug -x lint -x test --configure-on-demand --build-cache -PreactNativeDevServerPort=8081 -PreactNativeArchitectures=arm64- v8ayarn typescriptyarn lint-no-fixyarn test --runInBand --watchman=falseyarn docs build