2

I wanted to create a masonry like layout where when you click on an div then it resizes to full screen or at least become bigger than the other divs. Something like pinterest, where when you click on an image then that image becomes bigger. but in my code, I've been able to generate random height and color for the divs but the widths of the divs are too small and when I click on it then it doesn't scale to 100% of the screen. I also want them to be toggleable, where if I click on a fullscreen div again then, it comes back to it's normal size or the random height that will be generated. what am I doing wrong in the code?

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.addActiveClass = this.addActiveClass.bind(this);
    this.state = {
      activeIndex: -1
    };
  }
  addActiveClass(activeIndex) {
    this.setState(prev => ({
      activeIndex: prev.activeIndex === activeIndex ? -1 : activeIndex
    }));
  }

  render() {
    return (
      <div className="container">
        {Array.from({ length: 30 }).map((item, index) => {
          return (
            <div
              key={index}
              style={{
                background: randomColor(colors),
                height: randomHeight(100, 200)
              }}
              className={this.state.activeIndex === index ? "full" : ""}
              onClick={() => this.addActiveClass(index)}
            />
          );
        })}
      </div>
    );
  }
}
* {
  box-sizing: border-box;
}

body {
  margin: 40px;
  background-color: #fff;
  color: #444;
  font: 2em Sansita, sans-serif;
}

.container {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  max-height: 100vh;
  align-items: flex-start
}

.container > * {
  border: 2px solid orange;
  border-radius: 5px;
  margin: 10px;
  padding: 10px 20px;
  background-color: red;
  color: #fff;
}

.full{
  width: 100%;
  height: 100%;
  transition: 2s;
}


sandbox

2 Answers2

3

There might be an easier way that is already provided by the vendor. If you want to apply your custom css then you could make the element fixed when they should be full screen. Something like that:

.full {
    position: fixed;
    left: 0px;
    top: 0px;
    bottom: 0px;
    right: 0px;
    margin: 0px;
    height: 100vh !important;
    transition: 2s;
}

The height has to be overridden like this otherwise it won't work. You can make the rule more specific then you might be able to remove !important. But in this case, it should not matter too much anyway.

EDIT

To set a specific height/width for the tiles one way would be like this:

.container > * {
  width: 100px !important;
  height: 100px !important;
  ...

There might be a js-config somewhere where you can set min-height/min-width or the like with javascript. I don't know the library, therefore I would have to google it myself.

F. Müller
  • 3,969
  • 8
  • 38
  • 49
2

I believe that this tutorial covers completely what you want to accomplish: https://janosh.dev/blog/react-hooks-masonry

In your case you just have to expand to 100% width/height when clicked instead of the weird toggle that is implemented

Tasos
  • 1,880
  • 13
  • 19
  • it shows the tutorial with predefined height and colors for the tiles. can you show me how do I do it with random height and color please? – helpmepiliizzz Jul 23 '20 at 15:00
  • I've created a sandbox https://codesandbox.io/s/red-hooks-delhn?file=/src/App.js – helpmepiliizzz Jul 23 '20 at 15:08
  • 1
    You can add a function when you generate your array (or get it from an api) that randomises the colours and minHeights. Here is such a function for colors: https://stackoverflow.com/a/1484514/5605822 In the same way you can generate random heights – Tasos Jul 23 '20 at 15:15
  • I did everything as the website said but I'm getting `_hooks.useEventListener is not a function` error. can you have a look at the sandbox and figure out why it says so – helpmepiliizzz Jul 23 '20 at 15:18