This commit is contained in:
oneao committed 2025-11-06 22:50:03 +08:00
1 parent d60be4a75b
commit be8eaae7c5
24 files changed
+595 -232

No files matched your search

@@ -1,8 +0,0 @@
// utils/runOnMain.ts
// ✅ 安全封装 runOnJS,屏蔽 deprecated 警告
import { runOnJS as _runOnJS } from 'react-native-worklets'
// 屏蔽类型定义中的 deprecated 提示
export const runOnMain = ((fn: (...args: any[]) => void) => {
return _runOnJS(fn)
}) as typeof _runOnJS
+16 -66
View File
@@ -1,5 +1,5 @@
import type { IToastProps } from '@gluestack-ui/core/lib/esm/toast/creator/types'
import React, { createContext, useContext, useRef } from 'react'
import React, { useEffect, useRef } from 'react'
import { Toast, ToastDescription, ToastTitle, useToast as useGluestackToast } from '@/components/ui/toast'
type ToastVariant = 'solid' | 'outline'
@@ -9,29 +9,30 @@ interface ShowToastOptions extends IToastProps {
title?: React.ReactNode
description?: React.ReactNode
variant?: ToastVariant
action?: ToastAction
type?: ToastAction
}
// 创建全局 toast 实例
let globalToast: any = null
// 创建 Context 用于在组件内部使用
const ToastContext = createContext<ReturnType<typeof useGluestackToast> | null>(null)
// 全局 toast 实例
let globalToast: {
show: (options: ShowToastOptions) => string
close: (id: string) => void
closeAll: () => void
isActive: (id: string) => boolean
} | null = null
export function ToastProviderWrapper({ children }: { children: React.ReactNode }) {
const toast = useGluestackToast()
const toastRef = useRef(toast)
toastRef.current = toast
// 设置全局实例
React.useEffect(() => {
useEffect(() => {
globalToast = {
show: (options: ShowToastOptions) => {
const id = Math.random().toString()
toast.show({
id,
placement: options.placement ?? 'top',
duration: options.duration !== undefined ? options.duration : 3000,
duration: options.duration ?? 3000,
avoidKeyboard: options.avoidKeyboard ?? false,
containerStyle: options.containerStyle,
onCloseComplete: options.onCloseComplete,
@@ -41,7 +42,7 @@ export function ToastProviderWrapper({ children }: { children: React.ReactNode }
<Toast
nativeID={`toast-${id}`}
variant={options.variant ?? 'solid'}
action={options.action ?? 'muted'}
action={options.type ?? 'muted'}
>
{options.title && <ToastTitle className="font-blod">{options.title}</ToastTitle>}
{options.description && <ToastDescription>{options.description}</ToastDescription>}
@@ -60,60 +61,11 @@ export function ToastProviderWrapper({ children }: { children: React.ReactNode }
}
}, [toast])
return (
<ToastContext.Provider value={toast}>
{children}
</ToastContext.Provider>
)
return <>{children}</>
}
/**
* 自定义 Hook,用于 React 组件内部
*/
export function useToast() {
const toast = useContext(ToastContext)
if (!toast) {
throw new Error('useToast must be used within a ToastProviderWrapper')
}
return {
show: (options: ShowToastOptions) => {
const id = Math.random().toString()
toast.show({
id,
placement: options.placement ?? 'top',
duration: options.duration !== undefined ? options.duration : 3000,
avoidKeyboard: options.avoidKeyboard ?? false,
containerStyle: options.containerStyle,
onCloseComplete: options.onCloseComplete,
render: options.render
? () => options.render!({ id })
: ({ id }) => (
<Toast
nativeID={`toast-${id}`}
variant={options.variant ?? 'solid'}
action={options.action ?? 'muted'}
>
{options.title && <ToastTitle className="font-blod">{options.title}</ToastTitle>}
{options.description && <ToastDescription>{options.description}</ToastDescription>}
</Toast>
),
})
return id
},
close: (id: string) => {
toast.close(id)
},
closeAll: () => {
toast.closeAll()
},
isActive: (id: string) => toast.isActive(id),
}
}
/**
* 全局 toast 实例,可在组件外部使用
* 全局 toast 对象,组件内外都可以使用
*/
export const toast = {
show: (options: ShowToastOptions) => {
@@ -124,14 +76,12 @@ export const toast = {
return globalToast.show(options)
},
close: (id: string) => {
if (globalToast) {
if (globalToast)
globalToast.close(id)
}
},
closeAll: () => {
if (globalToast) {
if (globalToast)
globalToast.closeAll()
}
},
isActive: (id: string) => {
return globalToast ? globalToast.isActive(id) : false