generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathinternal.tsx
More file actions
144 lines (128 loc) · 4.16 KB
/
internal.tsx
File metadata and controls
144 lines (128 loc) · 4.16 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useLayoutEffect, useRef, useState } from 'react';
import clsx from 'clsx';
import { warnOnce } from '@cloudscape-design/component-toolkit/internal';
import { getBaseProps } from '../internal/base-component';
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
import { useMergeRefs } from '../internal/hooks/use-merge-refs';
import { useVisualRefresh } from '../internal/hooks/use-visual-mode';
import icons from './generated/icons';
import { IconProps } from './interfaces';
import styles from './styles.css.js';
type InternalIconProps = IconProps &
InternalBaseComponentProps & {
badge?: boolean;
};
function iconSizeMap(height: number | null) {
if (height === null) {
// This is the best guess for the contextual height while server rendering.
return 'normal';
}
if (height >= 50) {
return 'large';
} else if (height >= 36) {
return 'big';
} else if (height >= 24) {
return 'medium';
} else if (height <= 16) {
return 'small';
} else {
return 'normal';
}
}
const InternalIcon = ({
name,
size = 'normal',
variant = 'normal',
url,
alt,
ariaLabel,
svg,
badge,
__internalRootRef = null,
...props
}: InternalIconProps) => {
const iconRef = useRef<HTMLElement>(null);
// To ensure a re-render is triggered on visual mode changes
useVisualRefresh();
const [parentHeight, setParentHeight] = useState<number | null>(null);
const contextualSize = size === 'inherit';
const iconSize = contextualSize ? iconSizeMap(parentHeight) : size;
const inlineStyles = contextualSize && parentHeight !== null ? { height: `${parentHeight}px` } : {};
const baseProps = getBaseProps(props);
baseProps.className = clsx(
baseProps.className,
styles.icon,
contextualSize && styles['icon-flex-height'],
badge && styles.badge,
!contextualSize && styles[`size-${iconSize}-mapped-height`],
styles[`size-${iconSize}`],
styles[`variant-${variant}`],
styles[`name-${name}`]
);
// Possible infinite loop is not a concern here because line
// height should not change without an external state update.
// eslint-disable-next-line react-hooks/exhaustive-deps
useLayoutEffect(() => {
if (!contextualSize || !iconRef.current) {
return;
}
const { lineHeight } = getComputedStyle(iconRef.current);
const newParentHeight = parseInt(lineHeight, 10);
setParentHeight(newParentHeight);
});
const mergedRef = useMergeRefs(iconRef, __internalRootRef);
const hasAriaLabel = typeof ariaLabel === 'string';
const labelAttributes = hasAriaLabel ? { role: 'img', 'aria-label': ariaLabel } : {};
if (svg) {
if (url) {
warnOnce(
'Icon',
'You have specified both `url` and `svg`. `svg` will take precedence and `url` will be ignored.'
);
}
return (
<span {...baseProps} {...labelAttributes} ref={mergedRef} aria-hidden={!hasAriaLabel} style={inlineStyles}>
{svg}
</span>
);
}
if (url) {
return (
<span {...baseProps} ref={mergedRef} style={inlineStyles}>
<img src={url} alt={ariaLabel ?? alt} />
</span>
);
}
const validIcon = name && Object.prototype.hasOwnProperty.call(icons, name);
function iconMap(name: IconProps.Name) {
if (name === 'gen-ai' && iconSize === 'small') {
return (
<svg
width="12"
height="12"
viewBox="0 0 16 16"
xmlns="http://www.w3.org/2000/svg"
focusable="false"
aria-hidden="true"
data-testid="gen-ai-filled"
>
<path
d="m8 4.4 1.018 2.582L11.6 8 9.018 9.018 8 11.6 6.982 9.018 4.4 8l2.582-1.018L8 4.4ZM2.405 2.41l.002-.003.003-.002-.003-.002-.002-.003-.002.003-.003.002.003.002.002.003Z"
className="filled"
/>
</svg>
);
} else {
return icons[name];
}
}
return (
<span {...baseProps} {...labelAttributes} ref={mergedRef} style={inlineStyles}>
{validIcon ? iconMap(name) : undefined}
</span>
);
};
export { InternalIconProps };
export default InternalIcon;