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

Responsive Image Component #992

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions packages/core/src/ResponsiveImage/ResponsiveImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//This component can be used as <ResponsiveImage props/>
/* props consist of:
_______________________
| 1 | type | ds, dr |
| 2 | small | <url> |
| 3 | medium | <url> |
| 4 | large | <url> |
| 5 | xlarge | <url> |
_______________________

*/
const ResponsiveImage = (props) => {

//this object comprises of css styles that can be used as an inline style object for 'img' tag
const mystyle = {
maxWidth: "100%", //when this component is used within a parent div, maxWidth as 100% will not allow the image to overflow
height: "auto" //height auto will make sure that the height of the image is automatically adjusted in accordance with the width of it
};

//there are two types, 'dr' and 'ds' which stands for different sizes and different resolutions, respectively.
if(props.type === "ds"){

return (
//three different sizes are provided: 300w, 768w, 1280w and 3000w for 'small', 'medium', 'large' and 'xlarge' images respectively
//user needs to provide three types of images: small, medium and large which will be displayed depending on the size of the viewport
<div>
<img
src={props.small}
srcSet={`${props.small} 300w, ${props.medium} 768w, ${props.large} 1280w, ${props.xlarge} 3000w`}
style={mystyle}
/>
</div>
);
}

//for using different resolutions
else if(props.type === "dr"){

return (
//similarly for different resolutions, there are three different scales: 1x, 1.5x,2x and 2.5x for 'small', 'medium', 'large' and 'xlarge' respectively
<div>
<img
src={props.small}
srcSet={`${props.small} 1x, ${props.medium} 1.5x, ${props.large} 2x, ${props.xlarge} 2.5x`}
style={mystyle}
/>
</div>
);
}
}

export default ResponsiveImage
1 change: 1 addition & 0 deletions packages/core/src/ResponsiveImage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as ResponsiveImage } from './ResponsiveImage'
2 changes: 1 addition & 1 deletion packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export { Tooltip } from './Tooltip'
export { ToggleBadge } from './ToggleBadge'
export { Truncate } from './Truncate'
export { Checkbox } from './Checkbox'

export { ResponsiveImage } from './ResponsiveImage'
export { theme } from './theme'
export { ThemeProvider } from './ThemeProvider'

Expand Down