UNPKG

squircler

Version:

� The MOST PERFECT React squircle component ever created. Zero ovals, infinite possibilities.

31 lines (28 loc) 16.3 kB
(function(A,O){typeof exports=="object"&&typeof module<"u"?O(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],O):(A=typeof globalThis<"u"?globalThis:A||self,O(A.Squircler={},A.React))})(this,function(A,O){"use strict";var ne={exports:{}},J={};/** * @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 pe;function Le(){if(pe)return J;pe=1;var C=O,E=Symbol.for("react.element"),d=Symbol.for("react.fragment"),c=Object.prototype.hasOwnProperty,I=C.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0};function o(x,f,P){var s,_={},m=null,M=null;P!==void 0&&(m=""+P),f.key!==void 0&&(m=""+f.key),f.ref!==void 0&&(M=f.ref);for(s in f)c.call(f,s)&&!p.hasOwnProperty(s)&&(_[s]=f[s]);if(x&&x.defaultProps)for(s in f=x.defaultProps,f)_[s]===void 0&&(_[s]=f[s]);return{$$typeof:E,type:x,key:m,ref:M,props:_,_owner:I.current}}return J.Fragment=d,J.jsx=o,J.jsxs=o,J}var z={};/** * @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 be;function Ue(){return be||(be=1,process.env.NODE_ENV!=="production"&&function(){var C=O,E=Symbol.for("react.element"),d=Symbol.for("react.portal"),c=Symbol.for("react.fragment"),I=Symbol.for("react.strict_mode"),p=Symbol.for("react.profiler"),o=Symbol.for("react.provider"),x=Symbol.for("react.context"),f=Symbol.for("react.forward_ref"),P=Symbol.for("react.suspense"),s=Symbol.for("react.suspense_list"),_=Symbol.for("react.memo"),m=Symbol.for("react.lazy"),M=Symbol.for("react.offscreen"),w=Symbol.iterator,Z="@@iterator";function ae(e){if(e===null||typeof e!="object")return null;var r=w&&e[w]||e[Z];return typeof r=="function"?r:null}var $=C.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function y(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),n=1;n<r;n++)t[n-1]=arguments[n];D("error",e,t)}}function D(e,r,t){{var n=$.ReactDebugCurrentFrame,u=n.getStackAddendum();u!==""&&(r+="%s",t=t.concat([u]));var l=t.map(function(i){return String(i)});l.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,l)}}var F=!1,U=!1,W=!1,V=!1,ie=!1,K;K=Symbol.for("react.module.reference");function B(e){return!!(typeof e=="string"||typeof e=="function"||e===c||e===p||ie||e===I||e===P||e===s||V||e===M||F||U||W||typeof e=="object"&&e!==null&&(e.$$typeof===m||e.$$typeof===_||e.$$typeof===o||e.$$typeof===x||e.$$typeof===f||e.$$typeof===K||e.getModuleId!==void 0))}function S(e,r,t){var n=e.displayName;if(n)return n;var u=r.displayName||r.name||"";return u!==""?t+"("+u+")":t}function k(e){return e.displayName||"Context"}function j(e){if(e==null)return null;if(typeof e.tag=="number"&&y("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 c:return"Fragment";case d:return"Portal";case p:return"Profiler";case I:return"StrictMode";case P:return"Suspense";case s:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case x:var r=e;return k(r)+".Consumer";case o:var t=e;return k(t._context)+".Provider";case f:return S(e,e.render,"ForwardRef");case _:var n=e.displayName||null;return n!==null?n:j(e.type)||"Memo";case m:{var u=e,l=u._payload,i=u._init;try{return j(i(l))}catch{return null}}}return null}var Y=Object.assign,G=0,ye,me,he,Ee,Re,_e,Se;function xe(){}xe.__reactDisabledLog=!0;function Ve(){{if(G===0){ye=console.log,me=console.info,he=console.warn,Ee=console.error,Re=console.group,_e=console.groupCollapsed,Se=console.groupEnd;var e={configurable:!0,enumerable:!0,value:xe,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}G++}}function Ne(){{if(G--,G===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:Y({},e,{value:ye}),info:Y({},e,{value:me}),warn:Y({},e,{value:he}),error:Y({},e,{value:Ee}),group:Y({},e,{value:Re}),groupCollapsed:Y({},e,{value:_e}),groupEnd:Y({},e,{value:Se})})}G<0&&y("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var oe=$.ReactCurrentDispatcher,ue;function Q(e,r,t){{if(ue===void 0)try{throw Error()}catch(u){var n=u.stack.trim().match(/\n( *(at )?)/);ue=n&&n[1]||""}return` `+ue+e}}var se=!1,ee;{var qe=typeof WeakMap=="function"?WeakMap:Map;ee=new qe}function Te(e,r){if(!e||se)return"";{var t=ee.get(e);if(t!==void 0)return t}var n;se=!0;var u=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var l;l=oe.current,oe.current=null,Ve();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(R){n=R}Reflect.construct(e,[],i)}else{try{i.call()}catch(R){n=R}e.call(i.prototype)}}else{try{throw Error()}catch(R){n=R}e()}}catch(R){if(R&&n&&typeof R.stack=="string"){for(var a=R.stack.split(` `),h=n.stack.split(` `),v=a.length-1,b=h.length-1;v>=1&&b>=0&&a[v]!==h[b];)b--;for(;v>=1&&b>=0;v--,b--)if(a[v]!==h[b]){if(v!==1||b!==1)do if(v--,b--,b<0||a[v]!==h[b]){var T=` `+a[v].replace(" at new "," at ");return e.displayName&&T.includes("<anonymous>")&&(T=T.replace("<anonymous>",e.displayName)),typeof e=="function"&&ee.set(e,T),T}while(v>=1&&b>=0);break}}}finally{se=!1,oe.current=l,Ne(),Error.prepareStackTrace=u}var q=e?e.displayName||e.name:"",L=q?Q(q):"";return typeof e=="function"&&ee.set(e,L),L}function Je(e,r,t){return Te(e,!1)}function ze(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function re(e,r,t){if(e==null)return"";if(typeof e=="function")return Te(e,ze(e));if(typeof e=="string")return Q(e);switch(e){case P:return Q("Suspense");case s:return Q("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case f:return Je(e.render);case _:return re(e.type,r,t);case m:{var n=e,u=n._payload,l=n._init;try{return re(l(u),r,t)}catch{}}}return""}var H=Object.prototype.hasOwnProperty,Pe={},je=$.ReactDebugCurrentFrame;function te(e){if(e){var r=e._owner,t=re(e.type,e._source,r?r.type:null);je.setExtraStackFrame(t)}else je.setExtraStackFrame(null)}function Ke(e,r,t,n,u){{var l=Function.call.bind(H);for(var i in e)if(l(e,i)){var a=void 0;try{if(typeof e[i]!="function"){var h=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 h.name="Invariant Violation",h}a=e[i](r,i,n,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(v){a=v}a&&!(a instanceof Error)&&(te(u),y("%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),te(null)),a instanceof Error&&!(a.message in Pe)&&(Pe[a.message]=!0,te(u),y("Failed %s type: %s",t,a.message),te(null))}}}var Be=Array.isArray;function le(e){return Be(e)}function Ge(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function He(e){try{return Oe(e),!1}catch{return!0}}function Oe(e){return""+e}function Ce(e){if(He(e))return y("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",Ge(e)),Oe(e)}var we=$.ReactCurrentOwner,Xe={key:!0,ref:!0,__self:!0,__source:!0},ke,$e;function Ze(e){if(H.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function Qe(e){if(H.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function er(e,r){typeof e.ref=="string"&&we.current}function rr(e,r){{var t=function(){ke||(ke=!0,y("%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 tr(e,r){{var t=function(){$e||($e=!0,y("%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 nr=function(e,r,t,n,u,l,i){var a={$$typeof:E,type:e,key:r,ref:t,props:i,_owner:l};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:u}),Object.freeze&&(Object.freeze(a.props),Object.freeze(a)),a};function ar(e,r,t,n,u){{var l,i={},a=null,h=null;t!==void 0&&(Ce(t),a=""+t),Qe(r)&&(Ce(r.key),a=""+r.key),Ze(r)&&(h=r.ref,er(r,u));for(l in r)H.call(r,l)&&!Xe.hasOwnProperty(l)&&(i[l]=r[l]);if(e&&e.defaultProps){var v=e.defaultProps;for(l in v)i[l]===void 0&&(i[l]=v[l])}if(a||h){var b=typeof e=="function"?e.displayName||e.name||"Unknown":e;a&&rr(i,b),h&&tr(i,b)}return nr(e,a,h,u,n,we.current,i)}}var ce=$.ReactCurrentOwner,De=$.ReactDebugCurrentFrame;function N(e){if(e){var r=e._owner,t=re(e.type,e._source,r?r.type:null);De.setExtraStackFrame(t)}else De.setExtraStackFrame(null)}var fe;fe=!1;function de(e){return typeof e=="object"&&e!==null&&e.$$typeof===E}function Fe(){{if(ce.current){var e=j(ce.current.type);if(e)return` Check the render method of \``+e+"`."}return""}}function ir(e){return""}var Ae={};function or(e){{var r=Fe();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 Ie(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=or(r);if(Ae[t])return;Ae[t]=!0;var n="";e&&e._owner&&e._owner!==ce.current&&(n=" It was passed a child from "+j(e._owner.type)+"."),N(e),y('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),N(null)}}function Me(e,r){{if(typeof e!="object")return;if(le(e))for(var t=0;t<e.length;t++){var n=e[t];de(n)&&Ie(n,r)}else if(de(e))e._store&&(e._store.validated=!0);else if(e){var u=ae(e);if(typeof u=="function"&&u!==e.entries)for(var l=u.call(e),i;!(i=l.next()).done;)de(i.value)&&Ie(i.value,r)}}}function ur(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===f||r.$$typeof===_))t=r.propTypes;else return;if(t){var n=j(r);Ke(t,e.props,"prop",n,e)}else if(r.PropTypes!==void 0&&!fe){fe=!0;var u=j(r);y("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",u||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&y("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function sr(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var n=r[t];if(n!=="children"&&n!=="key"){N(e),y("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",n),N(null);break}}e.ref!==null&&(N(e),y("Invalid attribute `ref` supplied to `React.Fragment`."),N(null))}}var We={};function Ye(e,r,t,n,u,l){{var i=B(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 h=ir();h?a+=h:a+=Fe();var v;e===null?v="null":le(e)?v="array":e!==void 0&&e.$$typeof===E?(v="<"+(j(e.type)||"Unknown")+" />",a=" Did you accidentally export a JSX literal instead of a component?"):v=typeof e,y("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",v,a)}var b=ar(e,r,t,u,l);if(b==null)return b;if(i){var T=r.children;if(T!==void 0)if(n)if(le(T)){for(var q=0;q<T.length;q++)Me(T[q],e);Object.freeze&&Object.freeze(T)}else y("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 Me(T,e)}if(H.call(r,"key")){var L=j(e),R=Object.keys(r).filter(function(pr){return pr!=="key"}),ve=R.length>0?"{key: someKey, "+R.join(": ..., ")+": ...}":"{key: someKey}";if(!We[L+ve]){var vr=R.length>0?"{"+R.join(": ..., ")+": ...}":"{}";y(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`,ve,L,vr,L),We[L+ve]=!0}}return e===c?sr(b):ur(b),b}}function lr(e,r,t){return Ye(e,r,t,!0)}function cr(e,r,t){return Ye(e,r,t,!1)}var fr=cr,dr=lr;z.Fragment=c,z.jsx=fr,z.jsxs=dr}()),z}process.env.NODE_ENV==="production"?ne.exports=Le():ne.exports=Ue();var g=ne.exports;function X(C,E,d,c){const p=Math.min(d,c)/2,o=Math.min(C/50*p,p),x=.05*o,f=.552*o,P=.8*o;let s;if(E<=1)s=x+(f-x)*E;else{const m=E-1;s=f+(P-f)*m}return[`M ${o} 0`,`L ${d-o} 0`,`C ${d-o+s} 0, ${d} ${o-s}, ${d} ${o}`,`L ${d} ${c-o}`,`C ${d} ${c-o+s}, ${d-o+s} ${c}, ${d-o} ${c}`,`L ${o} ${c}`,`C ${o-s} ${c}, 0 ${c-o+s}, 0 ${c-o}`,`L 0 ${o}`,`C 0 ${o-s}, ${o-s} 0, ${o} 0`,"Z"].join(" ")}const ge=O.forwardRef(({borderRadius:C=25,tension:E=.5,width:d=200,height:c=200,as:I="div",stretch:p=!1,squircleBorder:o=0,squircleBorderColor:x="#000",children:f,style:P,className:s="",..._},m)=>{const M=I,w=O.useRef(null),[Z,ae]=O.useState({width:200,height:200});O.useEffect(()=>{if(!p||!w.current)return;const S=()=>{if(w.current){const j=w.current.getBoundingClientRect();ae({width:j.width,height:j.height})}};S();const k=new ResizeObserver(S);return k.observe(w.current),()=>k.disconnect()},[p]);const $=typeof d=="number"?d:parseInt((d==null?void 0:d.toString())||"200"),y=typeof c=="number"?c:parseInt((c==null?void 0:c.toString())||"200"),D=p?Z.width:$,F=p?Z.height:y;let U,W;U=X(C,E,D,F),W="userSpaceOnUse";const V=`squircle-${Math.random().toString(36).substr(2,9)}`,ie={width:p?"100%":d,height:p?"100%":c,display:p?"block":"inline-block",overflow:"hidden",clipPath:`url(#${V})`,...P};if(o<=0)return g.jsxs(g.Fragment,{children:[g.jsx("svg",{width:"0",height:"0",style:{position:"absolute"},viewBox:`0 0 ${D} ${F}`,children:g.jsx("defs",{children:g.jsx("clipPath",{id:V,clipPathUnits:W,children:g.jsx("path",{d:U})})})}),g.jsx(M,{ref:S=>{m&&(typeof m=="function"?m(S):m.current=S),w.current!==S&&(w.current=S)},className:`squircle ${s}`.trim(),style:ie,..._,children:f})]});const K=`squircle-border-${Math.random().toString(36).substr(2,9)}`;let B;if(p){const S=Math.max(10,D-o*2),k=Math.max(10,F-o*2);B=X(C,E,S,k)}else{const S=Math.max(10,D-o*2),k=Math.max(10,F-o*2);B=X(C,E,S,k)}return g.jsxs(g.Fragment,{children:[g.jsx("svg",{width:"0",height:"0",style:{position:"absolute"},viewBox:`0 0 ${D} ${F}`,children:g.jsxs("defs",{children:[g.jsx("clipPath",{id:K,clipPathUnits:W,children:g.jsx("path",{d:U})}),g.jsx("clipPath",{id:V,clipPathUnits:W,children:g.jsx("path",{d:B})})]})}),g.jsx("div",{ref:w,style:{width:p?"100%":d,height:p?"100%":c,display:p?"block":"inline-block",clipPath:`url(#${K})`,background:x,padding:o,boxSizing:"border-box"},children:g.jsx(M,{ref:m,className:s,style:{width:"100%",height:"100%",clipPath:`url(#${V})`,...P},..._,children:f})})]})});ge.displayName="Squircle",A.Squircle=ge,Object.defineProperty(A,Symbol.toStringTag,{value:"Module"})});