跪拜 Guibai
← All articles
Frontend · Uni-app · Vue.js

uView Pro X's 8 Composables Kill the Most Repetitive uni-app x Page Work

By 前端梦工厂 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Skipping these composables means re-solving the same timing, null-check, and platform-compat issues on every page; adopting them removes a whole class of subtle layout and gesture bugs from uni-app x projects.

Summary

Writing uni-app x pages forces developers to chase window dimensions, status bar offsets, safe area insets, gesture direction, and countdown drift, and to reconcile those values across App, H5, mini-programs, and HarmonyOS. uView Pro X's 8 composables pull that plumbing into a single import. They share a module-level data source with the library's own components, so a page's values line up with what `ux-tabbar` or `ux-safe-bottom` use internally.

Each composable targets a specific pain point. `useWindowInfo` refreshes at module load and again after mount to get accurate first-frame sizes; `useSafeArea` separates safe area insets from status bar height; `useRect` wraps `getBoundingClientRectAsync` and returns null when an element is absent; `useTouch` normalizes touch and mouse events via conditional compilation and locks gesture direction once a threshold is crossed. `useCountDown` bases its calculation on an end time, so background throttling doesn't erode accuracy; `useDebounce` and `useThrottle` cache factories by delay to avoid stale timers; `useDeviceInfo` captures device data once. All eight work without any `ux-*` component, so existing uni-app x projects can adopt them piecemeal.

Takeaways
— useWindowInfo refreshes at module load and again after mount to get accurate first-frame size.
— Safe area insets and status bar height are separate; useSafeArea only returns insets, and 0 is a valid value.
— useRect wraps getBoundingClientRectAsync and returns null when the element is missing.
— useTouch normalizes touch and mouse events via conditional compilation, and locks direction after a 10px threshold.
— useCountDown records endTime and recomputes remaining time, avoiding drift from setInterval throttling.
— useDebounce and useThrottle cache factories by delay, so changing delay rebuilds without stale callbacks.
— useDeviceInfo is captured once at module load; orientation changes are handled by comparing window dimensions.
— All composables import from a single entry and work without any ux-* component.
Conclusions

A module-level singleton for window info prevents the classic one-page-two-heights bug, because all call sites share one ref.

The distinction between statusBarHeight and safeAreaInsets is a subtle source of misalignment on landscape iOS; splitting them into separate APIs forces developers to choose the right one.

Normalizing touch and mouse events with conditional compilation keeps gesture code identical across H5 and App, moving platform quirks into the library.

The countdown's use of endTime rather than decrement logic is a direct fix for background throttling; the timer's late firing only affects refresh rate, not accuracy.

Concepts & terms
Composable
A Vue 3 hook-style function that encapsulates reactive state and logic; in uView Pro X, page-level composables share the same data source as components.
Safe area insets
The padding distances the system requires to avoid notches, rounded corners, and the bottom gesture bar; distinct from the status bar height.
Conditional compilation
uni-app's preprocessor that compiles different code for different platforms using `#ifdef` / `#ifndef` comments, letting the library normalize touch vs. mouse events.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗