Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | 66x 66x 66x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 96x 96x 97x 216x 193x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 216x 101x 101x 101x 101x 101x 101x 101x 101x 97x 101x | // @flow strict
import React, { useEffect, useRef } from 'react';
import cx from 'classnames';
import MDCTabBarFoundation from '@material/tab-bar/foundation';
import MDCTabFoundation from '@material/tab/foundation';
import type { Node } from 'react';
import type { HTMLTabScrollerElement, HTMLTabElement } from '../mt-types';
const { strings } = MDCTabBarFoundation;
const noOp = () => {};
type Props = {
initialActive?: number,
focusOnActivate?: boolean,
useAutomaticActivation?: boolean,
children?: Node,
className?: string,
onChange?: (index: number) => void,
};
const TabBar = ({
initialActive,
focusOnActivate = true,
useAutomaticActivation = false,
className,
children,
onChange = noOp,
...rest
}: Props) => {
const rootRef = useRef<?HTMLDivElement>();
const scrollerRef = useRef<?HTMLTabScrollerElement>();
const tabsRef = useRef<HTMLTabElement[]>([]);
const foundationRef = useRef<?MDCTabBarFoundation>();
useEffect(() => {
const root = rootRef.current;
Eif (root) {
scrollerRef.current = (root.querySelector(
strings.TAB_SCROLLER_SELECTOR,
): any); // flowlint-line unclear-type:off
tabsRef.current = [].slice.call(
root.querySelectorAll(strings.TAB_SELECTOR),
);
}
}, [children]);
useEffect(() => {
const foundation = new MDCTabBarFoundation({
scrollTo: scrollX =>
scrollerRef.current && scrollerRef.current.foundation.scrollTo(scrollX),
incrementScroll: scrollXIncrement =>
scrollerRef.current &&
scrollerRef.current.foundation.incrementScroll(scrollXIncrement),
getScrollPosition: () =>
scrollerRef.current &&
scrollerRef.current.foundation.getScrollPosition(),
getScrollContentWidth: () =>
scrollerRef.current && scrollerRef.current.content.offsetWidth,
getOffsetWidth: () => rootRef.current && rootRef.current.offsetWidth,
isRTL: () =>
window
.getComputedStyle(rootRef.current)
.getPropertyValue('direction') === 'rtl',
setActiveTab: index => foundation.activateTab(index),
activateTabAtIndex: (index, clientRect) =>
tabsRef.current &&
tabsRef.current[index].foundation.activate(clientRect),
deactivateTabAtIndex: index =>
tabsRef.current && tabsRef.current[index].foundation.deactivate(),
focusTabAtIndex: index =>
tabsRef.current && tabsRef.current[index].focus(),
getTabIndicatorClientRectAtIndex: index =>
tabsRef.current &&
tabsRef.current[index].indicator.computeContentClientRect(),
getTabDimensionsAtIndex: index =>
tabsRef.current[index].foundation.computeDimensions(),
getPreviousActiveTabIndex: () => {
Eif (tabsRef.current) {
return tabsRef.current.findIndex(elem => elem.foundation.isActive());
}
return -1;
},
getFocusedTabIndex: () => {
const { activeElement } = document;
return tabsRef.current.indexOf(activeElement);
},
getIndexOfTabById: id => {
if (tabsRef.current) {
return tabsRef.current.findIndex(elem => elem.dataset.tab === id);
}
return -1;
},
getTabListLength: () => (tabsRef.current ? tabsRef.current.length : 0),
});
foundationRef.current = foundation;
foundation.init();
const handleKeydown = (...handleKeydownRest) =>
foundation.handleKeyDown(...handleKeydownRest);
const handleTabInteraction = (...handleTabInteractionRest) =>
foundation.handleTabInteraction(...handleTabInteractionRest);
const root = rootRef.current;
Eif (root) {
root.addEventListener('keydown', handleKeydown);
root.addEventListener(
MDCTabFoundation.strings.INTERACTED_EVENT,
handleTabInteraction,
);
}
return () => {
Eif (root) {
root.removeEventListener('keydown', handleKeydown);
root.removeEventListener(
MDCTabFoundation.strings.INTERACTED_EVENT,
handleTabInteraction,
);
}
foundation.destroy();
foundationRef.current = null;
};
}, []);
useEffect(() => {
Eif (tabsRef.current) {
tabsRef.current.forEach(tab =>
tab.foundation.setFocusOnActivate(focusOnActivate),
);
}
}, [focusOnActivate]);
useEffect(() => {
Eif (foundationRef.current) {
foundationRef.current.setUseAutomaticActivation(useAutomaticActivation);
}
}, [useAutomaticActivation]);
useEffect(() => {
Eif (foundationRef.current) {
foundationRef.current.adapter_.notifyTabActivated = onChange;
}
}, [children, onChange]);
useEffect(() => {
if (
initialActive !== undefined &&
initialActive !== null &&
foundationRef.current
) {
foundationRef.current.activateTab(initialActive);
}
}, [initialActive]);
return (
<div
{...rest}
ref={rootRef}
className={cx('mdc-tab-bar', className)}
role="tablist"
>
{children}
</div>
);
};
export default TabBar;
|