Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow EuiImage to set custom width #3012

Merged
merged 26 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fd10148
allow EuiImage to set custom width
anishagg17 Mar 9, 2020
3b1522e
Update CHANGELOG.md
anishagg17 Mar 9, 2020
21bb0ef
Merge branch 'master' into test2
anishagg17 Mar 10, 2020
119f8b9
Merge branch 'master' into test2
anishagg17 Mar 11, 2020
f5c334c
Merge branch 'master' into test2
anishagg17 Mar 12, 2020
f4247c7
greater of height or width gets size ,lesser is set to auto
anishagg17 Mar 14, 2020
ba123dd
checked for both heaight and width
anishagg17 Mar 16, 2020
d4bf580
Merge branch 'master' into test2
anishagg17 Mar 16, 2020
01cedb4
Merge branch 'master' into test2
anishagg17 Mar 17, 2020
33cdb73
removed uneccessary className
anishagg17 Mar 17, 2020
7e60550
Merge branch 'test2' of https://github.com/anishagg17/eui into test2
anishagg17 Mar 17, 2020
bfa8665
Merge branch 'master' into test2
cchaos Mar 17, 2020
a7a510f
used maxWidth,maxHeight
anishagg17 Mar 18, 2020
3d66262
removed class
anishagg17 Mar 18, 2020
d54ab10
Merge branch 'test2' of https://github.com/anishagg17/eui into test2
anishagg17 Mar 18, 2020
5dd124a
added test for custom size
anishagg17 Mar 18, 2020
f5a263c
Pushing some extra help (#3)
cchaos Mar 18, 2020
f86d126
maintain the aspect ratio
anishagg17 Mar 18, 2020
fbb0bd7
Update src/components/image/image.tsx
anishagg17 Mar 19, 2020
174ed01
Update CHANGELOG.md
anishagg17 Mar 19, 2020
d8f6fcc
Update src-docs/src/views/image/image_example.js
anishagg17 Mar 19, 2020
84b2c92
Update src-docs/src/views/image/image_example.js
anishagg17 Mar 19, 2020
f8a2521
Update src/components/image/image.tsx
anishagg17 Mar 19, 2020
280a1f5
updated snap
anishagg17 Mar 19, 2020
a1d309f
Merge branch 'master' into test2
cchaos Mar 19, 2020
0c565aa
fixed prettier
anishagg17 Mar 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Allow `EuiImage` to set custom width ([#3012](https://github.com/elastic/eui/pull/3012))
anishagg17 marked this conversation as resolved.
Show resolved Hide resolved
- Updated `EuiFilterSelect` to retain the order of its filters ([#3063](https://github.com/elastic/eui/pull/3063))
- Added `href` prop to `EuiBadge` ([#3009](https://github.com/elastic/eui/pull/3009))
- Added props descriptions for `EuiComboBox` ([#3007](https://github.com/elastic/eui/pull/3007))
Expand Down
9 changes: 9 additions & 0 deletions src-docs/src/views/image/image_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { EuiImage, EuiSpacer } from '../../../../src/components';

export default () => (
<div>
<EuiImage
hasShadow
allowFullScreen
size={50}
caption="Custom size( 50px )"
alt="Accessible image alt goes here"
url="https://source.unsplash.com/1000x1000/?Nature"
/>
<EuiSpacer />
<EuiImage
size="s"
hasShadow
Expand Down
4 changes: 2 additions & 2 deletions src/components/image/__snapshots__/image.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`EuiImage is rendered 1`] = `
<figure
class="euiImage euiImage--large testClass1 testClass2"
class="euiImage testClass1 testClass2 euiImage--large"
>
<img
alt="alt"
Expand All @@ -16,7 +16,7 @@ exports[`EuiImage is rendered 1`] = `

exports[`EuiImage is rendered and allows full screen 1`] = `
<figure
class="euiImage euiImage--large euiImage--allowFullScreen testClass1 testClass2"
class="euiImage euiImage--allowFullScreen testClass1 testClass2 euiImage--large"
>
<button
aria-label="Open full screen alt image"
Expand Down
13 changes: 13 additions & 0 deletions src/components/image/_image.scss
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@
}
}

.euiImage--restrictHeight-custom {
width: auto;
height: auto;
margin-left: auto;
margin-right: auto;
anishagg17 marked this conversation as resolved.
Show resolved Hide resolved

img {
anishagg17 marked this conversation as resolved.
Show resolved Hide resolved
width: auto;
height: auto;
}
}

@keyframes euiImageFullScreen {
0% {
opacity: 0;
Expand All @@ -135,3 +147,4 @@
transform: translateY(0);
}
}

62 changes: 56 additions & 6 deletions src/components/image/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const fullScreenIconColorMap: { [color in FullScreenIconColor]: string } = {

interface EuiImageProps extends CommonProps, HTMLAttributes<HTMLElement> {
alt: string;
size?: ImageSize;
size?: ImageSize | number;
fullScreenIconColor?: FullScreenIconColor;
url: string;
caption?: string;
Expand All @@ -44,13 +44,45 @@ interface EuiImageProps extends CommonProps, HTMLAttributes<HTMLElement> {

interface State {
isFullScreenActive: boolean;
customStyle?: {
width?: number | 'auto';
height?: number | 'auto';
};
}

export class EuiImage extends Component<EuiImageProps, State> {
state: State = {
isFullScreenActive: false,
customStyle: {},
};

containerRef: React.RefObject<HTMLImageElement> = React.createRef();
getRectsInterval?: NodeJS.Timer = undefined;

componentDidMount = () => {
if (this.props.size && typeof this.props.size !== 'string') {
if (!this.containerRef || !this.containerRef.current) return;

this.getRectsInterval = setInterval(() => {
let width: 'auto' | number = this.containerRef.current!.width;
let height: 'auto' | number = this.containerRef.current!.height;

if (width > height) {
width = this.props.size as number;
height = 'auto';
} else {
height = this.props.size as number;
width = 'auto';
}
this.setState({ customStyle: { width, height } });
}, 10);
}
};

componentWillUnmount() {
if (this.getRectsInterval) clearInterval(this.getRectsInterval);
}

onKeyDown = (event: React.KeyboardEvent) => {
if (event.keyCode === keyCodes.ESCAPE) {
event.preventDefault();
Expand Down Expand Up @@ -84,18 +116,23 @@ export class EuiImage extends Component<EuiImageProps, State> {
...rest
} = this.props;

const { isFullScreenActive } = this.state;
const { isFullScreenActive, customStyle } = this.state;

const classes = classNames(
let classes = classNames(
'euiImage',
sizeToClassNameMap[size],
{
'euiImage--hasShadow': hasShadow,
'euiImage--allowFullScreen': allowFullScreen,
},
className
);

if (typeof size === 'string') {
classes = `${classes} ${sizeToClassNameMap[size]}`;
} else {
classes = `${classes} euiImage--restrictHeight-custom`;
}

let optionalCaption;
if (caption) {
optionalCaption = (
Expand Down Expand Up @@ -161,7 +198,14 @@ export class EuiImage extends Component<EuiImageProps, State> {
aria-label={openImage}
className="euiImage__button"
onClick={this.openFullScreen}>
<img src={url} alt={alt} className="euiImage__img" {...rest} />
<img
ref={this.containerRef}
src={url}
alt={alt}
className="euiImage__img"
style={customStyle as React.CSSProperties}
{...rest}
/>
{allowFullScreenIcon}
{isFullScreenActive && fullScreenDisplay}
</button>
Expand All @@ -173,7 +217,13 @@ export class EuiImage extends Component<EuiImageProps, State> {
} else {
return (
<figure className={classes} aria-label={caption}>
<img src={url} className="euiImage__img" alt={alt} {...rest} />
<img
style={customStyle as React.CSSProperties}
src={url}
className="euiImage__img"
alt={alt}
{...rest}
/>
{optionalCaption}
</figure>
);
Expand Down