UNPKG

@styleless-ui/react

Version:

Completely unstyled, headless and accessible React UI components.

1 lines 1.33 kB
import*as e from"react";var t=!0,n=!1,r=void 0,i={text:!0,search:!0,url:!0,tel:!0,email:!0,password:!0,number:!0,date:!0,month:!0,week:!0,time:!0,datetime:!0,"datetime-local":!0},o=function(e){e.metaKey||e.altKey||e.ctrlKey||(t=!0)},a=function(){t=!1},u=function(){"hidden"===document.visibilityState&&n&&(t=!0)};export var teardown=function(e){e.removeEventListener("keydown",o,!0),e.removeEventListener("mousedown",a,!0),e.removeEventListener("pointerdown",a,!0),e.removeEventListener("touchstart",a,!0),e.removeEventListener("visibilitychange",u,!0)};var c=function(e){if(!e.target)return!1;try{return e.target.matches(":focus-visible")}catch(e){}return t||function(e){if(!e)return!1;var t=e.tagName;return!("INPUT"!==t||!i[e.type]||e.readOnly)||"TEXTAREA"===t&&!e.readOnly||!!e.isContentEditable}(e.target)};export default function(){var t=e.useCallback((function(e){var t;e&&((t=e.ownerDocument).addEventListener("keydown",o,!0),t.addEventListener("mousedown",a,!0),t.addEventListener("pointerdown",a,!0),t.addEventListener("touchstart",a,!0),t.addEventListener("visibilitychange",u,!0))}),[]),i=e.useRef(!1);return{isFocusVisibleRef:i,onFocus:function(e){return!!c(e)&&(i.current=!0,!0)},onBlur:function(){return!!i.current&&(window.clearTimeout(r),n=!0,r=window.setTimeout((function(){n=!1}),100),i.current=!1,!0)},ref:t}}