@carbon/icon-helpers
TypeScript icon, indicating that this package has built-in type declarations

10.61.0 • Public • Published

@carbon/icon-helpers

Helpers used alongside icons for digital and software products using the Carbon Design System

Getting started

To install @carbon/icon-helpers in your project, you will need to run the following command using npm:

npm install -S @carbon/icon-helpers

If you prefer Yarn, use the following command instead:

yarn add @carbon/icon-helpers

Usage

@carbon/icon-helpers provides a couple of helpers for rendering <svg> nodes in a document, or to help get the correct attributes to set on an <svg> node. These include:

Name Type Description
getAttributes (attributes: Object) => Object Get the attributes for an <svg> node
formatAttributes (attributes: Object) => String Format the attributes into a string that can be applied to a node in the DOM
toString (descriptor: Object) => String Format the given icon descriptor into a string. Useful for templates
toSVG (descriptor: Object) => DOMNode Format the given icon descriptor into a DOM node

For most of the methods, attributes corresponds with whatever the name and value would be if you were writing the HTML for the <svg>. For example, if we wanted to set width and height we would do the following:

const { getAttributes } = require('@carbon/icon-helpers');
const attributes = getAttributes({ width: 20, height: 20 });

In order for the icon to be considered focusable, you will need to provide either aria-label, aria-labelledby, or title in the given attributes in addition to tabindex. For example:

const { getAttributes } = require('@carbon/icon-helpers');
const attributes = getAttributes({
  'aria-label': 'My icon label',
  tabindex: '0',
});

Icon descriptors

An icon descriptor is the term we use to describe icon objects exported by @carbon/icons. By default, they will have the following shape:

{
  elem: 'svg',
  attrs: {
    xmlns: 'http://www.w3.org/2000/svg',
    viewBox: '0 0 16 16',
    width: 16,
    height: 16,
  },
  content: [
    {
      elem: 'path',
      attrs: {
        d: '...',
      },
    },
  ],
  name: 'IconName',
  size: 16,
}

You can import these definitions directly from @carbon/icons and use them alongside toSVG or toString by doing:

import { IconName } from '@carbon/icons';
import { toString, toSVG } from '@carbon/icon-helpers';

const iconString = toString(IconName);
const iconSVG = toSVG({
  ...IconName,
  attrs: {
    ...IconName.attrs,
    myCustomAttribute: 'myCustomAttributeValue',
  },
});

🙌 Contributing

We're always looking for contributors to help us fix bugs, build new features, or help us improve the project documentation. If you're interested, definitely check out our Contributing Guide! 👀

📝 License

Licensed under the Apache 2.0 License.

IBM Telemetry IBM Telemetry

This package uses IBM Telemetry to collect de-identified and anonymized metrics data. By installing this package as a dependency you are agreeing to telemetry collection. To opt out, see Opting out of IBM Telemetry data collection. For more information on the data being collected, please see the IBM Telemetry documentation.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.1-alpha.320alpha
0.0.1-beta.5796beta
10.5.0-alpha.56400canary
10.11.0-alpha.00nightly
10.61.0
29,202latest
10.61.0
29,202next
10.28.51v10-next

Version History

VersionDownloads (Last 7 Days)Published
10.61.0
29,202
10.61.0-rc.0
54
10.60.0
10,166
10.60.0-rc.0
5
10.28.51
10.59.0
16,503
10.59.0-rc.0
0
10.58.0
3,148
10.58.0-rc.0
2
10.57.0
2,352
10.57.0-rc.0
2
10.56.0
14,147
10.56.0-rc.0
2
10.55.0
4,438
10.55.0-rc.0
2
10.54.0
18,645
10.54.0-rc.0
1
10.53.1
2,803
10.53.0
1,502
10.53.0-rc.0
2
10.52.0
1,697
10.52.0-rc.0
2
10.51.0
1,398
10.51.0-rc.0
2
10.50.0
3,317
10.50.0-rc.0
2
10.49.0
2,148
10.49.1
2,630
10.49.0-rc.0
2
10.48.0
3,220
10.28.41
10.48.0-rc.0
13
10.47.0
5,890
10.28.32
10.47.0-rc.0
1
10.46.0
4,038
10.46.0-rc.0
2
10.45.1
1,705
10.45.0
2,866
10.45.0-rc.0
1
10.28.20
10.44.0
1,503
10.44.0-rc.0
0
10.43.180
10.43.056
10.43.0-rc.00
10.42.1403
10.42.05
10.28.10
10.42.0-rc.01
10.41.0393
10.41.0-rc.00
10.40.0357
10.40.0-rc.00
10.39.01,966
10.39.0-rc.00
10.38.01,607
10.37.08,018
10.36.01,404
10.36.0-rc.00
10.35.0108
10.35.0-rc.00
10.34.02,846
10.34.0-rc.00
10.33.0253
10.33.0-rc.00
10.32.0230
10.32.0-rc.00
10.31.0912
10.31.0-rc.00
10.30.0355
10.30.0-rc.01
10.29.01,043
10.29.0-rc.02
10.28.0616
10.28.0-rc.02
10.27.047
10.27.0-rc.02
10.26.0115
10.26.0-rc.00
10.25.0702
10.24.0403
10.23.06
10.22.03
10.22.0-rc.00
10.21.0101
10.21.0-rc.00
10.20.013
10.20.0-rc.00
10.19.0277
10.19.0-rc.00
10.18.083
10.18.0-rc.00
10.17.08
10.17.0-rc.00
10.16.034
10.16.0-rc.00
10.15.0536
10.14.020
10.14.0-rc.00
10.13.028
10.13.0-rc.00
10.12.012
10.12.0-rc.01
10.11.020
10.11.0-rc.00
10.10.0254
10.11.0-alpha.00
10.10.0-rc.00
10.9.03,010
10.9.0-rc.00
10.8.00
10.8.0-rc.00
10.7.028
10.7.0-rc.00
10.7.0-alpha.65420
10.6.0669
10.6.0-rc.10
10.6.0-rc.00
10.5.22
10.5.2-rc.00
10.5.173
10.5.1-rc.00
10.5.04
10.5.0-rc.00
10.5.0-alpha.00
10.5.0-alpha.60870
10.5.0-alpha.60830
10.5.0-alpha.60820
10.5.0-alpha.60810
10.5.0-alpha.60760
10.5.0-alpha.60751
10.5.0-alpha.60700
10.5.0-alpha.60490
10.5.0-alpha.60460
10.5.0-alpha.59640
10.5.0-alpha.59630
10.5.0-alpha.59580
10.5.0-alpha.59420
10.5.0-alpha.59410
10.5.0-alpha.59280
10.5.0-alpha.59160
10.5.0-alpha.58870
10.5.0-alpha.58520
10.5.0-alpha.58510
10.5.0-alpha.58460
10.5.0-alpha.58380
10.5.0-alpha.58370
10.5.0-alpha.58350
10.5.0-alpha.58340
10.5.0-alpha.58160
10.5.0-alpha.58060
10.5.0-alpha.57780
10.5.0-alpha.57750
10.5.0-alpha.57690
10.5.0-alpha.57680
10.5.0-alpha.57640
10.5.0-alpha.57630
10.4.075
10.5.0-alpha.56400
10.4.0-alpha.56130
10.3.027
10.4.0-alpha.55180
10.3.0-rc.31
10.3.0-rc.20
10.3.0-alpha.55010
10.3.0-rc.10
10.3.0-rc.00
10.3.0-alpha.53230
10.2.040
10.1.13
10.1.00
10.0.00
10.0.0-rc.00
0.0.1-beta.5796
0.0.1-beta.42
0.0.1-beta.30
0.0.1-beta.20
0.0.1-beta.10
0.0.1-beta.01
0.0.1-alpha.320
0.0.1-alpha.311
0.0.1-alpha.300
0.0.1-alpha.290
0.0.1-alpha.280
0.0.1-alpha.270
0.0.1-alpha.261
0.0.1-alpha.2527
0.0.1-alpha.240
0.0.1-alpha.230
0.0.1-alpha.220
0.0.1-alpha.210
0.0.1-alpha.201
0.0.1-alpha.190
0.0.1-alpha.180
0.0.1-alpha.170
0.0.1-alpha.160
0.0.1-alpha.150
0.0.1-alpha.140
0.0.1-alpha.130
0.0.1-alpha.120
0.0.1-alpha.110
0.0.1-alpha.101
0.0.1-alpha.90
0.0.1-alpha.81

Package Sidebar

Install

npm i @carbon/icon-helpers

Weekly Downloads

161,504

Version

10.61.0

License

Apache-2.0

Unpacked Size

42.4 kB

Total Files

22

Last publish

Collaborators

  • carbon-design-system
  • carbon-bot
  • alisonjoseph
  • leechase
  • jeffreychew
  • sstrubberg
  • tay1orjones