@servicenow/ui-renderer-snabbdom
Version:
Snabbdom renderer for UI Framework on Next Experience
107 lines (92 loc) • 4.28 kB
JavaScript
/**
* Copyright (c) 2020 ServiceNow, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _uiInternal = require("@servicenow/ui-internal");
var _accessibilityTargets = _interopRequireDefault(require("../utils/accessibilityTargets"));
var _ariaRefAssignments = _interopRequireDefault(require("../utils/ariaRefAssignments"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* Checks for aria attributes that are set to an aria ref,
* and replaces the value with a generated id that will be
* later assigned to a clone of the element the aria ref
* is referencing.
*/
var checkForAriaAttributes = function checkForAriaAttributes(oldVnode, vnode) {
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
destroy = _ref.destroy;
if (!process.env.UNSAFE_ENABLE_SNABBDOM_ACCESSIBILITY) return;
var attrs = vnode.data.attrs;
var oldAriaAttrs = oldVnode && oldVnode.data && oldVnode.data.aria_attrs || {};
for (var key in oldAriaAttrs) {
if (oldVnode.data.aria_refs && oldVnode.data.aria_refs[key]) oldVnode.data.aria_refs[key].unsubscribe({
element: vnode.elm,
attribute: key
});
}
for (var _key in attrs) {
if (_key.startsWith('aria-') && attrs[_key].isAriaRef) {
if (!vnode.data.aria_refs) vnode.data.aria_refs = {};
if (!vnode.data.aria_attrs) vnode.data.aria_attrs = {};
var id = "a".concat((0, _uiInternal.guid)()); // guid() sometimes starts with a number which isn't allowed for ids
var targetRef = attrs[_key];
vnode.data.aria_refs[_key] = targetRef;
vnode.data.aria_attrs[_key] = true;
attrs[_key] = id;
_accessibilityTargets["default"][_accessibilityTargets["default"].length - 1].push({
element: vnode.elm,
targetRef: targetRef,
id: id,
ariaAttribute: _key
});
}
}
var ariaRef = vnode.data['now-aria-ref'];
if (ariaRef && _typeof(ariaRef) === 'object') {
// Queue the ref to be set after the patch is complete, otherwise
// the element won't have its entire DOM structure and can't be accurately
// cloned to other shadowRoots
_ariaRefAssignments["default"][_ariaRefAssignments["default"].length - 1].push({
ariaRef: ariaRef,
element: destroy ? null : vnode.elm
});
}
};
var _default = {
create: function create(oldVnode, vnode) {
return checkForAriaAttributes(null, vnode);
},
update: function update(oldVnode, vnode) {
return checkForAriaAttributes(oldVnode, vnode);
},
destroy: function destroy(vnode) {
return checkForAriaAttributes(null, vnode, {
destroy: true
});
}
};
exports["default"] = _default;
//# sourceMappingURL=aria.js.map