Skip to content

Commit

Permalink
toast android
Browse files Browse the repository at this point in the history
  • Loading branch information
nandorojo committed Dec 4, 2022
1 parent d09f6a6 commit bed6329
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 61 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "burnt",
"version": "0.7.0",
"version": "0.7.1",
"description": "Cross-platform toasts, powered by native elements.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
20 changes: 20 additions & 0 deletions src/BurntModule.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AlertOptions, ToastOptions } from "./types";
import { ToastAndroid } from "react-native";

export default {
alertAsync(_: AlertOptions) {
console.warn(
"[burnt] Burnt.alertAsync() is not implemented on this Android. Please try toastAsync()."
);
},
toastAsync({ title, duration = 5 }: ToastOptions) {
ToastAndroid.showWithGravityAndOffset(
title,
duration * 1000,
ToastAndroid.BOTTOM,
25,
50
);
},
dismissAllAlertsAsync() {},
};
2 changes: 1 addition & 1 deletion src/BurntModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ export default {
);
}
},
};
} as any;
29 changes: 29 additions & 0 deletions src/BurntModule.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const isDev =
typeof __DEV__ !== "undefined"
? __DEV__
: // @ts-expect-error
typeof process !== "undefined" && process.env.NODE_ENV !== "production";

export default {
toast() {
if (isDev) {
console.log(
"[burnt] Burnt.alert() is not implemented on this platform. Just making sure you know."
);
}
},
alert() {
if (isDev) {
console.log(
"[burnt] Burnt.alert() is not implemented on this platform. Just making sure you know."
);
}
},
dismissAllAlerts() {
if (isDev) {
console.log(
"[burnt] Burnt.dismissAllAlerts() is not implemented on this platform. Just making sure you know."
);
}
},
};
61 changes: 2 additions & 59 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,7 @@
// Import the native module. On web, it will be resolved to Burnt.web.ts
// and on native platforms to Burnt.ts
import BurntModule from "./BurntModule.ios";

type AlertOptions = {
title: string;
message: string;
/**
* Defaults to `true`.
*/
shouldDismissByTap?: boolean;
} & (
| {
preset: "heart" | "done" | "error";

/**
* Duration in seconds.
*/
duration?: number;
}
| {
preset: "spinner";
/**
* Max timeout of the spinner in seconds. Required for this preset to avoid an infinite spinner.
*
* It's highly, highly recommended that you manually dismiss the alert using `Burnt.dismissAllAlerts()`.
*
* If you don't, then you risk having an infinite loading spinner for users.
*
* ```ts
* Burnt.alert({
* preset: "spinner",
* title: 'Loading...',
* duration: 10, // Maximum of 10 seconds
* })
*
* try {
* await createUser()
* } finally {
* Burnt.dismissAllAlerts()
* }
* ```
*/
duration: number;
}
);
import BurntModule from "./BurntModule";
import { AlertOptions, ToastOptions } from "./types";

export function alert({
preset = "done",
Expand All @@ -53,21 +11,6 @@ export function alert({
return BurntModule.alertAsync({ duration, preset, ...options });
}

type ToastOptions = {
title: string;
message: string;
preset: "done" | "error"; // TODO custom option
/**
* Duration in seconds.
*/
duration?: number;
haptic?: "success" | "warning" | "error" | "none";
/**
* Defaults to `true`.
*/
shouldDismissByDrag?: boolean;
};

export function toast({ duration = 5, ...options }: ToastOptions) {
return BurntModule.toastAsync({ duration, ...options });
}
Expand Down
57 changes: 57 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export type AlertOptions = {
title: string;
message: string;
/**
* Defaults to `true`.
*/
shouldDismissByTap?: boolean;
} & (
| {
preset: "heart" | "done" | "error";

/**
* Duration in seconds.
*/
duration?: number;
}
| {
preset: "spinner";
/**
* Max timeout of the spinner in seconds. Required for this preset to avoid an infinite spinner.
*
* It's highly, highly recommended that you manually dismiss the alert using `Burnt.dismissAllAlerts()`.
*
* If you don't, then you risk having an infinite loading spinner for users.
*
* ```ts
* Burnt.alert({
* preset: "spinner",
* title: 'Loading...',
* duration: 10, // Maximum of 10 seconds
* })
*
* try {
* await createUser()
* } finally {
* Burnt.dismissAllAlerts()
* }
* ```
*/
duration: number;
}
);

export type ToastOptions = {
title: string;
message: string;
preset: "done" | "error"; // TODO custom option
/**
* Duration in seconds.
*/
duration?: number;
haptic?: "success" | "warning" | "error" | "none";
/**
* Defaults to `true`.
*/
shouldDismissByDrag?: boolean;
};

0 comments on commit bed6329

Please sign in to comment.