UNPKG

react-aria

Version:
1 lines 4.41 kB
{"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC,GAED,kEAAkE;AAClE,2DAA2D;AAC3D,yDAAyD;AACzD,kHAAkH;;;;;AAsB3G,SAAS,0CACd,KAAyB;IAEzB,IAAI,cAAC,UAAU,EAAE,SAAS,WAAW,EAAE,QAAQ,UAAU,iBAAE,aAAa,EAAC,GAAG;IAE5E,MAAM,SAAuC,CAAA,GAAA,wBAAU,EACrD,CAAC;QACC,IAAI,CAAA,GAAA,wCAAa,EAAE,OAAO,EAAE,aAAa,EAAE;YACzC,IAAI,YACF,WAAW;YAGb,IAAI,eACF,cAAc;YAGhB,OAAO;QACT;IACF,GACA;QAAC;QAAY;KAAc;IAG7B,MAAM,mBAAmB,CAAA,GAAA,+CAAoB,EAAU;IAEvD,MAAM,UAAyC,CAAA,GAAA,wBAAU,EACvD,CAAC;QACC,kGAAkG;QAClG,oDAAoD;QAEpD,IAAI,cAAc,CAAA,GAAA,wCAAa,EAAE;QACjC,MAAM,gBAAgB,CAAA,GAAA,0CAAe,EAAE;QACvC,MAAM,gBAAgB,gBAAgB,CAAA,GAAA,0CAAe,EAAE,iBAAiB,CAAA,GAAA,0CAAe;QACvF,IAAI,gBAAgB,EAAE,aAAa,IAAI,gBAAgB,eAAe;YACpE,IAAI,aACF,YAAY;YAGd,IAAI,eACF,cAAc;YAGhB,iBAAiB;QACnB;IACF,GACA;QAAC;QAAe;QAAa;KAAiB;IAGhD,OAAO;QACL,YAAY;YACV,SAAS,CAAC,cAAe,CAAA,eAAe,iBAAiB,UAAS,IAAK,UAAU;YACjF,QAAQ,CAAC,cAAe,CAAA,cAAc,aAAY,IAAK,SAAS;QAClE;IACF;AACF","sources":["packages/react-aria/src/interactions/useFocus.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\n// Portions of the code in this file are based on code from react.\n// Original licensing for the following can be found in the\n// NOTICE file in the root directory of this source tree.\n// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions\n\nimport {DOMAttributes, FocusableElement, FocusEvents} from '@react-types/shared';\nimport {FocusEvent, useCallback} from 'react';\nimport {getActiveElement, getEventTarget} from '../utils/shadowdom/DOMFunctions';\nimport {getOwnerDocument} from '../utils/domHelpers';\nimport {useSyntheticBlurEvent} from './utils';\n\nexport interface FocusProps<Target = FocusableElement> extends FocusEvents<Target> {\n /** Whether the focus events should be disabled. */\n isDisabled?: boolean;\n}\n\nexport interface FocusResult<Target = FocusableElement> {\n /** Props to spread onto the target element. */\n focusProps: DOMAttributes<Target>;\n}\n\n/**\n * Handles focus events for the immediate target.\n * Focus events on child elements will be ignored.\n */\nexport function useFocus<Target extends FocusableElement = FocusableElement>(\n props: FocusProps<Target>\n): FocusResult<Target> {\n let {isDisabled, onFocus: onFocusProp, onBlur: onBlurProp, onFocusChange} = props;\n\n const onBlur: FocusProps<Target>['onBlur'] = useCallback(\n (e: FocusEvent<Target>) => {\n if (getEventTarget(e) === e.currentTarget) {\n if (onBlurProp) {\n onBlurProp(e);\n }\n\n if (onFocusChange) {\n onFocusChange(false);\n }\n\n return true;\n }\n },\n [onBlurProp, onFocusChange]\n );\n\n const onSyntheticFocus = useSyntheticBlurEvent<Target>(onBlur);\n\n const onFocus: FocusProps<Target>['onFocus'] = useCallback(\n (e: FocusEvent<Target>) => {\n // Double check that document.activeElement actually matches e.target in case a previously chained\n // focus handler already moved focus somewhere else.\n\n let eventTarget = getEventTarget(e);\n const ownerDocument = getOwnerDocument(eventTarget);\n const activeElement = ownerDocument ? getActiveElement(ownerDocument) : getActiveElement();\n if (eventTarget === e.currentTarget && eventTarget === activeElement) {\n if (onFocusProp) {\n onFocusProp(e);\n }\n\n if (onFocusChange) {\n onFocusChange(true);\n }\n\n onSyntheticFocus(e);\n }\n },\n [onFocusChange, onFocusProp, onSyntheticFocus]\n );\n\n return {\n focusProps: {\n onFocus: !isDisabled && (onFocusProp || onFocusChange || onBlurProp) ? onFocus : undefined,\n onBlur: !isDisabled && (onBlurProp || onFocusChange) ? onBlur : undefined\n }\n };\n}\n"],"names":[],"version":3,"file":"useFocus.cjs.map"}