UNPKG

@styleless-ui/react

Version:

Completely unstyled, headless and accessible React UI components.

1 lines 2.44 kB
var e=this&&this.__assign||function(){return e=Object.assign||function(e){for(var t,o=1,r=arguments.length;o<r;o++)for(var n in t=arguments[o])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},e.apply(this,arguments)},t=this&&this.__rest||function(e,t){var o={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(o[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(e);n<r.length;n++)t.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(e,r[n])&&(o[r[n]]=e[r[n]])}return o};import{jsx as o}from"react/jsx-runtime";import*as r from"react";import{SystemKeys as n}from"../internals";import i from"../Portal";import{componentWithForwardedRef as u,useDeterministicId as l,useEventListener as a,useIsMounted as c,useOnChange as s,usePreviousValue as f}from"../utils";import d from"./context";import{Root as p}from"./slots";var m=u((function(u,m){var y=u.open,v=u.duration,h=u.style,b=u.onEscapeKeyUp,O=u.onDurationEnd,w=u.id,E=u.focusAfterClosed,g=u.role,j=u.keepMounted,x=void 0!==j&&j,P=u.children,_=u.className,k=t(u,["open","duration","style","onEscapeKeyUp","onDurationEnd","id","focusAfterClosed","role","keepMounted","children","className"]),N=c(),S=l(w,"styleless-ui__snackbar"),R=r.useRef(NaN),T=r.useRef(null),A=f(y);r.useEffect((function(){var e,t;y||"boolean"!=typeof A||y===A||("string"==typeof E?null===(e=document.querySelector(E))||void 0===e||e.focus():"object"==typeof E?null===(t=E.current)||void 0===t||t.focus():T.current?T.current.focus():document.body.focus())}),[y,A]),r.useEffect((function(){return function(){return window.clearTimeout(R.current)}}),[]),s(y,(function(e){N()&&(T.current=document.activeElement,e&&v&&O?R.current=window.setTimeout(O,v):window.clearTimeout(R.current))}));var C="function"==typeof _?_({openState:y}):_;"undefined"!=typeof document&&a({target:document,eventType:"keyup",handler:function(e){e.key===n.ESCAPE&&(null==b||b(e))}},y&&null!=b);var M=r.useMemo((function(){return{role:g,open:y}}),[g,y]);return x||!x&&y?o(i,{children:o("div",e({"data-slot":"Portal:Root",role:"presentation",tabIndex:-1,"aria-hidden":!y,style:{position:"absolute",top:0,left:0,right:0}},{children:o("div",e({},k,{style:e(e({},null!=h?h:{}),{position:"fixed"}),id:S,ref:m,className:C,"data-slot":p,"data-open":y?"":void 0},{children:o(d.Provider,e({value:M},{children:P}))}))}))}):null}));export default m;