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

10.54.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.51,040beta
10.5.0-alpha.56400canary
10.28.40v10-next
10.11.0-alpha.00nightly
10.54.0
40,798latest
10.54.0
40,798next

Version History

VersionDownloads (Last 7 Days)Published
10.54.0
40,798
10.54.0-rc.0
52
10.53.1
4,053
10.53.0
4,479
10.53.0-rc.0
1
10.52.0
2,695
10.52.0-rc.0
1
10.51.0
2,077
10.51.0-rc.0
1
10.50.0
6,795
10.50.0-rc.0
1
10.49.0
3,130
10.49.1
5,660
10.49.0-rc.0
3
10.48.0
8,816
10.28.40
10.48.0-rc.0
1
10.47.0
6,506
10.28.30
10.47.0-rc.0
0
10.46.0
5,148
10.46.0-rc.0
1
10.45.1
1,989
10.45.0
4,108
10.45.0-rc.0
0
10.28.20
10.44.0
2,626
10.44.0-rc.0
0
10.43.1172
10.43.0375
10.43.0-rc.03
10.42.1413
10.42.049
10.28.10
10.42.0-rc.00
10.41.0345
10.41.0-rc.01
10.40.0353
10.40.0-rc.00
10.39.02,228
10.39.0-rc.00
10.38.01,471
10.37.07,068
10.36.01,410
10.36.0-rc.00
10.35.068
10.35.0-rc.00
10.34.0830
10.34.0-rc.00
10.33.0465
10.33.0-rc.00
10.32.0266
10.32.0-rc.03
10.31.0964
10.31.0-rc.00
10.30.066
10.30.0-rc.00
10.29.0281
10.29.0-rc.00
10.28.0566
10.28.0-rc.01
10.27.021
10.27.0-rc.00
10.26.066
10.26.0-rc.00
10.25.01,209
10.24.090
10.23.02
10.22.02
10.22.0-rc.00
10.21.0104
10.21.0-rc.00
10.20.08
10.20.0-rc.00
10.19.0130
10.19.0-rc.02
10.18.040
10.18.0-rc.00
10.17.010
10.17.0-rc.00
10.16.031
10.16.0-rc.00
10.15.0271
10.14.017
10.14.0-rc.03
10.13.024
10.13.0-rc.00
10.12.01
10.12.0-rc.00
10.11.013
10.11.0-rc.00
10.10.015
10.11.0-alpha.00
10.10.0-rc.01
10.9.02,158
10.9.0-rc.03
10.8.02
10.8.0-rc.00
10.7.0101
10.7.0-rc.01
10.7.0-alpha.65420
10.6.01,071
10.6.0-rc.11
10.6.0-rc.00
10.5.21
10.5.2-rc.00
10.5.1153
10.5.1-rc.00
10.5.00
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.60813
10.5.0-alpha.60762
10.5.0-alpha.60750
10.5.0-alpha.60700
10.5.0-alpha.60491
10.5.0-alpha.60461
10.5.0-alpha.59641
10.5.0-alpha.59631
10.5.0-alpha.59580
10.5.0-alpha.59420
10.5.0-alpha.59412
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.58341
10.5.0-alpha.58160
10.5.0-alpha.58060
10.5.0-alpha.57780
10.5.0-alpha.57751
10.5.0-alpha.57690
10.5.0-alpha.57680
10.5.0-alpha.57640
10.5.0-alpha.57631
10.4.098
10.5.0-alpha.56400
10.4.0-alpha.56130
10.3.018
10.4.0-alpha.55181
10.3.0-rc.30
10.3.0-rc.21
10.3.0-alpha.55011
10.3.0-rc.10
10.3.0-rc.01
10.3.0-alpha.53230
10.2.028
10.1.11
10.1.00
10.0.00
10.0.0-rc.00
0.0.1-beta.51,040
0.0.1-beta.40
0.0.1-beta.30
0.0.1-beta.20
0.0.1-beta.10
0.0.1-beta.04
0.0.1-alpha.320
0.0.1-alpha.310
0.0.1-alpha.300
0.0.1-alpha.290
0.0.1-alpha.281
0.0.1-alpha.270
0.0.1-alpha.261
0.0.1-alpha.250
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.151
0.0.1-alpha.140
0.0.1-alpha.130
0.0.1-alpha.121
0.0.1-alpha.111
0.0.1-alpha.101
0.0.1-alpha.91
0.0.1-alpha.81

Package Sidebar

Install

npm i @carbon/icon-helpers

Weekly Downloads

123,106

Version

10.54.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
  • guidari
  • dmenend