UNPKG

@spark-web/next-utils

Version:

--- title: Next.js Utils isExperimentalPackage: true ---

97 lines (89 loc) 3.72 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _objectSpread = require('@babel/runtime/helpers/objectSpread2'); var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator'); var _regeneratorRuntime = require('@babel/runtime/regenerator'); var ssr = require('@spark-web/ssr'); var jsxRuntime = require('react/jsx-runtime'); var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties'); var link = require('@spark-web/link'); var NextLink = require('next/link'); function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime); var NextLink__default = /*#__PURE__*/_interopDefault(NextLink); function propsWithCssText(_x) { return _propsWithCssText.apply(this, arguments); } function _propsWithCssText() { _propsWithCssText = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee( /** Value returned from the getInitialProps static method on the Document class (in next/document). */ initialProps) { var _yield$renderStatic, css, ids; return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return ssr.renderStatic(initialProps.html); case 2: _yield$renderStatic = _context.sent; css = _yield$renderStatic.css; ids = _yield$renderStatic.ids; return _context.abrupt("return", _objectSpread(_objectSpread({}, initialProps), {}, { styles: /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [initialProps.styles, /*#__PURE__*/jsxRuntime.jsx("style", { "data-emotion": "css ".concat(ids.join(' ')), dangerouslySetInnerHTML: { __html: css } })] }) })); case 6: case "end": return _context.stop(); } } }, _callee); })); return _propsWithCssText.apply(this, arguments); } var _excluded = ["href", "onClick", "rel"]; /** * Resolves internal links using the * [Next.js Link component](https://nextjs.org/docs/api-reference/next/link), which * expects "href" to begin with a slash e.g. `href="/page"`. Uses a traditional * anchor element for everything else e.g. external, hash, tel, mailto. * * For compatibility with TS + Spark the "href" property may only accept a * string, so URL Objects must be resolved ahead of time. We recommend the [url * package](https://www.npmjs.com/package/url) for complex cases, though most of * the time it's simple to do this manually. */ var UniversalNextLink = link.makeLinkComponent(function (_ref, ref) { var href = _ref.href, _onClick = _ref.onClick, rel = _ref.rel, props = _objectWithoutProperties(_ref, _excluded); var shouldUseNext = href[0] === '/'; return shouldUseNext ? /*#__PURE__*/jsxRuntime.jsx(NextLink__default["default"], { href: href, passHref: true, children: /*#__PURE__*/jsxRuntime.jsx("a", _objectSpread({ ref: ref }, props)) }) : /*#__PURE__*/jsxRuntime.jsx("a", _objectSpread({ ref: ref, href: href, rel: rel || 'noreferrer noopener', onClick: function onClick(event) { if (href === '' || href === '#') { event.preventDefault(); } if (typeof _onClick === 'function') { _onClick(event); } } }, props)); }); exports.UniversalNextLink = UniversalNextLink; exports.propsWithCssText = propsWithCssText;