@carbon/icon-helpers
Version:
Helpers used alongside icons for digital and software products using the Carbon Design System
179 lines (170 loc) • 6.03 kB
JavaScript
function _defineProperty(e, r, t) {
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
value: t,
enumerable: !0,
configurable: !0,
writable: !0
}) : e[r] = t, e;
}
function ownKeys(e, r) {
var t = Object.keys(e);
if (Object.getOwnPropertySymbols) {
var o = Object.getOwnPropertySymbols(e);
r && (o = o.filter(function (r) {
return Object.getOwnPropertyDescriptor(e, r).enumerable;
})), t.push.apply(t, o);
}
return t;
}
function _objectSpread2(e) {
for (var r = 1; r < arguments.length; r++) {
var t = null != arguments[r] ? arguments[r] : {};
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
_defineProperty(e, r, t[r]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
});
}
return e;
}
function _objectWithoutProperties(e, t) {
if (null == e) return {};
var o,
r,
i = _objectWithoutPropertiesLoose(e, t);
if (Object.getOwnPropertySymbols) {
var n = Object.getOwnPropertySymbols(e);
for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
}
return i;
}
function _objectWithoutPropertiesLoose(r, e) {
if (null == r) return {};
var t = {};
for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
if (-1 !== e.indexOf(n)) continue;
t[n] = r[n];
}
return t;
}
function _toPrimitive(t, r) {
if ("object" != typeof t || !t) return t;
var e = t[Symbol.toPrimitive];
if (void 0 !== e) {
var i = e.call(t, r || "default");
if ("object" != typeof i) return i;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return ("string" === r ? String : Number)(t);
}
function _toPropertyKey(t) {
var i = _toPrimitive(t, "string");
return "symbol" == typeof i ? i : i + "";
}
var _excluded = ["width", "height", "viewBox"],
_excluded2 = ["tabindex"];
/**
* Copyright IBM Corp. 2018, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var defaultAttributes = {
// Reference:
// https://github.com/IBM/carbon-components-react/issues/1392
// https://github.com/PolymerElements/iron-iconset-svg/pull/47
// `focusable` is a string attribute which is why we do not use a boolean here
focusable: 'false',
preserveAspectRatio: 'xMidYMid meet'
};
/**
* Get supplementary HTML attributes for a given <svg> element based on existing
* attributes.
*/
function getAttributes() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
width = _ref.width,
height = _ref.height,
_ref$viewBox = _ref.viewBox,
viewBox = _ref$viewBox === void 0 ? "0 0 ".concat(width, " ").concat(height) : _ref$viewBox,
attributes = _objectWithoutProperties(_ref, _excluded);
var tabindex = attributes.tabindex,
rest = _objectWithoutProperties(attributes, _excluded2);
var iconAttributes = _objectSpread2(_objectSpread2(_objectSpread2({}, defaultAttributes), rest), {}, {
width: width,
height: height,
viewBox: viewBox
});
// TODO: attributes.title assumes that the consumer will implement <title> and
// correctly set `aria-labelledby`.
if (iconAttributes['aria-label'] || iconAttributes['aria-labelledby'] || iconAttributes.title) {
iconAttributes.role = 'img';
// Reference:
// https://allyjs.io/tutorials/focusing-in-svg.html
if (tabindex !== undefined && tabindex !== null) {
iconAttributes.focusable = 'true';
iconAttributes.tabindex = tabindex;
}
} else {
iconAttributes['aria-hidden'] = true;
}
return iconAttributes;
}
/**
* Copyright IBM Corp. 2018, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Convert an icon descriptor to a String
*/
function toString(descriptor) {
var _descriptor$elem = descriptor.elem,
elem = _descriptor$elem === void 0 ? 'svg' : _descriptor$elem,
_descriptor$attrs = descriptor.attrs,
attrs = _descriptor$attrs === void 0 ? {} : _descriptor$attrs,
_descriptor$content = descriptor.content,
content = _descriptor$content === void 0 ? [] : _descriptor$content;
var children = content.map(toString).join('');
if (elem !== 'svg') {
return "<".concat(elem, " ").concat(formatAttributes(attrs), ">").concat(children, "</").concat(elem, ">");
}
return "<".concat(elem, " ").concat(formatAttributes(getAttributes(attrs)), ">").concat(children, "</").concat(elem, ">");
}
function formatAttributes(attrs) {
return Object.keys(attrs).reduce(function (acc, key, index) {
var attribute = "".concat(key, "=\"").concat(attrs[key], "\"");
if (index === 0) {
return attribute;
}
return acc + ' ' + attribute;
}, '');
}
/**
* Copyright IBM Corp. 2018, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Convert an icon descriptor to a DOM node.
*/
function toSVG(descriptor) {
var _descriptor$elem = descriptor.elem,
elem = _descriptor$elem === void 0 ? 'svg' : _descriptor$elem,
_descriptor$attrs = descriptor.attrs,
attrs = _descriptor$attrs === void 0 ? {} : _descriptor$attrs,
_descriptor$content = descriptor.content,
content = _descriptor$content === void 0 ? [] : _descriptor$content;
var node = document.createElementNS('http://www.w3.org/2000/svg', elem);
var attributes = elem !== 'svg' ? attrs : getAttributes(attrs);
Object.keys(attributes).forEach(function (key) {
node.setAttribute(key, attrs[key]);
});
for (var i = 0; i < content.length; i++) {
node.appendChild(toSVG(content[i]));
}
return node;
}
export { defaultAttributes, formatAttributes, getAttributes, toSVG, toString };