UNPKG

react-head

Version:

SSR-ready Document Head management for React 16+

2 lines (1 loc) 3.58 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","react","react-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactHead={},e.React,e.ReactDOM)}(this,(function(e,t,r){"use strict";function n(){return(n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function a(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var o=t.createContext(null),i=o.Consumer,u=o.Provider,c=function(e){function o(){for(var t,r=arguments.length,n=new Array(r),a=0;a<r;a++)n[a]=arguments[a];return(t=e.call.apply(e,[this].concat(n))||this).state={canUseDOM:!1},t.headTags=null,t.index=-1,t}a(o,e);var u=o.prototype;return u.componentDidMount=function(){var e=this.props,t=e.tag,r=e.name,n=e.property;this.setState({canUseDOM:!0}),this.index=this.headTags.addClientTag(t,r||n)},u.componentWillUnmount=function(){var e=this.props.tag;this.headTags.removeClientTag(e,this.index)},u.render=function(){var e=this,a=this.props,o=a.tag,u=function(e,t){if(null==e)return{};var r,n,a={},o=Object.keys(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||(a[r]=e[r]);return a}(a,["tag"]),c=this.state.canUseDOM;return t.createElement(i,null,(function(a){if(null==a)throw Error("<HeadProvider /> should be in the tree");if(e.headTags=a,c){if(!a.shouldRenderTag(o,e.index))return null;var i=t.createElement(o,u);return r.createPortal(i,document.head)}var s=t.createElement(o,n({"data-rh":""},u));return a.addServerTag(s),null}))},o}(t.Component);function s(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}var l=["title","meta"],d=function(e){function r(){for(var t,r=arguments.length,n=new Array(r),a=0;a<r;a++)n[a]=arguments[a];return(t=e.call.apply(e,[this].concat(n))||this).indices=new Map,t.state={addClientTag:function(e,r){if(-1!==l.indexOf(e)){t.setState((function(t){var n,a=t[e]||[];return(n={})[e]=[].concat(a,[r]),n}));var n=s(t).indices,a=n.has(e)?n.get(e)+1:0;return n.set(e,a),a}return-1},shouldRenderTag:function(e,r){if(-1!==l.indexOf(e)){var n=t.state[e];return n&&n.lastIndexOf(n[r])===r}return!0},removeClientTag:function(e,r){t.setState((function(t){var n,a=t[e];return a?(a[r]=null,(n={})[e]=a,n):null}))},addServerTag:function(e){var r=t.props.headTags,n=void 0===r?[]:r;if(-1!==l.indexOf(e.type)){var a=n.findIndex((function(t){var r=t.props.name||t.props.property,n=e.props.name||e.props.property;return t.type===e.type&&r===n}));-1!==a&&n.splice(a,1)}n.push(e)}},t}a(r,e);var n=r.prototype;return n.componentDidMount=function(){var e=document.head.querySelectorAll('[data-rh=""]');Array.prototype.forEach.call(e,(function(e){return e.parentNode.removeChild(e)}))},n.render=function(){var e=this.props,r=e.headTags,n=e.children;if("undefined"==typeof window&&!1===Array.isArray(r))throw Error("headTags array should be passed to <HeadProvider /> in node");return t.createElement(u,{value:this.state},n)},r}(t.Component);e.Base=function(e){return t.createElement(c,n({tag:"base"},e))},e.HeadProvider=d,e.Link=function(e){return t.createElement(c,n({tag:"link"},e))},e.Meta=function(e){return t.createElement(c,n({tag:"meta"},e))},e.Style=function(e){return t.createElement(c,n({tag:"style"},e))},e.Title=function(e){return t.createElement(c,n({tag:"title"},e))},Object.defineProperty(e,"__esModule",{value:!0})}));