When to use it#
- Use it beside a word to make an action or a status quicker to recognise: add, search, warning.
- Draw it from the icon library (
@kvirn-ui/icons), from a component of another icon set, or from your own shapes. - Give it a name only when no text beside it says the same thing, and never let it be the only sign of a status.
- Not for something users can press: put the icon inside a Button or a Link.
Example#
import { Button, Icon } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
export function DefaultIcon() {
const { texts } = useIconTexts()
return (
<Button>
<Icon name="add" />
{texts.addChild}
</Button>
)
}Use cases#
KvirnUI has no icons of its own to fall back on. The icon library is the package @kvirn-ui/icons, and you bundle only the icons you import. Register the ones you want to draw by name once, in KvirnProvider. Register their types too, so a misspelt name is a type error:
import { close, search } from '@kvirn-ui/icons'
import { defineIcons, KvirnProvider } from '@kvirn-ui/react'
export const icons = defineIcons({ close, search })
declare module '@kvirn-ui/react' {
interface Register {
icons: typeof icons
}
}
<KvirnProvider icons={icons}>{children}</KvirnProvider>Export names are in camelCase (words joined, each new word starting with a capital letter) and have no “Icon” suffix, such as chevronDown. Two end in “Icon”, deleteIcon and newIcon, because the plain words are reserved in JavaScript. By convention, a registered name is the kebab-case word, such as chevron-down. To pick an icon, look through all of them on the Icons page.
An icon by name
Once an icon is registered, name draws it anywhere below the provider, with nothing to import. A name you did not register draws an empty icon and warns in development.
import { Icon, Link } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
export function ByName() {
const { texts } = useIconTexts()
return (
<ul>
<li>
<Icon name="download" /> <Link href="#permit-form">{texts.downloadForm}</Link>
</li>
<li>
<Icon name="calendar" /> <Link href="#book-a-time">{texts.bookTime}</Link>
</li>
</ul>
)
}Props in this example:
nameon Icon
Information: Did you know?
Registering the whole set from @kvirn-ui/icons/all adds every icon to your bundle (the code you send to the browser). Register only the names you need.
An icon without registering it
Where there is no provider to register in, or for one icon you draw once, import it and pass it as icon. Only the icons you import reach your bundle.
import { search } from '@kvirn-ui/icons'
import { Button, Icon } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
export function ByImport() {
const { texts } = useIconTexts()
return (
<Button>
<Icon icon={search} />
{texts.searchRegister}
</Button>
)
}Props in this example:
iconon Icon
KvirnUI’s own components pass both: name="close" and the close drawing as icon. A name you register wins over the drawing, so registering close changes it in every KvirnUI component at once, such as a Toast, a Dialog or an Alert:
import { defineIcons } from '@kvirn-ui/react'
import { Cross } from 'your-icon-library'
export const icons = defineIcons({ close: Cross })Use the icons of the Map and the rich text editor
Both packages draw icons from @kvirn-ui/icons. The Map has the marker pin and the control arrows. mapIcons in @kvirn-ui/map lists them by name. Pass one with icon, or register them with defineIcons. The editor draws its toolbar icons with RichTextEditor.Icon (exported as ToolbarIcon), from the names in richTextIconNames in @kvirn-ui/rich-text.
The Map
-
marker -
locate -
remove -
arrow-north -
arrow-south -
arrow-west -
arrow-east
The rich text editor
-
undo -
redo -
bold -
italic -
underline -
strike -
code -
bullet-list -
numbered-list -
indent -
outdent -
link -
image -
table -
clear-formatting -
heading-2 -
heading-3 -
heading-4 -
heading-5 -
heading-6
import { Grid, Heading, Icon } from '@kvirn-ui/react'
import { mapIcons } from '@kvirn-ui/map'
import { richTextIconNames, ToolbarIcon } from '@kvirn-ui/rich-text'
import { useIconTexts } from './texts.ts'
export function PackageSets() {
const { texts } = useIconTexts()
return (
<>
<Heading as="h4">{texts.mapHeading}</Heading>
<Grid.Root className="kv-grid--fit-sm kv-grid--gap-4" as="ul">
{Object.entries(mapIcons).map(([name, icon]) => (
<li key={name}>
<Icon icon={icon} /> <code>{name}</code>
</li>
))}
</Grid.Root>
<Heading as="h4">{texts.editorHeading}</Heading>
<Grid.Root className="kv-grid--fit-sm kv-grid--gap-4" as="ul">
{richTextIconNames.map((name) => (
<li key={name}>
<ToolbarIcon name={name} /> <code>{name}</code>
</li>
))}
</Grid.Root>
</>
)
}Props in this example:
iconon Icon
An icon that adds meaning
Here the icon says what the word beside it is: the language (“Svenska” is Swedish, and the example marks it with lang="sv"). A label turns the icon into an image with that name, so a screen reader says “Language” before the word. See What a screen reader reads.
Svenska
import { Icon } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
export function WithLabel() {
const { texts } = useIconTexts()
return (
<p>
<Icon name="language" label={texts.language} /> <span lang="sv">{texts.currentLanguage}</span>
</p>
)
}Props in this example:
labelon Icon
Warning: Don't forget
An icon that carries a meaning no text beside it gives needs a label (1.1.1 Non-text Content). If text beside it says the same, leave the label out, or it is read twice. See What you need to do.
A size that follows the reader’s settings
The size is a pixel size as a string, such as “24”. The icon is drawn in rem, so it grows with the browser’s text-size setting and zoom. For another size, add a class.
Sizes 16, 20 (the default), 24, 32 and 40:
import { Icon } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
export function Sizes() {
const { texts } = useIconTexts()
return (
<p>
{texts.sizes} <Icon name="info" size="16" /> <Icon name="info" />{' '}
<Icon name="info" size="24" /> <Icon name="info" size="32" /> <Icon name="info" size="40" />
</p>
)
}Props in this example:
sizeon Icon
An icon from another library, or of your own
Pass a component that spreads SVG props onto one svg and forwards its ref, such as one from Lucide, Heroicons or Phosphor, or your own. It needs no registration.
Home
import { Icon } from '@kvirn-ui/react'
import type { IconComponentProps } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
function HouseIcon({ ref, ...svgProps }: IconComponentProps) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={1.5}
strokeLinecap="round"
strokeLinejoin="round"
{...svgProps}
ref={ref}
>
<path d="M4 11 12 4l8 7" />
<path d="M6 10v10h12V10" />
</svg>
)
}
export function OwnComponent() {
const { texts } = useIconTexts()
return (
<p>
<Icon icon={HouseIcon} /> {texts.house}
</p>
)
}Props in this example:
iconon Icon
Information: Did you know?
Icon’s size and label replace the component’s own, so every icon on the page is sized the same way.
A one-off drawing
For a mark you use once, such as a municipality’s own, pass its shapes as children, with a viewBox. Icon is the svg, so the size, the name and the colour work as for any icon. Draw the shapes with currentColor so they follow the text colour, also in forced colours (a Windows contrast theme).
import { Icon } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
export function OneOffDrawing() {
const { texts } = useIconTexts()
return (
<Icon label={texts.ownDrawing} size="40" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" strokeWidth={1.5} />
<path d="M7 15l5-7 5 7z" fill="none" stroke="currentColor" strokeWidth={1.5} />
</Icon>
)
}Props in this example:
An arrow in right-to-left text
In right-to-left text the default theme flips the library’s directional icons, such as the forward and back arrows, the chevrons and external, so “next” still points forward. The library marks them, so you set nothing on them. Set mirrorInRtl yourself on a directional icon of your own.
Left to right: next
Right to left: next
import { Icon } from '@kvirn-ui/react'
import { useIconTexts } from './texts.ts'
export function MirrorInRtl() {
const { texts } = useIconTexts()
return (
<div>
<p>
{texts.nextLeftToRight} <Icon name="arrow-forward" />
</p>
<p dir="rtl">
{texts.nextRightToLeft} <Icon name="arrow-forward" />
</p>
</div>
)
}Information: Did you know?
Set mirrorInRtl only on icons that point somewhere: arrows and chevrons, never a tick, a logo or an icon with text in it.
Accessibility#
What it does for you#
- APG pattern: none. An SVG graphic, decorative or
role="img"(SVG-AAM, WAI images tutorial). - Deviations: none
- Native elements used:
<svg>. Icon is never interactive.
An icon is decorative by default: text next to it already says what it means, so it's hidden from assistive technology. With a label, it's an image with that name. Icons come from the app's registry (KvirnProvider icons), from a drawing or component reference (icon, with no registration; a registered name wins over it), or from as (your own component) and children for one-offs. Every route is hidden unless there is a label, and Icon's aria-hidden replaces a library's own. An icon drawn from icon data also carries focusable="false" when decorative. With as, the component gets the props as plain props, and Icon's aria-hidden, role and aria-label win over any of the same name: it must spread them on its <svg> and take a ref. Icon's own options (size, label) do not reach it under their own names. Allowed elements: none to list: as is a component that draws an <svg>.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Icon | <svg>, no role | aria-hidden="true" | Default: decorative. class="kv-icon kv-icon--size-20", width and height in rem |
<svg> → img, with label | role="img", aria-label={label}, no aria-hidden | A library's own aria-hidden (Heroicons, Lucide) is removed. aria-label, aria-hidden and role can't be passed directly: label sets them. tabIndex, onClick, aria-labelledby and aria-describedby are type errors too, because an icon takes no focus, press or description: put them on the control around it | |
unregistered name + icon | as the icon | Draws the icon with no warning. A registered entry under the name keeps the icon's RTL flip | |
unknown name, no icon | aria-hidden="true", empty | Sized like the icon, so nothing moves. A dev warning names the missing icon. With a label it is still role="img" with that name, so a screen-reader user keeps the name and the warning flags the bug | |
data-mirror-in-rtl | none | State for CSS: whether the icon flips in right-to-left text |
useIcon gives the same iconProps, and the component for a name, for your own <svg>.
SvgIcon (`@kvirn-ui/icons/react`)#
The standalone component for apps without KvirnUI. It has no hooks or context and takes icon (icon data), size and title, plus SVG props. Its roles:
- Decorative (no name):
aria-hidden="true",focusable="false", norole. This is the default. aria-label,aria-labelledbyor a non-emptytitle:role="img", named, noaria-hidden.titleis a<title>child;aria-labelledbynames by id.- An empty string for any of the three: an empty name is no name, so the icon stays decorative.
No keyboard stops: Tab and Shift+Tab never reach an <svg> (focusable="false"). WCAG 1.1.1 Non-text Content (A), 4.1.2 Name, Role, Value (A).
It announces nothing.
What you need to do#
- Give an icon that carries meaning on its own a
label(1.1.1 Non-text Content, A): a status icon with no text beside it, say. Give it alabelonly when no text next to it says the same thing: an icon with a label next to the same text is read twice. A decorative icon needs none. - Name an icon-only
Buttonwith anaria-label(oraria-labelledby) from your translations, and keep the icon decorative. In development, Button warns when it has no name. Use icon-only buttons only for universally known actions: close, search, menu, copy (copy always with a Tooltip). For anything else, prefer visible text. - A
variant(primary,secondary,accent,muted,info,success,warning,danger) only decorates and never replaces words, and never changes the name, role or state. Give a meaningful or status icon at least'20', and'24'withkv-icon--stroke-1. Forced colours drop the variant. - Never convey status by an icon's colour alone: the status icons differ in shape, and status always comes with text (1.4.1 Use of Color, A).
- Give a meaningful icon (one with a
label) at least 3:1 contrast against its background (1.4.11 Non-text Contrast, AA).currentColorfollows the text colour, which already has at least 4.5:1 contrast in the default theme, and every variant is measured at 3:1 or more on canvas, surface and surface-raised in all four themes. - Set
mirrorInRtlonly on directional icons (arrows, chevrons), never on a tick, a logo or anything with text in it. - Register or pass components that forward their ref and spread SVG props onto one
<svg>. An icon from@kvirn-ui/iconsis plain data and needs nothing.
Warning: Don't forget
Name an icon-only Button yourself, with an aria-label from your translations, and keep the icon inside it hidden. Without a name, a screen reader says only “button” (4.1.2 Name, Role, Value).
Visual and modes#
- Focus indicator: not applicable. Icon is never focusable.
- Target size: not applicable. An icon-only button's target is the Button's: the default theme makes an icon-only button square and larger than the 24 by 24 pixel minimum (2.5.8 Target Size (Minimum), AA).
- Forced colours: icons draw in the text colour (
currentColor), which follows the system colour. The default theme turns an explicitfill,strokeorcoloron.kv-icon(set by your own CSS) intocurrentColorin forced-colours mode, so a hard-coded colour can't vanish. Colours on child shapes are kept, so forced-colours contrast for a multi-colour, meaningful SVG is the consumer's job. Icon has nofillorstrokeoption: a one-off SVG setsfill="currentColor"on its own shapes, or it draws black in forced colours. - Reduced motion: icons do not move. The one exception is the
spinnericon with the consumer-added classkv-animate-spinner: it crawls only underprefers-reduced-motion: no-preference, and the app owns the 2.2.2 Pause, Stop, Hide control (the stop CSS is on the Theming page, "Moving indicators and WCAG 2.2.2"). - Text resize and reflow: icon sizes are in rem (
widthandheight, plus akv-icon--size-<px>class), so they scale with browser zoom and the user's root font size, not with the surrounding text. 1.4.4 Resize Text (AA) holds because text resizes and icons aren't text. The default size scales with the text. The largest icon is 6rem, so no horizontal scrolling is expected at 320 CSS px; the reflow sweep checks every story at that width for horizontal overflow (1.4.10 Reflow, AA).
Focus management#
- Initial focus: not moved.
- Trap: no.
- Restore to: not applicable.
- Never obscured by: Icon renders no overlay.
WCAG success criteria#
- 1.1.1 Non-text Content (A): decorative icons are hidden, meaningful ones have a
label. - 1.3.1 Info and Relationships (A): an icon adds no structure.
- 1.4.1 Use of Color (A): the status icons differ in shape.
- 1.4.4 Resize Text (AA), 1.4.10 Reflow (AA): rem sizes, at most 6rem.
- 1.4.11 Non-text Contrast (AA): the adopter's job, with the text colour by default.
- 4.1.2 Name, Role, Value (A):
role="img"with a name, or hidden. Button warns in development about an unnamed icon-only button.
Keyboard#
Icon has no keys of its own. What you put in it keeps its own keys.
Announcements#
What a screen reader reads#
| State or action | Read aloud | Politeness | Test |
|---|---|---|---|
Icon, decorative | nothing | none | icon.test.tsx › a labelled Icon is read as an image with its name, and a decorative one is not read |
Icon with a label | image, Close | none | icon.test.tsx › a labelled Icon is read as an image with its name, and a decorative one is not read |
SvgIcon, decorative | nothing | none | svg-icon.test.tsx › a decorative icon is not read aloud |
SvgIcon, aria-label="Add" | image, Add | none | svg-icon.test.tsx › an icon with an aria-label is read as an image with that name |
SvgIcon, aria-labelledby | image, Add | none | svg-icon.test.tsx › an icon with an aria-labelledby is read as an image with that name |
SvgIcon, title="Add" | image, Add | none | svg-icon.test.tsx › an icon with a title is read as an image with that name |
SvgIcon, empty aria-label | nothing | none | svg-icon.test.tsx › an empty name stays decorative and is not read aloud |
API reference#
import { Icon, defineIcons, useIcon } from '@kvirn-ui/react'Icon
Renders <svg class="kv-icon">, hidden from assistive technology, or role="img" with a label. It takes every attribute of an <svg> and passes ref to it, except the ones that would change what it is or does: width, height, aria-label, aria-hidden, role, color, fill, stroke, strokeWidth, tabIndex, onClick, aria-labelledby and aria-describedby. An icon can’t be focused or pressed, and its name comes only from label: put focus and press on the Button or Link around it. Register the icons you use once with defineIcons and the icons prop of KvirnProvider, then draw them by name, or pass an icon from @kvirn-ui/icons as icon. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
name | IconName | – | A name you registered with defineIcons in a KvirnProvider, such as close or search. KvirnUI has no built-in icons, so a name you did not register draws an empty icon and warns in development. Give one of name, icon, as or children. Together with icon, a registered name wins over the icon. |
icon | IconRegistryEntry | – | A drawing from @kvirn-ui/icons, or a component: icon={search} or icon={Search}. It needs no registration, and a registered name wins over it. Icon’s size and label replace the component’s own, and className is joined with it. |
as | ElementType | – | A one-off icon of your own that isn’t registered: as={MunicipalityMark}. It gets Icon’s props (width, height, aria-hidden, role, aria-label, className) as plain props and must spread them on its own <svg> and take a ref. Icon’s size and label do not reach it under those names. |
children | ReactNode | – | Your own shapes, with Icon as the <svg>. Pass a viewBox too. |
size | IconSize | 20 | The size in pixels, as a string: "12", "14", "16", "20", "24", "28", "32", "40", "48", "56", "64", "80" or "96". The theme turns it into rem with the class kv-icon--size-<px>, so it follows the root font size, not the text. Another size is a class you add. The default comes from KvirnProvider iconDefaults. |
label | string | – | Makes the icon an image (role="img") with this name, from your translations. Leave it out when text next to the icon says the same: the icon is then hidden from assistive technology. |
variant | 'primary' | 'secondary' | 'accent' | 'muted' | 'info' | 'success' | 'warning' | 'danger' | – | A colour from the theme, set with the class kv-icon--<variant>. Without one the icon keeps the text colour around it. In forced colours the icon uses the system text colour instead. It decorates: words carry the meaning, never the colour alone. |
mirrorInRtl | boolean | the entry’s, else false | Flips the icon in right-to-left text. Defaults to the registry entry’s, else the icon data’s, else false. Only for arrows and chevrons. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-icon | always | The part class. The default theme styles it. |
kv-icon--size-<px> | such as "kv-icon--size-20" | The size, in rem in the theme. Always present. |
data-mirror-in-rtl | present or absent | The icon flips in right-to-left text. |
aria-hidden | "true" or absent | Set unless the icon has a label. Not passed directly. |
role | "img" or absent | Set with a label. Not passed directly. |
useIcon
Options
| Prop | Type | Default | Description |
|---|---|---|---|
name | IconName | – | A registered icon name, to get its component. |
icon | IconRegistryEntry | – | An icon from @kvirn-ui/icons, or a component, used instead of a name. |
size | IconSize | 20 | The size in pixels, as a string. |
label | string | – | Makes the icon an image with this name. Without it the icon is decorative. |
variant | 'primary' | 'secondary' | 'accent' | 'muted' | 'info' | 'success' | 'warning' | 'danger' | – | A colour from the theme, set with the class kv-icon--<variant>. |
mirrorInRtl | boolean | the entry’s, else false | Flips the icon in right-to-left text. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
iconProps | IconPartProps | – | Spread on the root <svg>, or pass to an icon component: class, width and height, and the aria attributes. |
component | IconComponent | undefined | – | The component for a registered name, else for icon. Undefined without either, or for an unknown name. |
isDecorative | boolean | – | True unless the icon has a label. |
Known issues#
- Whether
var()works in thefillandstrokeattributes of your own shapes is up to the browser, not Icon. Check it by eye. - react-icons applies its own
sizeafter Icon'swidthandheight. Size react-icons on the element.
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | icon.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | icon.test.tsx › an icon with a label is an image with that name; next to a Button label, the button is named by its text only; no axe violations: decorative, labelled, in a Button, icon-only Button |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | icon.test.tsx › keyboard › Tab never stops on an icon; Shift+Tab never stops on an icon |
| Focus order and management | n/a | n/a | n/a | nothing focusable: the component never takes or moves focus |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
What to check#
- A decorative icon beside text: only the text is read.
- A labelled icon is read as an image named by its label ("Language").
- An icon inside a Button with text: the Button's name is the text only.
- Tab and Shift+Tab never stop on an icon.
- In Windows contrast themes the icon is visible in the text colour.