From 138b5d0a8d4595f73bcbc727f9ee5a87e42b0780 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Thu, 19 Nov 2026 12:53:45 +0100 Subject: [PATCH 1/3] fix(switch): readOnly without onValueChange warning fix --- docs/6.x/docs/guides/migration.md | 8 +++++++- example/src/Examples/SwitchExample.tsx | 2 +- src/components/Switch/Switch.tsx | 9 ++++++--- src/components/__tests__/Switch.test.tsx | 21 +++++++++++++++++++-- src/utils/useFocusRing.ts | 2 +- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 0b0b13c650..36a83520ab 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -1883,7 +1883,13 @@ When selected, an icon set with `checkedIcon` uses `onPrimaryContainer`. If you ```tsx import { Switch, useTheme } from 'react-native-paper'; -const TertiarySwitch = ({ value, onValueChange }) => { +const TertiarySwitch = ({ + value, + onValueChange, +}: { + value: boolean; + onValueChange: (value: boolean) => void; +}) => { const theme = useTheme(); const switchTheme = { colors: { diff --git a/example/src/Examples/SwitchExample.tsx b/example/src/Examples/SwitchExample.tsx index 09a3c7ec92..345c78be78 100644 --- a/example/src/Examples/SwitchExample.tsx +++ b/example/src/Examples/SwitchExample.tsx @@ -90,7 +90,7 @@ const SwitchExample = () => { - + { // Not the ring - it's real CSS on web now. This drives the @@ -504,8 +504,11 @@ const Switch = ({ // A read-only switch stays enabled on web, where React Native Web's // Pressable would take the press and stop the click from reaching a // row that owns it. Native platforms disable the Pressable instead. + // A switch that has focus when it stops being interactive keeps DOM + // focus, and the focus ring no longer hides the browser's outline, + // so this hides it too. Platform.OS === 'web' && !isInteractive - ? styles.nonInteractiveTouchable + ? [styles.nonInteractiveTouchable, webNoOutlineStyle] : undefined, ]} {...focusTarget.dataSetProps} diff --git a/src/components/__tests__/Switch.test.tsx b/src/components/__tests__/Switch.test.tsx index bdd378a556..72eee73d72 100644 --- a/src/components/__tests__/Switch.test.tsx +++ b/src/components/__tests__/Switch.test.tsx @@ -7,6 +7,7 @@ import type { JsonElement, JsonNode } from 'test-renderer'; import { fireEvent, render, screen, userEvent } from '../../test-utils'; import { LightTheme } from '../../theme/schemes'; import { tokens } from '../../theme/tokens'; +import { webNoOutlineStyle } from '../../utils/useFocusRing'; import Switch from '../Switch/Switch'; const { colors } = LightTheme; @@ -312,7 +313,9 @@ describe('Switch operability', () => { await render(); - expect(console.warn).not.toHaveBeenCalled(); + expect(console.warn).not.toHaveBeenCalledWith( + expect.stringContaining('Switch:') + ); } ); @@ -393,7 +396,7 @@ describe('Switch operability', () => { await render(); expect(screen.getByRole('switch')).toBeDisabled(); - expect(screen.getByRole('switch')).not.toHaveProp('aria-readonly', true); + expect(screen.getByRole('switch')).not.toHaveProp('aria-readonly'); }); }); @@ -422,6 +425,20 @@ describe('Switch operability', () => { expect(screen.getByRole('switch')).toBeEnabled(); }); + it('does not expose aria-readonly on an operable switch on web', async () => { + jest.replaceProperty(Platform, 'OS', 'web'); + await render( {}} />); + + expect(screen.getByRole('switch')).not.toHaveProp('aria-readonly'); + }); + + it('hides the browser focus outline on a read-only switch on web', async () => { + jest.replaceProperty(Platform, 'OS', 'web'); + await render(); + + expect(screen.getByRole('switch')).toHaveStyle(webNoOutlineStyle); + }); + it('exposes a missing-handler fallback as read-only on web', async () => { jest.replaceProperty(Platform, 'OS', 'web'); jest.spyOn(console, 'warn').mockImplementation(() => {}); diff --git a/src/utils/useFocusRing.ts b/src/utils/useFocusRing.ts index 762f70e5cb..9cfef8d88d 100644 --- a/src/utils/useFocusRing.ts +++ b/src/utils/useFocusRing.ts @@ -99,7 +99,7 @@ const EMPTY: FocusRingResult = { * `scope: 'within'` leaves that to the stylesheet below instead. */ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -const webNoOutlineStyle = { outline: 'none' } as unknown as ViewStyle; +export const webNoOutlineStyle = { outline: 'none' } as unknown as ViewStyle; /** * MD3 keyboard focus indicator, from one hook shared by every component built From 68441718d491dde53bedc6b7bc16a6de14f02ad1 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Mon, 23 Nov 2026 02:12:25 +0100 Subject: [PATCH 2/3] fix(switch): keep the browser outline on a focused read-only switch --- src/components/Switch/Switch.tsx | 7 ++----- src/components/__tests__/Switch.test.tsx | 8 -------- src/utils/useFocusRing.ts | 2 +- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/components/Switch/Switch.tsx b/src/components/Switch/Switch.tsx index 3de2f07ac8..7fca10847c 100644 --- a/src/components/Switch/Switch.tsx +++ b/src/components/Switch/Switch.tsx @@ -30,7 +30,7 @@ import { toRawSpring } from '../../theme/tokens/sys/motion'; import { cornerFull } from '../../theme/tokens/sys/shape'; import type { StateOpacityKey, ThemeProp } from '../../theme/types'; import { isKeyboardFocusEvent } from '../../utils/isKeyboardFocusEvent'; -import { useFocusRing, webNoOutlineStyle } from '../../utils/useFocusRing'; +import { useFocusRing } from '../../utils/useFocusRing'; import { useSpaceKeyPress } from '../../utils/useSpaceKeyPress'; import Icon, { type IconSource } from '../Icon'; @@ -504,11 +504,8 @@ const Switch = ({ // A read-only switch stays enabled on web, where React Native Web's // Pressable would take the press and stop the click from reaching a // row that owns it. Native platforms disable the Pressable instead. - // A switch that has focus when it stops being interactive keeps DOM - // focus, and the focus ring no longer hides the browser's outline, - // so this hides it too. Platform.OS === 'web' && !isInteractive - ? [styles.nonInteractiveTouchable, webNoOutlineStyle] + ? styles.nonInteractiveTouchable : undefined, ]} {...focusTarget.dataSetProps} diff --git a/src/components/__tests__/Switch.test.tsx b/src/components/__tests__/Switch.test.tsx index 72eee73d72..00add3d464 100644 --- a/src/components/__tests__/Switch.test.tsx +++ b/src/components/__tests__/Switch.test.tsx @@ -7,7 +7,6 @@ import type { JsonElement, JsonNode } from 'test-renderer'; import { fireEvent, render, screen, userEvent } from '../../test-utils'; import { LightTheme } from '../../theme/schemes'; import { tokens } from '../../theme/tokens'; -import { webNoOutlineStyle } from '../../utils/useFocusRing'; import Switch from '../Switch/Switch'; const { colors } = LightTheme; @@ -432,13 +431,6 @@ describe('Switch operability', () => { expect(screen.getByRole('switch')).not.toHaveProp('aria-readonly'); }); - it('hides the browser focus outline on a read-only switch on web', async () => { - jest.replaceProperty(Platform, 'OS', 'web'); - await render(); - - expect(screen.getByRole('switch')).toHaveStyle(webNoOutlineStyle); - }); - it('exposes a missing-handler fallback as read-only on web', async () => { jest.replaceProperty(Platform, 'OS', 'web'); jest.spyOn(console, 'warn').mockImplementation(() => {}); diff --git a/src/utils/useFocusRing.ts b/src/utils/useFocusRing.ts index 9cfef8d88d..762f70e5cb 100644 --- a/src/utils/useFocusRing.ts +++ b/src/utils/useFocusRing.ts @@ -99,7 +99,7 @@ const EMPTY: FocusRingResult = { * `scope: 'within'` leaves that to the stylesheet below instead. */ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -export const webNoOutlineStyle = { outline: 'none' } as unknown as ViewStyle; +const webNoOutlineStyle = { outline: 'none' } as unknown as ViewStyle; /** * MD3 keyboard focus indicator, from one hook shared by every component built From bb877b166a4a75862c8c52202b0b7fd30ac54a20 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Mon, 23 Nov 2026 12:48:11 +0100 Subject: [PATCH 3/3] Update src/components/Switch/Switch.tsx Co-authored-by: Luke Walczak --- src/components/Switch/Switch.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Switch/Switch.tsx b/src/components/Switch/Switch.tsx index 7fca10847c..888506030d 100644 --- a/src/components/Switch/Switch.tsx +++ b/src/components/Switch/Switch.tsx @@ -476,7 +476,7 @@ const Switch = ({ disabled={Platform.OS === 'web' ? isDisabled : !isInteractive} tabIndex={isInteractive ? 0 : -1} aria-readonly={ - Platform.OS === 'web' ? isAnnouncedReadOnly || undefined : undefined + Platform.OS === 'web' && isAnnouncedReadOnly ? true : undefined } onFocus={(e) => { // Not the ring - it's real CSS on web now. This drives the