Skip to content

Commit

Permalink
feat: mobile styles for tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
zstix committed Jun 2, 2021
1 parent 0f668fe commit 8365874
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
71 changes: 57 additions & 14 deletions src/components/Tabs/Bar.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
import React from 'react';
import PropTypes from 'prop-types';
import useMobileDetect from 'use-mobile-detect-hook';
import { css } from '@emotion/react';

const Bar = ({ children }) => (
<div
role="tablist"
css={css`
display: flex;
margin-bottom: 1em;
overflow: scroll;
`}
>
{React.Children.map(children, (child, index) =>
React.cloneElement(child, { ...child.props, index })
)}
</div>
);
import useTabs from './useTabs';

const MobileTabControl = ({ children }) => {
const [currentTab, setCurrentTab] = useTabs();

return (
<select
onChange={(e) => setCurrentTab(e.target.value)}
css={css`
width: 100%;
margin-bottom: 1em;
padding: 0.5em;
border-radius: 4px;
border-color: var(--secondary-background-color);
background-color: var(--primary-background-color);
color: var(--primary-text-color);
`}
>
{React.Children.map(children, ({ props }) => (
<option
key={props.id}
value={props.id}
selected={props.id === currentTab}
disabled={props.disabled}
>
{props.children}
{(props.count || props.count === 0) && ` (${props.count})`}
</option>
))}
</select>
);
};

const Bar = ({ children }) => {
const detectMobile = useMobileDetect();
const isMobile = detectMobile.isMobile();

if (isMobile) {
return <MobileTabControl children={children} />;
}

return (
<div
role="tablist"
css={css`
display: flex;
margin-bottom: 1em;
overflow: scroll;
`}
>
{React.Children.map(children, (child, index) =>
React.cloneElement(child, { ...child.props, index })
)}
</div>
);
};

Bar.propTypes = {
children: PropTypes.node.isRequired,
Expand Down
4 changes: 4 additions & 0 deletions src/components/Tabs/BarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const BarItem = ({ index, children, id, count, disabled }) => {
user-select: none;
white-space: nowrap;
@media (max-width: 760px) {
display: block;
}
.dark-mode & {
border-bottom-color: var(--color-dark-300);
}
Expand Down

0 comments on commit 8365874

Please sign in to comment.