Skip to content

Toast Message Prompt Component ​

The Toast component is mainly used for eye-catching prompt effects such as message notifications, loading indicators, and operation result prompts, providing a rich variety of APIs.

📌 Platform Differences ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

⚠️ Notes ​

Notes

  • Since elements cannot be created via JS in uni, you need to place the toast component in the page and then open it via ref
  • Use the useToast() hook function to create a toast instance, which supports global invocation
  • toast.loading() must be manually closed by calling toast.close(), otherwise it will not close automatically
  • When duration is set to -1, it will not close automatically
  • The icon property supports a boolean value (show the default icon), a string (custom icon name), or an icon property object value
  • The component communicates through the global events __hy_toast_open__ and __hy_toast_close__
  • It is recommended to add the <hy-toast></hy-toast> tag in the page root component

🏯 Basic Usage ​

html
<!-- Add the component in the page -->
<template>
    <view>
        <hy-toast></hy-toast>
        <!-- Other content -->
    </view>
</template>
typescript
import { useToast } from '@hy-app/ui';
import { onMounted } from 'vue';

const toast = useToast();

onMounted(() => {
    toast.success('Operation successful');
});

Multiple Types of Prompts ​

typescript
// Default prompt (no icon)
toast.show('Default prompt message');

// Info prompt
toast.info('This is an info message');

// Success prompt
toast.success('Operation successful');

// Error prompt
toast.error('Operation failed');

// Warning prompt
toast.warning('Warning message');

// Primary color prompt
toast.primary('Important notice');

Loading Prompt ​

typescript
// Show loading prompt (default text "Loading...")
toast.loading();

// Custom loading text
toast.loading('Loading, please wait...');

// Automatically close after 5 seconds
setTimeout(() => {
    toast.close();
}, 5000);

Custom Configuration ​

typescript
// Custom display position
toast.success('Top prompt', { position: 'top' });
toast.success('Bottom prompt', { position: 'bottom' });
toast.success('Center prompt', { position: 'center' }); // Default

// Custom display duration
toast.info('Close after 3 seconds', { duration: 3000 });

// No auto-close
toast.info('Manual close', { duration: -1 });

// Show overlay (prevent touch-through)
toast.success('Prompt', { overlay: true });

// Hide overlay (allow touch-through)
toast.success('Prompt', { overlay: false });

// Custom icon (string: icon name)
toast.info('Custom icon', { icon: 'star' });

// Custom icon (object: can set size, color, etc.)
toast.info('Custom icon', {
    icon: { name: 'time', size: '24', color: '#2979ff' },
});

// Callback function
toast.success('Operation successful', {
    complete: () => {
        console.log('toast closed');
    },
});

Complete Example ​

html
<template>
    <view class="demo">
        <hy-toast></hy-toast>
        <hy-button text="Show Success Prompt" @click="showSuccess"></hy-button>
        <hy-button text="Show Loading Prompt" @click="showLoading"></hy-button>
    </view>
</template>

<script setup lang="ts">
    import { useToast } from '@hy-app/ui';

    const toast = useToast();

    const showSuccess = () => {
        toast.success('Operation successful', {
            duration: 2000,
            position: 'bottom',
            complete: () => {
                console.log('Prompt closed');
            },
        });
    };

    const showLoading = () => {
        toast.loading('Loading data...', { position: 'center' });

        // Simulate an async operation
        setTimeout(() => {
            toast.close();
            toast.success('Loading complete');
        }, 3000);
    };
</script>

API ​

Options ​

ParameterDescriptionTypeDefault
messageText to displaystring-
typeTheme type'error' | 'warning' | 'success' | 'primary' | 'info'info
iconIcon name or boolean value (true shows the default icon)string | booleanfalse
loadingWhether in loading statebooleanfalse
loadModeLoading icon mode'circle' | 'spinner' | 'semicircle'circle
overlayWhether to prevent touch-throughbooleanfalse
coverWhether to show the overlaybooleanfalse
positionPosition where the toast appears'top' | 'center' | 'bottom'center
durationDisplay duration (ms), no auto-close when set to -1number2000
completeCallback function after completionFunction-

Methods ​

MethodDescriptionParameters
showShow default toast (no icon)message: string, options?: ToastOptions
infoShow info prompt (with icon)message: string, options?: ToastOptions
successShow success prompt (with icon)message: string, options?: ToastOptions
errorShow error prompt (with icon)message: string, options?: ToastOptions
warningShow warning prompt (with icon)message: string, options?: ToastOptions
primaryShow primary color prompt (with icon)message: string, options?: ToastOptions
loadingShow loading prompt (requires manual close)message?: string, options?: ToastOptions
closeClose all toasts-

Typings ​

Type Description
typescript
/** Toast configuration options */
export interface ToastOptions {
    /** Text to display */
    message?: string;
    /** Theme type: primary, success, error, warning, info */
    type?: string;
    /** Position where the toast appears: top, center, bottom */
    position?: string;
    /** Icon to display (true shows the default icon, a string is a custom icon name, an object is icon property configuration) */
    icon?: boolean | string | { name: string; size?: string | number; color?: string };
    /** Whether to show the overlay (prevent touch-through) */
    overlay?: boolean;
    /** Whether in loading state */
    loading?: boolean;
    /** Loading icon mode: circle, spinner, semicircle */
    loadMode?: string;
    /** Display duration (ms), no auto-close when set to -1 */
    duration?: number;
    /** Callback function after completion */
    complete?: Function | null;
}

CSS Variables ​

VariableDescriptionDefault
--hy-toast-z-indexStacking level11000
--hy-toast-paddingPadding20rpx 32rpx
--hy-toast-radiusBorder radius16rpx
--hy-toast-bg-colorBackground color (with icon mode)#585858
--hy-toast-max-widthMax width600rpx
--hy-toast-box-shadowShadow0 8rpx 24rpx rgba(0, 0, 0, 0.15)
--hy-toast-loading-paddingPadding in loading mode40rpx
--hy-toast-icon-sizeIcon size40rpx
--hy-toast-icon-margin-rightIcon right margin16rpx
--hy-toast-text-max-widthText max width480rpx
--hy-toast-default-bg-colorDefault mode background colorrgba(0, 0, 0, 0.75)
--hy-toast-icon-radiusIcon border radiusFollows --hy-radius-semicircle (100px)
--hy-toast-text-colorText color#ffffff
--hy-toast-text-font-sizeText font sizeFollows --hy-font-size-base (28rpx)
12:33
battery