Skip to content

feat(ui5-icon): display custom SVG, defined as JSX template #11966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ilhan007
Copy link
Member

@ilhan007 ilhan007 commented Jul 22, 2025

In case you need to use a fully custom SVG, that can be used ui5-icon, ui5-button or any component that offers API to display an icon via icon name, you can provide a custom JSX template, rendering the custom SVG and register it under a custom name.

Previously, one had to provide Handlebars template (compiled to lit) - this is deprecated, but still works for compatibility. However, it's strongly recommended to use JSX templates, instead of Handlebars as at some point this will be removed.

How it works

1. Create JSX template

First, create a JSX template for the icon you need:

// MyPensilSVGTemplate.tsx
export default function MyPensilSVGTemplate() {
    return (
        <svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
            <g clip-path="url(#clip0_2221_23716)"><path d="M11.3333 1.99998C11.503 1.79933 11.7131 1.63601 11.9499 1.52043C12.1868 1.40485 12.4453 1.33953 12.709 1.32865C12.9727 1.31777 13.2358 1.36156 13.4815 1.45723C13.7272 1.55291 13.9502 1.69836 14.1361 1.88432C14.3221 2.07029 14.467 2.29268 14.5616 2.53734C14.6562 2.78199 14.6985 3.04353 14.6857 3.3053C14.6728 3.56706 14.6052 3.8233 14.4872 4.05769C14.3691 4.29207 14.2032 4.49947 13.9999 4.66664L4.99992 13.6666L1.33325 14.6666L2.33325 11L11.3333 1.99998Z" stroke-linecap="round" stroke-linejoin="round"/><path d="M10 3.33331L12.6667 5.99998" stroke-linecap="round" stroke-linejoin="round"/></g>
            <defs>
                <clipPath id="clip0_2221_23716"><rect width="16" height="16"/></clipPath>
            </defs>
        </svg>
    )
};

2. Register the Custom Icon

You can use the registerIcon to register the custom icon as follows:

import { registerIcon } from "@ui5/webcomponents-base/dist/asset-registries/Icons.js";
import myPensilSVGTemplate from "./MyPensilSVGTemplate.js";

// create the icon data for registration
const iconPensil = {
    customTemplate: myPensilSVGTemplate,
    collection: "custom",
    viewBox: "0 0 24 24", // optional
}

// register the icon
registerIcon("pensil", iconPensil);

3. Use the Custom Icon

Finally, the icon can be used anywhere.

<ui5-icon name="custom/pensil"></ui5-icon>
<ui5-button icon="custom/pensil"></ui5-button>
<ui5-avatar icon="custom/pensil" size="XS"></ui5-avatar>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants