Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toast): Add durationMilliseconds option for iOS and Web #1971

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions toast/README.md
Expand Up @@ -59,10 +59,11 @@ Shows a Toast on the screen

#### ShowOptions

| Prop | Type | Description | Default | Since |
| -------------- | ------------------------------------------ | ---------------------------------------------------------------------------------- | --------------------- | ----- |
| **`text`** | <code>string</code> | Text to display on the Toast | | 1.0.0 |
| **`duration`** | <code>'short' \| 'long'</code> | Duration of the Toast, either 'short' (2000ms) or 'long' (3500ms) | <code>'short'</code> | 1.0.0 |
| **`position`** | <code>'top' \| 'center' \| 'bottom'</code> | Position of the Toast. On Android 12 and newer all toasts are shown at the bottom. | <code>'bottom'</code> | 1.0.0 |
| Prop | Type | Description | Default | Since |
| -------------------------- | ------------------------------------------ | ------------------------------------------------------------------------------------ | --------------------- | ----- |
| **`text`** | <code>string</code> | Text to display on the Toast | | 1.0.0 |
| **`duration`** | <code>'short' \| 'long'</code> | Duration of the Toast, either 'short' (2000ms) or 'long' (3500ms) | <code>'short'</code> | 1.0.0 |
| **`durationMilliseconds`** | <code>number</code> | Duration of the Toast in milliseconds. This option is only supported on iOS and Web. | | 6.0.0 |
| **`position`** | <code>'top' \| 'center' \| 'bottom'</code> | Position of the Toast. On Android 12 and newer all toasts are shown at the bottom. | <code>'bottom'</code> | 1.0.0 |

</docgen-api>
7 changes: 5 additions & 2 deletions toast/ios/Sources/ToastPlugin/ToastPlugin.swift
Expand Up @@ -14,8 +14,11 @@ public class ToastPlugin: CAPPlugin, CAPBridgedPlugin {
call.reject("text must be provided and must be a string.")
return
}
let durationType = call.getString("duration", "short")
let duration = durationType == "long" ? 3500 : 2000
var duration = call.getInt("durationMilliseconds", 0)
if(duration <= 0) {
let durationType = call.getString("duration", "short")
duration = durationType == "long" ? 3500 : 2000
}
let position = call.getString("position", "bottom")

guard let viewController = bridge?.viewController else {
Expand Down
9 changes: 9 additions & 0 deletions toast/src/definitions.ts
Expand Up @@ -23,6 +23,15 @@ export interface ShowOptions {
*/
duration?: 'short' | 'long';

/**
* Duration of the Toast in milliseconds.
*
* This option is only supported on iOS and Web.
*
* @since 6.0.0
*/
durationMilliseconds?: number;

/**
* Position of the Toast.
*
Expand Down
4 changes: 3 additions & 1 deletion toast/src/web.ts
Expand Up @@ -6,7 +6,9 @@ export class ToastWeb extends WebPlugin implements ToastPlugin {
async show(options: ShowOptions): Promise<void> {
if (typeof document !== 'undefined') {
let duration = 2000;
if (options.duration) {
if (options.durationMilliseconds) {
duration = options.durationMilliseconds;
} else if (options.duration) {
duration = options.duration === 'long' ? 3500 : 2000;
}
const toast = document.createElement('pwa-toast') as any;
Expand Down