@carbon/icon-helpers 0.0.1-alpha.9 → 0.0.1-beta.0

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2018 IBM Corp.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,22 +1,110 @@
1
- # `@carbon/icon-helpers`
1
+ # @carbon/icon-helpers
2
2
 
3
- > Helpers used alongside icons for digital and software products using the Carbon Design System.
3
+ > Helpers used alongside icons for digital and software products using
4
+ > the Carbon Design System
4
5
 
5
- ## Getting Started
6
+ ## Getting started
6
7
 
7
- Run the following command using [npm](https://www.npmjs.com/):
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 command
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
- ## 🤲 Contributing
22
+ ## Usage
21
23
 
22
- To learn more about how to contribute, look [here](/.github/CONTRIBUTING.md)!
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,12 +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
- focusable: false,
76
- preserveAspectRatio: 'xMidYMid meet'
81
+ // `focusable` is a string attribute which is why we do not use a boolean here
82
+ focusable: 'false',
83
+ preserveAspectRatio: 'xMidYMid meet',
84
+ // Reference:
85
+ // https://github.com/ckeditor/ckeditor5/issues/668#issuecomment-344844027
86
+ style: 'will-change: transform;'
77
87
  };
78
88
  /**
79
89
  * Get supplementary HTML attributes for a given <svg> element based on existing
@@ -88,7 +98,10 @@ function getAttributes() {
88
98
  viewBox = _ref$viewBox === void 0 ? "0 0 ".concat(width, " ").concat(height) : _ref$viewBox,
89
99
  attributes = _objectWithoutProperties(_ref, ["width", "height", "viewBox"]);
90
100
 
91
- var iconAttributes = _objectSpread({}, defaultAttributes, attributes, {
101
+ var tabindex = attributes.tabindex,
102
+ rest = _objectWithoutProperties(attributes, ["tabindex"]);
103
+
104
+ var iconAttributes = _objectSpread({}, defaultAttributes, rest, {
92
105
  width: width,
93
106
  height: height,
94
107
  viewBox: viewBox
@@ -96,9 +109,14 @@ function getAttributes() {
96
109
  // correctly set `aria-labelledby`.
97
110
 
98
111
 
99
- if (attributes['aria-label'] || attributes['aria-labelledby'] || attributes.title) {
100
- iconAttributes.role = 'img';
101
- iconAttributes.focusable = true;
112
+ if (iconAttributes['aria-label'] || iconAttributes['aria-labelledby'] || iconAttributes.title) {
113
+ iconAttributes.role = 'img'; // Reference:
114
+ // https://allyjs.io/tutorials/focusing-in-svg.html
115
+
116
+ if (tabindex !== undefined && tabindex !== null) {
117
+ iconAttributes.focusable = 'true';
118
+ iconAttributes.tabindex = tabindex;
119
+ }
102
120
  } else {
103
121
  iconAttributes['aria-hidden'] = true;
104
122
  }
@@ -106,6 +124,12 @@ function getAttributes() {
106
124
  return iconAttributes;
107
125
  }
108
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
+ */
109
133
  /**
110
134
  * Convert an icon descriptor to a String
111
135
  */
@@ -137,6 +161,12 @@ function formatAttributes(attrs) {
137
161
  }, '');
138
162
  }
139
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
+ */
140
170
  /**
141
171
  * Convert an icon descriptor to a DOM node.
142
172
  */
@@ -149,7 +179,8 @@ function toSVG(descriptor) {
149
179
  _descriptor$content = descriptor.content,
150
180
  content = _descriptor$content === void 0 ? [] : _descriptor$content;
151
181
  var node = document.createElementNS('http://www.w3.org/2000/svg', elem);
152
- Object.keys(elem !== 'svg' ? attrs : getAttributes(attrs)).forEach(function (key) {
182
+ var attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
183
+ Object.keys(attributes).forEach(function (key) {
153
184
  node.setAttribute(key, attrs[key]);
154
185
  });
155
186
 
@@ -160,4 +191,11 @@ function toSVG(descriptor) {
160
191
  return node;
161
192
  }
162
193
 
163
- export { getAttributes, toString, toSVG };
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,12 +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
- focusable: false,
80
- preserveAspectRatio: 'xMidYMid meet'
85
+ // `focusable` is a string attribute which is why we do not use a boolean here
86
+ focusable: 'false',
87
+ preserveAspectRatio: 'xMidYMid meet',
88
+ // Reference:
89
+ // https://github.com/ckeditor/ckeditor5/issues/668#issuecomment-344844027
90
+ style: 'will-change: transform;'
81
91
  };
82
92
  /**
83
93
  * Get supplementary HTML attributes for a given <svg> element based on existing
@@ -92,7 +102,10 @@ function getAttributes() {
92
102
  viewBox = _ref$viewBox === void 0 ? "0 0 ".concat(width, " ").concat(height) : _ref$viewBox,
93
103
  attributes = _objectWithoutProperties(_ref, ["width", "height", "viewBox"]);
94
104
 
95
- var iconAttributes = _objectSpread({}, defaultAttributes, attributes, {
105
+ var tabindex = attributes.tabindex,
106
+ rest = _objectWithoutProperties(attributes, ["tabindex"]);
107
+
108
+ var iconAttributes = _objectSpread({}, defaultAttributes, rest, {
96
109
  width: width,
97
110
  height: height,
98
111
  viewBox: viewBox
@@ -100,9 +113,14 @@ function getAttributes() {
100
113
  // correctly set `aria-labelledby`.
101
114
 
102
115
 
103
- if (attributes['aria-label'] || attributes['aria-labelledby'] || attributes.title) {
104
- iconAttributes.role = 'img';
105
- iconAttributes.focusable = true;
116
+ if (iconAttributes['aria-label'] || iconAttributes['aria-labelledby'] || iconAttributes.title) {
117
+ iconAttributes.role = 'img'; // Reference:
118
+ // https://allyjs.io/tutorials/focusing-in-svg.html
119
+
120
+ if (tabindex !== undefined && tabindex !== null) {
121
+ iconAttributes.focusable = 'true';
122
+ iconAttributes.tabindex = tabindex;
123
+ }
106
124
  } else {
107
125
  iconAttributes['aria-hidden'] = true;
108
126
  }
@@ -110,6 +128,12 @@ function getAttributes() {
110
128
  return iconAttributes;
111
129
  }
112
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
+ */
113
137
  /**
114
138
  * Convert an icon descriptor to a String
115
139
  */
@@ -141,6 +165,12 @@ function formatAttributes(attrs) {
141
165
  }, '');
142
166
  }
143
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
+ */
144
174
  /**
145
175
  * Convert an icon descriptor to a DOM node.
146
176
  */
@@ -153,7 +183,8 @@ function toSVG(descriptor) {
153
183
  _descriptor$content = descriptor.content,
154
184
  content = _descriptor$content === void 0 ? [] : _descriptor$content;
155
185
  var node = document.createElementNS('http://www.w3.org/2000/svg', elem);
156
- Object.keys(elem !== 'svg' ? attrs : getAttributes(attrs)).forEach(function (key) {
186
+ var attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
187
+ Object.keys(attributes).forEach(function (key) {
157
188
  node.setAttribute(key, attrs[key]);
158
189
  });
159
190
 
@@ -164,6 +195,15 @@ function toSVG(descriptor) {
164
195
  return node;
165
196
  }
166
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;
167
206
  exports.getAttributes = getAttributes;
207
+ exports.formatAttributes = formatAttributes;
168
208
  exports.toString = toString;
169
209
  exports.toSVG = toSVG;
package/package.json CHANGED
@@ -1,11 +1,24 @@
1
1
  {
2
2
  "name": "@carbon/icon-helpers",
3
- "version": "0.0.1-alpha.9",
3
+ "description": "Helpers used alongside icons for digital and software products using the Carbon Design System",
4
+ "version": "0.0.1-beta.0",
5
+ "license": "Apache-2.0",
4
6
  "main": "lib/index.js",
5
7
  "module": "es/index.js",
6
- "license": "Apache-2.0",
7
8
  "repository": "https://github.com/IBM/carbon-elements/tree/master/packages/icon-helpers",
8
9
  "bugs": "https://github.com/IBM/carbon-elements/issues",
10
+ "files": [
11
+ "es",
12
+ "lib",
13
+ "umd"
14
+ ],
15
+ "keywords": [
16
+ "ibm",
17
+ "elements",
18
+ "carbon",
19
+ "carbon-elements",
20
+ "carbon-design-system"
21
+ ],
9
22
  "publishConfig": {
10
23
  "access": "public"
11
24
  },
@@ -14,7 +27,9 @@
14
27
  "clean": "rimraf es lib umd"
15
28
  },
16
29
  "devDependencies": {
17
- "@carbon/bundler": "0.0.1-alpha.9",
30
+ "@carbon/bundler": "0.0.1-beta.0",
18
31
  "rimraf": "^2.6.2"
19
- }
32
+ },
33
+ "sideEffects": false,
34
+ "gitHead": "0e128739939b4d581420c598dbc795e4160a0352"
20
35
  }
package/umd/index.js CHANGED
@@ -74,12 +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
- focusable: false,
82
- preserveAspectRatio: 'xMidYMid meet'
87
+ // `focusable` is a string attribute which is why we do not use a boolean here
88
+ focusable: 'false',
89
+ preserveAspectRatio: 'xMidYMid meet',
90
+ // Reference:
91
+ // https://github.com/ckeditor/ckeditor5/issues/668#issuecomment-344844027
92
+ style: 'will-change: transform;'
83
93
  };
84
94
  /**
85
95
  * Get supplementary HTML attributes for a given <svg> element based on existing
@@ -94,7 +104,10 @@
94
104
  viewBox = _ref$viewBox === void 0 ? "0 0 ".concat(width, " ").concat(height) : _ref$viewBox,
95
105
  attributes = _objectWithoutProperties(_ref, ["width", "height", "viewBox"]);
96
106
 
97
- var iconAttributes = _objectSpread({}, defaultAttributes, attributes, {
107
+ var tabindex = attributes.tabindex,
108
+ rest = _objectWithoutProperties(attributes, ["tabindex"]);
109
+
110
+ var iconAttributes = _objectSpread({}, defaultAttributes, rest, {
98
111
  width: width,
99
112
  height: height,
100
113
  viewBox: viewBox
@@ -102,9 +115,14 @@
102
115
  // correctly set `aria-labelledby`.
103
116
 
104
117
 
105
- if (attributes['aria-label'] || attributes['aria-labelledby'] || attributes.title) {
106
- iconAttributes.role = 'img';
107
- iconAttributes.focusable = true;
118
+ if (iconAttributes['aria-label'] || iconAttributes['aria-labelledby'] || iconAttributes.title) {
119
+ iconAttributes.role = 'img'; // Reference:
120
+ // https://allyjs.io/tutorials/focusing-in-svg.html
121
+
122
+ if (tabindex !== undefined && tabindex !== null) {
123
+ iconAttributes.focusable = 'true';
124
+ iconAttributes.tabindex = tabindex;
125
+ }
108
126
  } else {
109
127
  iconAttributes['aria-hidden'] = true;
110
128
  }
@@ -112,6 +130,12 @@
112
130
  return iconAttributes;
113
131
  }
114
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
+ */
115
139
  /**
116
140
  * Convert an icon descriptor to a String
117
141
  */
@@ -143,6 +167,12 @@
143
167
  }, '');
144
168
  }
145
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
+ */
146
176
  /**
147
177
  * Convert an icon descriptor to a DOM node.
148
178
  */
@@ -155,7 +185,8 @@
155
185
  _descriptor$content = descriptor.content,
156
186
  content = _descriptor$content === void 0 ? [] : _descriptor$content;
157
187
  var node = document.createElementNS('http://www.w3.org/2000/svg', elem);
158
- Object.keys(elem !== 'svg' ? attrs : getAttributes(attrs)).forEach(function (key) {
188
+ var attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
189
+ Object.keys(attributes).forEach(function (key) {
159
190
  node.setAttribute(key, attrs[key]);
160
191
  });
161
192
 
@@ -166,7 +197,16 @@
166
197
  return node;
167
198
  }
168
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;
169
208
  exports.getAttributes = getAttributes;
209
+ exports.formatAttributes = formatAttributes;
170
210
  exports.toString = toString;
171
211
  exports.toSVG = toSVG;
172
212
 
@@ -1,32 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- 'use strict';
6
-
7
- describe('getAttributes', () => {
8
- let getAttributes;
9
-
10
- beforeEach(() => {
11
- getAttributes = require('../getAttributes').default;
12
- });
13
-
14
- it('should default `aria-hidden="true"` and `focusable="false"` for SVGs', () => {
15
- const attrs = getAttributes();
16
- expect(attrs['aria-hidden']).toBe(true);
17
- expect(attrs.focusable).toBe(false);
18
- expect(attrs.role).not.toBeDefined();
19
- });
20
-
21
- test.each(['aria-label', 'aria-labelledby', 'title'])(
22
- 'should set role and remove aria-hidden if `%s` is set',
23
- attr => {
24
- const attrs = getAttributes({
25
- [attr]: 'attribute',
26
- });
27
- expect(attrs.role).toBe('img');
28
- expect(attrs.focusable).toBe(true);
29
- expect(attrs['aria-hidden']).not.toBeDefined();
30
- }
31
- );
32
- });
@@ -1,48 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- 'use strict';
6
-
7
- const defaultDescriptor = {
8
- name: 'default',
9
- size: '32',
10
- attrs: {
11
- xmlns: 'http://www.w3.org/2000/svg',
12
- viewBox: '0 0 32 32',
13
- width: '32',
14
- height: '32',
15
- },
16
- content: [
17
- {
18
- elem: 'path',
19
- attrs: {
20
- d:
21
- 'M12 11.03v4h-2v-4H8v6h4v4h2v-10h-2zm12.19 0H22l-3 4.39v-4.39h-2v10h2V18.3l.91-1.33L22 21.03h2.19l-2.99-5.62 2.99-4.38z',
22
- },
23
- },
24
- {
25
- elem: 'path',
26
- attrs: {
27
- d:
28
- 'M28 26H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h24a2 2 0 0 1 2 2v16a2 2 0 0 1-2 2zM4 8v16h24V8z',
29
- },
30
- },
31
- ],
32
- };
33
-
34
- describe('toSVG', () => {
35
- let toSVG;
36
-
37
- beforeEach(() => {
38
- toSVG = require('../toSVG').default;
39
- });
40
-
41
- it('should convert a given descriptor to a valid DOM node', () => {
42
- expect(() => {
43
- const node = toSVG(defaultDescriptor);
44
- document.body.appendChild(node);
45
- node.parentNode.removeChild(node);
46
- }).not.toThrow();
47
- });
48
- });
@@ -1,47 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- 'use strict';
6
-
7
- const defaultDescriptor = {
8
- name: 'default',
9
- size: '32',
10
- attrs: {
11
- xmlns: 'http://www.w3.org/2000/svg',
12
- viewBox: '0 0 32 32',
13
- width: '32',
14
- height: '32',
15
- },
16
- content: [
17
- {
18
- elem: 'path',
19
- attrs: {
20
- d:
21
- 'M12 11.03v4h-2v-4H8v6h4v4h2v-10h-2zm12.19 0H22l-3 4.39v-4.39h-2v10h2V18.3l.91-1.33L22 21.03h2.19l-2.99-5.62 2.99-4.38z',
22
- },
23
- },
24
- {
25
- elem: 'path',
26
- attrs: {
27
- d:
28
- 'M28 26H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h24a2 2 0 0 1 2 2v16a2 2 0 0 1-2 2zM4 8v16h24V8z',
29
- },
30
- },
31
- ],
32
- };
33
-
34
- describe('toString', () => {
35
- let toString;
36
-
37
- beforeEach(() => {
38
- toString = require('../toString').default;
39
- });
40
-
41
- it('should convert a given descriptor to a string', () => {
42
- expect(() => {
43
- toString(defaultDescriptor);
44
- }).not.toThrow();
45
- expect(typeof toString(defaultDescriptor)).toBe('string');
46
- });
47
- });
@@ -1,41 +0,0 @@
1
- const defaultAttributes = {
2
- // Reference:
3
- // https://github.com/IBM/carbon-components-react/issues/1392
4
- // https://github.com/PolymerElements/iron-iconset-svg/pull/47
5
- focusable: false,
6
- preserveAspectRatio: 'xMidYMid meet',
7
- };
8
-
9
- /**
10
- * Get supplementary HTML attributes for a given <svg> element based on existing
11
- * attributes.
12
- */
13
- export default function getAttributes({
14
- width,
15
- height,
16
- viewBox = `0 0 ${width} ${height}`,
17
- ...attributes
18
- } = {}) {
19
- const iconAttributes = {
20
- ...defaultAttributes,
21
- ...attributes,
22
- width,
23
- height,
24
- viewBox,
25
- };
26
-
27
- // TODO: attributes.title assumes that the consumer will implement <title> and
28
- // correctly set `aria-labelledby`.
29
- if (
30
- attributes['aria-label'] ||
31
- attributes['aria-labelledby'] ||
32
- attributes.title
33
- ) {
34
- iconAttributes.role = 'img';
35
- iconAttributes.focusable = true;
36
- } else {
37
- iconAttributes['aria-hidden'] = true;
38
- }
39
-
40
- return iconAttributes;
41
- }
package/src/index.js DELETED
@@ -1,5 +0,0 @@
1
- import getAttributes from './getAttributes';
2
- import toString from './toString';
3
- import toSVG from './toSVG';
4
-
5
- export { getAttributes, toString, toSVG };
package/src/toSVG.js DELETED
@@ -1,19 +0,0 @@
1
- import getAttributes from './getAttributes';
2
-
3
- /**
4
- * Convert an icon descriptor to a DOM node.
5
- */
6
- export default function toSVG(descriptor) {
7
- const { elem = 'svg', attrs = {}, content = [] } = descriptor;
8
- const node = document.createElementNS('http://www.w3.org/2000/svg', elem);
9
-
10
- Object.keys(elem !== 'svg' ? attrs : getAttributes(attrs)).forEach(key => {
11
- node.setAttribute(key, attrs[key]);
12
- });
13
-
14
- for (let i = 0; i < content.length; i++) {
15
- node.appendChild(toSVG(content[i]));
16
- }
17
-
18
- return node;
19
- }
package/src/toString.js DELETED
@@ -1,25 +0,0 @@
1
- import getAttributes from './getAttributes';
2
-
3
- /**
4
- * Convert an icon descriptor to a String
5
- */
6
- export default function toString(descriptor) {
7
- const { elem = 'svg', attrs = {}, content = [] } = descriptor;
8
- const children = content.map(toString).join('');
9
- if (elem !== 'svg') {
10
- return `<${elem} ${formatAttributes(attrs)}>${children}</${elem}>`;
11
- }
12
- return `<${elem} ${formatAttributes(
13
- getAttributes(attrs)
14
- )}>${children}</${elem}>`;
15
- }
16
-
17
- export function formatAttributes(attrs) {
18
- return Object.keys(attrs).reduce((acc, key, index) => {
19
- const attribute = `${key}="${attrs[key]}"`;
20
- if (index === 0) {
21
- return attribute;
22
- }
23
- return acc + ' ' + attribute;
24
- }, '');
25
- }