MCPcopy
hub / github.com/adobe/react-spectrum / useTab

Function useTab

packages/react-aria/src/tabs/useTab.ts:52–104  ·  view source on GitHub ↗
(
  props: AriaTabProps,
  state: TabListState<T>,
  ref: RefObject<FocusableElement | null>
)

Source from the content-addressed store, hash-verified

50 * When selected, the associated tab panel is shown.
51 */
52export function useTab<T>(
53 props: AriaTabProps,
54 state: TabListState<T>,
55 ref: RefObject<FocusableElement | null>
56): TabAria {
57 let {key, isDisabled: propsDisabled, shouldSelectOnPressUp} = props;
58 let {selectionManager: manager, selectedKey} = state;
59
60 let isSelected = key === selectedKey;
61
62 let isDisabled = propsDisabled || state.isDisabled || state.selectionManager.isDisabled(key);
63 let item = state.collection.getItem(key);
64 let {itemProps, isPressed} = useSelectableItem({
65 selectionManager: manager,
66 key,
67 ref,
68 isDisabled,
69 // Link tabs should behave like native anchors (navigate on press up)
70 // This avoids reopening beforeunload dialogs when browsers replay
71 // queued pointer enter/leave events after cancellation.
72 shouldSelectOnPressUp: shouldSelectOnPressUp ?? item?.props.href != null,
73 linkBehavior: 'selection'
74 });
75
76 let tabId = generateId(state, key, 'tab');
77 let tabPanelId = generateId(state, key, 'tabpanel');
78 let {tabIndex} = itemProps;
79
80 let domProps = filterDOMProps(item?.props, {labelable: true});
81 delete domProps.id;
82 let linkProps = useLinkProps(item?.props);
83 let {focusableProps} = useFocusable(
84 {
85 ...item?.props,
86 isDisabled
87 },
88 ref
89 );
90
91 return {
92 tabProps: mergeProps(domProps, focusableProps, linkProps, itemProps, {
93 id: tabId,
94 'aria-selected': isSelected,
95 'aria-disabled': isDisabled || undefined,
96 'aria-controls': isSelected ? tabPanelId : undefined,
97 tabIndex: isDisabled ? undefined : tabIndex,
98 role: 'tab'
99 }),
100 isSelected,
101 isDisabled,
102 isPressed
103 };
104}

Callers 3

TabFunction · 0.90
Tabs.tsxFile · 0.85
TabFunction · 0.85

Calls 8

useSelectableItemFunction · 0.90
generateIdFunction · 0.90
filterDOMPropsFunction · 0.90
useLinkPropsFunction · 0.90
useFocusableFunction · 0.90
mergePropsFunction · 0.90
isDisabledMethod · 0.65
getItemMethod · 0.65

Tested by

no test coverage detected