2023-05-12 14:53:47 +08:00
|
|
|
import React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
import type { SizeType } from '../SizeContext';
|
|
|
|
import SizeContext from '../SizeContext';
|
|
|
|
|
|
|
|
const useSize = <T>(customSize?: T | ((ctxSize: SizeType) => T)): T => {
|
|
|
|
const size = React.useContext<SizeType>(SizeContext);
|
|
|
|
const mergedSize = React.useMemo<T>(() => {
|
|
|
|
if (!customSize) {
|
|
|
|
return size as T;
|
|
|
|
}
|
|
|
|
if (typeof customSize === 'string') {
|
|
|
|
return customSize ?? size;
|
|
|
|
}
|
|
|
|
if (customSize instanceof Function) {
|
|
|
|
return customSize(size);
|
|
|
|
}
|
|
|
|
return size as T;
|
|
|
|
}, [customSize, size]);
|
|
|
|
return mergedSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useSize;
|