UNPKG

r-scale-screen

Version:

react 大屏自适应组件

28 lines (24 loc) 16.1 kB
(function(b,O){typeof exports=="object"&&typeof module<"u"?module.exports=O(require("react")):typeof define=="function"&&define.amd?define(["react"],O):(b=typeof globalThis<"u"?globalThis:b||self,b.RScaleScreen=O(b.React))})(this,function(b){"use strict";var O={},We={get exports(){return O},set exports(l){O=l}},M={};/** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var le;function Ae(){if(le)return M;le=1;var l=b,m=Symbol.for("react.element"),E=Symbol.for("react.fragment"),R=Object.prototype.hasOwnProperty,k=l.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,W={key:!0,ref:!0,__self:!0,__source:!0};function P(f,u,C){var h,g={},S=null,D=null;C!==void 0&&(S=""+C),u.key!==void 0&&(S=""+u.key),u.ref!==void 0&&(D=u.ref);for(h in u)R.call(u,h)&&!W.hasOwnProperty(h)&&(g[h]=u[h]);if(f&&f.defaultProps)for(h in u=f.defaultProps,u)g[h]===void 0&&(g[h]=u[h]);return{$$typeof:m,type:f,key:S,ref:D,props:g,_owner:k.current}}return M.Fragment=E,M.jsx=P,M.jsxs=P,M}var L={};/** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var ce;function Ie(){return ce||(ce=1,process.env.NODE_ENV!=="production"&&function(){var l=b,m=Symbol.for("react.element"),E=Symbol.for("react.portal"),R=Symbol.for("react.fragment"),k=Symbol.for("react.strict_mode"),W=Symbol.for("react.profiler"),P=Symbol.for("react.provider"),f=Symbol.for("react.context"),u=Symbol.for("react.forward_ref"),C=Symbol.for("react.suspense"),h=Symbol.for("react.suspense_list"),g=Symbol.for("react.memo"),S=Symbol.for("react.lazy"),D=Symbol.for("react.offscreen"),N=Symbol.iterator,Q="@@iterator";function J(e){if(e===null||typeof e!="object")return null;var r=N&&e[N]||e[Q];return typeof r=="function"?r:null}var _=l.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function v(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),n=1;n<r;n++)t[n-1]=arguments[n];ee("error",e,t)}}function ee(e,r,t){{var n=_.ReactDebugCurrentFrame,o=n.getStackAddendum();o!==""&&(r+="%s",t=t.concat([o]));var s=t.map(function(i){return String(i)});s.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,s)}}var w=!1,V=!1,U=!1,z=!1,A=!1,j;j=Symbol.for("react.module.reference");function I(e){return!!(typeof e=="string"||typeof e=="function"||e===R||e===W||A||e===k||e===C||e===h||z||e===D||w||V||U||typeof e=="object"&&e!==null&&(e.$$typeof===S||e.$$typeof===g||e.$$typeof===P||e.$$typeof===f||e.$$typeof===u||e.$$typeof===j||e.getModuleId!==void 0))}function Le(e,r,t){var n=e.displayName;if(n)return n;var o=r.displayName||r.name||"";return o!==""?t+"("+o+")":t}function fe(e){return e.displayName||"Context"}function T(e){if(e==null)return null;if(typeof e.tag=="number"&&v("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case R:return"Fragment";case E:return"Portal";case W:return"Profiler";case k:return"StrictMode";case C:return"Suspense";case h:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case f:var r=e;return fe(r)+".Consumer";case P:var t=e;return fe(t._context)+".Provider";case u:return Le(e,e.render,"ForwardRef");case g:var n=e.displayName||null;return n!==null?n:T(e.type)||"Memo";case S:{var o=e,s=o._payload,i=o._init;try{return T(i(s))}catch{return null}}}return null}var F=Object.assign,H=0,de,ve,he,pe,ge,ye,be;function me(){}me.__reactDisabledLog=!0;function Ne(){{if(H===0){de=console.log,ve=console.info,he=console.warn,pe=console.error,ge=console.group,ye=console.groupCollapsed,be=console.groupEnd;var e={configurable:!0,enumerable:!0,value:me,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}H++}}function Ve(){{if(H--,H===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:F({},e,{value:de}),info:F({},e,{value:ve}),warn:F({},e,{value:he}),error:F({},e,{value:pe}),group:F({},e,{value:ge}),groupCollapsed:F({},e,{value:ye}),groupEnd:F({},e,{value:be})})}H<0&&v("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var re=_.ReactCurrentDispatcher,te;function G(e,r,t){{if(te===void 0)try{throw Error()}catch(o){var n=o.stack.trim().match(/\n( *(at )?)/);te=n&&n[1]||""}return` `+te+e}}var ne=!1,q;{var Ue=typeof WeakMap=="function"?WeakMap:Map;q=new Ue}function Ee(e,r){if(!e||ne)return"";{var t=q.get(e);if(t!==void 0)return t}var n;ne=!0;var o=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var s;s=re.current,re.current=null,Ne();try{if(r){var i=function(){throw Error()};if(Object.defineProperty(i.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(i,[])}catch(x){n=x}Reflect.construct(e,[],i)}else{try{i.call()}catch(x){n=x}e.call(i.prototype)}}else{try{throw Error()}catch(x){n=x}e()}}catch(x){if(x&&n&&typeof x.stack=="string"){for(var a=x.stack.split(` `),p=n.stack.split(` `),c=a.length-1,d=p.length-1;c>=1&&d>=0&&a[c]!==p[d];)d--;for(;c>=1&&d>=0;c--,d--)if(a[c]!==p[d]){if(c!==1||d!==1)do if(c--,d--,d<0||a[c]!==p[d]){var y=` `+a[c].replace(" at new "," at ");return e.displayName&&y.includes("<anonymous>")&&(y=y.replace("<anonymous>",e.displayName)),typeof e=="function"&&q.set(e,y),y}while(c>=1&&d>=0);break}}}finally{ne=!1,re.current=s,Ve(),Error.prepareStackTrace=o}var Y=e?e.displayName||e.name:"",Fe=Y?G(Y):"";return typeof e=="function"&&q.set(e,Fe),Fe}function ze(e,r,t){return Ee(e,!1)}function He(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function K(e,r,t){if(e==null)return"";if(typeof e=="function")return Ee(e,He(e));if(typeof e=="string")return G(e);switch(e){case C:return G("Suspense");case h:return G("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case u:return ze(e.render);case g:return K(e.type,r,t);case S:{var n=e,o=n._payload,s=n._init;try{return K(s(o),r,t)}catch{}}}return""}var X=Object.prototype.hasOwnProperty,Re={},_e=_.ReactDebugCurrentFrame;function Z(e){if(e){var r=e._owner,t=K(e.type,e._source,r?r.type:null);_e.setExtraStackFrame(t)}else _e.setExtraStackFrame(null)}function Be(e,r,t,n,o){{var s=Function.call.bind(X);for(var i in e)if(s(e,i)){var a=void 0;try{if(typeof e[i]!="function"){var p=Error((n||"React class")+": "+t+" type `"+i+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[i]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw p.name="Invariant Violation",p}a=e[i](r,i,n,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(c){a=c}a&&!(a instanceof Error)&&(Z(o),v("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",n||"React class",t,i,typeof a),Z(null)),a instanceof Error&&!(a.message in Re)&&(Re[a.message]=!0,Z(o),v("Failed %s type: %s",t,a.message),Z(null))}}}var Je=Array.isArray;function ae(e){return Je(e)}function Ge(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function qe(e){try{return we(e),!1}catch{return!0}}function we(e){return""+e}function Se(e){if(qe(e))return v("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",Ge(e)),we(e)}var B=_.ReactCurrentOwner,Ke={key:!0,ref:!0,__self:!0,__source:!0},Te,xe,ie;ie={};function Xe(e){if(X.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function Ze(e){if(X.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function Qe(e,r){if(typeof e.ref=="string"&&B.current&&r&&B.current.stateNode!==r){var t=T(B.current.type);ie[t]||(v('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref',T(B.current.type),e.ref),ie[t]=!0)}}function er(e,r){{var t=function(){Te||(Te=!0,v("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}}function rr(e,r){{var t=function(){xe||(xe=!0,v("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"ref",{get:t,configurable:!0})}}var tr=function(e,r,t,n,o,s,i){var a={$$typeof:m,type:e,key:r,ref:t,props:i,_owner:s};return a._store={},Object.defineProperty(a._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(a,"_self",{configurable:!1,enumerable:!1,writable:!1,value:n}),Object.defineProperty(a,"_source",{configurable:!1,enumerable:!1,writable:!1,value:o}),Object.freeze&&(Object.freeze(a.props),Object.freeze(a)),a};function nr(e,r,t,n,o){{var s,i={},a=null,p=null;t!==void 0&&(Se(t),a=""+t),Ze(r)&&(Se(r.key),a=""+r.key),Xe(r)&&(p=r.ref,Qe(r,o));for(s in r)X.call(r,s)&&!Ke.hasOwnProperty(s)&&(i[s]=r[s]);if(e&&e.defaultProps){var c=e.defaultProps;for(s in c)i[s]===void 0&&(i[s]=c[s])}if(a||p){var d=typeof e=="function"?e.displayName||e.name||"Unknown":e;a&&er(i,d),p&&rr(i,d)}return tr(e,a,p,o,n,B.current,i)}}var oe=_.ReactCurrentOwner,Oe=_.ReactDebugCurrentFrame;function $(e){if(e){var r=e._owner,t=K(e.type,e._source,r?r.type:null);Oe.setExtraStackFrame(t)}else Oe.setExtraStackFrame(null)}var ue;ue=!1;function se(e){return typeof e=="object"&&e!==null&&e.$$typeof===m}function Pe(){{if(oe.current){var e=T(oe.current.type);if(e)return` Check the render method of \``+e+"`."}return""}}function ar(e){{if(e!==void 0){var r=e.fileName.replace(/^.*[\\\/]/,""),t=e.lineNumber;return` Check your code at `+r+":"+t+"."}return""}}var Ce={};function ir(e){{var r=Pe();if(!r){var t=typeof e=="string"?e:e.displayName||e.name;t&&(r=` Check the top-level render call using <`+t+">.")}return r}}function je(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=ir(r);if(Ce[t])return;Ce[t]=!0;var n="";e&&e._owner&&e._owner!==oe.current&&(n=" It was passed a child from "+T(e._owner.type)+"."),$(e),v('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',t,n),$(null)}}function ke(e,r){{if(typeof e!="object")return;if(ae(e))for(var t=0;t<e.length;t++){var n=e[t];se(n)&&je(n,r)}else if(se(e))e._store&&(e._store.validated=!0);else if(e){var o=J(e);if(typeof o=="function"&&o!==e.entries)for(var s=o.call(e),i;!(i=s.next()).done;)se(i.value)&&je(i.value,r)}}}function or(e){{var r=e.type;if(r==null||typeof r=="string")return;var t;if(typeof r=="function")t=r.propTypes;else if(typeof r=="object"&&(r.$$typeof===u||r.$$typeof===g))t=r.propTypes;else return;if(t){var n=T(r);Be(t,e.props,"prop",n,e)}else if(r.PropTypes!==void 0&&!ue){ue=!0;var o=T(r);v("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",o||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&v("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function ur(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var n=r[t];if(n!=="children"&&n!=="key"){$(e),v("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",n),$(null);break}}e.ref!==null&&($(e),v("Invalid attribute `ref` supplied to `React.Fragment`."),$(null))}}function De(e,r,t,n,o,s){{var i=I(e);if(!i){var a="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(a+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var p=ar(o);p?a+=p:a+=Pe();var c;e===null?c="null":ae(e)?c="array":e!==void 0&&e.$$typeof===m?(c="<"+(T(e.type)||"Unknown")+" />",a=" Did you accidentally export a JSX literal instead of a component?"):c=typeof e,v("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",c,a)}var d=nr(e,r,t,o,s);if(d==null)return d;if(i){var y=r.children;if(y!==void 0)if(n)if(ae(y)){for(var Y=0;Y<y.length;Y++)ke(y[Y],e);Object.freeze&&Object.freeze(y)}else v("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else ke(y,e)}return e===R?ur(d):or(d),d}}function sr(e,r,t){return De(e,r,t,!0)}function lr(e,r,t){return De(e,r,t,!1)}var cr=lr,fr=sr;L.Fragment=R,L.jsx=cr,L.jsxs=fr}()),L}(function(l){process.env.NODE_ENV==="production"?l.exports=Ae():l.exports=Ie()})(We);function $e(l){return Promise.resolve().then(l)}function Ye(l,m){let E;return function(...R){E&&clearTimeout(E),E=setTimeout(()=>{typeof l=="function"&&l.apply(null,R),clearTimeout(E)},m>0?m:100)}}function Me(l){const{width:m,height:E,autoScale:R=!0,bodyOverflowHidden:k=!0,delay:W=500}=l;let P;const f=b.useRef(null),[u,C]=b.useState({width:m,height:E,originalHeight:0,originalWidth:0}),h={box:{overflow:"hidden",backgroundSize:"100% 100%",backgroundColor:"#000",width:"100vw",height:"100vh"},wrapper:{transitionProperty:"all",transitionTimingFunction:"cubic-bezier(0.4, 0, 0.2, 1)",transitionDuration:"500ms",position:"relative",overflow:"hidden",zIndex:100,transformOrigin:"left top"}};let g;function S(){k&&(P=document.body.style.overflow,document.body.style.overflow="hidden")}function D(){return new Promise(w=>{$e(()=>{C({...u,originalWidth:window.screen.width,originalHeight:window.screen.height}),w()})})}function N(){u.width&&u.height?(f.current.style.width=`${u.width}px`,f.current.style.height=`${u.height}px`):(f.current.style.width=`${u.originalWidth}px`,f.current.style.height=`${u.originalHeight}px`)}function Q(w){if(!R)return;const V=f.current.clientWidth,U=f.current.clientHeight,z=document.body.clientWidth,A=document.body.clientHeight;f.current.style.transform=`scale(${w},${w})`;let j=Math.max((z-V*w)/2,0),I=Math.max((A-U*w)/2,0);typeof l.autoScale=="object"&&(!l.autoScale.x&&(j=0),!l.autoScale.y&&(I=0)),f.current.style.margin=`${I}px ${j}px`}function J(){const w=document.body.clientWidth,V=document.body.clientHeight,U=u.width||u.originalWidth,z=u.height||u.originalHeight,A=w/+U,j=V/+z;if(l.fullScreen)return f.current.style.transform=`scale(${A},${j})`,!1;const I=Math.min(A,j);Q(I)}const _=Ye(async()=>{f.current&&(await D(),N(),J())},W);function v(){g=new MutationObserver(()=>{_()}),g.observe(f.current,{attributes:!0,attributeFilter:["style"],attributeOldValue:!0})}async function ee(){S(),v(),await D(),N(),J(),window.addEventListener("resize",_)}return b.useEffect(()=>(ee(),()=>{g.disconnect(),window.removeEventListener("resize",_),k&&(document.body.style.overflow=P)}),[]),O.jsx("div",{style:{...h.box,...l.boxStyle},className:"react-screen-box",children:O.jsx("div",{className:"screen-wrapper",style:{...h.wrapper,...l.wrapperStyle},ref:f,children:l.children})})}return Me});