UNPKG

create-dan

Version:

Dan frameworks

1 lines 4.82 kB
{"ast":null,"code":"\"use strict\";\n\nvar _Set = require(\"@babel/runtime-corejs2/core-js/set\");\n\nvar _Object$defineProperty = require(\"@babel/runtime-corejs2/core-js/object/define-property\");\n\nvar __importDefault = this && this.__importDefault || function (mod) {\n return mod && mod.__esModule ? mod : {\n \"default\": mod\n };\n};\n\n_Object$defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nconst react_1 = __importDefault(require(\"react\"));\n\nconst side_effect_1 = __importDefault(require(\"./side-effect\"));\n\nconst amp_context_1 = require(\"./amp-context\");\n\nconst head_manager_context_1 = require(\"./head-manager-context\");\n\nconst amp_1 = require(\"./amp\");\n\nfunction defaultHead(inAmpMode = false) {\n const head = [react_1.default.createElement(\"meta\", {\n charSet: \"utf-8\"\n })];\n\n if (!inAmpMode) {\n head.push(react_1.default.createElement(\"meta\", {\n name: \"viewport\",\n content: \"width=device-width,minimum-scale=1,initial-scale=1\"\n }));\n }\n\n return head;\n}\n\nexports.defaultHead = defaultHead;\n\nfunction onlyReactElement(list, child) {\n // React children can be \"string\" or \"number\" in this case we ignore them for backwards compat\n if (typeof child === 'string' || typeof child === 'number') {\n return list;\n } // Adds support for React.Fragment\n\n\n if (child.type === react_1.default.Fragment) {\n return list.concat(react_1.default.Children.toArray(child.props.children).reduce((fragmentList, fragmentChild) => {\n if (typeof fragmentChild === 'string' || typeof fragmentChild === 'number') {\n return fragmentList;\n }\n\n return fragmentList.concat(fragmentChild);\n }, []));\n }\n\n return list.concat(child);\n}\n\nconst METATYPES = ['name', 'httpEquiv', 'charSet', 'itemProp'];\n/*\n returns a function for filtering head child elements\n which shouldn't be duplicated, like <title/>\n Also adds support for deduplicated `key` properties\n*/\n\nfunction unique() {\n const keys = new _Set();\n const tags = new _Set();\n const metaTypes = new _Set();\n const metaCategories = {};\n return h => {\n let unique = true;\n\n if (h.key && typeof h.key !== 'number' && h.key.indexOf('$') > 0) {\n const key = h.key.slice(h.key.indexOf('$') + 1);\n\n if (keys.has(key)) {\n unique = false;\n } else {\n keys.add(key);\n }\n } // eslint-disable-next-line default-case\n\n\n switch (h.type) {\n case 'title':\n case 'base':\n if (tags.has(h.type)) {\n unique = false;\n } else {\n tags.add(h.type);\n }\n\n break;\n\n case 'meta':\n for (let i = 0, len = METATYPES.length; i < len; i++) {\n const metatype = METATYPES[i];\n if (!h.props.hasOwnProperty(metatype)) continue;\n\n if (metatype === 'charSet') {\n if (metaTypes.has(metatype)) {\n unique = false;\n } else {\n metaTypes.add(metatype);\n }\n } else {\n const category = h.props[metatype];\n const categories = metaCategories[metatype] || new _Set();\n\n if (categories.has(category)) {\n unique = false;\n } else {\n categories.add(category);\n metaCategories[metatype] = categories;\n }\n }\n }\n\n break;\n }\n\n return unique;\n };\n}\n/**\n *\n * @param headElement List of multiple <Head> instances\n */\n\n\nfunction reduceComponents(headElements, props) {\n return headElements.reduce((list, headElement) => {\n const headElementChildren = react_1.default.Children.toArray(headElement.props.children);\n return list.concat(headElementChildren);\n }, []).reduce(onlyReactElement, []).reverse().concat(defaultHead(props.inAmpMode)).filter(unique()).reverse().map((c, i) => {\n const key = c.key || i;\n return react_1.default.cloneElement(c, {\n key\n });\n });\n}\n\nconst Effect = side_effect_1.default();\n/**\n * This component injects elements to `<head>` of your page.\n * To avoid duplicated `tags` in `<head>` you can use the `key` property, which will make sure every tag is only rendered once.\n */\n\nfunction Head({\n children\n}) {\n return react_1.default.createElement(amp_context_1.AmpStateContext.Consumer, null, ampState => react_1.default.createElement(head_manager_context_1.HeadManagerContext.Consumer, null, updateHead => react_1.default.createElement(Effect, {\n reduceComponentsToState: reduceComponents,\n handleStateChange: updateHead,\n inAmpMode: amp_1.isInAmpMode(ampState)\n }, children)));\n}\n\nHead.rewind = Effect.rewind;\nexports.default = Head;","map":null,"metadata":{},"sourceType":"script"}