Skip to content

fix(switch)!: residual MD3 deviations in switch - #5222

Merged
Doberjohn merged 35 commits into
callstack:mainfrom
jacekAtCK:fix/switch-md3-deviations
Nov 18, 2026
Merged

Doberjohn merged 35 commits into
callstack:mainfrom
jacekAtCK:fix/switch-md3-deviations

Conversation

@jacekAtCK

@jacekAtCK jacekAtCK commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Takes over #5102 by @likevy and keeps the original commits. It fixes residual MD3 deviations in Switch: a 48dp tall touch target, focus handle color and state layer, immediate press growth, Space on web, and the onPrimaryContainer selected icon. A switch with no handler renders read-only and warns.

The switch now uses the shared focus ring from #5107, and a read-only switch shows no ring.

Also: Switch now reuses useSpaceKeyPress, the hook that also is used by Checkbox.
Warning for a switch without onValueChange ran during render. It now runs in an effect, once per mount (on non prod build).

Related issue

#5102. Merge order: #5150 (small rewrites, after #5110).

Screenshots / Videos

From #5102:

pr5102-switch-android.mp4

Test plan

Switch example: hold (the handle grows at once),
Tab on web (ring on the track) and Space (toggles),
Read-only (ignores taps, skipped by Tab, no ring).

likevy added 30 commits November 6, 2026 12:40
BREAKING CHANGE: a `Switch` with no `onValueChange` that is neither
`readOnly` nor `disabled` now renders read-only and logs a warning.
`Switch` reserves a 48dp tall touch target.
The handler branch of the union only accepted `readOnly={false}`, so a switch
with a stable handler could not become read-only from state, even though the
runtime already gives `readOnly` precedence and `disabled` allowed exactly that
pattern. The branch that requires `disabled` was equally strict.

A handler still guarantees operability whenever read-only is false, and a
switch with neither a handler nor a statically-true `readOnly`/`disabled` is
still rejected.
Interaction handlers are withheld while a switch is disabled, read-only, or
missing a handler, so one that was hovered, pressed, or focused at the moment
it flipped never received the matching hover-out, press-out, or blur. The
shared value stayed at 1 and the switch kept painting that state -- a disabled
switch could show a focus ring, and the stale value was still there if it
became interactive again.
The read-only test covered focusability and checked state but never the
attribute the read-only contract exists for on web, so dropping it left the
test green.
The runtime fallback for an untyped `<Switch value />` withheld the interaction
handlers but still emitted `aria-readonly={false}` alongside
`aria-disabled={false}`, so on web it was still exposed as an enabled switch
rather than the state indicator the guard renders.

Read-only is now derived from the rendered state -- non-operable but not
disabled -- which covers both an explicit `readOnly` and the fallback, while
`aria-disabled` keeps carrying the disabled case on its own.
The focus tests only fired a generic focus event on the default native
platform, where `isKeyboardFocusEvent` always returns true, so nothing covered
the `:focus-visible` gating that keeps a mouse click from lighting the ring.

Adds both web paths, driving `currentTarget.matches` directly.
Removing the 100ms delay was a user-visible fix with no test behind it, so the
delay could have come back unnoticed.

Asserting the size needs a testID on the view that carries it: `-handle` now
names the view holding the handle's size and position, and the tinted fill
inside it becomes `-handle-fill`.
Moving the selected icon to `onPrimaryContainer` left the tertiary-theme
example with a mixed palette: it overrides `primary`, `onPrimary` and
`primaryContainer`, and a partial theme falls back to the app theme for
omitted roles, so the icon stayed in the default palette.

Documents the role change in the migration guide too, since any partial
`Switch` theme in consumer code has the same gap.
The read-only tests checked the exposed metadata but never the guarantee it
stands for. The props type deliberately permits `readOnly` alongside
`onValueChange`, so read-only has to win at runtime -- and nothing would have
caught the handler being attached anyway.
The press test flushed all timers before asserting, so restoring the old
`withDelay(100, ...)` snap still reached the pressed size and the test passed.
It verified the end state, not the immediacy it was written for.

Now asserts the handle has already started growing two frames in, well inside
the delay the old implementation waited out, and keeps a separate case for the
settled sizes on press and release.
Each test that overrode `Platform.OS` or stubbed `console.warn` restored it on
the last line, which never runs when an assertion throws. A single failing
test left every later test in the file running as web, turning one real
failure into a cascade of misleading ones.

Matches the teardown FABExtended and Surface already use.
The operability union rejected props that are valid at runtime, such as
a conditional handler paired with `disabled`, and it collapsed in
wrappers built with `Omit`. `SwitchProps` is the flat `Props` again,
like `Checkbox` and `RadioButton`. A switch with no `onValueChange`
that is neither `readOnly` nor `disabled` still renders as read-only
and logs a warning.
The focus, press and read-only tests flattened every style in the tree
and compared or snapshotted the whole dump, so a change to any internal
layer broke them and the snapshots did not show which value mattered.
They now read the animated style of the one layer that changes: the
focus ring opacity, the state layer opacity, the handle size and the
handle color.
React Native Web's Pressable shows a pointer cursor whenever it is not
`disabled`, and a read-only switch keeps `disabled` false on web so it
can expose `aria-readonly`. The pointer suggested a click that does
nothing, so a switch that cannot be operated now uses the default
cursor.
`focusable` and `tabIndex` set the same thing. React Native's View
treats `tabIndex` as an alias of `focusable` on Android, and React
Native Web uses `tabIndex` when it is set and ignores the deprecated
`focusable`. The tests check `tabIndex` instead.

The disabled render snapshots now show Pressable's default
`focusable={true}` next to `tabIndex={-1}`, because the jest View mock
skips the aliasing that the real View applies.
In the pattern where a row owns the press and the switch only shows the
state, screen readers got the on/off state from neither element: iOS
merged the inner switch into the row, Android exposed a clickable row
next to a separate disabled switch, and on web the focusable row had no
role or `aria-checked`. The migration guide and the drawer, FAB and
TextInput example rows now give the row the switch role and state and
hide the inner switch from assistive technology.

Also drop the em dashes from the Switch migration notes.
A read-only switch keeps `disabled` false on web so it can expose
`aria-readonly`, but React Native Web's Pressable then still takes the
responder and stops the click, even without `onPress`. Inside a row
that owns the press, such as `List.Item` with the switch in `right`,
clicking the switch did nothing on web, while iOS and Android pass the
press to the row. A switch that cannot be operated now has
`pointerEvents: 'none'` on web instead of the `cursor: 'auto'`
override, so the click and the cursor come from what is underneath.
On web a focused switch that becomes disabled or read-only keeps DOM
focus, because React Native Web only changes its `tabIndex` and ARIA
state. The focus handlers were detached and the focus state cleared
meanwhile, so once the switch was operable again it was still focused
but showed no ring or state layer. Focus and blur are now tracked
regardless of interactivity, and the focus ring, state layer and focus
handle color are shown only while the switch can be operated.
The guide covers migrating from 5.x, where `Switch` was the platform
switch with no icon and no 40dp box, so notes saying the icon color
changed from `primary` and the height grew from 40dp only made sense
against a 6.0 prerelease. Both notes now describe the 6.x switch: the
selected icon uses `onPrimaryContainer`, and the switch is a 52x32
track inside a 48dp tall touch target.
React Native 0.88 types `onKeyDown` and `onKeyUp` as `KeyDownEvent` and
`KeyUpEvent`, so the DOM `KeyboardEvent` annotations no longer type
check. The handlers only read `nativeEvent.key`, `nativeEvent.repeat`
and `preventDefault()`, which both React Native's event and the React
synthetic event that React Native Web passes provide.
likevy and others added 2 commits November 6, 2026 12:41
React Native's legacy types, still the default before 0.87 and checked in CI, export neither KeyDownEvent nor KeyUpEvent. Type the Space handlers with a NativeSyntheticEvent of the fields they read, which both type sets accept.
Main now draws the Switch focus ring with the shared useFocusRing hook
(callstack#5107). The switch keeps that ring and drops its own ring view. The
hook gets !isInteractive, so a read-only switch shows no ring. The
focus tests check the outline on the track on native, and the ring
attribute on the track on web.

The migration guide keeps the Switch intro and removed props from
main, then adds the size, read-only and icon color notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Doberjohn
Doberjohn self-requested a review November 17, 2026 18:52
@Doberjohn

Copy link
Copy Markdown
Collaborator

/review

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on 320c35c. The verdict and the full list are in the summary comment.

Comment thread src/components/Switch/Switch.tsx Resolved
Comment thread src/components/Switch/Switch.tsx Resolved
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of d25fb43

Since the review of 320c35c: 0 new · 0 still open · 2 no longer raised

No open findings.

Not in the reviewers' prompt (1 file)
  • src/components/__tests__/__snapshots__/Switch.test.tsx.snap: generated or a lockfile, so its lines were left out

1 new finding away from what changed since 320c35c not shown. Comment @apex-sentinel-staging review for a full review.

Reviewed with this repository's own Sentinel configuration (a525c28839e4).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@Doberjohn

Copy link
Copy Markdown
Collaborator

One change for a follow-up PR:

  • src/components/__tests__/Switch.test.tsx:315: the "does not warn when readOnly is set without onValueChange" test fails when it runs on its own (yarn jest src/components/__tests__/Switch.test.tsx -t "does not warn when"). The first Switch render in a Jest worker also logs Reanimated's one-time "Dependencies should only be used on the web" warnings, so console.warn has 4 calls. In the full file it passes only because earlier tests already used those up. Checking for the Switch warning itself keeps it independent of test order:

    expect(console.warn).not.toHaveBeenCalledWith(
      expect.stringContaining('Switch:')
    );

Smaller things, optional:

  • src/components/Switch/Switch.tsx:442: every interactive switch on web renders aria-readonly="false". isAnnouncedReadOnly || undefined would leave the attribute off unless the switch is read-only.
  • src/components/Switch/Switch.tsx:465: a switch that has keyboard focus when it becomes read-only keeps DOM focus, useFocusRing drops the outline: none style, and the browser draws its own outline around the 52x48 box. disabled already behaves this way on main, so leaving it is fine. I'm mentioning it because the description says a read-only switch shows no ring.
  • example/src/Examples/SwitchExample.tsx:93: aria-label="Read-only" on the new switch would give the screen reader check in the test plan a name to announce.
  • docs/6.x/docs/guides/migration.md:1464: TertiarySwitch's props are untyped in a tsx block, so the snippet fails under strict if pasted as is. Typing them as { value: boolean; onValueChange: (value: boolean) => void } fixes that.

@Doberjohn
Doberjohn merged commit 677ab9f into callstack:main Nov 18, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants