JSFiddle - React, Tailwind, and code Playground

by Michael Prosser

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

CSS

html{
  height: 100%;
}
body{
  margin: 30px;
  background-color: #eee;
  height: 100%;
}


.docubin-icon-selection-component{
  display: inline-block;
  width: 32px;
  height: 32px;
  position: relative;
}
.docubin-icon-selection-component>img{
  max-width: calc(100% - 8px);
  max-height: calc(100% - 8px);
  transition: all 0.5s;
  cursor: pointer;
  background-color: #fff;
  padding: 4px;
  border-radius: 2px;
  border: solid 0.5px #666;
}
.docubin-icon-selection-component.focus>img{
  transform: scale(2);
}
.docubin-icon-selection-component>ul{
  display: block;
  visibility: none;
  opacity: 0;
  transition: all 0.6s;
  position: absolute;
  left: 200%;
  top: -17px;
  width: 180px;
  padding: 5px;
  margin: 0;
  list-style: none;
  border: solid 2px #000;
  background-color: #ddd;
  max-height: 190px;
  overflow: auto;
  border-radius: 4px;
  border: solid 1px #666;
}
.docubin-icon-selection-component>ul>li{
  margin: 0;
  width: calc(33.33% - 12px);
  display: inline-block;
  background-color: #fff;
  border-radius: 4px;
  padding:3px;
  margin: 3px;
  padding-bottom: 0px;
  position: relative;
  overflow: hidden;
  cursor: pointer;
}
.docubin-icon-selection-component>ul>li.selected:before{
  content: "SELECTED";
    display: block;
    font-family: helvetica;
    font-size: 6px;
    background-color: green;
    color: #fff;
    position: absolute;
    bottom: 14px;
    right: -28px;
    width: 90px;
    text-align: center;
    transform: rotate(-45deg);
    padding: 3px;
}
.docubin-icon-selection-component>ul>li>img{
  width: 100%;
}
.docubin-icon-selection-component.focus>ul{
   visibility: visible;
    opacity: 1;
}

JavaScript

class DocubinIconSelectionComponent {

	constructor( parent, property, libraryIcons, onchange ){
  
  	this.parent = parent;
    this.property = property;
    this.libraryIcons = libraryIcons;
    this.onchange = onchange;
    
    this.buildComponent();
  
  }
  
  buildComponent(){
  
  	this.component = $('<div class="docubin-icon-selection-component"><img src="' + this.property + '"><ul></ul></div>');
    
   	let menu = this.component.find('>ul');
    
    for(let i=0;i<this.libraryIcons.length;i++){
    
    	let item = $('</li><li><img src="' + this.libraryIcons[i] + '"></li>');
      
      if(this.property == this.libraryIcons[i]){
      
      	item.addClass('selected');
      
      }
      
      item.click((e) => {
      
      	e.stopPropagation();
        
        menu.find('>li').removeClass('selected');
        
        item.addClass('selected');
        
        this.component.find('>img').attr('src',item.find('>img').attr('src'));
        
        this.property = item.find('>img').attr('src');
        
        this.component.removeClass('focus');
        
        if(typeof(this.onchange) == 'function'){
        
        	this.onchange(this.property);
        
        }
      
      });
      
    	menu.append(item);
    
    }
    
    this.component.find('>img').click((e) => {
    
   		e.stopPropagation();
      
      this.component.toggleClass('focus');
    
    })
    
    this.parent.append(this.component);
    
    $('body').click(() => {
    
    	this.component.removeClass('focus');
    
    });
  
  }

}

let libraryIcons =...