UNPKG

@ranceheart/sticky-elevator

Version:

A lightweight scroll‑spy elevator nav for React

32 lines (29 loc) 17.8 kB
(function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode('@charset "UTF-8";.elevator{position:relative;overflow-x:auto;-webkit-overflow-scrolling:touch;max-width:100%}.elevator::-webkit-scrollbar{display:none}.elevator.fixed{position:fixed;top:0;left:0;right:0;margin:0 auto}.elevator.safe-padding{padding-top:env(safe-area-inset-top)}.elevator-wrapper{display:flex;width:100%;white-space:nowrap;box-sizing:content-box;padding:0;margin:0;gap:0}.elevator-item-wrapper{display:flex}.elevator-item{display:block;width:100%;background-size:cover;background-position:center;transition:background-position .2s}')),document.head.appendChild(e)}}catch(t){console.error("vite-plugin-css-injected-by-js",t)}})(); "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("react"),Le=require("clsx"),Ie=require("resize-observer-polyfill"),vr=require("smoothscroll-polyfill");var ve={exports:{}},G={};/** * @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 $e;function pr(){if($e)return G;$e=1;var A=m,w=Symbol.for("react.element"),L=Symbol.for("react.fragment"),C=Object.prototype.hasOwnProperty,O=A.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,R={key:!0,ref:!0,__self:!0,__source:!0};function j(x,E,I){var g,_={},k=null,D=null;I!==void 0&&(k=""+I),E.key!==void 0&&(k=""+E.key),E.ref!==void 0&&(D=E.ref);for(g in E)C.call(E,g)&&!R.hasOwnProperty(g)&&(_[g]=E[g]);if(x&&x.defaultProps)for(g in E=x.defaultProps,E)_[g]===void 0&&(_[g]=E[g]);return{$$typeof:w,type:x,key:k,ref:D,props:_,_owner:O.current}}return G.Fragment=L,G.jsx=j,G.jsxs=j,G}var X={};/** * @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 We;function mr(){return We||(We=1,process.env.NODE_ENV!=="production"&&function(){var A=m,w=Symbol.for("react.element"),L=Symbol.for("react.portal"),C=Symbol.for("react.fragment"),O=Symbol.for("react.strict_mode"),R=Symbol.for("react.profiler"),j=Symbol.for("react.provider"),x=Symbol.for("react.context"),E=Symbol.for("react.forward_ref"),I=Symbol.for("react.suspense"),g=Symbol.for("react.suspense_list"),_=Symbol.for("react.memo"),k=Symbol.for("react.lazy"),D=Symbol.for("react.offscreen"),U=Symbol.iterator,B="@@iterator";function q(e){if(e===null||typeof e!="object")return null;var r=U&&e[U]||e[B];return typeof r=="function"?r:null}var P=A.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function p(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),n=1;n<r;n++)t[n-1]=arguments[n];Y("error",e,t)}}function Y(e,r,t){{var n=P.ReactDebugCurrentFrame,s=n.getStackAddendum();s!==""&&(r+="%s",t=t.concat([s]));var u=t.map(function(i){return String(i)});u.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,u)}}var ae=!1,z=!1,M=!1,F=!1,J=!1,Z;Z=Symbol.for("react.module.reference");function a(e){return!!(typeof e=="string"||typeof e=="function"||e===C||e===R||J||e===O||e===I||e===g||F||e===D||ae||z||M||typeof e=="object"&&e!==null&&(e.$$typeof===k||e.$$typeof===_||e.$$typeof===j||e.$$typeof===x||e.$$typeof===E||e.$$typeof===Z||e.getModuleId!==void 0))}function c(e,r,t){var n=e.displayName;if(n)return n;var s=r.displayName||r.name||"";return s!==""?t+"("+s+")":t}function f(e){return e.displayName||"Context"}function l(e){if(e==null)return null;if(typeof e.tag=="number"&&p("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 L:return"Portal";case R:return"Profiler";case O:return"StrictMode";case I:return"Suspense";case g:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case x:var r=e;return f(r)+".Consumer";case j:var t=e;return f(t._context)+".Provider";case E:return c(e,e.render,"ForwardRef");case _:var n=e.displayName||null;return n!==null?n:l(e.type)||"Memo";case k:{var s=e,u=s._payload,i=s._init;try{return l(i(u))}catch{return null}}}return null}var h=Object.assign,T=0,$,K,pe,me,Ee,ge,he;function ye(){}ye.__reactDisabledLog=!0;function Me(){{if(T===0){$=console.log,K=console.info,pe=console.warn,me=console.error,Ee=console.group,ge=console.groupCollapsed,he=console.groupEnd;var e={configurable:!0,enumerable:!0,value:ye,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}T++}}function Ne(){{if(T--,T===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:h({},e,{value:$}),info:h({},e,{value:K}),warn:h({},e,{value:pe}),error:h({},e,{value:me}),group:h({},e,{value:Ee}),groupCollapsed:h({},e,{value:ge}),groupEnd:h({},e,{value:he})})}T<0&&p("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var oe=P.ReactCurrentDispatcher,ie;function Q(e,r,t){{if(ie===void 0)try{throw Error()}catch(s){var n=s.stack.trim().match(/\n( *(at )?)/);ie=n&&n[1]||""}return` `+ie+e}}var se=!1,ee;{var Ve=typeof WeakMap=="function"?WeakMap:Map;ee=new Ve}function Re(e,r){if(!e||se)return"";{var t=ee.get(e);if(t!==void 0)return t}var n;se=!0;var s=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var u;u=oe.current,oe.current=null,Me();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(b){n=b}Reflect.construct(e,[],i)}else{try{i.call()}catch(b){n=b}e.call(i.prototype)}}else{try{throw Error()}catch(b){n=b}e()}}catch(b){if(b&&n&&typeof b.stack=="string"){for(var o=b.stack.split(` `),y=n.stack.split(` `),d=o.length-1,v=y.length-1;d>=1&&v>=0&&o[d]!==y[v];)v--;for(;d>=1&&v>=0;d--,v--)if(o[d]!==y[v]){if(d!==1||v!==1)do if(d--,v--,v<0||o[d]!==y[v]){var S=` `+o[d].replace(" at new "," at ");return e.displayName&&S.includes("<anonymous>")&&(S=S.replace("<anonymous>",e.displayName)),typeof e=="function"&&ee.set(e,S),S}while(d>=1&&v>=0);break}}}finally{se=!1,oe.current=u,Ne(),Error.prepareStackTrace=s}var V=e?e.displayName||e.name:"",W=V?Q(V):"";return typeof e=="function"&&ee.set(e,W),W}function Ue(e,r,t){return Re(e,!1)}function Be(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function re(e,r,t){if(e==null)return"";if(typeof e=="function")return Re(e,Be(e));if(typeof e=="string")return Q(e);switch(e){case I:return Q("Suspense");case g:return Q("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case E:return Ue(e.render);case _:return re(e.type,r,t);case k:{var n=e,s=n._payload,u=n._init;try{return re(u(s),r,t)}catch{}}}return""}var H=Object.prototype.hasOwnProperty,be={},_e=P.ReactDebugCurrentFrame;function te(e){if(e){var r=e._owner,t=re(e.type,e._source,r?r.type:null);_e.setExtraStackFrame(t)}else _e.setExtraStackFrame(null)}function qe(e,r,t,n,s){{var u=Function.call.bind(H);for(var i in e)if(u(e,i)){var o=void 0;try{if(typeof e[i]!="function"){var y=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 y.name="Invariant Violation",y}o=e[i](r,i,n,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(d){o=d}o&&!(o instanceof Error)&&(te(s),p("%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 o),te(null)),o instanceof Error&&!(o.message in be)&&(be[o.message]=!0,te(s),p("Failed %s type: %s",t,o.message),te(null))}}}var ze=Array.isArray;function ue(e){return ze(e)}function Je(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function Ke(e){try{return we(e),!1}catch{return!0}}function we(e){return""+e}function Te(e){if(Ke(e))return p("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",Je(e)),we(e)}var Se=P.ReactCurrentOwner,He={key:!0,ref:!0,__self:!0,__source:!0},Oe,Ce;function Ge(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 Xe(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 Ze(e,r){typeof e.ref=="string"&&Se.current}function Qe(e,r){{var t=function(){Oe||(Oe=!0,p("%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 er(e,r){{var t=function(){Ce||(Ce=!0,p("%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 rr=function(e,r,t,n,s,u,i){var o={$$typeof:w,type:e,key:r,ref:t,props:i,_owner:u};return o._store={},Object.defineProperty(o._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(o,"_self",{configurable:!1,enumerable:!1,writable:!1,value:n}),Object.defineProperty(o,"_source",{configurable:!1,enumerable:!1,writable:!1,value:s}),Object.freeze&&(Object.freeze(o.props),Object.freeze(o)),o};function tr(e,r,t,n,s){{var u,i={},o=null,y=null;t!==void 0&&(Te(t),o=""+t),Xe(r)&&(Te(r.key),o=""+r.key),Ge(r)&&(y=r.ref,Ze(r,s));for(u in r)H.call(r,u)&&!He.hasOwnProperty(u)&&(i[u]=r[u]);if(e&&e.defaultProps){var d=e.defaultProps;for(u in d)i[u]===void 0&&(i[u]=d[u])}if(o||y){var v=typeof e=="function"?e.displayName||e.name||"Unknown":e;o&&Qe(i,v),y&&er(i,v)}return rr(e,o,y,s,n,Se.current,i)}}var ce=P.ReactCurrentOwner,xe=P.ReactDebugCurrentFrame;function N(e){if(e){var r=e._owner,t=re(e.type,e._source,r?r.type:null);xe.setExtraStackFrame(t)}else xe.setExtraStackFrame(null)}var le;le=!1;function fe(e){return typeof e=="object"&&e!==null&&e.$$typeof===w}function Pe(){{if(ce.current){var e=l(ce.current.type);if(e)return` Check the render method of \``+e+"`."}return""}}function nr(e){return""}var je={};function ar(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 ke(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=ar(r);if(je[t])return;je[t]=!0;var n="";e&&e._owner&&e._owner!==ce.current&&(n=" It was passed a child from "+l(e._owner.type)+"."),N(e),p('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 De(e,r){{if(typeof e!="object")return;if(ue(e))for(var t=0;t<e.length;t++){var n=e[t];fe(n)&&ke(n,r)}else if(fe(e))e._store&&(e._store.validated=!0);else if(e){var s=q(e);if(typeof s=="function"&&s!==e.entries)for(var u=s.call(e),i;!(i=u.next()).done;)fe(i.value)&&ke(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===E||r.$$typeof===_))t=r.propTypes;else return;if(t){var n=l(r);qe(t,e.props,"prop",n,e)}else if(r.PropTypes!==void 0&&!le){le=!0;var s=l(r);p("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",s||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&p("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function ir(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),p("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),p("Invalid attribute `ref` supplied to `React.Fragment`."),N(null))}}var Fe={};function Ae(e,r,t,n,s,u){{var i=a(e);if(!i){var o="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(o+=" 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 y=nr();y?o+=y:o+=Pe();var d;e===null?d="null":ue(e)?d="array":e!==void 0&&e.$$typeof===w?(d="<"+(l(e.type)||"Unknown")+" />",o=" Did you accidentally export a JSX literal instead of a component?"):d=typeof e,p("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",d,o)}var v=tr(e,r,t,s,u);if(v==null)return v;if(i){var S=r.children;if(S!==void 0)if(n)if(ue(S)){for(var V=0;V<S.length;V++)De(S[V],e);Object.freeze&&Object.freeze(S)}else p("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 De(S,e)}if(H.call(r,"key")){var W=l(e),b=Object.keys(r).filter(function(dr){return dr!=="key"}),de=b.length>0?"{key: someKey, "+b.join(": ..., ")+": ...}":"{key: someKey}";if(!Fe[W+de]){var fr=b.length>0?"{"+b.join(": ..., ")+": ...}":"{}";p(`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} />`,de,W,fr,W),Fe[W+de]=!0}}return e===C?ir(v):or(v),v}}function sr(e,r,t){return Ae(e,r,t,!0)}function ur(e,r,t){return Ae(e,r,t,!1)}var cr=ur,lr=sr;X.Fragment=C,X.jsx=cr,X.jsxs=lr}()),X}process.env.NODE_ENV==="production"?ve.exports=pr():ve.exports=mr();var ne=ve.exports;vr.polyfill();const Ye=A=>{let w=0;const L="onscrollend"in window,C=()=>{window.removeEventListener("scroll",R),window.removeEventListener("scrollend",O),clearTimeout(w)},O=()=>{C(),A()},R=()=>{clearTimeout(w),w=window.setTimeout(O,480)};L?window.addEventListener("scrollend",O,{once:!0}):(window.addEventListener("scroll",R),R())},Er=({anchorPoints:A,anchorImages:w,anchorActiveImages:L,number:C,navbarHeight:O,paddingTab:R,align:j,width:x,zIndex:E,className:I,style:g})=>{const _=m.useMemo(()=>w.map((a,c)=>{const f=new Image,l=new Image;return f.src=a,l.src=L[c]||a,{id:A[c]||"",img:f,activeImg:l}}),[A,w,L]),k=m.useMemo(()=>({width:`${x??"728px"}`,margin:j==="left"?"0 auto 0 0":j==="right"?"0 0 0 auto":"0 auto",zIndex:E??10,...g}),[x,j,E,g]),D=m.useRef([]),U=m.useRef([]),B=m.useRef(null),q=m.useRef(null),P=m.useRef(!1),p=m.useRef(0),[Y,ae]=m.useState(0),[z,M]=m.useState(0),F=m.useCallback(()=>{var c;return(((c=D.current[0])==null?void 0:c.offsetHeight)??O??75)+(R??0)},[O,R]);m.useLayoutEffect(()=>{U.current=_.map(f=>document.getElementById(f.id)),M(F());const a=D.current[0];if(!a)return;const c=new Ie(()=>M(F()));return c.observe(a),()=>c.disconnect()},[_,F]);const J=m.useCallback((a,c=!1)=>{const f=B.current,l=D.current[a];if(!f||!l)return;const h=Math.max(l.offsetLeft-(f.clientWidth-l.offsetWidth)/2,0);c?f.scrollTo({left:h,behavior:"smooth"}):f.scrollLeft=h},[]);m.useEffect(()=>{let a=!1;const c=()=>{let l=0;U.current.forEach((h,T)=>{const $=h==null?void 0:h.getBoundingClientRect();$&&$.top<z+z*.2&&(l=T)}),ae(l),a=!1},f=()=>{a||(window.requestAnimationFrame(c),a=!0)};return window.addEventListener("scroll",f,{passive:!0}),()=>window.removeEventListener("scroll",f)},[z]),m.useEffect(()=>{Ye(()=>{P.current||J(Y,!0)})},[Y,J]),m.useEffect(()=>{const a=()=>M(F());return window.addEventListener("resize",a,{passive:!0}),()=>window.removeEventListener("resize",a)},[F]),m.useEffect(()=>{const a=B.current;if(!a)return;const c=()=>{if(!a)return;const T=R??0;q.current&&(q.current.style.height=`${a.offsetHeight-T}px`),a.classList.contains("fixed")?a.style.paddingTop=`${T}px`:p.current=a.getBoundingClientRect().top+window.scrollY-T,M(F())},f=new Ie(c);f.observe(a);const l=document.createElement("div");l.style.width="100%",l.style.visibility="hidden",q.current=l,c();const h=()=>{var $,K;window.scrollY>=p.current&&p.current!==0?a.classList.contains("fixed")||(($=a.parentNode)==null||$.insertBefore(l,a),a.classList.add("fixed","safe-padding"),a.style.paddingTop=`${R??0}px`):a.classList.contains("fixed")&&(a.classList.remove("fixed","safe-padding"),(K=l.parentNode)==null||K.removeChild(l),a.style.paddingTop="")};return window.addEventListener("scroll",h,{passive:!0}),()=>{f.disconnect(),window.removeEventListener("scroll",h)}},[F,R]);const Z=(a,c)=>{const f=document.getElementById(c);f&&(P.current=!0,f.scrollIntoView({behavior:"smooth",block:"start"}),Ye(()=>{P.current=!1,J(a,!0)}))};return ne.jsx("div",{ref:B,className:Le("elevator",I),style:{...k,...g},children:ne.jsx("div",{className:"elevator-wrapper",children:_.map((a,c)=>ne.jsx("div",{ref:f=>D.current[c]=f,className:"elevator-item-wrapper",style:{flex:`0 0 calc(100% / ${C})`},onClick:()=>Z(c,a.id),children:ne.jsx("img",{src:(Y===c?a.activeImg:a.img).src,alt:`sticky-elevator-tab-${c}`,className:Le("elevator-item",Y===c&&"elevator-item-active")})},a.id))})})};exports.Elevator=Er;