1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import {FocusableElement} from '@react-types/shared';
|
14 | import {mergeProps, useObjectRef, useSyncRef} from '@react-aria/utils';
|
15 | import {PressProps} from './usePress';
|
16 | import {PressResponderContext} from './context';
|
17 | import React, {ForwardedRef, ReactNode, useContext, useEffect, useMemo, useRef} from 'react';
|
18 |
|
19 | interface PressResponderProps extends PressProps {
|
20 | children: ReactNode
|
21 | }
|
22 |
|
23 | export const PressResponder = React.forwardRef(({children, ...props}: PressResponderProps, ref: ForwardedRef<FocusableElement>) => {
|
24 | let isRegistered = useRef(false);
|
25 | let prevContext = useContext(PressResponderContext);
|
26 | ref = useObjectRef(ref || prevContext?.ref);
|
27 | let context = mergeProps(prevContext || {}, {
|
28 | ...props,
|
29 | ref,
|
30 | register() {
|
31 | isRegistered.current = true;
|
32 | if (prevContext) {
|
33 | prevContext.register();
|
34 | }
|
35 | }
|
36 | });
|
37 |
|
38 | useSyncRef(prevContext, ref);
|
39 |
|
40 | useEffect(() => {
|
41 | if (!isRegistered.current) {
|
42 | console.warn(
|
43 | 'A PressResponder was rendered without a pressable child. ' +
|
44 | 'Either call the usePress hook, or wrap your DOM node with <Pressable> component.'
|
45 | );
|
46 | isRegistered.current = true;
|
47 | }
|
48 | }, []);
|
49 |
|
50 | return (
|
51 | <PressResponderContext.Provider value={context}>
|
52 | {children}
|
53 | </PressResponderContext.Provider>
|
54 | );
|
55 | });
|
56 |
|
57 | export function ClearPressResponder({children}: {children: ReactNode}) {
|
58 | let context = useMemo(() => ({register: () => {}}), []);
|
59 | return (
|
60 | <PressResponderContext.Provider value={context}>
|
61 | {children}
|
62 | </PressResponderContext.Provider>
|
63 | );
|
64 | }
|