Inside other components you do not assemble an icon separately: CdsButton, CdsTextInput, CdsList and others have the
icon,prepend-iconandappend-iconprops. For a standalone action take CdsFunctionalButton — an icon button with a label and states. All the available icons are in the gallery, and connecting your own pack is described in the guide.
Variations
Icon
The icon prop takes the name of an icon from the default pack — add or search, for example. The name prop does the same and reads more naturally when the icon is set by a string. Besides a name, icon also accepts an icon component and an array of SVG paths, so you can pass your own graphics into it.
An icon from another connected pack is written with the prefix of that pack — i-ri:settings-3-line. How to connect a pack is described in the icons guide.
Sizes
The size prop takes one of the scale values — from xs to xl — or a number in pixels when the icon works as an illustration. The default is md, level with the body text.
Color
Without color the icon inherits the text color of its parent — this is the default behavior and the right one in most cases. The color prop takes a palette token (content-secondary, brand-primary), a status color alias (green, red, lemon) or a hex/rgb value. The opacity prop mutes the icon without changing its color.
Spacing from the text
The start and end props add spacing on the side of the text: start for an icon before the label, end for one after it. This saves you from picking margins by hand.
Reacting to a click
If you attach a @click handler to an icon, it gets role="button", joins the Tab order and changes the cursor; disabled removes it from the order and stops it from accepting clicks. This is handy for small toggles inside your own components — for a full-fledged action CdsFunctionalButton fits better.
Slot
The name of the icon can be passed as text into the default slot — the result is the same as with icon. Useful when the name comes from an expression in the template.
Guidelines
Scenarios
Statuses in a list
An icon to the left of a label sets the status by color and shape, while the text names it in words. The colors come from the status aliases, size="sm" fits the icon to small text, and start adds the spacing before the label.
List of attachments
The icon shows the type of a file and keeps the eye on the left edge of the list. The lg size makes it more noticeable than the label, and the content-secondary color keeps it from competing with the file name. The action next to it is already a button: CdsFunctionalButton with an aria-label.
Progress of an operation
The icon reflects the state of a process: the request is ready, is being sent, has been sent. Only icon and color change — the component repaints in place and the status line does not jump.
A note inside text
Inside a paragraph the icon aligns to the middle of the line, so you can put it right into the text. The sm size and start keep it from breaking the line.
API
Props
| Name | Type | Default | Description |
|---|---|---|---|
activeColor | string | undefined | — | Color of the component in its active state |
baseColor | string | undefined | — | Color of the component in its inactive state |
bgColor | string | undefined | — | Background color of the component |
color | string | undefined | — | Component color |
disabled | boolean | null | undefined | null | Disabled state |
end | boolean | undefined | — | Adds spacing to the left of the icon — for an icon after the text |
icon | IconValue | undefined | — | Name of the icon or the icon component to render |
name | string | undefined | — | String name of an icon from a registered pack (an alias for icon) |
opacity | string | number | undefined | — | Opacity of the icon — a number between 0 and 1 |
size | number | "xs" | "sm" | "md" | "lg" | "xl" | undefined | 'md' | Size of the component |
start | boolean | undefined | — | Adds spacing to the right of the icon — for an icon before the text |
tag | string | JSXComponent<any> | undefined | 'i' | Custom HTML tag or component to render |
textColor | string | undefined | — | Text color of the component |
theme | string | undefined | — | Component theme |
Slots
| Name | Type | Description |
|---|---|---|
default | any | Name of the icon as text — an alternative to the icon prop |