Skip to content

Hooks Utilities

SeeYouUI provides a set of practical Hooks for handling common scenarios like routing, safe area, scroll monitoring, and tree data management.

useRoute

Cross-platform routing unified wrapper with automatic params encoding.

typescript
import { useRoute } from '@/uni_modules/see-u-ui/utils/hooks/useRoute'

const { navigateTo, redirectTo, switchTab, reLaunch, navigateBack, currentRoute, getQuery } = useRoute()

Returns

Property/MethodTypeDescription
navigateTo(options: RouteOptions) => Promise<void>Navigate to new page (push)
redirectTo(options: RouteOptions) => Promise<void>Redirect to new page (replace)
switchTab(options: RouteOptions) => Promise<void>Switch tab page
reLaunch(options: RouteOptions) => Promise<void>Close all pages and open new page
navigateBack(delta?: number) => Promise<void>Go back, default delta=1
currentRouteRef<{ path, params }>Current route info
getQuery<T>() => TGet current page params

RouteOptions

PropertyTypeDescription
urlstringTarget page path
paramsRecord<string, any>Route params, auto encoded
animationTypestringPage animation type (APP only)

Example

typescript
// Navigate with params
await navigateTo({
  url: '/pages/detail/index',
  params: { id: '123', name: 'test' }
})

// Go back
await navigateBack()

// Go back 2 pages
await navigateBack(2)

useSafeArea

Get device safe area insets, cached at module level (singleton pattern).

typescript
import { useSafeArea } from '@/uni_modules/see-u-ui/utils/hooks/useSafeArea'

const { top, bottom, left, right } = useSafeArea()

Returns

PropertyTypeDescription
topRef<number>Top safe area inset (px)
bottomRef<number>Bottom safe area inset (px)
leftRef<number>Left safe area inset (px)
rightRef<number>Right safe area inset (px)

Example

vue
<template>
  <view :style="{ paddingTop: top + 'px' }">
    <text>Content area</text>
  </view>
</template>

<script setup>
import { useSafeArea } from '@/uni_modules/see-u-ui/utils/hooks/useSafeArea'
const { top, bottom } = useSafeArea()
</script>

useScrollSpy

Monitor page or container scroll position with throttling and direction detection.

typescript
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'

const { scrollTop, isScrolling, scrollDirection } = useScrollSpy()

Options

PropertyTypeDefaultDescription
targetstring''Scroll container selector (H5 only); falls back to window/page scroll when empty
throttlenumber0Throttle interval (ms). 0 (default) uses requestAnimationFrame; > 0 uses setTimeout throttling (leading + trailing)

Returns

PropertyTypeDescription
scrollTopRef<number>Current scroll position (px)
isScrollingRef<boolean>Whether scrolling
scrollDirectionRef<'up' | 'down' | 'idle'>Scroll direction, initial value is 'idle'

Example

vue
<template>
  <view v-if="scrollTop > 200" class="back-top" @tap="scrollToTop">
    Back to top
  </view>
</template>

<script setup>
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'
const { scrollTop, scrollDirection } = useScrollSpy()
</script>

useTree

Tree data management Hook providing flattening, expand/collapse, selection/cascade, search filtering, and lazy loading.

typescript
import { useTree } from '@/uni_modules/see-u-ui/utils/hooks/useTree'

const {
  flatNodes, expandedKeys, checkedKeys,
  toggleExpand, toggleCheck, expandAll, collapseAll,
  filterNodes, getCheckedNodes, appendNode, removeNode
} = useTree({
  data: treeData,
  keyField: 'id',
  labelField: 'label',
  childrenField: 'children',
  selectMode: 'check-cascade'
})

Options

PropertyTypeDefaultDescription
dataRef<TreeNode[]>-Tree data (reactive)
keyFieldstring'id'Key field name
labelFieldstring'label'Label field name
childrenFieldstring'children'Children field name
selectModestring'single'Selection mode: single / multiple / check-cascade
isLazybooleanfalseWhether lazy loading

TreeNode

PropertyTypeDescription
idstring | numberNode unique identifier
labelstringNode label
childrenTreeNode[]Children nodes
isDisabledbooleanWhether disabled
isLeafbooleanWhether leaf node

Returns

Property/MethodTypeDescription
flatNodesComputedRef<FlatNode[]>Flattened node list
expandedKeysRef<Set>Expanded node keys
checkedKeysRef<Set>Checked node keys
toggleExpand(key) => voidToggle expand/collapse
toggleCheck(key) => voidToggle check state
expandAll() => voidExpand all nodes
collapseAll() => voidCollapse all nodes
filterNodes(query) => voidSearch filter
getCheckedNodes() => TreeNode[]Get checked nodes
appendNode(parentKey, node) => voidAdd node
removeNode(key) => voidRemove node
updateNode(key, data) => voidUpdate node

Example

typescript
import { ref } from 'vue'
import { useTree } from '@/uni_modules/see-u-ui/utils/hooks/useTree'

const treeData = ref([
  {
    id: 1,
    label: 'Node 1',
    children: [
      { id: 11, label: 'Node 1-1' },
      { id: 12, label: 'Node 1-2' }
    ]
  }
])

const { flatNodes, toggleExpand, expandAll, getCheckedNodes } = useTree({
  data: treeData,
  selectMode: 'check-cascade'
})

Liao ICP No. 2025070134