UNPKG

ucc-ui

Version:

ucc-ui components library

53 lines (52 loc) 2.27 kB
import { DebounceSettings, DebouncedFunc } from 'lodash-es'; export interface URootFontSizeOptions { rootFontSize?: number; isResize?: boolean; resizeTimeout?: number; immediate?: boolean; debounceOpt?: DebounceSettings; afterRefreshCallback?: (rootFontSize: number | undefined) => void; beforeRefreshCallback?: (rootFontSize: number | undefined) => void; } export interface URootFontSizeReturn { rootFontSize?: number; destory: () => void; refreshRootFontSize: () => void; refreshRootFontSizeDebounce?: DebouncedFunc<() => void>; setRootFontSize: (rootFontSize: number) => void; } /** * 处理根字体大小 rem 响应式布局 * @author Yuluo * @link https://github.com/YuluoY * @date 2024-08-24 * @param {URootFontSizeOptions} options 配置项 * @param {number} [options.rootFontSize=16] 根字体大小 * @param {number} [options.resizeTimeout=300] resize事件防抖时间, 默认300ms * @param {boolean} [options.isResize=true] 是否开启resize事件, 默认true * @param {boolean} [options.immediate=false] 是否立即执行, 默认false * @param {(val:number) => void} [options.beforeRefreshCallback] 在resize刷新字体大小前执行的回调函数 * @param {(val:number) => void} [options.afterRefreshCallback] 在resize刷新字体大小后执行的回调函数 * @param {DebounceSettings} [options.debounceOpt] lodash防抖配置, 默认{} * @returns {URootFontSizeReturn} * @example * ```js * const { * destory, // 销毁resize事件 * rootFontSize, // 根字体大小 * refreshRootFontSize, // 刷新根字体大小函数 * refreshRootFontSizeDebounce, // lodash的防抖函数 * } = useRootFontSize({ * setRootFontSizeCallback: (rootFontSize) => { * console.log(rootFontSize) * }, * immediate: true, * isResize: true, * resizeTimeout: 300, * debounceOpt: { * // lodash防抖配置 * } * }) * ``` */ export default function useRootFontSize(options: URootFontSizeOptions): URootFontSizeReturn;