In this post, we'll walk through enhancing NativeScript Dialogs.alert by enabling custom color options. We'll reuse existing alert code from @nativescript/core, ensuring minimal additional setup.
Begin by creating a directory named custom-dialog
in your project's source folder. We'll use the recommended directory structure from NativeScript's Customizing View Elements documentation.
Inside the custom-dialog
directory, create the following files:
dialogs-common.ts
index.ios.ts
index.android.ts
index.d.ts
Copy content from the NativeScript Core repo into your project:
Update the import paths in both files to reference @nativescript/core
:
import {
Button,
Color,
CSSUtils,
Frame,
Label,
Page,
TextField,
Utils,
View,
} from '@nativescript/core';
Enhance DialogOptions
to accommodate your custom styles in dialogs-common.ts
:
export const DIALOG_STYLES = Symbol("dialogStyles");
export interface CustomDialogStyle {
style?: {
backgroundColor?: string;
buttonsBackgroundColor?: string;
buttonsTextColor?: string;
};
}
export interface DialogOptions extends CancelableOptions, CustomDialogStyle {
// existing properties
}
Add a helper function to create color instances from string values:
export function createButtonsColors(styles: { [DIALOG_STYLES]: CustomDialogStyle['style'] }): {
color?: Color;
backgroundColor?: Color;
} {
const colors: { color?: Color; backgroundColor?: Color } = {};
if (styles[DIALOG_STYLES]?.buttonsBackgroundColor) {
colors.backgroundColor = new Color(styles[DIALOG_STYLES].buttonsBackgroundColor);
}
if (styles[DIALOG_STYLES]?.buttonsTextColor) {
colors.color = new Color(styles[DIALOG_STYLES].buttonsTextColor);
}
return colors;
}
In your createDialog
function, ensure custom colors are attached properly:
const colorsAttached = Object.assign(alert, {
styles: options?.style,
});
Then, modify your showDialog
function to use the new createButtonsColors
:
const { color, backgroundColor } = createButtonsColors({
[DIALOG_STYLES]: builder[DIALOG_STYLES],
});
Create a helper function applyButtonColors
in index.ios.ts
to style iOS alert buttons using a method from this StackOverflow solution:
function applyButtonColors(
alertController: UIAlertController,
colors: { textColor?: Color; backgroundColor?: Color },
) {
if (colors.backgroundColor) {
const alertOkButton = alertController.view.subviews[0].subviews[0].subviews[0].subviews[1];
alertOkButton.backgroundColor = colors.backgroundColor.ios;
}
if (colors.textColor) {
alertController.view.tintColor = colors.textColor.ios;
}
}
In your alert
function, integrate the custom colors:
const { color, backgroundColor } = createButtonsColors({
[DIALOG_STYLES]: options.style,
});
applyButtonColors(alertController, { textColor: color, backgroundColor });
Now, you can easily apply custom colors to your dialogs.
First, import your customized Dialogs instead of the core package:
import { Dialogs } from '../custom-dialog/dialogs';
Then, apply your custom styles directly when invoking dialogs:
openDialog() {
Dialogs.alert({
title: "Alert",
message: "This is an alert dialog.",
style: {
buttonsBackgroundColor: "#FF7F00",
buttonsTextColor: "#FFFFFF",
},
okButtonText: "OK",
});
}
Customizing dialogs in NativeScript with custom colors is straightforward and powerful, allowing consistent theming across your application. Leveraging built-in platform APIs alongside NativeScript’s flexible architecture enhances both the appearance and user experience of your mobile apps.