Skip to content

Commit

Permalink
Add. SVG sprite support for styled option.
Browse files Browse the repository at this point in the history
  • Loading branch information
afonja14755 committed May 30, 2024
1 parent eef8ef6 commit 6f10f18
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 136 deletions.
271 changes: 140 additions & 131 deletions dist/js/ux-select.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/ux-select.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/ux-select.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/ux-select.iife.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ux-select",
"version": "2.2.0",
"version": "2.3.0",
"description": "UX Select is a lightweight Native JavaScript plugin that replace native select elements with customization",
"keywords": [
"web",
Expand Down
5 changes: 5 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export type UxSelectOptions = {
width: number;
height: number;
};
svg?: {
src: string;
width: number;
height: number;
};
element: HTMLOptionElement;
uxOption: HTMLLIElement | undefined;
};
Expand Down
25 changes: 24 additions & 1 deletion src/ux-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export default class UxSelect {
uxOption = this.uxEl.querySelector(`.ux-select-group__elem[data-value='${option.value}']`);
}

let optionImage = undefined;
let optionImage = undefined,
optionSvg = undefined;
if (this.config.optionStyle === 'image' && option.dataset.imageSrc) {
optionImage = {
src: option.dataset.imageSrc,
Expand All @@ -100,6 +101,12 @@ export default class UxSelect {
width: option.dataset.imageWidth ? Number(option.dataset.imageWidth) : 24,
height: option.dataset.imageHeight ? Number(option.dataset.imageHeight) : 24,
};
} else if (this.config.optionStyle === 'image' && option.dataset.svgSrc) {
optionSvg = {
src: option.dataset.svgSrc,
width: option.dataset.svgWidth ? Number(option.dataset.svgWidth) : 24,
height: option.dataset.svgHeight ? Number(option.dataset.svgHeight) : 24,
};
}

optionsData.push(<UxSelectOptions>{
Expand All @@ -113,6 +120,7 @@ export default class UxSelect {
value: option.value,
},
image: optionImage,
svg: optionSvg,
element: option,
uxOption,
});
Expand Down Expand Up @@ -242,6 +250,21 @@ export default class UxSelect {
selectListElem.appendChild(optionImageElem);
}

if (this.config.optionStyle === 'image' && option.svg) {
const optionSvgElem = document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
optionUseElem = document.createElementNS('http://www.w3.org/2000/svg', 'use');

optionSvgElem.classList.add('ux-select-group-elem__image');
optionSvgElem.setAttribute('viewBox', `0 0 ${String(option.svg.width)} ${String(option.svg.height)}`);
optionSvgElem.setAttribute('width', String(option.svg.width));
optionSvgElem.setAttribute('height', String(option.svg.height));
optionUseElem.setAttribute('href', option.svg.src);

optionSvgElem.appendChild(optionUseElem);

selectListElem.appendChild(optionSvgElem);
}

selectListElem.addEventListener('click', this.onClickOption.bind(this));

optionsFragmentsByGroup[option.attributes.group].appendChild(selectListElem);
Expand Down

0 comments on commit 6f10f18

Please sign in to comment.