Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The icon is not attached to the selected item

I have a customised combo box component, i wanted to add style to the highlighted item and the selected item. I want to add beside the selected one but it is always showing to the first item, what is wrong here?
enter image description here

.combobox-option {
  padding: 0.25em 0.5em;
  cursor: pointer;

  &.selected {
    background-color: red;
    color: var(--color-white);

    &::after {
      content: " ";
      cursor: pointer;
      display: block;
      position: absolute;
      top: 0;
      right: 0;
      width: 25px;
      height: 25px;
      background-image: url(../check.svg);
      background-repeat: no-repeat;
      background-position: right;
      background-size: contain;
    }
  }
  &.highlighted {
    background-color: var(--color-bg-blue);
    color: var(--color-white);
  }
}
<li
    tabIndex={0}
    className={clsx(
       "combobox-option",
        option.value === search && "selected",
        index === highlightedIndex && "highlighted", 
     )}
     key={option.label}
     onClick={() => {
         optionSelected(index);
         setIsOpen(false);
      }}
>
     <span>{option.value}</span>
</li>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You seem to be missing a relatively positioned ancestor of the pseudo element. Most likely you should add position: relative to the .combobox-option class description — thus the CSS would know in relation to which element it should position your checkmark icon. Otherwise it would stick to the closest element with a position different from default static — or, if none is present on a page, to the very top of the document. Hope it helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading