|
1 | | -import React, { forwardRef } from 'react'; |
| 1 | +import React, { forwardRef, useMemo } from 'react'; |
2 | 2 | import { useLink } from './useLink'; |
3 | 3 | import { mergeRefs } from '@gluestack-ui/utils'; |
4 | 4 |
|
5 | 5 | import { composeEventHandlers } from '@gluestack-ui/utils'; |
6 | 6 |
|
7 | 7 | import { useFocusRing, useFocus } from '@react-native-aria/focus'; |
8 | 8 | import { useHover, usePress } from '@react-native-aria/interactions'; |
| 9 | +import { LinkContext } from './Context'; |
9 | 10 |
|
10 | 11 | export const Link = <LinkProps,>(StyledLink: React.ComponentType<LinkProps>) => |
11 | 12 | forwardRef( |
@@ -37,60 +38,82 @@ export const Link = <LinkProps,>(StyledLink: React.ComponentType<LinkProps>) => |
37 | 38 | href, |
38 | 39 | onPress, |
39 | 40 | _ref, |
| 41 | + isDisabled, |
40 | 42 | }); |
41 | 43 |
|
| 44 | + const contextValue = useMemo(() => { |
| 45 | + return { |
| 46 | + isHovered: isHoveredProp || isHovered, |
| 47 | + isFocused: isFocusedProp || isFocused, |
| 48 | + isPressed: isPressedProp || isPressed, |
| 49 | + isDisabled: isDisabled, |
| 50 | + isFocusVisible: isFocusVisibleProp || isFocusVisible, |
| 51 | + }; |
| 52 | + }, [ |
| 53 | + isHoveredProp, |
| 54 | + isHovered, |
| 55 | + isFocusedProp, |
| 56 | + isFocused, |
| 57 | + isPressedProp, |
| 58 | + isPressed, |
| 59 | + isDisabled, |
| 60 | + isFocusVisibleProp, |
| 61 | + isFocusVisible, |
| 62 | + ]); |
42 | 63 | return ( |
43 | | - <StyledLink |
44 | | - ref={mergeRefs([_ref, ref])} |
45 | | - states={{ |
46 | | - hover: isHoveredProp || isHovered, |
47 | | - focus: isFocusedProp || isFocused, |
48 | | - active: isPressedProp || isPressed, |
49 | | - disabled: isDisabled, |
50 | | - focusVisible: isFocusVisibleProp || isFocusVisible, |
51 | | - }} |
52 | | - dataSet={{ |
53 | | - hover: isHoveredProp || isHovered ? 'true' : 'false', |
54 | | - focus: isFocusedProp || isFocused ? 'true' : 'false', |
55 | | - active: isPressedProp || isPressed ? 'true' : 'false', |
56 | | - disabled: isDisabled ? 'true' : 'false', |
57 | | - focusVisible: |
58 | | - isFocusVisibleProp || isFocusVisible ? 'true' : 'false', |
59 | | - }} |
60 | | - disabled={isDisabled} |
61 | | - {...linkProps} |
62 | | - {...props} |
63 | | - onPressIn={composeEventHandlers( |
64 | | - props?.onPressIn, |
65 | | - pressProps.onPressIn |
66 | | - )} |
67 | | - onPressOut={composeEventHandlers( |
68 | | - props?.onPressOut, |
69 | | - pressProps.onPressOut |
70 | | - )} |
71 | | - // @ts-ignore - web only |
72 | | - onHoverIn={composeEventHandlers( |
73 | | - props?.onHoverIn, |
74 | | - hoverProps.onHoverIn |
75 | | - )} |
76 | | - // @ts-ignore - web only |
77 | | - onHoverOut={composeEventHandlers( |
78 | | - props?.onHoverOut, |
79 | | - hoverProps.onHoverOut |
80 | | - )} |
81 | | - // @ts-ignore - web only |
82 | | - onFocus={composeEventHandlers( |
83 | | - composeEventHandlers(props?.onFocus, focusProps.onFocus), |
84 | | - focusRingProps.onFocus |
85 | | - )} |
86 | | - // @ts-ignore - web only |
87 | | - onBlur={composeEventHandlers( |
88 | | - composeEventHandlers(props?.onBlur, focusProps.onBlur), |
89 | | - focusRingProps.onBlur |
90 | | - )} |
91 | | - > |
92 | | - {children} |
93 | | - </StyledLink> |
| 64 | + <LinkContext.Provider value={contextValue}> |
| 65 | + <StyledLink |
| 66 | + ref={mergeRefs([_ref, ref])} |
| 67 | + states={{ |
| 68 | + hover: isHoveredProp || isHovered, |
| 69 | + focus: isFocusedProp || isFocused, |
| 70 | + active: isPressedProp || isPressed, |
| 71 | + disabled: isDisabled, |
| 72 | + focusVisible: isFocusVisibleProp || isFocusVisible, |
| 73 | + }} |
| 74 | + dataSet={{ |
| 75 | + hover: isHoveredProp || isHovered ? 'true' : 'false', |
| 76 | + focus: isFocusedProp || isFocused ? 'true' : 'false', |
| 77 | + active: isPressedProp || isPressed ? 'true' : 'false', |
| 78 | + disabled: isDisabled ? 'true' : 'false', |
| 79 | + focusVisible: |
| 80 | + isFocusVisibleProp || isFocusVisible ? 'true' : 'false', |
| 81 | + }} |
| 82 | + disabled={isDisabled} |
| 83 | + {...linkProps} |
| 84 | + {...props} |
| 85 | + onPressIn={composeEventHandlers( |
| 86 | + props?.onPressIn, |
| 87 | + pressProps.onPressIn |
| 88 | + )} |
| 89 | + onPressOut={composeEventHandlers( |
| 90 | + props?.onPressOut, |
| 91 | + pressProps.onPressOut |
| 92 | + )} |
| 93 | + // @ts-ignore - web only |
| 94 | + onHoverIn={composeEventHandlers( |
| 95 | + props?.onHoverIn, |
| 96 | + hoverProps.onHoverIn |
| 97 | + )} |
| 98 | + // @ts-ignore - web only |
| 99 | + onHoverOut={composeEventHandlers( |
| 100 | + props?.onHoverOut, |
| 101 | + hoverProps.onHoverOut |
| 102 | + )} |
| 103 | + // @ts-ignore - web only |
| 104 | + onFocus={composeEventHandlers( |
| 105 | + composeEventHandlers(props?.onFocus, focusProps.onFocus), |
| 106 | + focusRingProps.onFocus |
| 107 | + )} |
| 108 | + // @ts-ignore - web only |
| 109 | + onBlur={composeEventHandlers( |
| 110 | + composeEventHandlers(props?.onBlur, focusProps.onBlur), |
| 111 | + focusRingProps.onBlur |
| 112 | + )} |
| 113 | + > |
| 114 | + {children} |
| 115 | + </StyledLink> |
| 116 | + </LinkContext.Provider> |
94 | 117 | ); |
95 | 118 | } |
96 | 119 | ); |
0 commit comments