Skip to content

Commit 35d834a

Browse files
authored
Merge pull request #6 from blazejkustra/fix/is-preferred-on-web
refactor: allow for customizing preferred option on web
2 parents 920db30 + 6533fb4 commit 35d834a

6 files changed

Lines changed: 422 additions & 326 deletions

File tree

.cursor/mcp.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function App() {
1111
const showAlert = () => {
1212
Alert.alert('Alert Title', 'This is a simple alert message', [
1313
{ text: 'Cancel', style: 'cancel' },
14-
{ text: 'OK', style: 'default' },
14+
{ text: 'OK', style: 'default', isPreferred: true },
1515
]);
1616
};
1717

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@blazejkustra/react-native-alert",
3-
"version": "1.0.0",
3+
"version": "1.0.1-rc.0",
44
"description": "Universal alerts for react native",
55
"main": "./lib/module/index.js",
66
"types": "./lib/typescript/src/index.d.ts",
@@ -79,8 +79,8 @@
7979
"prettier": "^3.6.2",
8080
"react": "19.1.0",
8181
"react-native": "0.81.1",
82-
"react-native-builder-bob": "^0.40.13",
83-
"release-it": "^19.0.4",
82+
"react-native-builder-bob": "0.40.16",
83+
"release-it": "19.0.6",
8484
"turbo": "^2.5.6",
8585
"typescript": "^5.9.2"
8686
},

src/Alert/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Alert {
6060
buttons: buttons.map((b) => ({
6161
text: b?.text ?? '',
6262
style: b?.style ?? 'default',
63+
isPreferred: b?.isPreferred ?? false,
6364
})),
6465
type: type ?? 'plain-text',
6566
defaultValue: defaultValue ?? '',

src/Alert/utils/web.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,36 @@ function createCustomButtons(
9696
buttons: AlertButton[] | null,
9797
inputField?: string
9898
): void {
99-
buttons?.forEach(({ text, style, onPress }, index) => {
99+
buttons?.forEach(({ text, style, onPress, isPreferred }, index) => {
100100
const button = document.createElement('button');
101101
button.innerText = text || 'OK';
102102

103-
// Apply appropriate button styling
103+
// Determine button variant based on priority:
104+
// 1. Destructive style (highest priority)
105+
// 2. Preferred button
106+
// 3. Fallback: first button in 2-button dialog (if second isn't preferred)
107+
// 4. Default: secondary
108+
const baseClass = 'rn-alert__button';
109+
let variantClass: string;
110+
104111
if (style === 'destructive') {
105-
button.className = 'rn-alert__button rn-alert__button--destructive';
106-
} else if (index === 0 && buttons.length === 2) {
107-
// First button in two-button dialog gets primary styling
108-
button.className = 'rn-alert__button rn-alert__button--primary';
112+
variantClass = 'rn-alert__button--destructive';
113+
} else if (isPreferred === true) {
114+
variantClass = 'rn-alert__button--primary';
109115
} else {
110-
button.className = 'rn-alert__button rn-alert__button--secondary';
116+
const isFirstButtonInTwoButtonDialog =
117+
index === 0 && buttons.length === 2;
118+
const secondButtonIsNotPreferred = buttons[1]?.isPreferred !== true;
119+
120+
if (isFirstButtonInTwoButtonDialog && secondButtonIsNotPreferred) {
121+
variantClass = 'rn-alert__button--primary';
122+
} else {
123+
variantClass = 'rn-alert__button--secondary';
124+
}
111125
}
112126

127+
button.className = `${baseClass} ${variantClass}`;
128+
113129
button.addEventListener('click', () => {
114130
dialog.close();
115131
if (onPress) {

0 commit comments

Comments
 (0)