Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                
              
            
!

CSS

              
                .color-list__item {
  margin: 0.25rem; /* We need to know the margin size in the JS file */
}

color-list.css hosted with ❤ by GitHub
.color-swatch {
  width: 10rem; /* We need to know this width in the JS file */
}
              
            
!

JS

              
                /** 
 * Determines how many items can fit on one line, 
 * given the line width and item width.
 */
const getMaxItemsPerLine = (lineWidth, itemSize) =>
  Math.floor(lineWidth / itemSize);

/**
 * Determines if the item should be hidden, given
 * the items index and the max number of items allowed
 * on one line.
 */
const isItemHidden = (maxItemsPerLine, itemIndex) =>
  itemIndex > maxItemsPerLine - 1;

export const ColorList = ({ colorSwatches, handleClick }) => {
  const [listWidth, setListWidth] = React.useState(-1);
  
  // Here are the values from our CSS files
  const colorSwatchSize = 160; // px, equivalent to the 10rem width assuming our root font-size is 16px
  const listItemMargin = 4; // px, equivalent to the 0.25rem width assuming our root font-size is 16px
  
  const colorSwatchTotalWidth = colorSwatchSize + 2 * listItemMargin;
  const maxItemsPerLine = getMaxItemsPerLine(listWidth, colorSwatchTotalWidth);

  return (
    <div className="color-list__container">
      <Measure
        bounds
        onResize={contentRect => setListWidth(contentRect.bounds.width)}
      >
        {({ measureRef }) => (
          <ul ref={measureRef} className="color-list">
            {colorSwatches.map((colorInfo, i) => {
              const hidden = isItemHidden(maxItemsPerLine, i); {/* Item will be hidden if doesn't fit on the first line */}
              return (
                <li key={i} className="color-list__item" hidden={hidden}>
                  <ColorSwatch
                    element="button"
                    {...colorInfo}
                    onClick={handleClick(colorInfo)}
                    disabled={hidden}
                  />
                </li>
              );
            })}
          </ul>
        )}
      </Measure>
    </div>
  );
};
              
            
!
999px

Console