@carbon/icon-helpers 0.0.1-alpha.22 → 0.0.1-alpha.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -8
- package/es/index.js +32 -3
- package/lib/index.js +32 -2
- package/package.json +3 -2
- package/umd/index.js +32 -2
package/README.md
CHANGED
|
@@ -1,22 +1,110 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @carbon/icon-helpers
|
|
2
2
|
|
|
3
|
-
> Helpers used alongside icons for digital and software products using
|
|
3
|
+
> Helpers used alongside icons for digital and software products using
|
|
4
|
+
> the Carbon Design System
|
|
4
5
|
|
|
5
|
-
## Getting
|
|
6
|
+
## Getting started
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
To install `@carbon/icon-helpers` in your project, you will need to
|
|
9
|
+
run the following command using [npm](https://www.npmjs.com/):
|
|
8
10
|
|
|
9
11
|
```bash
|
|
10
12
|
npm install -S @carbon/icon-helpers
|
|
11
13
|
```
|
|
12
14
|
|
|
13
|
-
If you prefer [Yarn](https://yarnpkg.com/en/), use the following
|
|
14
|
-
instead:
|
|
15
|
+
If you prefer [Yarn](https://yarnpkg.com/en/), use the following
|
|
16
|
+
command instead:
|
|
15
17
|
|
|
16
18
|
```bash
|
|
17
19
|
yarn add @carbon/icon-helpers
|
|
18
20
|
```
|
|
19
21
|
|
|
20
|
-
##
|
|
22
|
+
## Usage
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
`@carbon/icon-helpers` provides a couple of helpers for rendering `<svg>` nodes in a document, or to help get the correct attributes to
|
|
25
|
+
set on an `<svg>` node. These include:
|
|
26
|
+
|
|
27
|
+
| Name | Type | Description |
|
|
28
|
+
| ------------------ | --------------------------------- | ---------------------------------------------------------------------------- |
|
|
29
|
+
| `getAttributes` | `(attributes: Object) => Object` | Get the attributes for an `<svg>` node |
|
|
30
|
+
| `formatAttributes` | `(attributes: Object) => String` | Format the attributes into a string that can be applied to a node in the DOM |
|
|
31
|
+
| `toString` | `(descriptor: Object) => String` | Format the given icon descriptor into a string. Useful for templates |
|
|
32
|
+
| `toSVG` | `(descriptor: Object) => DOMNode` | Format the given icon descriptor into a DOM node |
|
|
33
|
+
|
|
34
|
+
For most of the methods, `attributes` corresponds with whatever the
|
|
35
|
+
name and value would be if you were writing the HTML for the `<svg>`.
|
|
36
|
+
For example, if we wanted to set `width` and `height` we would do the
|
|
37
|
+
following:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
const { getAttributes } = require('@carbon/icon-helpers');
|
|
41
|
+
const attributes = getAttributes({ width: 20, height: 20 });
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
In order for the icon to be considered focusable, you will need to
|
|
45
|
+
provide either `aria-label`, `aria-labelledby`, or `title` in the
|
|
46
|
+
given `attributes` in addition to `tabindex`. For example:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const { getAttributes } = require('@carbon/icon-helpers');
|
|
50
|
+
const attributes = getAttributes({
|
|
51
|
+
'aria-label': 'My icon label',
|
|
52
|
+
tabindex: '0',
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Icon descriptors
|
|
57
|
+
|
|
58
|
+
An icon descriptor is the term we use to describe icon objects
|
|
59
|
+
exported by `@carbon/icons`. By default, they will have the following
|
|
60
|
+
shape:
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
{
|
|
64
|
+
elem: 'svg',
|
|
65
|
+
attrs: {
|
|
66
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
67
|
+
viewBox: '0 0 16 16',
|
|
68
|
+
width: 16,
|
|
69
|
+
height: 16,
|
|
70
|
+
},
|
|
71
|
+
content: [
|
|
72
|
+
{
|
|
73
|
+
elem: 'path',
|
|
74
|
+
attrs: {
|
|
75
|
+
d: '...',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
name: 'IconName',
|
|
80
|
+
size: 16,
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
You can import these definitions directly from `@carbon/icons` and use
|
|
85
|
+
them alongside `toSVG` or `toString` by doing:
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
import { IconName } from '@carbon/icons';
|
|
89
|
+
import { toString, toSVG } from '@carbon/icon-helpers';
|
|
90
|
+
|
|
91
|
+
const iconString = toString(IconName);
|
|
92
|
+
const iconSVG = toSVG({
|
|
93
|
+
...IconName,
|
|
94
|
+
attrs: {
|
|
95
|
+
...IconName.attrs,
|
|
96
|
+
myCustomAttribute: 'myCustomAttributeValue',
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 🙌 Contributing
|
|
102
|
+
|
|
103
|
+
We're always looking for contributors to help us fix bugs, build new
|
|
104
|
+
features, or help us improve the project documentation. If you're
|
|
105
|
+
interested, definitely check out our [Contributing Guide](/.github/CONTRIBUTING.md)
|
|
106
|
+
! 👀
|
|
107
|
+
|
|
108
|
+
## 📝 License
|
|
109
|
+
|
|
110
|
+
Licensed under the [Apache 2.0 License](/LICENSE).
|
package/es/index.js
CHANGED
|
@@ -68,13 +68,22 @@ function _objectWithoutProperties(source, excluded) {
|
|
|
68
68
|
return target;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Copyright IBM Corp. 2018, 2018
|
|
73
|
+
*
|
|
74
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
75
|
+
* LICENSE file in the root directory of this source tree.
|
|
76
|
+
*/
|
|
71
77
|
var defaultAttributes = {
|
|
72
78
|
// Reference:
|
|
73
79
|
// https://github.com/IBM/carbon-components-react/issues/1392
|
|
74
80
|
// https://github.com/PolymerElements/iron-iconset-svg/pull/47
|
|
75
81
|
// `focusable` is a string attribute which is why we do not use a boolean here
|
|
76
82
|
focusable: 'false',
|
|
77
|
-
preserveAspectRatio: 'xMidYMid meet'
|
|
83
|
+
preserveAspectRatio: 'xMidYMid meet',
|
|
84
|
+
// Reference:
|
|
85
|
+
// https://github.com/ckeditor/ckeditor5/issues/668#issuecomment-344844027
|
|
86
|
+
style: 'will-change: transform;'
|
|
78
87
|
};
|
|
79
88
|
/**
|
|
80
89
|
* Get supplementary HTML attributes for a given <svg> element based on existing
|
|
@@ -115,6 +124,12 @@ function getAttributes() {
|
|
|
115
124
|
return iconAttributes;
|
|
116
125
|
}
|
|
117
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Copyright IBM Corp. 2018, 2018
|
|
129
|
+
*
|
|
130
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
131
|
+
* LICENSE file in the root directory of this source tree.
|
|
132
|
+
*/
|
|
118
133
|
/**
|
|
119
134
|
* Convert an icon descriptor to a String
|
|
120
135
|
*/
|
|
@@ -146,6 +161,12 @@ function formatAttributes(attrs) {
|
|
|
146
161
|
}, '');
|
|
147
162
|
}
|
|
148
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Copyright IBM Corp. 2018, 2018
|
|
166
|
+
*
|
|
167
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
168
|
+
* LICENSE file in the root directory of this source tree.
|
|
169
|
+
*/
|
|
149
170
|
/**
|
|
150
171
|
* Convert an icon descriptor to a DOM node.
|
|
151
172
|
*/
|
|
@@ -158,7 +179,8 @@ function toSVG(descriptor) {
|
|
|
158
179
|
_descriptor$content = descriptor.content,
|
|
159
180
|
content = _descriptor$content === void 0 ? [] : _descriptor$content;
|
|
160
181
|
var node = document.createElementNS('http://www.w3.org/2000/svg', elem);
|
|
161
|
-
|
|
182
|
+
var attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
|
|
183
|
+
Object.keys(attributes).forEach(function (key) {
|
|
162
184
|
node.setAttribute(key, attrs[key]);
|
|
163
185
|
});
|
|
164
186
|
|
|
@@ -169,4 +191,11 @@ function toSVG(descriptor) {
|
|
|
169
191
|
return node;
|
|
170
192
|
}
|
|
171
193
|
|
|
172
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Copyright IBM Corp. 2018, 2018
|
|
196
|
+
*
|
|
197
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
198
|
+
* LICENSE file in the root directory of this source tree.
|
|
199
|
+
*/
|
|
200
|
+
|
|
201
|
+
export { defaultAttributes, getAttributes, formatAttributes, toString, toSVG };
|
package/lib/index.js
CHANGED
|
@@ -72,13 +72,22 @@ function _objectWithoutProperties(source, excluded) {
|
|
|
72
72
|
return target;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Copyright IBM Corp. 2018, 2018
|
|
77
|
+
*
|
|
78
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
79
|
+
* LICENSE file in the root directory of this source tree.
|
|
80
|
+
*/
|
|
75
81
|
var defaultAttributes = {
|
|
76
82
|
// Reference:
|
|
77
83
|
// https://github.com/IBM/carbon-components-react/issues/1392
|
|
78
84
|
// https://github.com/PolymerElements/iron-iconset-svg/pull/47
|
|
79
85
|
// `focusable` is a string attribute which is why we do not use a boolean here
|
|
80
86
|
focusable: 'false',
|
|
81
|
-
preserveAspectRatio: 'xMidYMid meet'
|
|
87
|
+
preserveAspectRatio: 'xMidYMid meet',
|
|
88
|
+
// Reference:
|
|
89
|
+
// https://github.com/ckeditor/ckeditor5/issues/668#issuecomment-344844027
|
|
90
|
+
style: 'will-change: transform;'
|
|
82
91
|
};
|
|
83
92
|
/**
|
|
84
93
|
* Get supplementary HTML attributes for a given <svg> element based on existing
|
|
@@ -119,6 +128,12 @@ function getAttributes() {
|
|
|
119
128
|
return iconAttributes;
|
|
120
129
|
}
|
|
121
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Copyright IBM Corp. 2018, 2018
|
|
133
|
+
*
|
|
134
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
135
|
+
* LICENSE file in the root directory of this source tree.
|
|
136
|
+
*/
|
|
122
137
|
/**
|
|
123
138
|
* Convert an icon descriptor to a String
|
|
124
139
|
*/
|
|
@@ -150,6 +165,12 @@ function formatAttributes(attrs) {
|
|
|
150
165
|
}, '');
|
|
151
166
|
}
|
|
152
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Copyright IBM Corp. 2018, 2018
|
|
170
|
+
*
|
|
171
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
172
|
+
* LICENSE file in the root directory of this source tree.
|
|
173
|
+
*/
|
|
153
174
|
/**
|
|
154
175
|
* Convert an icon descriptor to a DOM node.
|
|
155
176
|
*/
|
|
@@ -162,7 +183,8 @@ function toSVG(descriptor) {
|
|
|
162
183
|
_descriptor$content = descriptor.content,
|
|
163
184
|
content = _descriptor$content === void 0 ? [] : _descriptor$content;
|
|
164
185
|
var node = document.createElementNS('http://www.w3.org/2000/svg', elem);
|
|
165
|
-
|
|
186
|
+
var attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
|
|
187
|
+
Object.keys(attributes).forEach(function (key) {
|
|
166
188
|
node.setAttribute(key, attrs[key]);
|
|
167
189
|
});
|
|
168
190
|
|
|
@@ -173,6 +195,14 @@ function toSVG(descriptor) {
|
|
|
173
195
|
return node;
|
|
174
196
|
}
|
|
175
197
|
|
|
198
|
+
/**
|
|
199
|
+
* Copyright IBM Corp. 2018, 2018
|
|
200
|
+
*
|
|
201
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
202
|
+
* LICENSE file in the root directory of this source tree.
|
|
203
|
+
*/
|
|
204
|
+
|
|
205
|
+
exports.defaultAttributes = defaultAttributes;
|
|
176
206
|
exports.getAttributes = getAttributes;
|
|
177
207
|
exports.formatAttributes = formatAttributes;
|
|
178
208
|
exports.toString = toString;
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carbon/icon-helpers",
|
|
3
|
-
"
|
|
3
|
+
"description": "Helpers used alongside icons for digital and software products using the Carbon Design System",
|
|
4
|
+
"version": "0.0.1-alpha.26",
|
|
4
5
|
"license": "Apache-2.0",
|
|
5
6
|
"main": "lib/index.js",
|
|
6
7
|
"module": "es/index.js",
|
|
@@ -26,7 +27,7 @@
|
|
|
26
27
|
"clean": "rimraf es lib umd"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"@carbon/bundler": "0.0.1-alpha.
|
|
30
|
+
"@carbon/bundler": "0.0.1-alpha.26",
|
|
30
31
|
"rimraf": "^2.6.2"
|
|
31
32
|
},
|
|
32
33
|
"sideEffects": false
|
package/umd/index.js
CHANGED
|
@@ -74,13 +74,22 @@
|
|
|
74
74
|
return target;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Copyright IBM Corp. 2018, 2018
|
|
79
|
+
*
|
|
80
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
81
|
+
* LICENSE file in the root directory of this source tree.
|
|
82
|
+
*/
|
|
77
83
|
var defaultAttributes = {
|
|
78
84
|
// Reference:
|
|
79
85
|
// https://github.com/IBM/carbon-components-react/issues/1392
|
|
80
86
|
// https://github.com/PolymerElements/iron-iconset-svg/pull/47
|
|
81
87
|
// `focusable` is a string attribute which is why we do not use a boolean here
|
|
82
88
|
focusable: 'false',
|
|
83
|
-
preserveAspectRatio: 'xMidYMid meet'
|
|
89
|
+
preserveAspectRatio: 'xMidYMid meet',
|
|
90
|
+
// Reference:
|
|
91
|
+
// https://github.com/ckeditor/ckeditor5/issues/668#issuecomment-344844027
|
|
92
|
+
style: 'will-change: transform;'
|
|
84
93
|
};
|
|
85
94
|
/**
|
|
86
95
|
* Get supplementary HTML attributes for a given <svg> element based on existing
|
|
@@ -121,6 +130,12 @@
|
|
|
121
130
|
return iconAttributes;
|
|
122
131
|
}
|
|
123
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Copyright IBM Corp. 2018, 2018
|
|
135
|
+
*
|
|
136
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
137
|
+
* LICENSE file in the root directory of this source tree.
|
|
138
|
+
*/
|
|
124
139
|
/**
|
|
125
140
|
* Convert an icon descriptor to a String
|
|
126
141
|
*/
|
|
@@ -152,6 +167,12 @@
|
|
|
152
167
|
}, '');
|
|
153
168
|
}
|
|
154
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Copyright IBM Corp. 2018, 2018
|
|
172
|
+
*
|
|
173
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
174
|
+
* LICENSE file in the root directory of this source tree.
|
|
175
|
+
*/
|
|
155
176
|
/**
|
|
156
177
|
* Convert an icon descriptor to a DOM node.
|
|
157
178
|
*/
|
|
@@ -164,7 +185,8 @@
|
|
|
164
185
|
_descriptor$content = descriptor.content,
|
|
165
186
|
content = _descriptor$content === void 0 ? [] : _descriptor$content;
|
|
166
187
|
var node = document.createElementNS('http://www.w3.org/2000/svg', elem);
|
|
167
|
-
|
|
188
|
+
var attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
|
|
189
|
+
Object.keys(attributes).forEach(function (key) {
|
|
168
190
|
node.setAttribute(key, attrs[key]);
|
|
169
191
|
});
|
|
170
192
|
|
|
@@ -175,6 +197,14 @@
|
|
|
175
197
|
return node;
|
|
176
198
|
}
|
|
177
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Copyright IBM Corp. 2018, 2018
|
|
202
|
+
*
|
|
203
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
204
|
+
* LICENSE file in the root directory of this source tree.
|
|
205
|
+
*/
|
|
206
|
+
|
|
207
|
+
exports.defaultAttributes = defaultAttributes;
|
|
178
208
|
exports.getAttributes = getAttributes;
|
|
179
209
|
exports.formatAttributes = formatAttributes;
|
|
180
210
|
exports.toString = toString;
|