Skip to content

Commit 8365874

Browse files
committed
feat: mobile styles for tabs
1 parent 0f668fe commit 8365874

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

src/components/Tabs/Bar.js

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,64 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import useMobileDetect from 'use-mobile-detect-hook';
34
import { css } from '@emotion/react';
45

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+
};
1962

2063
Bar.propTypes = {
2164
children: PropTypes.node.isRequired,

src/components/Tabs/BarItem.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const BarItem = ({ index, children, id, count, disabled }) => {
2929
user-select: none;
3030
white-space: nowrap;
3131
32+
@media (max-width: 760px) {
33+
display: block;
34+
}
35+
3236
.dark-mode & {
3337
border-bottom-color: var(--color-dark-300);
3438
}

0 commit comments

Comments
 (0)