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

              
                <div id="app">
  <div class="inner" v-bind:class="{'is-visible': showDetail}" v-bind:style="{'--bg': bg, 'margin-right': -scrollWidth + 'px'}">
    <div class="filter">
      <input type="text" class="filter__input" placeholder="Name or Value" v-model="filterString" @input="filterColorName"/>
      <label class="filter-color">
        <input v-model="pickedColor" type="color" @input="filterColor"/>
        <span class="wheel"></span>     
      </label>
    </div>
    <virtual-scroller
      page-mode
      :items="colorList"
      content-tag="div"
      :item-height="66"
    >
      <template scope="colors">
        <div class="color" v-on:click="showLarge($event, colors.item)" v-bind:class="{'is-dark': isDark(colors.item.rgb)}" :style="{'--color': colors.item.hex}" :key="colors.item.hex">
          <strong data-name>{{colors.item.name}}</strong>
          <span data-hex>{{colors.item.hex}}<i v-if="colors.item.hex == '#f0c'">k</i></span>
        </div>
      </template>
    </virtual-scroller>
    <div class="dialog" ref="dialog">
      <div class="dialog__bg" ref="bg"></div>
      <div class="dialog__header">
        <h1>{{title}}</h1>
        <h2>{{hex}}</h2>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                @import url('https://rsms.me/inter/inter-ui.css');
@import url('https://fonts.googleapis.com/css?family=Space+Mono:400');
$letterspace: -0.02em;
:root {
  --bg: #212121;
}
body {
  overflow: hidden;
}
html, .filter__input {
  font-family: 'Inter UI', sans-serif;
  font-size: 2rem;
  color: #212121;
  background: #212121;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.inner {
  position: absolute;
  padding: 0 .5rem 1rem;
  top: 3.47rem; left: 0; right: 0; bottom: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
}
.filter {
  &::after {
    content: '↓';
    position: absolute;
    top: .76rem; right: .8rem;
    z-index: 2;
    font-size: 1.5rem;
    line-height: 2rem;
    transform: translate(0, 0);
    will-change: transform;
    transition: 200ms transform ease-out;

    .is-visible & {
      transform: translate(0, -30%);
    }
  }
  &__input {
    font-size: 1rem; 
    font-weight: 900; 
    appearance: none;
    -webkit-appearance: none;
    border-radius: 0;
    display: block;
    padding: .5rem 2rem .5rem .75rem;
    box-sizing: border-box;
    width: calc(100% - 90px);
    margin-left: 90px;
    color: #212121;
    background: #fff;
    line-height: 1;
    border: 4px solid #212121;
    letter-spacing: $letterspace;
    will-change: transform;
  }
  
  background: var(--bg);
  position: fixed;
  padding: 0.5rem .5rem;
  top: 0; left: 0; right: 0;
  z-index: 1;
  
  .is-visible & {
    transition: 200ms ease-in;
    transform: translate(0, -200%);
  }
}

.dialog {
  opacity: 0;
  pointer-events: none;
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  z-index: 10;
  overflow: hidden;
  will-change: opacity;
  
  .is-visible & {
    opacity: 1;
    transition: opacity 200ms linear;
  }
  
  &__header {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    padding: 1rem;
    transform: translate(0, 120%);
    font-size: calc(.5rem + 3.75vw);
    overflow: hidden;
    will-change: transform;
    h1 {
      font-weight: 900;
      
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      line-height: 1.2;
      transform: translate(0, 120%); 
      will-change: transform;
      .is-visible & {
        transform: translate(0, 0);
        transition: 300ms transform ease-out 400ms;
      }
    }
    h2 {
      margin-top: .15em;
      font-size: .6em;
      transform: translate(0, 160%);
      will-change: transform;
      .is-visible & {
        transform: translate(0, 0);
        transition: 200ms transform ease-out 600ms;
      }
    }
    .is-visible & {
      transform: translate(0, 0);
      transition: 300ms transform ease-out 333ms;
    }
  }
  &__bg {
    position: absolute;
    left: .5rem; 
    right: .5rem;
  }
}
.color {
  cursor: pointer;
  line-height: 66px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  padding: 0 .5rem;
  height: 66px;
  overflow: hidden;
  box-sizing: border-box;
  color: var(--color);
  transition: 100ms color linear, 100ms background-color linear;
  &:hover {
    color: #000;
    background: var(--color);
  }
  &.is-dark:hover {
    color: #fff;
  }
  
}
strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-right: 0.5rem;
  letter-spacing: $letterspace;
  font-weight: 900;
}
span {
  font-family: 'Space Mono', monospace;
  font-size: .7em;
  //font-feature-settings: 'tnum' 1;
}
.filter-color {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  input {
    display: none;
  }
}
.wheel {
  position: absolute;
  top: .5rem;
  left: .5rem;
  background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
  border-radius:50%;
  height: 70px;
  width: 70px;
  cursor: pointer;
  transform: translate(0, 0);
  will-change: transform;
  transition: 200ms transform ease-out, 200ms box-shadow;
  
    border: 4px solid #212121;
  &:hover, &:focus, &:active {
    box-shadow: inset 0 0 0 .2rem var(--bg);
  }
  .is-visible & {
    transform: translate(0, -30%);
  }
}
              
            
!

JS

              
                Vue.use(VueVirtualScroller);
Vue.component('virtual-scroller', VueVirtualScroller.VirtualScroller);

function rgbToHsv (color) {
    let r = color.r / 255;
    let g = color.g / 255;
    let b = color.b / 255;

    let h,s,min,max,del,dR,dG,dB,hsl;

    hsl = {};

    min = Math.min(r, g, b);
    max = Math.max(r, g, b);
    del = max - min;

    if (del == 0) {
        h = 0;
        s = 0;
    } else {
        s = del / max;

        dR = (((max - r) / 6) + (del / 2)) / del;
        dG = (((max - g) / 6) + (del / 2)) / del;
        dB = (((max - b) / 6) + (del / 2)) / del;

        if (r == max) {
            h = dB - dG;
        } else if (g == max) {
            h = (1 / 3) + dR - dB;
        } else if (b == max) {
            h = (2 / 3) + dG - dR;
        };

        if (h < 0) {
            h++;
        };

        if (h > 1) {
            h--;
        };
    };

    hsl['h'] = h;
    hsl['s'] = s;
    hsl['v'] = 0.9;

    return hsl;
}

function measureScroll () {
  var testDiv, cssAttributes, attr;

  //creates a DOM element
  testDiv = document.createElement('div');

  //stores the CSS atributes
  cssAttributes = {
    width: '100px',
    height: '100px',
    overflow: 'scroll',
    position: 'absolute',
    top: '-999px'
  };

  //sets all the styles on testDiv
  for(attr in cssAttributes){
    testDiv.style[attr] = cssAttributes[attr];
  }

  //adds the testDiv to the DOM
  document.body.appendChild(testDiv);

  //measures the the scrollWidth
  width = testDiv.offsetWidth - testDiv.clientWidth;

  //removes the testDiv from the DOM
  document.body.removeChild(testDiv);

  //returns the width
  return width;
}

const hexToRgb = (hex) => {
  const int = parseInt(hex.replace('#', ''), 16);
  return {
    r: (int >> 16) & 255,
    g: (int >> 8) & 255,
    b: int & 255,
  };
};

const app = new Vue({
  el: '#app',
  data: () => ({
    colors: [{
      hex: '#ffffff', 
      rgb: {r:255,g:255,b:255},
      name: `Loading lots of colors`
    }],
    scrollWidth: measureScroll(),
    similarsLength: 60,
    filterString: '',
    title: 'Title',
    hex: '#f00f00',
    showDetail: false,
    timer: null,
    colorList: [{
      hex: '#ffffff', 
      rgb: {r:255,g:255,b:255},
      name: `Loading lots of colors`
    }],
    pickedColor: '#ffffff',
  }),
  computed: {
    bg: function(){
      return this.isDark(hexToRgb(this.pickedColor)) ? '#fff' : '#212121';
    }
  },
  methods: {
    filterColor: function(e) {
      const colval = hexToRgb(e.target.value);
      this.filterString = e.target.value;
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
      let i = 0;
      const similarColors = [];

      while (i < this.similarsLength) {
        similarColors.push(
          this.colors[
            this.closestColors.get([colval.r,colval.g,colval.b]).index
          ]
        );
        i++;
      }

      this.colorList = similarColors;
      this.closestColors.clearCache();
    },100);
  },
    
  filterColorName: function(e) {
    this.pickedColor = '#ffffff';
    const name = e.target.value.toLowerCase();

    const colors = this.colors.filter(col => {
      return col.name.toLowerCase().indexOf(name) > -1 || col.hex.indexOf(name) > -1;
    });
      
    this.colorList = colors.length ? colors : [{
      hex: '#f0c', 
      rgb: {
        r:255,
        g:255,
        b:255,
      },
      name: `${this.colors.length} colors and you can't find any!`
    }];
      
  },
    
  showLarge: function ($event, color) {
    this.showDetail = !this.showDetail;

    this.title = color.name;
    this.hex = color.hex;
    const rect = $event.target.getBoundingClientRect();

    this.$refs.bg.style.transition = 'none'
    this.$refs.bg.style.top = rect.top + 'px';
    this.$refs.bg.style.height = rect.height + 'px';

    this.$refs.bg.style['background-color'] = color.hex;

    setTimeout(() => {

      this.$refs.bg.style.transition = `
        477ms top cubic-bezier(.2,.4,.2,1.2), 
        477ms left cubic-bezier(.2,.4,.2,1.2), 
        477ms height cubic-bezier(.2,.4,.2,1.2), 
        477ms width cubic-bezier(.2,.4,.2,1.2)
      `
      setTimeout(() => {
        this.$refs.bg.style.top = 0;
        this.$refs.bg.style.left = 0;
        this.$refs.bg.style.height = window.innerHeight + 'px';
        this.$refs.bg.style.width = window.innerWidth + 'px';
      }, 10)
    },10)
  },
  isDark: rgb => Math.round(
      (
        (
          (parseInt(rgb.r) * 299) +
          (parseInt(rgb.g) * 587) +
          (parseInt(rgb.b) * 114)
        ) / 1000
      )
  ) < 125,
  },
});

const xhr = new XMLHttpRequest();
xhr.open('GET', '//color-names.herokuapp.com/v1/');
xhr.onload = e => {
    if (xhr.status === 200) {
      let resp = JSON.parse(xhr.responseText);
      app.colors = resp.colors /*.sort((a,b) => ( a.luminance - b.luminance )).reverse()*/;
      app.closestColors = new Closest(resp.colors.map(c => [c.rgb.r,c.rgb.g, c.rgb.b]), true);
      
      app.colorList = [...app.colors];
      
    } else {
        console.log(xhr.status);
    }
};
xhr.send();

              
            
!
999px

Console