primereact
Version:
PrimeReact is an open source UI library for React featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime
2 lines (1 loc) • 3.81 kB
JavaScript
import e from"react";import{PrimeReactContext as t}from"primereact/api";import{useStyle as r,useMountEffect as n}from"primereact/hooks";import{ObjectUtils as o,DomHandler as c}from"primereact/utils";import{ComponentBase as u}from"primereact/componentbase";function a(e){return a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a(e)}function s(e,t){if("object"!==a(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==a(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function i(e){var t=s(e,"string");return"symbol"===a(t)?t:String(t)}var l=u.extend({defaultProps:{__TYPE:"FocusTrap",children:void 0},css:{styles:""},getProps:function(e){return o.getMergedProps(e,l.defaultProps)},getOtherProps:function(e){return o.getDiffProps(e,l.defaultProps)}});function f(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function p(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?f(Object(r),!0).forEach((function(t){var n,o,c;n=e,c=r[t],(o=i(o=t))in n?Object.defineProperty(n,o,{value:c,enumerable:!0,configurable:!0,writable:!0}):n[o]=c})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):f(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var d=e.memo(e.forwardRef((function(o,u){var a=e.useRef(null),s=e.useRef(null),i=e.useRef(null),f=e.useContext(t),d=l.getProps(o,f),m={props:d};r(l.css.styles,{name:"focustrap"});l.setMetaData(p({},m));e.useImperativeHandle(u,(function(){return{props:d,getInk:function(){return s.current},getTarget:function(){return a.current}}})),n((function(){d.disabled||(a.current=P(),O(a.current))}));var b,g,y,v,h,P=function(){return s.current&&s.current.parentElement},O=function(e){var t=d||{},r=t.autoFocusSelector,n=t.firstFocusableSelector,o=void 0===n?"":n,u=t.autoFocus,a=void 0!==u&&u,s="".concat(_(void 0===r?"":r)),i="[autofocus]".concat(s,", [data-pc-autofocus='true']").concat(s),l=c.getFirstFocusableElement(e,i);a&&!l&&(l=c.getFirstFocusableElement(e,_(o))),c.focus(l)},_=function(e){return':not(.p-hidden-focusable):not([data-p-hidden-focusable="true"])'.concat(null!=e?e:"")},j=function(e){var t,r=e.currentTarget,n=e.relatedTarget,o=n!==r.$_pfocustrap_firsthiddenfocusableelement&&null!==(t=a.current)&&void 0!==t&&t.contains(n)?r.$_pfocustrap_firsthiddenfocusableelement:c.getLastFocusableElement(r.parentElement,_(r.$_pfocustrap_focusableselector));c.focus(o)};return g=void 0===(b=(d||{}).tabIndex)?0:b,v=(y=function(t,r){return e.createElement("span",{ref:"firstfocusableelement"===r?s:i,className:"p-hidden-accessible p-hidden-focusable",tabIndex:g,role:"presentation","aria-hidden":!0,"data-p-hidden-accessible":!0,"data-p-hidden-focusable":!0,onFocus:t,"data-pc-section":r})})((function(e){var t,r=e.currentTarget,n=e.relatedTarget,o=n!==r.$_pfocustrap_lasthiddenfocusableelement&&null!==(t=a.current)&&void 0!==t&&t.contains(n)?r.$_pfocustrap_lasthiddenfocusableelement:c.getFirstFocusableElement(r.parentElement,_(r.$_pfocustrap_focusableselector));c.focus(o)}),"firstfocusableelement"),h=y(j,"lastfocusableelement"),v.ref.current&&h.ref.current&&(v.ref.current.$_pfocustrap_lasthiddenfocusableelement=h.ref.current,h.ref.current.$_pfocustrap_firsthiddenfocusableelement=v.ref.current),e.createElement(e.Fragment,null,v,d.children,h)})));export{d as FocusTrap,d as default};