-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
hook.js
77 lines (67 loc) · 1.89 KB
/
hook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useContextSystem } from '../../ui/context';
import { useSurface } from '../../surface';
import * as styles from '../styles';
import { useCx } from '../../utils/hooks/use-cx';
/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').Props, 'div'>} props
*/
function useDeprecatedProps( { elevation, isElevated, ...otherProps } ) {
/**@type {import('../../ui/context').WordPressComponentProps<import('../types').Props, 'div'>} */
const propsToReturn = {
...otherProps,
};
let computedElevation = elevation;
if ( isElevated ) {
deprecated( 'Card isElevated prop', {
since: '5.9',
alternative: 'elevation',
} );
computedElevation ??= 2;
}
// The `elevation` prop should only be passed when it's not `undefined`,
// otherwise it will override the value that gets derived from `useContextSystem`.
if ( typeof computedElevation !== 'undefined' ) {
propsToReturn.elevation = computedElevation;
}
return propsToReturn;
}
/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').Props, 'div'>} props
*/
export function useCard( props ) {
const {
className,
elevation = 0,
isBorderless = false,
isRounded = true,
size = 'medium',
...otherProps
} = useContextSystem( useDeprecatedProps( props ), 'Card' );
const cx = useCx();
const classes = useMemo( () => {
return cx(
styles.Card,
isBorderless && styles.boxShadowless,
isRounded && styles.rounded,
// This classname is added for legacy compatibility reasons.
'components-card',
className
);
}, [ className, isBorderless, isRounded ] );
const surfaceProps = useSurface( { ...otherProps, className: classes } );
return {
...surfaceProps,
elevation,
isBorderless,
isRounded,
size,
};
}