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 all 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
Expand Up @@ -4,6 +4,7 @@
- Added `Enter` key press functionality to `EuiSuperDatePicker` ([#3048](https://github.com/elastic/eui/pull/3048))
- Added `title` to headers of `EuiTable` in case of truncation ([#3094](https://github.com/elastic/eui/pull/3094))
- Added i18n to `EuiTableHeaderCell` ([#3094](https://github.com/elastic/eui/pull/3094))
- Added `number` and `string` to `size` type of `EuiImage` for setting custom sizes ([#3012](https://github.com/elastic/eui/pull/3012))

**Bug Fixes**

Expand Down
45 changes: 10 additions & 35 deletions src-docs/src/views/image/image_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const imageZoomSource = require('!!raw-loader!./image_zoom');
const imageZoomHtml = renderToHtml(ImageZoom);
const imageZoomSnippet = `<EuiImage
allowFullScreen
alt={description}
alt={description}
url={someUrl}
/>
`;
Expand All @@ -53,39 +53,8 @@ export const ImageExample = {
<div>
<p>
Use <EuiCode>EuiImage</EuiCode> when you need to place a static
image into a page with an optional caption. It has the following
props.
image into a page with an optional caption.
</p>
<ul>
<li>
<EuiCode>size</EuiCode> accepts{' '}
<EuiCode>s / m / l / xl / original / fullWidth</EuiCode>. The
latter will set the figure to stretch to 100% of its container.
</li>
<li>
<EuiCode>allowFullScreen</EuiCode> when set to true will make the
image clickable to a larger version.
</li>
<li>
<EuiCode>fullScreenIconColor</EuiCode> allows you to change the
color of the icon that floats above the image when it can be
clicked to fullscreen. The default value of{' '}
<EuiCode>light</EuiCode> is fine unless your image has a white
background, in which case you should change it to{' '}
<EuiCode>dark</EuiCode>.
</li>
<li>
<EuiCode>hasShadow</EuiCode> when set to true (default) will apply
a slight shadow below the image.
</li>
<li>
<EuiCode>caption</EuiCode> will provide a caption to the image.
</li>
<li>
<EuiCode>alt</EuiCode> Sepearate from the caption is a title on
the alt tag itself. This one is required for accessibility.
</li>
</ul>
</div>
),
props: { EuiImage },
Expand Down Expand Up @@ -131,8 +100,14 @@ export const ImageExample = {
text: (
<p>
Images can be sized by passing the <EuiCode>size</EuiCode> prop a
value of <EuiCode>s / m / l / xl / original / fullWidth</EuiCode>.
Note that this size is applied to the width of the image.
value of{' '}
<EuiCode>
s / m / l / xl / original / fullWidth / number / string
</EuiCode>
. This size sets the <strong>maximum</strong> length of the longest
edge of the image, whether that is height or width, and scales it.
Only the provided sizing values will also increase the size of a
smaller image.
</p>
),
demo: <ImageSizes />,
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 (50)"
alt="Accessible image alt goes here"
url="https://source.unsplash.com/1000x1000/?Nature"
/>
<EuiSpacer />
<EuiImage
size="s"
hasShadow
Expand Down
19 changes: 17 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 All @@ -38,3 +38,18 @@ exports[`EuiImage is rendered and allows full screen 1`] = `
</button>
</figure>
`;

exports[`EuiImage is rendered with custom size 1`] = `
<figure
class="euiImage testClass1 testClass2"
>
<img
alt="alt"
aria-label="aria-label"
class="euiImage__img"
data-test-subj="test subject string"
src="/cat.jpg"
style="max-width:50px;max-height:50px;width:auto"
/>
</figure>
`;
1 change: 1 addition & 0 deletions src/components/image/_image.scss
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@
transform: translateY(0);
}
}

8 changes: 8 additions & 0 deletions src/components/image/image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ describe('EuiImage', () => {

expect(component).toMatchSnapshot();
});

test('is rendered with custom size', () => {
const component = render(
<EuiImage alt="alt" size={50} url="/cat.jpg" {...requiredProps} />
);

expect(component).toMatchSnapshot();
});
});
57 changes: 51 additions & 6 deletions src/components/image/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,35 @@ const fullScreenIconColorMap: { [color in FullScreenIconColor]: string } = {
dark: 'default',
};

interface EuiImageProps extends CommonProps, HTMLAttributes<HTMLElement> {
interface EuiImageProps extends CommonProps, HTMLAttributes<HTMLImageElement> {
/**
* Sepearate from the caption is a title on the alt tag itself.
* This one is required for accessibility.
*/
alt: string;
size?: ImageSize;
/**
* 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?: string;
/**
* 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;
}

Expand Down Expand Up @@ -81,21 +103,32 @@ export class EuiImage extends Component<EuiImageProps, State> {
allowFullScreen,
fullScreenIconColor = 'light',
alt,
style,
...rest
} = this.props;

const { isFullScreenActive } = this.state;
const customStyle: React.CSSProperties = { ...style };

const classes = classNames(
let classes = classNames(
'euiImage',
sizeToClassNameMap[size],
{
'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';
}

let optionalCaption;
if (caption) {
optionalCaption = (
Expand Down Expand Up @@ -161,7 +194,13 @@ 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
src={url}
alt={alt}
className="euiImage__img"
style={customStyle}
{...rest}
/>
{allowFullScreenIcon}
{isFullScreenActive && fullScreenDisplay}
</button>
Expand All @@ -173,7 +212,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}
src={url}
className="euiImage__img"
alt={alt}
{...rest}
/>
{optionalCaption}
</figure>
);
Expand Down