-
Notifications
You must be signed in to change notification settings - Fork 839
/
Copy pathimage.tsx
239 lines (216 loc) Β· 6.49 KB
/
image.tsx
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React, {
FunctionComponent,
HTMLAttributes,
useState,
ReactNode,
} from 'react';
import classNames from 'classnames';
import { CommonProps } from '../common';
import { EuiOverlayMask } from '../overlay_mask';
import { EuiIcon } from '../icon';
import { useEuiI18n } from '../i18n';
import { EuiFocusTrap } from '../focus_trap';
import { keys } from '../../services';
import { useInnerText } from '../inner_text';
type ImageSize = 's' | 'm' | 'l' | 'xl' | 'fullWidth' | 'original';
const sizeToClassNameMap: { [size in ImageSize]: string } = {
s: 'euiImage--small',
m: 'euiImage--medium',
l: 'euiImage--large',
xl: 'euiImage--xlarge',
fullWidth: 'euiImage--fullWidth',
original: '',
};
export const SIZES = Object.keys(sizeToClassNameMap);
type FullScreenIconColor = 'light' | 'dark';
const fullScreenIconColorMap: { [color in FullScreenIconColor]: string } = {
light: 'ghost',
dark: 'default',
};
interface EuiImageProps extends CommonProps, HTMLAttributes<HTMLImageElement> {
/**
* Separate from the caption is a title on the alt tag itself.
* This one is required for accessibility.
*/
alt: string;
/**
* Accepts `s` / `m` / `l` / `xl` / `original` / `fullWidth` / or a CSS size of `number` or `string`.
* `fullWidth` will set the figure to stretch to 100% of its container.
* `string` and `number` types will max both the width or height, whichever is greater.
*/
size?: ImageSize | number | string;
/**
* Changes the color of the icon that floats above the image when it can be clicked to fullscreen.
* The default value of `light` is fine unless your image has a white background, in which case you should change it to `dark`.
*/
fullScreenIconColor?: FullScreenIconColor;
url: string;
/**
* Provides the visible caption to the image
*/
caption?: ReactNode;
/**
* When set to `true` (default) will apply a slight shadow to the image
*/
hasShadow?: boolean;
/**
* When set to `true` will make the image clickable to a larger version
*/
allowFullScreen?: boolean;
}
export const EuiImage: FunctionComponent<EuiImageProps> = ({
className,
url,
size = 'original',
caption,
hasShadow,
allowFullScreen,
fullScreenIconColor = 'light',
alt,
style,
...rest
}) => {
const [isFullScreenActive, setIsFullScreenActive] = useState(false);
const onKeyDown = (event: React.KeyboardEvent) => {
if (event.key === keys.ESCAPE) {
event.preventDefault();
event.stopPropagation();
closeFullScreen();
}
};
const closeFullScreen = () => {
setIsFullScreenActive(false);
};
const openFullScreen = () => {
setIsFullScreenActive(true);
};
const customStyle: React.CSSProperties = { ...style };
let classes = classNames(
'euiImage',
{
'euiImage--hasShadow': hasShadow,
'euiImage--allowFullScreen': allowFullScreen,
},
className
);
if (typeof size === 'string' && SIZES.includes(size)) {
classes = `${classes} ${sizeToClassNameMap[size as ImageSize]}`;
} else {
classes = `${classes}`;
customStyle.maxWidth = size;
customStyle.maxHeight = size;
// Set width back to auto to ensure aspect ratio is kept
customStyle.width = 'auto';
}
const [optionalCaptionRef, optionalCaptionText] = useInnerText();
let optionalCaption;
if (caption) {
optionalCaption = (
<figcaption ref={optionalCaptionRef} className="euiImage__caption">
{caption}
</figcaption>
);
}
const allowFullScreenIcon = (
<EuiIcon
type="fullScreen"
color={fullScreenIconColorMap[fullScreenIconColor]}
className="euiImage__icon"
/>
);
const fullScreenDisplay = (
<EuiOverlayMask
data-test-subj="fullScreenOverlayMask"
onClick={closeFullScreen}>
<EuiFocusTrap clickOutsideDisables={true}>
<figure
className="euiImage euiImage-isFullScreen"
aria-label={optionalCaptionText}>
<button
type="button"
aria-label={useEuiI18n(
'euiImage.closeImage',
'Close full screen {alt} image',
{ alt }
)}
className="euiImage__button"
data-test-subj="deactivateFullScreenButton"
onClick={closeFullScreen}
onKeyDown={onKeyDown}>
<img
src={url}
alt={alt}
className="euiImage-isFullScreen__img"
{...rest}
/>
<EuiIcon
type="cross"
color={fullScreenIconColorMap[fullScreenIconColor]}
className="euiImage-isFullScreen__icon"
/>
</button>
{optionalCaption}
</figure>
</EuiFocusTrap>
</EuiOverlayMask>
);
const fullscreenLabel = useEuiI18n(
'euiImage.openImage',
'Open full screen {alt} image',
{ alt }
);
if (allowFullScreen) {
return (
<figure className={classes} aria-label={optionalCaptionText}>
<button
type="button"
aria-label={fullscreenLabel}
className="euiImage__button"
data-test-subj="activateFullScreenButton"
onClick={openFullScreen}>
<img
src={url}
alt={alt}
className="euiImage__img"
style={customStyle}
{...rest}
/>
{allowFullScreenIcon}
</button>
{isFullScreenActive && fullScreenDisplay}
{optionalCaption}
</figure>
);
} else {
return (
<figure className={classes} aria-label={optionalCaptionText}>
<img
style={customStyle}
src={url}
className="euiImage__img"
alt={alt}
{...rest}
/>
{optionalCaption}
</figure>
);
}
};