|
| 1 | +--- |
| 2 | +sidebar_position: 7 |
| 3 | +--- |
| 4 | + |
| 5 | +# Dark Mode widget (Optional) |
| 6 | + |
| 7 | +This library supports using different colors for the widget when the system is in dark mode. |
| 8 | +It lets you provide separate widget UIs for light and dark system themes so your widget looks correct in both modes. |
| 9 | + |
| 10 | +## How it works |
| 11 | + |
| 12 | +When updating or rendering a widget you provide an object with `light` and/or `dark` React elements. The host system will choose the appropriate one based on the current theme. If only `light` is provided, it will be used for both modes as a fallback. |
| 13 | + |
| 14 | +## registerWidgetTaskHandler |
| 15 | + |
| 16 | +In a widget task handler call `props.renderWidget` and pass `light` and `dark` render trees. |
| 17 | + |
| 18 | +```tsx title="widget-task-handler.tsx" |
| 19 | +import React from 'react'; |
| 20 | +import { WidgetTaskHandlerProps } from 'react-native-android-widget'; |
| 21 | +import { Widget } from '../../widgets/Widget'; |
| 22 | + |
| 23 | +export async function widgetTaskHandler(props: WidgetTaskHandlerProps) { |
| 24 | + switch (props.widgetAction) { |
| 25 | + case 'WIDGET_ADDED': |
| 26 | + case 'WIDGET_UPDATED': |
| 27 | + // props.renderWidget(<Widget />); |
| 28 | + props.renderWidget({ |
| 29 | + light: <Widget theme="light" />, |
| 30 | + dark: <Widget theme="dark" />, |
| 31 | + }); |
| 32 | + break; |
| 33 | + // handle other actions |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Provide both variants when colors or images need swapping for readability. |
| 39 | + |
| 40 | +## requestWidgetUpdate |
| 41 | + |
| 42 | +You can also request updates from anywhere in your app. The `renderWidget` callback should return the same `light`/`dark` object. |
| 43 | + |
| 44 | +```tsx title="SomeComponent.tsx" |
| 45 | +import React from 'react'; |
| 46 | +import { requestWidgetUpdate } from 'react-native-android-widget'; |
| 47 | +import { Widget } from '../widgets/Widget'; |
| 48 | + |
| 49 | +function onSomeEvent() { |
| 50 | + requestWidgetUpdate({ |
| 51 | + widgetName: 'Counter', |
| 52 | + renderWidget: () => ({ |
| 53 | + light: <Widget theme="light" />, |
| 54 | + dark: <Widget theme="dark" />, |
| 55 | + }), |
| 56 | + }); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Limitations |
| 61 | + |
| 62 | +The light and dark variants must match in structure and interactivity — they should expose the same buttons, lists, and touch targets in the same positions. Only visual styling (colors, images, icons) should differ. |
| 63 | + |
| 64 | +- Structure: Keep layouts and element placement identical between variants so touch targets and spacing stay consistent. |
| 65 | +- Images and assets: If you swap images between themes, ensure they use the same intrinsic size and aspect ratio so the layout and touch regions remain unchanged. |
| 66 | +- Click handling: The native host uses the light variant for wiring click actions; the dark variant is used only for visual appearance. Make sure interactive elements in the dark variant mirror the light variant so interactions behave correctly. |
| 67 | +- Fallback: If you omit the `dark` render tree, the `light` variant will be shown in dark mode as a fallback. |
0 commit comments