Icon
Documentation
An icon component with URL, inline SVG, and CSS mask modes. A click handler makes
it keyboard operable. Use --icon-container-direction for the icon-and-text
layout; monochrome mask icons follow the surrounding text color.
Usage
<script>
import { Icon } from '@juspay/svelte-ui-components';
</script>
<!-- Image-based icon -->
<Icon icon="/icons/home.svg" text="Home" />
<!-- Inline SVG icon -->
<Icon
svg={'<svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/></svg>'}
text="Star"
/>
Interactivity
Monochrome mask icons
Use mask for an existing monochrome asset that follows the surrounding text
color. Omit label for decoration; provide it when the image has meaning of its
own. A mask icon with an onclick handler renders a native Button around its
painted shape, keeping the focus ring outside the mask.
<Icon mask icon="/icons/address.svg" size={20} label="Saved address" />
<Icon mask icon="/icons/close.svg" label="Close" onclick={close} />
An icon given an onclick handler becomes a real control: it takes role="button",
joins the tab order, and activates on Enter and Space as well as click. Without a
click handler it stays a plain decorative element with no role and no tab stop, so a
screen reader does not announce a button that does nothing.
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| icon | string |
No | - |
URL of the icon image to display. Rendered as <img src>. |
| mask | boolean |
No | false |
Use a CSS mask to paint the icon asset with currentColor. |
| size | number |
No | 24 |
Mask size in CSS pixels; when omitted CSS can override --icon-size. |
| color | string |
No | currentColor |
Optional mask color override. |
| label | string |
No | - |
Accessible mask-image or action name. Omit for decorative mask icons. |
| svg | string |
No | - |
Raw SVG markup string to render inline via {@html}. When provided, takes priority over icon. Inherits currentColor for styling. Must be trusted content — never pass unsanitized user input (see Security note below). |
| text | string | null |
No | - |
Optional text label displayed below the icon. |
| classes | string |
No | - |
CSS class string applied to the component's top-level element. Useful for theming — define classes with CSS variable overrides and pass them to create variant styles. |
| testId | string |
No | - |
Value for the data-pw attribute on the root element, used for end-to-end testing selectors. |
Note: At least one of
iconorsvgshould be provided. If both are provided,svgtakes priority.
Security: The
svgprop uses{@html}for rendering and is not sanitized. Only pass trusted SVG markup — never pass unsanitized user input, as it may enable XSS attacks (e.g.<svg onload="...">).
Events
| Event | Type | Description |
|---|---|---|
| onclick | (event: MouseEvent) => void |
Fires when the icon container is clicked. |
| onkeydown | (event: KeyboardEvent) => void |
Fires when a key is pressed while the icon container has focus. Runs for every key, including the Enter and Space the icon handles itself. |
CSS Variables
Override these custom properties to theme the component.
| Variable | Default | CSS Property | Description |
|---|---|---|---|
--icon-container-paddding |
4px |
padding | Inner padding of the icon container. |
--icon-container-direction |
column |
flex-direction | Layout direction of icon + text (column for vertical, row for horizontal). |
--icon-height |
20px |
height | Height of the icon image or SVG container. |
--icon-width |
20px |
width | Width of the icon image or SVG container. |
--icon-padding |
4px |
padding | Padding around the icon image or SVG container. |
--icon-svg-color |
currentColor |
color | Color of the inline SVG icon (only applies when using svg prop). |
--icon-text-padding |
4px |
padding | Padding around the text label. |
--icon-text-direction |
column |
flex-direction | Layout direction of the text area. |
--icon-text-font-size |
12px |
font-size | Font size of the text label. |
--icon-size |
24px |
width, height | Size of the mask shape. Explicit size takes precedence. |
--icon-color |
currentColor |
background-color | Mask paint, inherited from the surrounding theme. |
--icon-url |
supplied by icon |
mask-image | Mask asset URL; assigned by the component. |
Web Component
Tag: <sui-icon>
<!-- Image-based -->
<sui-icon icon="/icons/home.svg" text="Home"></sui-icon>
<!-- Inline SVG -->
<sui-icon
svg="<svg viewBox='0 0 24 24' fill='currentColor'><path d='M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z'/></svg>"
text="Star"
></sui-icon>
onclick and onkeydown are JS-property callbacks only (icon.onclick = (event) => ...) and do not dispatch DOM events: both names are already native HTMLElement handlers, so an
icon.addEventListener('click', ...) (or 'keydown') listener already sees the real event that
bubbles out of the shadow root on its own, and a synthetic duplicate under the same name would
double-deliver it.
Mask asset URLs are escaped as quoted CSS URLs, including literal quotes and backslashes. Raw quoted SVG data URLs and their URI-encoded equivalents paint the same mask.