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 | 66x 66x 444x 222x 222x 444x 444x 444x 444x 444x 444x 444x 444x 222x 222x 444x 222x 192x 96x 216x 96x 6x 222x 222x 222x 222x 222x 222x 222x 222x 222x 222x 222x 444x | // @flow strict
import React, { useState, useRef, useEffect, useReducer } from 'react';
import cx from 'classnames';
import MDCTabFoundation from '@material/tab/foundation';
import type { ElementType, Node } from 'react';
import TabIndicator from '../tab-indicator';
import { useRipple } from '../ripple';
import { createEvent } from '../utils';
import { mtClassNamesReducer, mtAttrsReducer } from '../reducers';
import type { HTMLTabElement, HTMLTabIndicatorElement } from '../mt-types';
type Props = {
as?: ElementType,
id?: string,
children?: Node,
className?: string,
contentIndicator?: boolean,
fade?: boolean,
minWidth?: boolean,
stacked?: boolean,
};
let tabIdCounter = 0;
const Tab = ({
as: Component = 'button',
contentIndicator = false,
fade = false,
minWidth = false,
stacked = false,
id,
className,
children,
...rest
}: Props) => {
const [tabId] = useState(() => {
tabIdCounter += 1;
return `mdc-tab-${tabIdCounter}`;
});
const indicatorRef = useRef<?HTMLTabIndicatorElement>();
const rootRef = useRef<?HTMLElement>();
const rippleSurface = useRef<?HTMLSpanElement>();
const contentRef = useRef<?HTMLSpanElement>();
const [mtClassName, mtClassNameDispatch] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
const [mtAttrs, mtAttrsDispatch] = useReducer(mtAttrsReducer, {
'aria-selected': false,
tabIndex: '-1',
});
const { rippleStyle, rippleClassName } = useRipple({
activator: rootRef,
surface: rippleSurface,
});
useEffect(() => {
Eif (rootRef.current) {
indicatorRef.current = (rootRef.current.querySelector(
MDCTabFoundation.strings.TAB_INDICATOR_SELECTOR,
): any); // flowlint-line unclear-type:off
}
}, [children]);
useEffect(() => {
const foundation = new MDCTabFoundation({
setAttr: (attr, value) =>
mtAttrsDispatch({ type: 'set', name: attr, value }),
addClass: mClassName =>
mtClassNameDispatch({ type: 'add', className: mClassName }),
removeClass: mClassName =>
mtClassNameDispatch({ type: 'remove', className: mClassName }),
hasClass: mClassName =>
rootRef.current && rootRef.current.classList.contains(mClassName),
activateIndicator: previousIndicatorClientRect =>
indicatorRef.current &&
indicatorRef.current.foundation.activate(previousIndicatorClientRect),
deactivateIndicator: () =>
indicatorRef.current && indicatorRef.current.foundation.deactivate(),
notifyInteracted: () => {
if (rootRef.current) {
rootRef.current.dispatchEvent(
createEvent(
MDCTabFoundation.strings.INTERACTED_EVENT,
{
tabId: rootRef.current.dataset.tab,
},
true /* bubble */,
),
);
}
},
getOffsetLeft: () => rootRef.current && rootRef.current.offsetLeft,
getOffsetWidth: () => rootRef.current && rootRef.current.offsetWidth,
getContentOffsetLeft: () =>
contentRef.current && contentRef.current.offsetLeft,
getContentOffsetWidth: () =>
contentRef.current && contentRef.current.offsetWidth,
focus: () => rootRef.current && rootRef.current.focus(),
});
foundation.init();
const handleClick = (...handleClickRef) => {
foundation.handleClick(...handleClickRef);
};
// flowlint-next-line unclear-type:off
const root: HTMLTabElement = (rootRef.current: any);
Eif (root && indicatorRef.current) {
root.foundation = foundation;
root.indicator = indicatorRef.current.foundation;
root.addEventListener('click', handleClick);
}
return () => {
Eif (root) {
root.removeEventListener('click', handleClick);
}
foundation.destroy();
};
}, []);
return (
<Component
{...(rest: mixed)}
{...mtAttrs}
data-tab={tabId}
ref={rootRef}
className={cx('mdc-tab', className, ...Array.from(mtClassName), {
'mdc-tab--min-width': minWidth,
'mdc-tab--stacked': stacked,
})}
role="tab"
>
<span ref={contentRef} className="mdc-tab__content">
{children}
{contentIndicator && <TabIndicator fade={fade} />}
</span>
<span
className={cx('mdc-tab__ripple', ...Array.from(rippleClassName))}
ref={rippleSurface}
style={rippleStyle}
/>
{!contentIndicator && <TabIndicator fade={fade} />}
</Component>
);
};
export default Tab;
|