-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
hook.js
190 lines (168 loc) · 4.29 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* External dependencies
*/
import { css } from '@emotion/react';
import { isPlainObject } from 'lodash';
/**
* WordPress dependencies
*/
import { useMemo, Children, cloneElement } from '@wordpress/element';
/**
* Internal dependencies
*/
import { hasConnectNamespace, useContextSystem } from '../ui/context';
import { useTruncate } from '../truncate';
import { getOptimalTextShade } from '../ui/utils';
import * as styles from './styles';
import { createHighlighterText } from './utils';
import { getFontSize } from '../ui/utils/font-size';
import { CONFIG, COLORS } from '../utils';
import { getLineHeight } from './get-line-height';
import { useCx } from '../utils/hooks/use-cx';
/**
* @param {import('../ui/context').WordPressComponentProps<import('./types').Props, 'span'>} props
*/
export default function useText( props ) {
const {
adjustLineHeightForInnerControls,
align,
children,
className,
color,
ellipsizeMode,
isDestructive = false,
display,
highlightEscape = false,
highlightCaseSensitive = false,
highlightWords,
highlightSanitize,
isBlock = false,
letterSpacing,
lineHeight: lineHeightProp,
optimizeReadabilityFor,
size,
truncate = false,
upperCase = false,
variant,
weight = CONFIG.fontWeight,
...otherProps
} = useContextSystem( props, 'Text' );
/** @type {import('react').ReactNode} */
let content = children;
const isHighlighter = Array.isArray( highlightWords );
const isCaption = size === 'caption';
if ( isHighlighter ) {
if ( typeof children !== 'string' ) {
throw new TypeError(
'`children` of `Text` must only be `string` types when `highlightWords` is defined'
);
}
content = createHighlighterText( {
autoEscape: highlightEscape,
// Disable reason: We need to disable this otherwise it erases the cast
// eslint-disable-next-line object-shorthand
children: /** @type {string} */ ( children ),
caseSensitive: highlightCaseSensitive,
searchWords: highlightWords,
sanitize: highlightSanitize,
} );
}
const cx = useCx();
const classes = useMemo( () => {
const sx = {};
const lineHeight = getLineHeight(
adjustLineHeightForInnerControls,
lineHeightProp
);
sx.Base = css( {
color,
display,
fontSize: getFontSize( size ),
/* eslint-disable jsdoc/valid-types */
fontWeight:
/** @type {import('react').CSSProperties['fontWeight']} */ (
weight
),
/* eslint-enable jsdoc/valid-types */
lineHeight,
letterSpacing,
textAlign: align,
} );
sx.upperCase = css( { textTransform: 'uppercase' } );
sx.optimalTextColor = null;
if ( optimizeReadabilityFor ) {
const isOptimalTextColorDark =
getOptimalTextShade( optimizeReadabilityFor ) === 'dark';
sx.optimalTextColor = isOptimalTextColorDark
? css( { color: COLORS.gray[ 900 ] } )
: css( { color: COLORS.white } );
}
return cx(
styles.Text,
sx.Base,
sx.optimalTextColor,
isDestructive && styles.destructive,
!! isHighlighter && styles.highlighterText,
isBlock && styles.block,
isCaption && styles.muted,
variant && styles[ variant ],
upperCase && sx.upperCase,
className
);
}, [
adjustLineHeightForInnerControls,
align,
className,
color,
cx,
display,
isBlock,
isCaption,
isDestructive,
isHighlighter,
letterSpacing,
lineHeightProp,
optimizeReadabilityFor,
size,
upperCase,
variant,
weight,
] );
/** @type {undefined | 'auto' | 'none'} */
let finalEllipsizeMode;
if ( truncate === true ) {
finalEllipsizeMode = 'auto';
}
if ( truncate === false ) {
finalEllipsizeMode = 'none';
}
const finalComponentProps = {
...otherProps,
className: classes,
children,
ellipsizeMode: ellipsizeMode || finalEllipsizeMode,
};
const truncateProps = useTruncate( finalComponentProps );
/**
* Enhance child `<Link />` components to inherit font size.
*/
if ( ! truncate && Array.isArray( children ) ) {
content = Children.map( children, ( child ) => {
// @ts-ignore
if ( ! isPlainObject( child ) || ! ( 'props' in child ) ) {
return child;
}
const isLink = hasConnectNamespace( child, [ 'Link' ] );
if ( isLink ) {
return cloneElement( child, {
size: child.props.size || 'inherit',
} );
}
return child;
} );
}
return {
...truncateProps,
children: truncate ? truncateProps.children : content,
};
}