Laravel Inertia Toast 拉拉维尔惯性Toast
Laravel Inertia Toast

原始链接: https://github.com/veekthoven/laravel-inertia-toast

## Laravel Inertia Toast:轻松打造美观通知 Laravel Inertia Toast 是一个具有明确观点的包,用于在 Laravel + Inertia.js 应用程序(支持 Vue 3 和 React)中添加美观且可定制的 toast 通知。它提供了流畅的 PHP API 和客户端 JavaScript API (`useToast()`),可以从后端控制器和前端组件触发通知。 主要功能包括:多 toast 支持、重定向安全、6 种可定制的位置、Tailwind CSS 样式(兼容 v3 和 v4)以及流畅的动画。配置选项允许控制持续时间、位置和最大可见 toast 数量。 **安装:** 需要 PHP 8.1+、Laravel 10/11/12、Inertia.js v2.3.3+ 以及 Vue 3.3+ 或 React 18+。通过 Composer 安装 PHP 包 (`composer require veekthoven/laravel-inertia-toast`),并通过 npm 安装相应的前端适配器 (`@laravel-inertia-toast/vue` 或 `@laravel-inertia-toast/react`)。 记住发布配置文件 (`php artisan vendor:publish --tag=inertia-toast-config`) 并配置 Tailwind 以识别包的 CSS 类。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Laravel Inertia Toast (github.com/veekthoven) 3 分,由 veekthoven 1小时前发布 | 隐藏 | 过去 | 收藏 | 2 条评论 帮助 layer8 9分钟前 | 下一个 [–] 然而:https://news.ycombinator.com/item?id=41298794 https://news.ycombinator.com/item?id=46196831 回复 veekthoven 1小时前 | 上一个 [–] 一个提供精美吐司通知的 Laravel + Inertia.js 应用的、带有主观意见的包。流畅的 PHP API,多吐司支持,重定向安全,并带有 Vue 3 和 React 适配器。吐司通知可以从后端 (PHP) 和前端 (JavaScript) 触发。在控制器中使用 Toast::success('保存成功!'),或在组件中使用 useToast().success('复制成功!')。无论哪种方式,都能显示相同的精美吐司。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

An opinionated package that provides beautiful toast notifications for Laravel + Inertia.js applications. Fluent PHP API, multi-toast support, redirect-safe, with Vue 3 and React adapters.

Toast notifications can can be triggered from both your backend (PHP) and frontend (JavaScript). Use Toast::success('Saved!') in your controllers or useToast().success('Copied!') in your components — same beautiful toasts either way.

PHP: PHP

React: React

Vue: Vue

See it in action: Vue

  • Fluent PHP API: toast('Saved!')->success() or Toast::success('Saved!')
  • Vue 3 and React adapters with TypeScript support
  • Position-aware with 6 positions (top-right, top-left, top-center, bottom-right, bottom-left, bottom-center)
  • Tailwind CSS styling (v3 & v4 compatible)
  • Client-side toast API via useToast() composable/hook
  • Beautiful in/out animations.
composer require veekthoven/laravel-inertia-toast

Optionally publish the config:

php artisan vendor:publish --tag=inertia-toast-config

Vue 3:

npm install @laravel-inertia-toast/vue

React:

npm install @laravel-inertia-toast/react

Register the plugin in your app.js:

// In your resources/js/app.js

import { createApp } from 'vue'
import { InertiaToast } from '@laravel-inertia-toast/vue'

const app = createApp(App)
app.use(InertiaToast, {
    duration: 5000,
    position: 'top-right',
    maxVisible: 5,
})
app.mount('#app')

Add the <Toasts /> component to your layout:

<script setup>
import { Toasts } from '@laravel-inertia-toast/vue'
</script>

<template>
  <div>
    <slot />
    <Toasts />
  </div>
</template>

Wrap your app with <ToastProvider> and add <Toasts />:

// in your resources/js/app.tsx

import { createRoot } from 'react-dom/client';
import { ToastProvider, Toasts } from '@laravel-inertia-toast/react'

setup({ el, App, props }) {
    const root = createRoot(el);

    root.render(
        <ToastProvider
            config={{
                position: 'top-right'
                duration: 5000,
                maxVisible: 5,
            }}
        >
            <App {...props} />
            <Toasts />
        </ToastProvider>
    );
},

Since the toast components use Tailwind classes internally, you need to add the package to Tailwind's source detection so the required classes are generated.

If you use Tailwind v4, add the following @source directive to your main CSS file (e.g. resources/css/app.css):

Vue 3:

@source "../../node_modules/@laravel-inertia-toast/vue/dist/**/*.js"; 

React:

@source "../../node_modules/@laravel-inertia-toast/react/dist/**/*.js";

The relative path above assumes your CSS file is at resources/css/app.css. Adjust accordingly if your setup differs.

Or if you are still on Tailwind v3, add the following to your tailwind.config.js file.

Vue 3:

// tailwind.config.js

module.exports = {                                                                                   
    content: [
        //...

        './node_modules/@laravel-inertia-toast/vue/dist/**/*.js',

        //...
    ],
}

React:

// tailwind.config.js

module.exports = {                                                                                   
    content: [
        //...

        './node_modules/@laravel-inertia-toast/react/dist/**/*.js',
        
        //...
    ],
}

The relative paths above assume your tailwind.config.js is at the root of your project. Adjust accordingly if your setup differs.

use InertiaToast\Facades\Toast;

// In a controller
Toast::success('Profile updated!');
Toast::error('Something went wrong.');
Toast::info('Check your email for a confirmation link.');
Toast::warning('Your subscription is about to expire.');

return redirect()->route('dashboard');
// Fluent builder
toast('Profile updated!')->success();
toast('Something went wrong.')->error();
toast('Slow message.')->duration(10000)->warning();

// Direct access to Toaster
toast()->success('Quick shorthand');
// Via facade
Toast::success('Saved!', 3000); // 3 seconds

// Via helper
toast('Done!')->duration(3000)->success();

Both adapters expose a useToast() composable/hook for triggering toasts from the frontend:

<script setup>
import { useToast } from '@laravel-inertia-toast/vue'

const { success, error, info, warning } = useToast()

function handleClick() {
  success('Copied to clipboard!')
}
</script>
import { useToast } from '@laravel-inertia-toast/react'

function MyComponent() {
  const { success, error, info, warning } = useToast()

  return (
    <button onClick={() => success('Copied to clipboard!')}>
      Copy
    </button>
  )
}

Publish the config file:

php artisan vendor:publish --tag=inertia-toast-config
// config/inertia-toast.php
return [
    'duration' => 5000,         // Default auto-dismiss duration (ms)
    'position' => 'top-right',  // Toast position on screen
    'max_visible' => 5,         // Max simultaneous toasts
    'prop_key' => 'toasts',     // Inertia flash key
];

Frontend config can be passed when registering the plugin (Vue) or via the config prop on <ToastProvider> (React).

Note: You can install and use the frontend adapters without the backend package. But to use the backend package, you'd need to install and setup the adapter of the frontend framework you are using. To get the full experience though, use both backend and frontend packages.

  • PHP 8.1+
  • Laravel 10, 11, or 12
  • Inertia.js v2.3.3+
  • Vue 3.3+ or React 18+
  • Tailwind v3, v4

MIT

联系我们 contact @ memedata.com