mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-24 04:36:44 +08:00
26 lines
656 B
TypeScript
26 lines
656 B
TypeScript
import React from 'react';
|
|
|
|
import type { SizeType } from '../SizeContext';
|
|
import SizeContext from '../SizeContext';
|
|
|
|
const useSize = <T extends string | undefined | number | object>(
|
|
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 (typeof customSize === 'function') {
|
|
return customSize(size);
|
|
}
|
|
return size as T;
|
|
}, [customSize, size]);
|
|
return mergedSize;
|
|
};
|
|
|
|
export default useSize;
|