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 178 179 180 | 66x 200x 200x 200x 200x 200x 200x 200x 200x 100x 100x 100x 288x 96x 96x 96x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 200x | // @flow strict
import React, { useReducer, useEffect, useRef } from 'react';
import cx from 'classnames';
import MDCTabScrollerFoundation from '@material/tab-scroller/foundation';
import * as util from '@material/tab-scroller/util';
import { matches } from '@material/dom/ponyfill';
import { applyPassive } from '@material/dom/events';
import type { Node } from 'react';
import { mtStylesReducer, mtClassNamesReducer } from '../reducers';
import { normalizePropToReactStyle } from '../utils';
import type { HTMLTabScrollerElement } from '../mt-types';
type Props = {
children: Node,
align?: 'start' | 'end' | 'center',
};
const TabScroller = ({ children, align, ...rest }: Props) => {
const rootRef = useRef<?HTMLDivElement>();
const contentRef = useRef<?HTMLDivElement>();
const areaRef = useRef<?HTMLDivElement>();
const [mtRootClassName, mtRootClassNameDispatch] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
const [mtAreaClassName, mtAreaClassNameDispatch] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
const [mtContentStyle, mtContentStyleDispatch] = useReducer(
mtStylesReducer,
{},
);
const [mtAreaStyle, mtAreaStyleDispatch] = useReducer(mtStylesReducer, {});
useEffect(() => {
const foundation = new MDCTabScrollerFoundation({
eventTargetMatchesSelector: (evtTarget, selector) =>
matches(evtTarget, selector),
addClass: mClassName =>
mtRootClassNameDispatch({ type: 'add', className: mClassName }),
removeClass: mClassName =>
mtRootClassNameDispatch({ type: 'remove', className: mClassName }),
addScrollAreaClass: mClassName =>
mtAreaClassNameDispatch({ type: 'add', className: mClassName }),
setScrollAreaStyleProperty: (prop, value) =>
mtAreaStyleDispatch({
type: 'set',
name: normalizePropToReactStyle(prop),
value,
}),
setScrollContentStyleProperty: (prop, value) =>
mtContentStyleDispatch({
type: 'set',
name: normalizePropToReactStyle(prop),
value,
}),
getScrollContentStyleValue: propName =>
window.getComputedStyle(contentRef.current).getPropertyValue(propName),
setScrollAreaScrollLeft: scrollX => {
if (areaRef.current) {
areaRef.current.scrollLeft = scrollX;
}
},
getScrollAreaScrollLeft: () =>
areaRef.current && areaRef.current.scrollLeft,
getScrollContentOffsetWidth: () =>
contentRef.current && contentRef.current.offsetWidth,
getScrollAreaOffsetWidth: () =>
areaRef.current && areaRef.current.offsetWidth,
computeScrollAreaClientRect: () =>
areaRef.current && areaRef.current.getBoundingClientRect(),
computeScrollContentClientRect: () =>
contentRef.current && contentRef.current.getBoundingClientRect(),
computeHorizontalScrollbarHeight: () =>
util.computeHorizontalScrollbarHeight(document),
});
foundation.init();
const content = contentRef.current;
const area = areaRef.current;
const handleInteraction = (...handleInteractionRest) =>
foundation.handleInteraction(...handleInteractionRest);
const handleTransitionEnd = (...handleTransitionEndRest) =>
foundation.handleTransitionEnd(...handleTransitionEndRest);
Eif (area) {
area.addEventListener('wheel', handleInteraction, applyPassive());
area.addEventListener('touchstart', handleInteraction, applyPassive());
area.addEventListener('pointerdown', handleInteraction, applyPassive());
area.addEventListener('mousedown', handleInteraction, applyPassive());
area.addEventListener('keydown', handleInteraction, applyPassive());
}
Eif (content) {
content.addEventListener('transitionend', handleTransitionEnd);
}
Eif (rootRef.current && contentRef.current) {
// flowlint-next-line unclear-type:off
const root: HTMLTabScrollerElement = (rootRef.current: any);
root.foundation = foundation;
root.content = contentRef.current;
}
return () => {
Eif (area) {
area.removeEventListener('wheel', handleInteraction, applyPassive());
area.removeEventListener(
'touchstart',
handleInteraction,
applyPassive(),
);
area.removeEventListener(
'pointerdown',
handleInteraction,
applyPassive(),
);
area.removeEventListener(
'mousedown',
handleInteraction,
applyPassive(),
);
area.removeEventListener('keydown', handleInteraction, applyPassive());
}
Eif (content) {
content.removeEventListener('transitionend', handleTransitionEnd);
}
foundation.destroy();
};
}, []);
return (
<div
ref={rootRef}
{...(rest: mixed)}
className={cx('mdc-tab-scroller', ...Array.from(mtRootClassName))}
>
<div
ref={areaRef}
style={mtAreaStyle}
className={cx(
'mdc-tab-scroller__scroll-area',
...Array.from(mtAreaClassName),
{
'mdc-tab-scroller--align-start': align === 'start',
'mdc-tab-scroller--align-end': align === 'end',
'mdc-tab-scroller--align-center': align === 'center',
},
)}
>
<div
style={mtContentStyle}
ref={contentRef}
className={cx('mdc-tab-scroller__scroll-content')}
>
{children}
</div>
</div>
</div>
);
};
export default TabScroller;
|