UNPKG

4.21 kBSource Map (JSON)View Raw
1{"version":3,"sources":["useLinkProps.tsx"],"names":["useLinkProps","to","action","root","React","useContext","NavigationContainerRefContext","navigation","NavigationHelpersContext","linkTo","onPress","e","shouldHandle","Platform","OS","defaultPrevented","metaKey","altKey","ctrlKey","shiftKey","button","undefined","includes","currentTarget","target","preventDefault","dispatch","Error","href","accessibilityRole"],"mappings":";;;;;;;AAAA;;AAKA;;AACA;;AAEA;;;;;;;;AAOA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,YAAT,CAEb;AAAEC,EAAAA,EAAF;AAAMC,EAAAA;AAAN,CAFa,EAEqB;AAClC,QAAMC,IAAI,GAAGC,KAAK,CAACC,UAAN,CAAiBC,mCAAjB,CAAb;AACA,QAAMC,UAAU,GAAGH,KAAK,CAACC,UAAN,CAAiBG,8BAAjB,CAAnB;AACA,QAAMC,MAAM,GAAG,yBAAf;;AAEA,QAAMC,OAAO,GACXC,CADc,IAEX;AAAA;;AACH,QAAIC,YAAY,GAAG,KAAnB;;AAEA,QAAIC,sBAASC,EAAT,KAAgB,KAAhB,IAAyB,CAACH,CAA9B,EAAiC;AAC/BC,MAAAA,YAAY,GAAGD,CAAC,GAAG,CAACA,CAAC,CAACI,gBAAN,GAAyB,IAAzC;AACD,KAFD,MAEO,IACL,CAACJ,CAAC,CAACI,gBAAH,IAAuB;AACvB;AACA,MAAEJ,CAAC,CAACK,OAAF,IAAaL,CAAC,CAACM,MAAf,IAAyBN,CAAC,CAACO,OAA3B,IAAsCP,CAAC,CAACQ,QAA1C,CAFA,MAEuD;AACvD;AACCR,IAAAA,CAAC,CAACS,MAAF,IAAY,IAAZ,IAAoBT,CAAC,CAACS,MAAF,KAAa,CAJlC,KAIwC;AACxC;AACA,KAACC,SAAD,EAAY,IAAZ,EAAkB,EAAlB,EAAsB,MAAtB,EAA8BC,QAA9B,qBAAuCX,CAAC,CAACY,aAAzC,qDAAuC,iBAAiBC,MAAxD,CAPK,CAO2D;AAP3D,MAQL;AACAb,QAAAA,CAAC,CAACc,cAAF;AACAb,QAAAA,YAAY,GAAG,IAAf;AACD;;AAED,QAAIA,YAAJ,EAAkB;AAChB,UAAIV,MAAJ,EAAY;AACV,YAAIK,UAAJ,EAAgB;AACdA,UAAAA,UAAU,CAACmB,QAAX,CAAoBxB,MAApB;AACD,SAFD,MAEO,IAAIC,IAAJ,EAAU;AACfA,UAAAA,IAAI,CAACuB,QAAL,CAAcxB,MAAd;AACD,SAFM,MAEA;AACL,gBAAM,IAAIyB,KAAJ,CACJ,kFADI,CAAN;AAGD;AACF,OAVD,MAUO;AACLlB,QAAAA,MAAM,CAACR,EAAD,CAAN;AACD;AACF;AACF,GAnCD;;AAqCA,SAAO;AACL2B,IAAAA,IAAI,EAAE3B,EADD;AAEL4B,IAAAA,iBAAiB,EAAE,MAFd;AAGLnB,IAAAA;AAHK,GAAP;AAKD","sourcesContent":["import {\n NavigationAction,\n NavigationContainerRefContext,\n NavigationHelpersContext,\n} from '@react-navigation/core';\nimport * as React from 'react';\nimport { GestureResponderEvent, Platform } from 'react-native';\n\nimport useLinkTo, { To } from './useLinkTo';\n\ntype Props<ParamList extends ReactNavigation.RootParamList> = {\n to: To<ParamList>;\n action?: NavigationAction;\n};\n\n/**\n * Hook to get props for an anchor tag so it can work with in page navigation.\n *\n * @param props.to Absolute path to screen (e.g. `/feeds/hot`).\n * @param props.action Optional action to use for in-page navigation. By default, the path is parsed to an action based on linking config.\n */\nexport default function useLinkProps<\n ParamList extends ReactNavigation.RootParamList\n>({ to, action }: Props<ParamList>) {\n const root = React.useContext(NavigationContainerRefContext);\n const navigation = React.useContext(NavigationHelpersContext);\n const linkTo = useLinkTo<ParamList>();\n\n const onPress = (\n e?: React.MouseEvent<HTMLAnchorElement, MouseEvent> | GestureResponderEvent\n ) => {\n let shouldHandle = false;\n\n if (Platform.OS !== 'web' || !e) {\n shouldHandle = e ? !e.defaultPrevented : true;\n } else if (\n !e.defaultPrevented && // onPress prevented default\n // @ts-expect-error: these properties exist on web, but not in React Native\n !(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && // ignore clicks with modifier keys\n // @ts-expect-error: these properties exist on web, but not in React Native\n (e.button == null || e.button === 0) && // ignore everything but left clicks\n // @ts-expect-error: these properties exist on web, but not in React Native\n [undefined, null, '', 'self'].includes(e.currentTarget?.target) // let browser handle \"target=_blank\" etc.\n ) {\n e.preventDefault();\n shouldHandle = true;\n }\n\n if (shouldHandle) {\n if (action) {\n if (navigation) {\n navigation.dispatch(action);\n } else if (root) {\n root.dispatch(action);\n } else {\n throw new Error(\n \"Couldn't find a navigation object. Is your component inside NavigationContainer?\"\n );\n }\n } else {\n linkTo(to);\n }\n }\n };\n\n return {\n href: to,\n accessibilityRole: 'link' as const,\n onPress,\n };\n}\n"]}
\No newline at end of file