UNPKG

design-system-govbr-rnp

Version:

## Padrão Digital de Governo - React Biblioteca de componentes React que implementa o [Padrão Visual digital do governo](https://www.gov.br/ds/). O objetivo da biblioteca é facilitar a implementação e promover a padronização das interfaces de sistemas do

117 lines (115 loc) 7.34 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _Tag = _interopRequireDefault(require("../Tag/Tag")); var _types = require("./types"); var _styles = require("./styles"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var SkipLink = function SkipLink(_ref) { var options = _ref.options, type = _ref.type; var refCompound = (0, _react.useRef)(); var skipLinks = document.querySelectorAll(".skipLink"); var keyPress = useKeyPress("Tab"); // hook function useKeyPress(targetKey) { var _useState = (0, _react.useState)(false), _useState2 = _slicedToArray(_useState, 2), keyPressed = _useState2[0], setKeyPressed = _useState2[1]; var _useState3 = (0, _react.useState)(), _useState4 = _slicedToArray(_useState3, 2), srcElement = _useState4[0], setSrcElement = _useState4[1]; var _useState5 = (0, _react.useState)(false), _useState6 = _slicedToArray(_useState5, 2), shiftKey = _useState6[0], setShiftKey = _useState6[1]; var downHandler = (0, _react.useCallback)(function (_ref2) { var key = _ref2.key, srcElement = _ref2.srcElement, shiftKey = _ref2.shiftKey; if (key === targetKey) { setKeyPressed(true); setSrcElement(srcElement); setShiftKey(shiftKey); } }, [targetKey]); var upHandler = (0, _react.useCallback)(function (_ref3) { var key = _ref3.key; if (key === targetKey) { setKeyPressed(false); } }, [targetKey]); (0, _react.useEffect)(function () { window.addEventListener("keydown", downHandler); window.addEventListener("keyup", upHandler); return function () { window.removeEventListener("keydown", downHandler); window.removeEventListener("keyup", upHandler); }; }, [downHandler, upHandler]); return { keyPressed: keyPressed, srcElement: srcElement, shiftKey: shiftKey }; } var accessibilityCompound = function accessibilityCompound(srcElement, shiftKey, skipLinks) { var _refCompound$current; (_refCompound$current = refCompound.current) === null || _refCompound$current === void 0 ? void 0 : _refCompound$current.classList.add("show"); if (!shiftKey) { if (srcElement === skipLinks[skipLinks.length - 1]) { var _refCompound$current2; (_refCompound$current2 = refCompound.current) === null || _refCompound$current2 === void 0 ? void 0 : _refCompound$current2.classList.remove("show"); } } }; if (type === _types.compound) { keyPress.keyPressed && accessibilityCompound(keyPress.srcElement, keyPress.shiftKey, skipLinks); document.addEventListener("click", function () { var _refCompound$current3; (_refCompound$current3 = refCompound.current) === null || _refCompound$current3 === void 0 ? void 0 : _refCompound$current3.classList.remove("show"); }); } var renderSkipLink = function renderSkipLink() { return options.map(function (option, index) { return /*#__PURE__*/_react.default.createElement(_styles.SkipLinkContainer, { type: type, key: option.link, className: "skipLink", href: "".concat(option.link), target: "_self" }, /*#__PURE__*/_react.default.createElement("span", { className: "text" }, "".concat(option.text, " (").concat(index + 1, "/").concat(options.length, ")")), /*#__PURE__*/_react.default.createElement(_Tag.default, { bgColor: "var(--blue-warm-vivid-70)", density: "medium", label: "".concat(index + 1), type: "number" })); }); }; return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, type === _types.compound ? /*#__PURE__*/_react.default.createElement(_styles.SkipLinkCompound, { ref: refCompound }, renderSkipLink()) : renderSkipLink()); }; SkipLink.propTypes = { options: _propTypes.default.array, type: _propTypes.default.string.isRequired }; var _default = SkipLink; exports.default = _default;