From 7198331c22094feb54f33f42905b5f1eee8fbbb0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 20:08:51 +0000 Subject: [PATCH] feat(android): support themeVariant on the default date and time dialogs themeVariant was iOS-only. On Android the default (non-material) dialogs always followed the Activity's night mode, so apps with an in-app theme that differs from the system theme got a mismatched picker unless they changed the Activity's night mode at runtime. Forward themeVariant from the component and DateTimePickerAndroid.open through androidUtils.getOpenPicker to both native modules, and build each dialog on a context whose resources have the uiMode night bit forced and whose theme is created fresh from the Activity's theme resource (not copied from the Activity's live Theme). The Activity is not changed. Without themeVariant, behaviour is unchanged; material pickers are untouched. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019NZ2QVLavesbwzc7npEiDM --- README.md | 8 +- .../rndatetimepicker/Common.java | 96 +++++++++++++++++++ .../rndatetimepicker/RNConstants.java | 1 + .../RNDatePickerDialogFragment.java | 2 +- .../RNTimePickerDialogFragment.java | 2 +- src/DateTimePickerAndroid.android.js | 2 + src/androidUtils.js | 5 + src/datetimepicker.android.js | 2 + src/index.d.ts | 9 +- src/specs/NativeModuleDatePicker.js | 1 + src/specs/NativeModuleTimePicker.js | 1 + src/types.js | 9 +- test/index.test.js | 39 ++++++++ 13 files changed, 170 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 926ba472..0434583c 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ React Native date & time picker component for iOS, Android and Windows (please n - [`firstDayOfWeek` (`optional`, `Android and Windows only`)](#firstdayofweek-optional-android-and-windows-only) - [`textColor` (`optional`, `iOS only`)](#textColor-optional-ios-only) - [`accentColor` (`optional`, `iOS only`)](#accentColor-optional-ios-only) - - [`themeVariant` (`optional`, `iOS only`)](#themevariant-optional-ios-only) + - [`themeVariant` (`optional`)](#themevariant-optional) - [`locale` (`optional`, `iOS only`)](#locale-optional-ios-only) - [`is24Hour` (`optional`, `Windows and Android only`)](#is24hour-optional-windows-and-android-only) - [`positiveButton` (`optional`, `Android only`)](#positiveButton-optional-android-only) @@ -477,12 +477,14 @@ Allows changing of the textColor of the date picker. Has effect only when `displ Allows changing the accentColor (tintColor) of the date picker. Has no effect when `display` is `"spinner"`. -#### `themeVariant` (`optional`, `iOS only`) +#### `themeVariant` (`optional`) Allows overriding system theme variant (dark or light mode) used by the date picker. + +On Android this applies to the default (non-material) date and time dialogs, per dialog, without changing the Activity's night mode. This helps apps with an in-app theme switch that differs from the system theme. However, we recommend that you instead control the theme of the whole application using [react-native-theme-control](https://github.com/vonovak/react-native-theme-control). -:warning: Has effect only on iOS 14 and later. On iOS 13 & less, use `textColor` to make the picker dark-theme compatible +:warning: On iOS, has effect only on iOS 14 and later. On iOS 13 & less, use `textColor` to make the picker dark-theme compatible List of possible values: diff --git a/android/src/main/java/com/reactcommunity/rndatetimepicker/Common.java b/android/src/main/java/com/reactcommunity/rndatetimepicker/Common.java index 104a1814..0ccf1906 100644 --- a/android/src/main/java/com/reactcommunity/rndatetimepicker/Common.java +++ b/android/src/main/java/com/reactcommunity/rndatetimepicker/Common.java @@ -4,7 +4,12 @@ import android.app.DatePickerDialog; import android.content.Context; import android.content.DialogInterface; +import android.app.Activity; +import android.content.pm.PackageManager; +import android.content.res.Configuration; import android.content.res.Resources; +import android.content.ContextWrapper; +import android.view.LayoutInflater; import android.graphics.Color; import android.os.Bundle; import android.util.TypedValue; @@ -15,6 +20,7 @@ import androidx.annotation.ColorInt; import androidx.annotation.ColorRes; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.FragmentActivity; @@ -244,6 +250,90 @@ public static Bundle createFragmentArguments(ReadableMap options) { return args; } + /** + * Returns a context for the picker dialog whose light/dark look matches the + * `themeVariant` arg ('light' | 'dark'), independent of the Activity's + * current night mode. The theme is built fresh from the Activity's theme + * resource on resources whose uiMode night bit is forced; it is not copied + * from the Activity's live Theme, which may hold attributes resolved under + * the other night mode. Window/system services still come from the + * Activity. Without a themeVariant the Activity context is returned as is. + */ + @Nullable + public static Context applyThemeVariant(@Nullable Context activityContext, @Nullable Bundle args) { + if (activityContext == null || args == null) { + return activityContext; + } + final String variant = args.getString(RNConstants.ARG_THEME_VARIANT); + final int nightFlag; + if ("dark".equals(variant)) { + nightFlag = Configuration.UI_MODE_NIGHT_YES; + } else if ("light".equals(variant)) { + nightFlag = Configuration.UI_MODE_NIGHT_NO; + } else { + return activityContext; + } + + int themeResId = 0; + if (activityContext instanceof Activity) { + Activity activity = (Activity) activityContext; + try { + themeResId = activity.getPackageManager() + .getActivityInfo(activity.getComponentName(), 0) + .getThemeResource(); + } catch (PackageManager.NameNotFoundException ignored) { + // fall through to the application theme + } + } + if (themeResId == 0) { + themeResId = activityContext.getApplicationInfo().theme; + } + + final Configuration current = activityContext.getResources().getConfiguration(); + final Configuration override = new Configuration(current); + override.uiMode = (current.uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | nightFlag; + final Resources resources = activityContext.createConfigurationContext(override).getResources(); + final Resources.Theme theme = resources.newTheme(); + theme.applyStyle(themeResId, true); + + return new ThemeVariantContext(activityContext, resources, theme); + } + + /** Activity context with the picker's own resources and theme. */ + private static final class ThemeVariantContext extends ContextWrapper { + private final Resources mResources; + private final Resources.Theme mTheme; + + ThemeVariantContext(Context base, Resources resources, Resources.Theme theme) { + super(base); + mResources = resources; + mTheme = theme; + } + + @Override + public Resources getResources() { + return mResources; + } + + @Override + public Resources.Theme getTheme() { + return mTheme; + } + + @Override + public void setTheme(int resid) { + mTheme.applyStyle(resid, true); + } + + @Override + public Object getSystemService(String name) { + if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) { + return LayoutInflater.from(getBaseContext()).cloneInContext(this); + } + return super.getSystemService(name); + } + } + public static Bundle createDatePickerArguments(ReadableMap options) { final Bundle args = Common.createFragmentArguments(options); @@ -276,6 +366,9 @@ public static Bundle createDatePickerArguments(ReadableMap options) { if (options.hasKey(RNConstants.ARG_START_ON_YEAR_SELECTION) && !options.isNull(RNConstants.ARG_START_ON_YEAR_SELECTION)) { args.putBoolean(RNConstants.ARG_START_ON_YEAR_SELECTION, options.getBoolean(RNConstants.ARG_START_ON_YEAR_SELECTION)); } + if (options.hasKey(RNConstants.ARG_THEME_VARIANT) && !options.isNull(RNConstants.ARG_THEME_VARIANT)) { + args.putString(RNConstants.ARG_THEME_VARIANT, options.getString(RNConstants.ARG_THEME_VARIANT)); + } return args; } @@ -297,6 +390,9 @@ public static Bundle createTimePickerArguments(ReadableMap options) { if (options.hasKey(RNConstants.ARG_TZOFFSET_MINS) && !options.isNull(RNConstants.ARG_TZOFFSET_MINS)) { args.putLong(RNConstants.ARG_TZOFFSET_MINS, (long) options.getDouble(RNConstants.ARG_TZOFFSET_MINS)); } + if (options.hasKey(RNConstants.ARG_THEME_VARIANT) && !options.isNull(RNConstants.ARG_THEME_VARIANT)) { + args.putString(RNConstants.ARG_THEME_VARIANT, options.getString(RNConstants.ARG_THEME_VARIANT)); + } return args; } diff --git a/android/src/main/java/com/reactcommunity/rndatetimepicker/RNConstants.java b/android/src/main/java/com/reactcommunity/rndatetimepicker/RNConstants.java index b5d09330..cfd6848e 100644 --- a/android/src/main/java/com/reactcommunity/rndatetimepicker/RNConstants.java +++ b/android/src/main/java/com/reactcommunity/rndatetimepicker/RNConstants.java @@ -23,6 +23,7 @@ public final class RNConstants { public static final String ACTION_NEUTRAL_BUTTON = "neutralButtonAction"; public static final String FIRST_DAY_OF_WEEK = "firstDayOfWeek"; public static final String ARG_START_ON_YEAR_SELECTION = "startOnYearSelection"; + public static final String ARG_THEME_VARIANT = "themeVariant"; /** * Minimum date supported by {@link TimePickerDialog}, 01 Jan 1900 diff --git a/android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogFragment.java b/android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogFragment.java index bbf0b319..64819a0d 100644 --- a/android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogFragment.java +++ b/android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogFragment.java @@ -95,7 +95,7 @@ DatePickerDialog getDialog( } private DatePickerDialog createDialog(Bundle args) { - Context activityContext = getActivity(); + Context activityContext = Common.applyThemeVariant(getActivity(), args); DatePickerDialog dialog = getDialog(args, activityContext, mOnDateSetListener); if (args != null) { diff --git a/android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogFragment.java b/android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogFragment.java index f3c8830e..ce3438a4 100644 --- a/android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogFragment.java +++ b/android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogFragment.java @@ -94,7 +94,7 @@ static TimePickerDialog getDialog( } private TimePickerDialog createDialog(Bundle args) { - Context activityContext = getActivity(); + Context activityContext = Common.applyThemeVariant(getActivity(), args); TimePickerDialog dialog = getDialog(args, activityContext, mOnTimeSetListener); if (args != null) { diff --git a/src/DateTimePickerAndroid.android.js b/src/DateTimePickerAndroid.android.js index 6f5bb3d2..f64ab3ec 100644 --- a/src/DateTimePickerAndroid.android.js +++ b/src/DateTimePickerAndroid.android.js @@ -53,6 +53,7 @@ function open(props: AndroidNativeProps) { design, fullscreen, startOnYearSelection, + themeVariant, } = props; validateAndroidProps(props); warnIfOnChangeIsUsed(onChange); @@ -98,6 +99,7 @@ function open(props: AndroidNativeProps) { initialInputMode, fullscreen, startOnYearSelection, + themeVariant, }); switch (action) { diff --git a/src/androidUtils.js b/src/androidUtils.js index f2a4e0e7..d101c764 100644 --- a/src/androidUtils.js +++ b/src/androidUtils.js @@ -39,6 +39,7 @@ type OpenParams = { design: AndroidNativeProps['design'], fullscreen: AndroidNativeProps['fullscreen'], startOnYearSelection: AndroidNativeProps['startOnYearSelection'], + themeVariant: AndroidNativeProps['themeVariant'], }; export type PresentPickerCallback = @@ -62,6 +63,7 @@ function getOpenPicker( dialogButtons, title, initialInputMode, + themeVariant, }: OpenParams) => // $FlowFixMe - `AbstractComponent` [1] is not an instance type. pickers[mode].open({ @@ -74,6 +76,7 @@ function getOpenPicker( dialogButtons, title, initialInputMode, + themeVariant, }); default: return ({ @@ -90,6 +93,7 @@ function getOpenPicker( initialInputMode, fullscreen, startOnYearSelection, + themeVariant, }: OpenParams) => // $FlowFixMe - `AbstractComponent` [1] is not an instance type. pickers[ANDROID_MODE.date].open({ @@ -106,6 +110,7 @@ function getOpenPicker( initialInputMode, fullscreen, startOnYearSelection, + themeVariant, }); } } diff --git a/src/datetimepicker.android.js b/src/datetimepicker.android.js index 7aea9d86..93031dcb 100644 --- a/src/datetimepicker.android.js +++ b/src/datetimepicker.android.js @@ -38,6 +38,7 @@ export default function RNDateTimePickerAndroid( design, fullscreen, startOnYearSelection, + themeVariant, } = props; const valueTimestamp = value.getTime(); @@ -74,6 +75,7 @@ export default function RNDateTimePickerAndroid( design, fullscreen, startOnYearSelection, + themeVariant, }; DateTimePickerAndroid.open(params); }, diff --git a/src/index.d.ts b/src/index.d.ts index 24d0eb74..387edf1c 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -139,7 +139,8 @@ export type IOSNativeProps = Readonly< accentColor?: string; /** - * Override theme variant used by iOS native picker + * Override theme variant used by the native picker (iOS, and Android's + * default design dialogs) */ themeVariant?: 'dark' | 'light'; @@ -166,6 +167,12 @@ export type AndroidNativeProps = Readonly< */ mode?: AndroidMode; + /** + * Override the light/dark theme of the default (non-material) date and + * time dialogs, independent of the Activity's night mode. + */ + themeVariant?: 'dark' | 'light'; + /** * The display options. * diff --git a/src/specs/NativeModuleDatePicker.js b/src/specs/NativeModuleDatePicker.js index 4ad5f85d..71f72868 100644 --- a/src/specs/NativeModuleDatePicker.js +++ b/src/specs/NativeModuleDatePicker.js @@ -12,6 +12,7 @@ export type DatePickerOpenParams = $ReadOnly<{ timeZoneName?: number, timeZoneOffsetInMinutes?: number, startOnYearSelection?: boolean, + themeVariant?: string, }>; type DateSetAction = 'dateSetAction' | 'dismissedAction'; diff --git a/src/specs/NativeModuleTimePicker.js b/src/specs/NativeModuleTimePicker.js index eaed3971..a16840e1 100644 --- a/src/specs/NativeModuleTimePicker.js +++ b/src/specs/NativeModuleTimePicker.js @@ -9,6 +9,7 @@ export type TimePickerOpenParams = $ReadOnly<{ is24Hour?: boolean, minuteInterval?: number, timeZoneOffsetInMinutes?: number, + themeVariant?: string, }>; type TimeSetAction = 'timeSetAction' | 'dismissedAction'; diff --git a/src/types.js b/src/types.js index 98af06c3..49ac0fab 100644 --- a/src/types.js +++ b/src/types.js @@ -168,7 +168,8 @@ export type IOSNativeProps = $ReadOnly<{| accentColor?: ?ColorValue, /** - * Override theme variant used by iOS native picker + * Override theme variant used by the native picker (iOS, and Android's + * default design dialogs) */ themeVariant?: 'dark' | 'light', @@ -195,6 +196,12 @@ export type AndroidNativeProps = $ReadOnly<{| */ mode: AndroidMode, + /** + * Override the light/dark theme of the default (non-material) date and + * time dialogs, independent of the Activity's night mode. + */ + themeVariant?: 'dark' | 'light', + /** * The display options. * diff --git a/test/index.test.js b/test/index.test.js index 2bda1512..c77f48d9 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -105,6 +105,45 @@ describe('DateTimePicker', () => { jest.restoreAllMocks(); }); + + it('forwards themeVariant to the default date and time pickers', async () => { + const result = {action: 'dismissedAction', timestamp: 0, utcOffset: 0}; + const dateOpen = jest.fn(() => Promise.resolve(result)); + const timeOpen = jest.fn(() => Promise.resolve(result)); + let IsolatedDateTimePickerAndroid; + jest.isolateModules(() => { + jest.doMock('../src/picker', () => ({ + __esModule: true, + default: {date: {open: dateOpen}, time: {open: timeOpen}}, + })); + ({ + DateTimePickerAndroid: IsolatedDateTimePickerAndroid, + } = require('../src/DateTimePickerAndroid.android')); + }); + + const onValueChange = jest.fn(); + IsolatedDateTimePickerAndroid.open({ + value: new Date(DATE), + mode: 'date', + themeVariant: 'light', + onValueChange, + }); + IsolatedDateTimePickerAndroid.open({ + value: new Date(DATE), + mode: 'time', + themeVariant: 'dark', + onValueChange, + }); + await waitFor(() => expect(timeOpen).toHaveBeenCalled()); + + expect(dateOpen).toHaveBeenCalledWith( + expect.objectContaining({themeVariant: 'light'}), + ); + expect(timeOpen).toHaveBeenCalledWith( + expect.objectContaining({themeVariant: 'dark'}), + ); + jest.dontMock('../src/picker'); + }); }); describe('RNDateTimePickerIOS', () => {