|
1 | 1 | import React from 'react';
|
2 | 2 | import PropTypes from 'prop-types';
|
| 3 | +import useMobileDetect from 'use-mobile-detect-hook'; |
3 | 4 | import { css } from '@emotion/react';
|
4 | 5 |
|
5 |
| -const Bar = ({ children }) => ( |
6 |
| - <div |
7 |
| - role="tablist" |
8 |
| - css={css` |
9 |
| - display: flex; |
10 |
| - margin-bottom: 1em; |
11 |
| - overflow: scroll; |
12 |
| - `} |
13 |
| - > |
14 |
| - {React.Children.map(children, (child, index) => |
15 |
| - React.cloneElement(child, { ...child.props, index }) |
16 |
| - )} |
17 |
| - </div> |
18 |
| -); |
| 6 | +import useTabs from './useTabs'; |
| 7 | + |
| 8 | +const MobileTabControl = ({ children }) => { |
| 9 | + const [currentTab, setCurrentTab] = useTabs(); |
| 10 | + |
| 11 | + return ( |
| 12 | + <select |
| 13 | + onChange={(e) => setCurrentTab(e.target.value)} |
| 14 | + css={css` |
| 15 | + width: 100%; |
| 16 | + margin-bottom: 1em; |
| 17 | + padding: 0.5em; |
| 18 | + border-radius: 4px; |
| 19 | + border-color: var(--secondary-background-color); |
| 20 | + background-color: var(--primary-background-color); |
| 21 | + color: var(--primary-text-color); |
| 22 | + `} |
| 23 | + > |
| 24 | + {React.Children.map(children, ({ props }) => ( |
| 25 | + <option |
| 26 | + key={props.id} |
| 27 | + value={props.id} |
| 28 | + selected={props.id === currentTab} |
| 29 | + disabled={props.disabled} |
| 30 | + > |
| 31 | + {props.children} |
| 32 | + {(props.count || props.count === 0) && ` (${props.count})`} |
| 33 | + </option> |
| 34 | + ))} |
| 35 | + </select> |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +const Bar = ({ children }) => { |
| 40 | + const detectMobile = useMobileDetect(); |
| 41 | + const isMobile = detectMobile.isMobile(); |
| 42 | + |
| 43 | + if (isMobile) { |
| 44 | + return <MobileTabControl children={children} />; |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div |
| 49 | + role="tablist" |
| 50 | + css={css` |
| 51 | + display: flex; |
| 52 | + margin-bottom: 1em; |
| 53 | + overflow: scroll; |
| 54 | + `} |
| 55 | + > |
| 56 | + {React.Children.map(children, (child, index) => |
| 57 | + React.cloneElement(child, { ...child.props, index }) |
| 58 | + )} |
| 59 | + </div> |
| 60 | + ); |
| 61 | +}; |
19 | 62 |
|
20 | 63 | Bar.propTypes = {
|
21 | 64 | children: PropTypes.node.isRequired,
|
|
0 commit comments