Skip to content

Hooks Composition API ​

useShare Mini Program Global Share Configuration ​

useShare(options?) => ​

Configures global sharing for the Mini Program and returns sharing methods to be exposed by pages.

Parameters

ParameterTypeRequiredDefaultDescription
optionsobjectNo-Share configuration
options.titlestringNo-Title name
options.pathstringNo-Mini Program path
options.friendImageUrlstringNo-Cover image for sharing with friends
options.timelineImageUrlstringNo-Cover image for sharing to Moments

Return Value

TypeDescription
objectContains the onShareAppMessage and onShareTimeline methods

Example

  1. Single page usage When a page needs custom share content, register it in <script setup> together with hooks from @dcloudio/uni-app:
Index.vue
typescript
<script setup lang="ts">
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { useShare } from '@hy-app/ui'

const share = useShare({
  title: 'Page Title',
  path: '/pages/index/index',
  friendImageUrl: '/static/share-friend.png',
  timelineImageUrl: '/static/share-timeline.png'
})

// Registered at the page level; takes precedence over the global mixin's default sharing
onShareAppMessage(share.onShareAppMessage)
onShareTimeline(share.onShareTimeline)
</script>
  1. Global usage

Once registered globally, the "Forward to Friend / Share to Moments" options in the WeChat Mini Program's top-right menu become available, so you don't need to write it separately for each page.

main.ts
typescript
import { useShare } from '@hy-app/ui';

app.mixin(
  useShare({
    title: 'Hy Component Library'
  })
)

useToast Global Toast Messages ​

useToast() => ToastInstance ​

A Composition API for global toast messages, providing multiple types of message prompts.

Return Value

TypeDescription
ToastInstanceToast instance, including the show, info, success, error, warning, primary, loading, and close methods

ToastInstance Methods

MethodParametersDescription
showmessage, options?Default toast
infomessage, options?Info toast
successmessage, options?Success toast
errormessage, options?Error toast
warningmessage, options?Warning toast
primarymessage, options?Primary toast
loadingmessage?, options?Loading
close-Close all toasts

options Parameters

ParameterTypeRequiredDefaultDescription
messagestringYes-Text message to display
typestringNo-Theme type: primary, success, error, warning, info
positionstringNo-Position where the toast appears: top, center, bottom
iconboolean | stringNo-Icon to display
overlaybooleanNo-Whether to prevent touch pass-through
durationnumberNo-Duration (in milliseconds)

Example

typescript
import { useToast } from '@hy-app/ui';

const toast = useToast();

toast.success('Operation successful!');
toast.error('Operation failed');
toast.loading('Loading...');

useMessage Message Dialog Composition API ​

useMessage() => MessageInstance ​

A Composition API for message dialogs, providing the alert and confirm methods.

Note

useMessage() only injects the global message state into descendant components. A hy-modal carrying the message attribute must be mounted in the calling component's template as the driver instance for the dialog to render:

  • Do not bind v-model on the driver instance; its visibility is fully controlled by message.alert / message.confirm;
  • If custom hy-modal instances also exist on the same page, explicitly pass :message="false" to them to prevent button clicks from being hijacked by the global callback and failing. See Modal: Coexistence of Global Messages and Custom Modals for details.

Return Value

TypeDescription
MessageInstanceMessage instance, including the alert and confirm methods

MessageInstance Methods

MethodParametersDescription
alertmessage | optionsAlert dialog
confirmmessage | optionsConfirm dialog

options Parameters

ParameterTypeRequiredDefaultDescription
titlestringNo-Dialog title
contentstringYes-Dialog content
confirmTextstringNo-Confirm button text
cancelTextstringNo-Cancel button text
showConfirmButtonbooleanNo-Whether to show the confirm button
showCancelButtonbooleanNo-Whether to show the cancel button
confirmColorstringNo-Confirm button color
cancelColorstringNo-Cancel button color
confirmfunctionNo-Confirm button click callback
cancelfunctionNo-Cancel button click callback

Example

html
<template>
    <view>
        <hy-button text="Delete" @click="onRemove"></hy-button>

        <!-- Global message driver instance -->
        <hy-modal message></hy-modal>
    </view>
</template>

<script setup>
    import { useMessage } from '@hy-app/ui';

    const message = useMessage();

    const onRemove = async () => {
        const result = await message.confirm({
            title: 'Delete Confirmation',
            content: 'Are you sure you want to delete this?',
            confirmText: 'Confirm',
            cancelText: 'Cancel'
        });
        if (result) {
            uni.showToast({ title: 'Deleted', icon: 'success' });
        }
    };
</script>

useTouch Touch Event Composition API ​

useTouch() => TouchInstance ​

A Composition API for touch events, used to track and analyze user touch interactions.

Return Value

TypeDescription
TouchInstanceTouch instance

TouchInstance Properties and Methods

NameTypeDescription
touchStartfunctionTouch start handler
touchMovefunctionTouch move handler
directionrefTouch direction
deltaXrefHorizontal delta
deltaYrefVertical delta
offsetXrefHorizontal offset
offsetYrefVertical offset

Example

typescript
import { useTouch } from '@hy-app/ui';

const { touchStart, touchMove, direction, deltaX, deltaY } = useTouch();

useShakeService Shake Sensor Composition API ​

useShakeService(threshold?) => ​

A Composition API for the shake sensor, implementing shake-to-activate functionality by listening to device acceleration.

Parameters

ParameterTypeRequiredDefaultDescription
thresholdnumberNo-Shake threshold

Return Value

TypeDescription
objectContains the startShakeListener and stopShakeListener methods

Example

typescript
import { useShakeService } from '@hy-app/ui';

const { startShakeListener, stopShakeListener } = useShakeService();

startShakeListener(() => {
    console.log('Shake triggered');
});

useTranslate Internationalization Composition API ​

useTranslate(module?) => ​

A Composition API for internationalization, used to implement multi-language switching in components or pages.

Parameters

ParameterTypeRequiredDefaultDescription
modulestringNo-Language pack module name

Return Value

TypeDescription
objectContains the t translation method

Example

typescript
import { useTranslate } from '@hy-app/ui';

const { t } = useTranslate('common');

console.log(t('hello'));
console.log(t('welcome', 'Hy'));

useQueue Queue Management Composition API ​

useQueue() => QueueInstance ​

A Composition API for queue management, used to manage the display order of components and mutually exclusive closing logic.

Return Value

TypeDescription
QueueInstanceQueue instance

QueueInstance Methods

MethodParametersDescription
pushToQueuecomponentAdd a component to the queue
removeFromQueuecomponentRemove a component from the queue
closeOthercomponentClose all components except the current one
closeOutside-Close all components

Example

typescript
import { useQueue } from '@hy-app/ui';

const { pushToQueue, removeFromQueue, closeOther } = useQueue();

pushToQueue(popupRef.value);
closeOther(popupRef.value);