UNPKG

@geist-ui/react

Version:

Modern and minimalist React UI library.

181 lines (143 loc) 4.77 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } 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 _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."); } var getId = function getId() { return Math.random().toString(32).slice(2, 10); }; var isBrowser = function isBrowser() { return Boolean(typeof window !== 'undefined' && window.document && window.document.createElement); }; var useSSR = function useSSR() { var _useState = React.useState(false), _useState2 = _slicedToArray(_useState, 2), browser = _useState2[0], setBrowser = _useState2[1]; React.useEffect(function () { setBrowser(isBrowser()); }, []); return { isBrowser: browser, isServer: !browser }; }; var createElement = function createElement(id) { var el = document.createElement('div'); el.setAttribute('id', id); return el; }; var usePortal = function usePortal() { var selectId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getId(); var getContainer = arguments.length > 1 ? arguments[1] : undefined; var id = "geist-ui-".concat(selectId); var _useSSR = useSSR(), isBrowser = _useSSR.isBrowser; var _useState = React.useState(isBrowser ? createElement(id) : null), _useState2 = _slicedToArray(_useState, 2), elSnapshot = _useState2[0], setElSnapshot = _useState2[1]; React.useEffect(function () { var customContainer = getContainer ? getContainer() : null; var parentElement = customContainer || document.body; var hasElement = parentElement.querySelector("#".concat(id)); var el = hasElement || createElement(id); if (!hasElement) { parentElement.appendChild(el); } setElSnapshot(el); }, []); return elSnapshot; }; var warningStack = {}; var useWarning = function useWarning(message, component) { var tag = component ? " [".concat(component, "]") : ' '; var log = "[Geist UI]".concat(tag, ": ").concat(message); if (typeof console === 'undefined') return; if (warningStack[log]) return; warningStack[log] = true; if (process.env.NODE_ENV !== 'production') { return console.error(log); } console.warn(log); }; var defaultOptions = { onError: function onError() { return useWarning('Failed to copy.', 'use-clipboard'); } }; var useClipboard = function useClipboard() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultOptions; var el = usePortal('clipboard'); var copyText = function copyText(el, text) { if (!el || !text) return; var selection = window.getSelection(); if (!selection) return; el.style.whiteSpace = 'pre'; el.textContent = text; var range = window.document.createRange(); selection.removeAllRanges(); range.selectNode(el); selection.addRange(range); try { window.document.execCommand('copy'); } catch (e) { options.onError && options.onError(); } selection.removeAllRanges(); if (el) { el.textContent = ''; } }; var copy = React.useCallback(function (text) { copyText(el, text); }, [el]); return { copy: copy }; }; exports["default"] = useClipboard;