PHP code example of victorycodedev / toastkit

1. Go to this page and download the library: Download victorycodedev/toastkit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

victorycodedev / toastkit example snippets


use Victorycodedev\ToastKit\Facades\Toast;

Toast::success('Changes saved')->show();

Toast::make('Profile updated')
    ->title('Success')
    ->success()
    ->icon('check')
    ->position('top')
    ->duration(3000)
    ->animation('spring')
    ->swipeToDismiss()
    ->show();

Toast::success('Saved')->show();
Toast::error('Something went wrong')->show();
Toast::warning('Storage almost full')->show();
Toast::info('Downloading...')->show();
Toast::neutral('Copied')->show();

Toast::make('Download complete')
    ->title('invoice.pdf')
    ->success()
    ->icon('check')
    ->position('top')
    ->background('#111827')
    ->foreground('#FFFFFF')
    ->iconColor('#22C55E')
    ->cornerRadius(18)
    ->padding(16)
    ->shadow()
    ->animation('spring')
    ->swipeToDismiss()
    ->duration(3000)
    ->show();

Toast::make('Download complete')->icon('check')->show();
Toast::make('New message')->icon('email')->show();
Toast::make('Storage full')->icon('warning')->show();

Toast::make('Saved')
    ->icon(
        'check',
        ios: 'checkmark.circle.fill', // SF Symbol
        android: 'done',              // Material Icon ligature
    )
    ->show();

Toast::make('Rated')->icon(ios: 'star.fill')->show();

Toast::make('Your download is complete')
    ->title('Download complete')
    ->text(
        font: 'Inter',
        size: 'sm',
        weight: 'normal',
    )
    ->titleText(
        size: 'lg',
        weight: 'bold',
    )
    ->success()
    ->show();

use Victorycodedev\ToastKit\Enums\ToastTextAlign;
use Victorycodedev\ToastKit\Enums\ToastTextSize;
use Victorycodedev\ToastKit\Enums\ToastTextWeight;

Toast::make('Saved')
    ->text(
        size: ToastTextSize::Small,
        weight: ToastTextWeight::Medium,
        align: ToastTextAlign::Center,
    )
    ->show();

Toast::update($id)
    ->message('Download complete')
    ->text(weight: 'semibold')
    ->success()
    ->show();

Toast::make('Saved')->text(font: 'accent')->show();

$id = Toast::info('Uploading file...')
    ->persistent()
    ->show();

// Later...
Toast::update($id)
    ->message('Upload complete')
    ->success()
    ->icon('check')
    ->duration(2000)
    ->show();

$id = Toast::info('Syncing contacts...')
    ->unique('contacts-sync')
    ->persistent()
    ->show();

$originalId = Toast::info('Syncing...')->unique('contacts-sync')->show();
$sameId = Toast::success('Duplicate request')->unique('contacts-sync')->show();

// $sameId === $originalId; the original toast is unchanged.

Toast::updateUnique('contacts-sync')
    ->success()
    ->message('Contacts synced')
    ->duration(2000)
    ->show();

Toast::dismissUnique('contacts-sync');

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Victorycodedev\ToastKit\PendingToast;
use Victorycodedev\ToastKit\Facades\Toast;

final class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Toast::definePreset('contacts-sync', fn (PendingToast $toast) => $toast
            ->info()
            ->unique('contacts-sync')
            ->persistent()
            ->loading());
    }
}

Toast::preset('contacts-sync')
    ->message('Syncing contacts...')
    ->show();

Toast::update($id)
    ->message('Uploading 50%...')
    ->show();

// Native appearance on iOS and Android.
Toast::success('Saved')
    ->native()
    ->show();

// Native iOS, custom ToastKit Android.
Toast::success('Saved')
    ->native(ios: true, android: false)
    ->show();

// Custom ToastKit iOS, native Android.
Toast::success('Saved')
    ->native(ios: false, android: true)
    ->show();

Toast::make('Saved')
    ->background('#000000')
    ->native(ios: true, android: false)
    ->show();

Toast::update($id)
    ->native(ios: false, android: true)
    ->show();

Toast::success('Saved')->opacity()->show();      // 0.8
Toast::success('Saved')->opacity(0.5)->show();   // 50%
Toast::success('Saved')->opacity(1.0)->show();   // Fully opaque

Toast::definePreset('subtle', fn ($toast) => $toast->opacity()->animation('fade'));

Toast::update($id)->opacity(0.9)->show();

$id = Toast::info('Uploading...')->persistent()->progress(0)->show();

Toast::update($id)->progress(42.5)->show();
Toast::update($id)->progress(100)->message('Upload complete')->show();

$id = Toast::info('Connecting...')->persistent()->loading()->show();
Toast::update($id)->loading(false)->message('Connected')->duration(1500)->show();

Toast::success('Published')
    ->animation('bounce')
    ->direction('right')
    ->show();

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Victorycodedev\ToastKit\PendingToast;
use Victorycodedev\ToastKit\Facades\Toast;

final class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Toast::definePreset('syncing', fn (PendingToast $toast) => $toast
            ->message('Syncing...')
            ->info()
            ->persistent()
            ->loading());
    }
}

Toast::preset('syncing')->message('Syncing contacts...')->show();

namespace App\Toasts;

use Victorycodedev\ToastKit\Facades\Toast;

final class ToastPresets
{
    public static function register(): void
    {
        Toast::definePreset('payment-success', fn ($toast) => $toast
            ->success()->icon('check_circle')->animation('reveal')
            ->position('top')->duration(3000));

        Toast::definePreset('payment-failed', fn ($toast) => $toast
            ->error()->icon('error')->animation('snap')->direction('left')
            ->position('top')->duration(5000));

        Toast::definePreset('uploading', fn ($toast) => $toast
            ->info()->loading()->persistent());
    }
}

namespace App\Providers;

use App\Toasts\ToastPresets;
use Illuminate\Support\ServiceProvider;

final class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        ToastPresets::register();
    }
}

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Victorycodedev\ToastKit\PendingToast;
use Victorycodedev\ToastKit\Facades\Toast;

final class ToastServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Toast::definePreset('payment-success', fn (PendingToast $toast) => $toast
            ->success()
            ->icon('check_circle')
            ->animation('reveal')
            ->position('top')
            ->duration(3000));

        Toast::definePreset('payment-failed', fn (PendingToast $toast) => $toast
            ->error()
            ->icon('error')
            ->animation('snap')
            ->direction('left')
            ->position('top')
            ->duration(5000));

        Toast::definePreset('uploading', fn (PendingToast $toast) => $toast
            ->info()
            ->loading()
            ->persistent());
    }
}

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\ToastServiceProvider::class,
];

use Victorycodedev\ToastKit\Exceptions\ToastKitException;

try {
    Toast::preset('payment-success')->message('Paid')->show();
} catch (ToastKitException $exception) {
    report($exception);
}

use Victorycodedev\ToastKit\Exceptions\PresetNotFoundException;

try {
    Toast::preset('payment-success');
} catch (PresetNotFoundException $exception) {
    // The requested preset was not registered.
}

// Before
catch (\InvalidArgumentException $exception) {
    // ...
}

// Now
catch (\Victorycodedev\ToastKit\Exceptions\ToastKitException $exception) {
    // ...
}

$id = Toast::info('Syncing...')
    ->persistent()
    ->show();

Toast::dismiss($id);

Toast::dismissAll();

use Native\Mobile\Attributes\On;
use Victorycodedev\ToastKit\Events\ToastActionPressed;

Toast::error('Connection lost')
    ->action(
        label: 'Retry',
        id: 'retry',
    )
    ->show();

#[On(ToastActionPressed::class)]
public function handleToastAction(
    string $toastId,
    string $actionId,
): void {
    if ($actionId === 'retry') {
        $this->retry();
    }
}

use Native\Mobile\Attributes\On;
use Victorycodedev\ToastKit\Events\ToastShown;
use Victorycodedev\ToastKit\Events\ToastDismissed;

#[On(ToastShown::class)]
public function handleToastShown(string $toastId): void
{
    // The toast is now visible.
}

#[On(ToastDismissed::class)]
public function handleToastDismissed(string $toastId, string $reason): void
{
    // $reason is one of: timeout, swipe, programmatic, action.
}

Toast::info('First')->queue()->show();
Toast::info('Second')->queue()->show();

Toast::success('Saved')
    ->stack()
    ->maxVisible(3)
    ->show();



namespace App\Screens;

use Native\Mobile\Attributes\On;
use Native\Mobile\Edge\NativeComponent;
use Victorycodedev\ToastKit\Events\ToastActionPressed;
use Victorycodedev\ToastKit\Facades\Toast;

class ToastDemoScreen extends NativeComponent
{
    public ?string $toastId = null;

    public function startUpload(): void
    {
        $this->toastId = Toast::info('Uploading...')
            ->persistent()
            ->show();
    }

    public function updateUpload(): void
    {
        if (! $this->toastId) {
            return;
        }

        Toast::update($this->toastId)
            ->message('Uploading 50%...')
            ->show();
    }

    public function completeUpload(): void
    {
        if (! $this->toastId) {
            return;
        }

        Toast::update($this->toastId)
            ->message('Upload complete')
            ->success()
            ->icon('check')
            ->duration(2000)
            ->show();
    }

    public function dismissToast(): void
    {
        if ($this->toastId) {
            Toast::dismiss($this->toastId);
        }
    }
}

$toastId = Toast::info('Downloading...')
    ->persistent()
    ->show();

$request = Fetch::timeout(120);

$this->requestId = $request->id();

$request->download(/* ... */);

use Native\Mobile\Attributes\On;
use Victorycodedev\ToastKit\Facades\Toast;

#[On(FetchDownloadProgress::class)]
public function onDownloadProgress(
    string $requestId,
    int $bytesReceived,
    ?int $bytesTotal,
    ?float $progress,
): void {
    if ($progress === null || ! $this->toastId) {
        return;
    }

    Toast::update($this->toastId)
        ->message(
            'Downloading '.(int) round($progress * 100).'%...'
        )
        ->show();
}

#[On(FetchDownloadCompleted::class)]
public function onDownloadCompleted(): void
{
    Toast::update($this->toastId)
        ->message('Download complete')
        ->success()
        ->icon('check')
        ->duration(2000)
        ->show();
}

#[On(FetchRequestFailed::class)]
public function onDownloadFailed(): void
{
    Toast::update($this->toastId)
        ->message('Download failed')
        ->error()
        ->duration(3000)
        ->show();
}

use Native\Mobile\Attributes\On;
use Victorycodedev\ToastKit\Events\ToastDismissed;

#[On(ToastDismissed::class)]
public function handleToastDismissed(string $toastId, string $reason): void
{
    // $reason is one of: timeout, swipe, programmatic, action.
}

use Native\Mobile\Testing\Native;

Native::test(ProfileScreen::class)
    ->call('save')
    ->assertToastShownWithMessage('Profile updated');

use Native\Mobile\Testing\Native;
use Victorycodedev\ToastKit\Events\ToastActionPressed;

Native::test(ProfileScreen::class)
    ->emitNative(ToastActionPressed::class, [
        'toastId' => 'one',
        'actionId' => 'retry',
    ])
    ->assertSet('retried', true);
bash
php artisan vendor:publish --tag=nativephp-plugins-provider
php artisan native:plugin:register victorycodedev/toastkit
bash
php artisan native:plugin:list
bash
php artisan native:run