Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #root.container
              
            
!

CSS

              
                *
  box-sizing: border-box

img
  width: 100%
  height: auto

figure
  display: block
  margin: 0
  padding: 0

.flex
  display: flex

.flex-item
  padding: 0 .5em
  width: 20%

.tag
  display: inline-block
  color: #424242
  font-size: 12px
  background: #e3e2e3
  border-radius: .25em
  padding: .2em .5em

.tag-list
  margin: 0 -.25em
  .tag
    margin: 0 .25em .5em
 
.card
  border: 1px solid #bcbdc0
  background: #fff

.card-cover
  max-height: 12em
  overflow: hidden

.card-body
  padding: 0 .5em .5em
  
              
            
!

JS

              
                const useState    = React.useState;
const useEffect   = React.useEffect;
const useRef      = React.useRef;
const createRef   = React.createRef;
const useCallback = React.useCallback;
const useMemo     = React.useMemo;

const itemProps = (i) => {
  const rand = Math.floor(Math.random() * 12) + 3;
  return {
    id: i,
    images: `http://loremflickr.com/640/360/cat,animal?random=${i}`,
    title: `title-${i+1}`,
    tags: [...Array(rand)].map((_, i) => {
      return `TAG-${('00' + (i + 1)).slice(-2)}`;
    }),
  };
};

const Item = React.forwardRef(({images, title, tags}, ref) => {
  const tagList = useMemo(() => {
    return tags.map((item) => {
      return <span className="tag">{item}</span>;
    })
  }, [tags]);
  
  return (
    <div className="flex-item">
      <div ref={ref} className="card">
        <figure className="card-cover">
          <img src={images} />
        </figure>
        <div class="card-body">
          <span>{title}</span>
          <div className="tag-list">
            {tagList}
          </div>
        </div>
      </div>
    </div>
  );
});

const useDebounceFn = (fn, delay = 100) => {
  const timer = useRef(null);
  
  const dispatch = useCallback((_val) => {
    timer.current && clearTimeout(timer.current);
    timer.current = setTimeout(() => {
      fn(_val);
    }, delay);
  }, [fn, delay, timer]);
  
  return [dispatch];
};

function App({ initItems }) {
  const [items, setItems] = useState(initItems);
  console.log( items );
  const itemRefs = useRef(
    [...Array(items.length)].map(() => createRef())
  );
  
  const getMaxHeight = useCallback((refList) => {
    return refList.reduce((maxHeight, ref) => {
      if ( !ref.current ) { return maxHeight; }
      const itemHeight = ref.current.offsetHeight;
      return Math.max(itemHeight, maxHeight);
    }, 0);
  }, []);
  
  const setMaxHeight = useCallback(() => {
    itemRefs.current.forEach((ref) => {
      if (ref.current) {
        ref.current.removeAttribute('style');
      }
    });
    
    const max = getMaxHeight(itemRefs.current);
    
    if ( max ) {
      itemRefs.current.forEach((ref) => {
        ref.current.style.height = `${max}px`;
      });
    }
  }, [itemRefs]);
  
  const [onResizeHandler] = useDebounceFn(setMaxHeight);
  
  useEffect(() => {
    window.addEventListener('resize', onResizeHandler);
    setTimeout(() => {
      setMaxHeight();
    }, 1000);
    return () => window.removeEventListener('resize', onResizeHandler);
  }, []);
  
  const itemsDOM = items.map(({id, ...props}, i) => {
    return (
      <Item
        key={id}
        ref={itemRefs.current[i]}
        {...props}
      />
    );
  });
  
  return (
    <div className="app">
      <div className="flex">
        {itemsDOM}
      </div>
    </div>
  );
}

const initItems = Array.from(Array(5), (_, i) => itemProps(i));
console.log('initItems', initItems);
ReactDOM.render(<App initItems={initItems} />, document.getElementById('root'));
              
            
!
999px

Console