You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
6.0 KiB
1 line
6.0 KiB
{"ast":null,"code":"import * as React from 'react';\nimport NavigationContext from \"./NavigationContext\";\nexport default function useFocusEvents(_ref) {\n var state = _ref.state,\n emitter = _ref.emitter;\n var navigation = React.useContext(NavigationContext);\n var lastFocusedKeyRef = React.useRef();\n var currentFocusedKey = state.routes[state.index].key;\n React.useEffect(function () {\n return navigation === null || navigation === void 0 ? void 0 : navigation.addListener('focus', function () {\n lastFocusedKeyRef.current = currentFocusedKey;\n emitter.emit({\n type: 'focus',\n target: currentFocusedKey\n });\n });\n }, [currentFocusedKey, emitter, navigation]);\n React.useEffect(function () {\n return navigation === null || navigation === void 0 ? void 0 : navigation.addListener('blur', function () {\n lastFocusedKeyRef.current = undefined;\n emitter.emit({\n type: 'blur',\n target: currentFocusedKey\n });\n });\n }, [currentFocusedKey, emitter, navigation]);\n React.useEffect(function () {\n var lastFocusedKey = lastFocusedKeyRef.current;\n lastFocusedKeyRef.current = currentFocusedKey;\n if (lastFocusedKey === undefined && !navigation) {\n emitter.emit({\n type: 'focus',\n target: currentFocusedKey\n });\n }\n if (lastFocusedKey === currentFocusedKey || !(navigation ? navigation.isFocused() : true)) {\n return;\n }\n if (lastFocusedKey === undefined) {\n return;\n }\n emitter.emit({\n type: 'blur',\n target: lastFocusedKey\n });\n emitter.emit({\n type: 'focus',\n target: currentFocusedKey\n });\n }, [currentFocusedKey, emitter, navigation]);\n}","map":{"version":3,"sources":["useFocusEvents.tsx"],"names":["React","NavigationContext","useFocusEvents","state","emitter","navigation","useContext","lastFocusedKeyRef","useRef","currentFocusedKey","routes","index","key","useEffect","addListener","current","emit","type","target","undefined","lastFocusedKey","isFocused"],"mappings":"AACA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAE9B,OAAOC,iBAAiB;AAYxB,eAAe,SAASC,cAAc,CAAA,IAAA,EAGnB;EAHmD,IACpEC,KAAK,GAEU,IAAA,CA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,IAAI,CAAC;QAAEC,IAAI,EAAE,OAAO;QAAEC,MAAM,EAAET;MAAkB,CAAC,CAAC;IAC5D;IAIA,IACEW,cAAc,KAAKX,iBAAiB,IACpC,EAAEJ,UAAU,GAAGA,UAAU,CAACgB,SAAS,EAAE,GAAG,IAAI,CAAC,EAC7C;MACA;IACF;IAEA,IAAID,cAAc,KAAKD,SAAS,EAAE;MAEhC;IACF;IAEAf,OAAO,CAACY,IAAI,CAAC;MAAEC,IAAI,EAAE,MAAM;MAAEC,MAAM,EAAEE;IAAe,CAAC,CAAC;IACtDhB,OAAO,CAACY,IAAI,CAAC;MAAEC,IAAI,EAAE,OAAO;MAAEC,MAAM,EAAET;IAAkB,CAAC,CAAC;EAC5D,CAAC,EAAE,CAACA,iBAAiB,EAAEL,OAAO,EAAEC,UAAU,CAAC,CAAC;AAC9C","sourceRoot":"../../src","sourcesContent":["import * as React from 'react';\nimport NavigationContext from './NavigationContext';\n/**\n * Hook to take care of emitting `focus` and `blur` events.\n */\nexport default function useFocusEvents(_ref) {\n let {\n state,\n emitter\n } = _ref;\n const navigation = React.useContext(NavigationContext);\n const lastFocusedKeyRef = React.useRef();\n const currentFocusedKey = state.routes[state.index].key;\n\n // When the parent screen changes its focus state, we also need to change child's focus\n // Coz the child screen can't be focused if the parent screen is out of focus\n React.useEffect(() => navigation === null || navigation === void 0 ? void 0 : navigation.addListener('focus', () => {\n lastFocusedKeyRef.current = currentFocusedKey;\n emitter.emit({\n type: 'focus',\n target: currentFocusedKey\n });\n }), [currentFocusedKey, emitter, navigation]);\n React.useEffect(() => navigation === null || navigation === void 0 ? void 0 : navigation.addListener('blur', () => {\n lastFocusedKeyRef.current = undefined;\n emitter.emit({\n type: 'blur',\n target: currentFocusedKey\n });\n }), [currentFocusedKey, emitter, navigation]);\n React.useEffect(() => {\n const lastFocusedKey = lastFocusedKeyRef.current;\n lastFocusedKeyRef.current = currentFocusedKey;\n\n // We wouldn't have `lastFocusedKey` on initial mount\n // Fire focus event for the current route on mount if there's no parent navigator\n if (lastFocusedKey === undefined && !navigation) {\n emitter.emit({\n type: 'focus',\n target: currentFocusedKey\n });\n }\n\n // We should only emit events when the focused key changed and navigator is focused\n // When navigator is not focused, screens inside shouldn't receive focused status either\n if (lastFocusedKey === currentFocusedKey || !(navigation ? navigation.isFocused() : true)) {\n return;\n }\n if (lastFocusedKey === undefined) {\n // Only fire events after initial mount\n return;\n }\n emitter.emit({\n type: 'blur',\n target: lastFocusedKey\n });\n emitter.emit({\n type: 'focus',\n target: currentFocusedKey\n });\n }, [currentFocusedKey, emitter, navigation]);\n}\n//# sourceMappingURL=useFocusEvents.js.map"]},"metadata":{},"sourceType":"module"} |