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

[ImageList] Wrap image with a link <a> #22597

Closed
szewah opened this issue Sep 14, 2020 · 6 comments
Closed

[ImageList] Wrap image with a link <a> #22597

szewah opened this issue Sep 14, 2020 · 6 comments
Labels
component: image list This is the name of the generic UI component, not the React module!

Comments

@szewah
Copy link

szewah commented Sep 14, 2020

I want to add a link tag around the images. However, when I resize the browser to greater than 1245px, the images separate.

Here's the sandbox:
https://codesandbox.io/s/thirsty-diffie-3e2fg?file=/src/App.js

Adding screen cap of the original
Screen Shot 2020-09-14 at 12 17 12 PM

I'm using:
@material-ui/core": "^4.11.0"
react: "^16.13.1"

@szewah szewah added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 14, 2020
@alexmotoc
Copy link
Contributor

alexmotoc commented Sep 14, 2020

I bumped into the same issue quite recently. The way I solved it was to add an onClick to the GridListTile and redirect to the desired link that way.

From the official documentation:

Theoretically you can pass any node as children, but the main use case is to pass an img, in which case GridListTile takes care of making the image "cover" available space

@oliviertassinari oliviertassinari added the component: image list This is the name of the generic UI component, not the React module! label Sep 14, 2020
@oliviertassinari oliviertassinari changed the title Images in GridList not resizing properly if wrapped in <a> [ImageList] Wrap image with a link <a> Sep 14, 2020
@oliviertassinari oliviertassinari removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 14, 2020
@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 14, 2020

While we have revamped the component in #22395, the limitation still stands. You need to apply the object-fit style and to grow the image to cover the whole space of the cell:

/* eslint-disable @typescript-eslint/no-use-before-define */
import * as React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import ImageList from '@material-ui/core/ImageList';
import ImageListItem from '@material-ui/core/ImageListItem';

const useStyles = makeStyles({
  a: {
    display: 'block',
    height: '100%',
  },
  img: {
    objectFit: 'cover',
    width: '100%',
    height: '100%',
  },
});

export default function StandardImageList() {
  const classes = useStyles();

  return (
    <ImageList cols={3} rowHeight={164}>
      {itemData.map((item) => (
        <ImageListItem key={item.img}>
          <a className={classes.a} href="#">
            <img
              className={classes.img}
              srcSet={`${item.img}?w=164&h=164&fit=crop&auto=format 1x,
                          ${item.img}?w=164&h=164&fit=crop&auto=format&dpr=2 2x`}
              alt={item.title}
            />
          </a>
        </ImageListItem>
      ))}
    </ImageList>
  );
}

const itemData = [
  {
    img: 'https://images.unsplash.com/photo-1551963831-b3b1ca40c98e',
    title: 'Breakfast',
  },
  {
    img: 'https://images.unsplash.com/photo-1551782450-a2132b4ba21d',
    title: 'Burger',
  },
  {
    img: 'https://images.unsplash.com/photo-1522770179533-24471fcdba45',
    title: 'Camera',
  },
  {
    img: 'https://images.unsplash.com/photo-1444418776041-9c7e33cc5a9c',
    title: 'Coffee',
  },
  {
    img: 'https://images.unsplash.com/photo-1533827432537-70133748f5c8',
    title: 'Hats',
  },
  {
    img: 'https://images.unsplash.com/photo-1558642452-9d2a7deb7f62',
    title: 'Honey',
  },
  {
    img: 'https://images.unsplash.com/photo-1516802273409-68526ee1bdd6',
    title: 'Basketball',
  },
  {
    img: 'https://images.unsplash.com/photo-1518756131217-31eb79b20e8f',
    title: 'Fern',
  },
  {
    img: 'https://images.unsplash.com/photo-1597645587822-e99fa5d45d25',
    title: 'Mushrooms',
  },
  {
    img: 'https://images.unsplash.com/photo-1567306301408-9b74779a11af',
    title: 'Tomato basil',
  },
  {
    img: 'https://images.unsplash.com/photo-1471357674240-e1a485acb3e1',
    title: 'Sea star',
  },
  {
    img: 'https://images.unsplash.com/photo-1589118949245-7d38baf380d6',
    title: 'Bike',
  },
];

@mbrookes I can't think of a better approach. Any idea? Maybe we could close the issue with this propose workaround?

@szewah
Copy link
Author

szewah commented Sep 14, 2020

Thanks @alexmotoc your hack worked.
Thanks @oliviertassinari. Will wait for the updates.

@mbrookes
Copy link
Member

@oliviertassinari We could target any img descendent of ImageListItem, which would remove the need for React.cloneElement, but at the expense of ease of customisation.

@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 16, 2020

@mbrookes the issue is that a developer also needs to change the size of the wrapping element of the image, it's required to have the correct placement. So I don't think that it's enough to solve this specific issue.

Now, maybe using a & > img selector instead of clone element would help for a different type of problem. It could make it easier when wrapping the img into another component without changing the DOM structure. We would keep a flat CSS specificity, so still easy to override.

@oliviertassinari
Copy link
Member

My bad .MuiImageListItem-standard > img has a specificity of two. I think that it would still be better to ask the developers to rewrite the styles than have to fight with CSS specificity. At least, now, they are in control. Considering the initial question was answered, I'm closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: image list This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

4 participants