Skip to content

Commit a781621

Browse files
committed
feat: Enhance addIcon to support svg currentColor
1 parent 3d4e628 commit a781621

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

src/lib/acode.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,14 +629,46 @@ export default class Acode {
629629
return Url.join(...args);
630630
}
631631

632-
addIcon(className, src) {
632+
/**
633+
* Adds a custom icon class that can be used with the .icon element
634+
* @param {string} className - The class name for the icon (used as .icon.className)
635+
* @param {string} src - URL or data URI of the icon image
636+
* @param {object} [options] - Optional settings
637+
* @param {boolean} [options.monochrome=false] - If true, icon will use currentColor and adapt to theme
638+
*/
639+
addIcon(className, src, options = {}) {
633640
let style = document.head.get(`style[icon="${className}"]`);
634641
if (!style) {
635-
style = (
636-
<style
637-
icon={className}
638-
>{`.icon.${className}{background-image: url(${src})}`}</style>
639-
);
642+
let css;
643+
if (options.monochrome) {
644+
// Monochrome icons: use mask-image (on ::before) for currentColor/theme support
645+
// Using ::before ensures we don't mask the ::after active indicator or the background
646+
css = `.icon.${className}::before {
647+
content: '';
648+
display: inline-block;
649+
width: 24px;
650+
height: 24px;
651+
vertical-align: middle;
652+
-webkit-mask-image: url(${src});
653+
mask-image: url(${src});
654+
-webkit-mask-size: contain;
655+
mask-size: contain;
656+
-webkit-mask-repeat: no-repeat;
657+
mask-repeat: no-repeat;
658+
-webkit-mask-position: center;
659+
mask-position: center;
660+
background-color: currentColor;
661+
}`;
662+
} else {
663+
// Default: preserve original icon colors
664+
css = `.icon.${className}{
665+
background-image: url(${src});
666+
background-size: 24px;
667+
background-repeat: no-repeat;
668+
background-position: center;
669+
}`;
670+
}
671+
style = <style icon={className}>{css}</style>;
640672
document.head.appendChild(style);
641673
}
642674
}

0 commit comments

Comments
 (0)