mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-25 03:49:11 +00:00
feat(design-system): redesign tab-view around quick nav
This commit is contained in:
parent
a5f819bc3e
commit
2b7b245a0d
10 changed files with 268 additions and 53 deletions
|
@ -28,6 +28,10 @@ export class BreakpointsProvider extends React.Component<{}, ScreenSize> {
|
|||
Object.entries(this.mediaQueries).forEach(([key, mediaQuery]) =>
|
||||
mediaQuery.addEventListener('change', this.handleMediaEvent)
|
||||
);
|
||||
|
||||
this.handleMediaEvent();
|
||||
setTimeout(() => this.handleMediaEvent(), 0);
|
||||
setTimeout(() => this.handleMediaEvent(), 10);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
|
@ -36,8 +40,7 @@ export class BreakpointsProvider extends React.Component<{}, ScreenSize> {
|
|||
);
|
||||
}
|
||||
|
||||
handleMediaEvent = (event: MediaQueryListEvent) => {
|
||||
console.log('handleMediaEvent', { event });
|
||||
handleMediaEvent = () => {
|
||||
this.setState({
|
||||
...resetScreen,
|
||||
...this.calculateScreen(),
|
||||
|
|
31
packages/design-system/atoms/quick-nav/QuickNav.stories.tsx
Normal file
31
packages/design-system/atoms/quick-nav/QuickNav.stories.tsx
Normal file
|
@ -0,0 +1,31 @@
|
|||
import { useState } from 'react';
|
||||
import { BreakpointsProvider } from '../breakpoints/BreakpointProvider';
|
||||
import { QuickNav, QuickNavCollapsed, QuickNavExpanded, QuickNavProps } from './QuickNav';
|
||||
|
||||
export default {
|
||||
title: 'Atoms/Quick Nav',
|
||||
component: QuickNav,
|
||||
decorators: [(story) => <BreakpointsProvider>{story()}</BreakpointsProvider>],
|
||||
args: {
|
||||
navItems: ['AAAA', 'BBBB', 'CCCC'],
|
||||
currentNavItem: 'AAAA',
|
||||
},
|
||||
};
|
||||
|
||||
const stateWrapper = (args: QuickNavProps, Component: typeof QuickNav) => {
|
||||
const [currentNavItem, setCurrentNavItem] = useState(args.currentNavItem);
|
||||
return (
|
||||
<Component
|
||||
{...args}
|
||||
currentNavItem={currentNavItem}
|
||||
onNavChange={(newNavItem) => {
|
||||
setCurrentNavItem(newNavItem);
|
||||
args.onNavChange(newNavItem);
|
||||
}}
|
||||
></Component>
|
||||
);
|
||||
};
|
||||
|
||||
export const quickNav = (args) => stateWrapper(args, QuickNav);
|
||||
export const expanded = (args) => stateWrapper(args, QuickNavExpanded);
|
||||
export const collapsed = (args) => stateWrapper(args, QuickNavCollapsed);
|
45
packages/design-system/atoms/quick-nav/QuickNav.styled.ts
Normal file
45
packages/design-system/atoms/quick-nav/QuickNav.styled.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import styled, { css } from 'styled-components';
|
||||
import { palette } from '../colors';
|
||||
import { transitions } from '../timings';
|
||||
|
||||
export const NavItem = styled.div<{ selected: boolean }>`
|
||||
padding: 7px;
|
||||
border-radius: 2px;
|
||||
margin-bottom: 2px;
|
||||
transition: background-color ${transitions.actionable}s ease-in-out;
|
||||
|
||||
&:hover {
|
||||
background-color: ${palette.taupe400};
|
||||
}
|
||||
|
||||
${(props) =>
|
||||
props.selected &&
|
||||
css`
|
||||
background-color: ${palette.taupe300};
|
||||
`}
|
||||
`;
|
||||
|
||||
export const DropdownNavIcon = styled.div`
|
||||
padding: 5px;
|
||||
transition: transform ${transitions.actionable}s ease-in-out;
|
||||
transform: translateY(2px);
|
||||
`;
|
||||
|
||||
export const DropdownNavCurrent = styled.div`
|
||||
padding: 5px;
|
||||
`;
|
||||
|
||||
export const DropdownNavOpener = styled.div`
|
||||
padding: 5px;
|
||||
display: flex;
|
||||
cursor: pointer;
|
||||
border-radius: 2px;
|
||||
transition: background-color ${transitions.actionable}s ease-in-out;
|
||||
|
||||
&:hover {
|
||||
background-color: ${palette.taupe300};
|
||||
${DropdownNavIcon} {
|
||||
transform: translateY(3px);
|
||||
}
|
||||
}
|
||||
`;
|
85
packages/design-system/atoms/quick-nav/QuickNav.tsx
Normal file
85
packages/design-system/atoms/quick-nav/QuickNav.tsx
Normal file
|
@ -0,0 +1,85 @@
|
|||
// thing should be everything visible on desktop/tablet and a popover when small
|
||||
|
||||
import { useBreakpointContext } from '@roleypoly/design-system/atoms/breakpoints';
|
||||
import { Popover } from '@roleypoly/design-system/atoms/popover';
|
||||
import { useState } from 'react';
|
||||
import { GoChevronDown } from 'react-icons/go';
|
||||
import {
|
||||
DropdownNavCurrent,
|
||||
DropdownNavIcon,
|
||||
DropdownNavOpener,
|
||||
NavItem,
|
||||
} from './QuickNav.styled';
|
||||
|
||||
export type QuickNavProps = {
|
||||
navItems: string[];
|
||||
onNavChange?: (newNavItem: string) => void;
|
||||
currentNavItem?: string;
|
||||
};
|
||||
|
||||
export const QuickNav = (props: QuickNavProps) => {
|
||||
const breakpoints = useBreakpointContext();
|
||||
|
||||
if (breakpoints.screenSize.onSmallScreen) {
|
||||
return <QuickNavCollapsed {...props} />;
|
||||
}
|
||||
|
||||
return <QuickNavExpanded {...props} />;
|
||||
};
|
||||
|
||||
export const QuickNavExpanded = (props: QuickNavProps) => {
|
||||
return (
|
||||
<div>
|
||||
{props.navItems.map((navItem) => (
|
||||
<NavItem
|
||||
onClick={() => props.onNavChange?.(navItem)}
|
||||
selected={props.currentNavItem === navItem}
|
||||
key={navItem}
|
||||
>
|
||||
{navItem}
|
||||
</NavItem>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const QuickNavCollapsed = (props: QuickNavProps) => {
|
||||
const [popoverState, setPopoverState] = useState(false);
|
||||
|
||||
return (
|
||||
<div>
|
||||
{popoverState ? (
|
||||
<Popover
|
||||
headContent={<>Server Editor</>}
|
||||
position={'top left'}
|
||||
active={popoverState}
|
||||
onExit={() => setPopoverState(false)}
|
||||
>
|
||||
{() => (
|
||||
<>
|
||||
{props.navItems.map((navItem) => (
|
||||
<NavItem
|
||||
onClick={() => {
|
||||
setPopoverState(false);
|
||||
props.onNavChange?.(navItem);
|
||||
}}
|
||||
selected={props.currentNavItem === navItem}
|
||||
key={navItem}
|
||||
>
|
||||
{navItem}
|
||||
</NavItem>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
</Popover>
|
||||
) : (
|
||||
<DropdownNavOpener onClick={() => setPopoverState(true)}>
|
||||
<DropdownNavIcon>
|
||||
<GoChevronDown />
|
||||
</DropdownNavIcon>
|
||||
<DropdownNavCurrent>{props.currentNavItem}</DropdownNavCurrent>
|
||||
</DropdownNavOpener>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
1
packages/design-system/atoms/quick-nav/index.ts
Normal file
1
packages/design-system/atoms/quick-nav/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './QuickNav';
|
|
@ -1,8 +1,10 @@
|
|||
import * as React from 'react';
|
||||
import { BreakpointsProvider } from '../breakpoints';
|
||||
import { Tab, TabView } from './TabView';
|
||||
|
||||
export default {
|
||||
title: 'Atoms/Tab View',
|
||||
decorators: [(story) => <BreakpointsProvider>{story()}</BreakpointsProvider>],
|
||||
argTypes: {
|
||||
tabCount: { control: 'range', min: 1, max: 100 },
|
||||
},
|
||||
|
@ -16,7 +18,7 @@ export const ManyTabs = ({ tabCount }) => {
|
|||
<Tab title={`tab ${i}`}>
|
||||
{() => (
|
||||
<>
|
||||
<h1>tab {i}</h1>
|
||||
<p>tab {i}</p>
|
||||
<p>hello!!!!!</p>
|
||||
</>
|
||||
)}
|
||||
|
|
|
@ -1,42 +1,69 @@
|
|||
import { onTablet } from '@roleypoly/design-system/atoms/breakpoints';
|
||||
import { palette } from '@roleypoly/design-system/atoms/colors';
|
||||
import { transitions } from '@roleypoly/design-system/atoms/timings';
|
||||
import styled, { css } from 'styled-components';
|
||||
import { onSmallScreen } from '../breakpoints';
|
||||
import { palette } from '../colors';
|
||||
import { transitions } from '../timings';
|
||||
import { text500 } from '../typography';
|
||||
|
||||
export const TabViewStyled = styled.div``;
|
||||
export const TabViewStyled = styled.div`
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
overflow: hidden;
|
||||
|
||||
${onSmallScreen(
|
||||
css`
|
||||
flex-direction: column;
|
||||
`
|
||||
)}
|
||||
`;
|
||||
|
||||
export const TabTitleRow = styled.div`
|
||||
display: flex;
|
||||
border-bottom: 1px solid ${palette.taupe100};
|
||||
overflow-x: auto;
|
||||
overflow-y: hidden;
|
||||
white-space: nowrap;
|
||||
flex: 1;
|
||||
width: 23vw;
|
||||
position: fixed;
|
||||
${onSmallScreen(
|
||||
css`
|
||||
position: unset;
|
||||
max-width: 100vw;
|
||||
`
|
||||
)}
|
||||
`;
|
||||
|
||||
export const TabTitle = styled.div<{ selected: boolean }>`
|
||||
flex: 1;
|
||||
text-align: center;
|
||||
padding: 0.7em 1em;
|
||||
border-bottom: 3px solid transparent;
|
||||
transition: border-color ${transitions.in2out}s ease-in-out,
|
||||
color ${transitions.in2out}s ease-in-out;
|
||||
padding: 7px;
|
||||
cursor: pointer;
|
||||
color: ${palette.taupe500};
|
||||
transition: background-color ${transitions.actionable}s ease-in-out;
|
||||
border-radius: 2px;
|
||||
margin-bottom: 5px;
|
||||
|
||||
&:hover {
|
||||
background-color: ${palette.taupe400};
|
||||
}
|
||||
|
||||
${(props) =>
|
||||
props.selected
|
||||
? css`
|
||||
color: unset;
|
||||
border-bottom-color: ${palette.taupe500};
|
||||
`
|
||||
: css`
|
||||
&:hover {
|
||||
border-bottom-color: ${palette.taupe300};
|
||||
color: unset;
|
||||
}
|
||||
`};
|
||||
${onTablet(css`
|
||||
padding: 0.45em 1em;
|
||||
`)}
|
||||
props.selected &&
|
||||
css`
|
||||
background-color: ${palette.taupe300};
|
||||
`}
|
||||
`;
|
||||
|
||||
export const TabContent = styled.div``;
|
||||
export const TabContent = styled.div`
|
||||
padding-left: 1em;
|
||||
margin-left: 23vw;
|
||||
flex: 1;
|
||||
|
||||
${onSmallScreen(
|
||||
css`
|
||||
padding-left: 0;
|
||||
margin-left: 0;
|
||||
position: unset;
|
||||
max-width: 100vw;
|
||||
`
|
||||
)}
|
||||
`;
|
||||
|
||||
export const TabContentTitle = styled.div`
|
||||
${text500}
|
||||
|
||||
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
|
||||
padding: 10px;
|
||||
`;
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
import * as React from 'react';
|
||||
import { TabContent, TabTitle, TabTitleRow, TabViewStyled } from './TabView.styled';
|
||||
import { QuickNav } from '../quick-nav';
|
||||
import {
|
||||
TabContent,
|
||||
TabContentTitle,
|
||||
TabTitleRow,
|
||||
TabViewStyled,
|
||||
} from './TabView.styled';
|
||||
|
||||
export type TabViewProps = {
|
||||
children: React.ReactNode[];
|
||||
|
@ -25,26 +31,40 @@ export const TabView = (props: TabViewProps) => {
|
|||
}
|
||||
|
||||
const [currentTab, setCurrentTab] = React.useState<number>(props.initialTab ?? 0);
|
||||
const tabRefs = tabNames.reduce<{ [name: string]: React.RefObject<HTMLDivElement> }>(
|
||||
(acc, name) => ({ ...acc, [name]: React.createRef() }),
|
||||
{}
|
||||
);
|
||||
|
||||
return (
|
||||
<TabViewStyled>
|
||||
<TabTitleRow>
|
||||
{tabNames.map((tabName, idx) => (
|
||||
<TabTitle
|
||||
selected={currentTab === idx}
|
||||
onClick={() => setCurrentTab(idx)}
|
||||
key={`tab${tabName}${idx}`}
|
||||
>
|
||||
{tabName}
|
||||
</TabTitle>
|
||||
))}
|
||||
<QuickNav
|
||||
currentNavItem={tabNames[currentTab]}
|
||||
navItems={tabNames}
|
||||
onNavChange={(newTabName) => {
|
||||
setCurrentTab(tabNames.findIndex((name) => newTabName === name));
|
||||
tabRefs[newTabName].current?.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</TabTitleRow>
|
||||
<TabContent>
|
||||
{props.children[currentTab] || (
|
||||
<i onLoad={() => setCurrentTab(0)}>
|
||||
Tabs were misconfigured, resetting to zero.
|
||||
</i>
|
||||
)}
|
||||
{React.Children.map(props.children, (child) => {
|
||||
if (!React.isValidElement(child)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div key={child.props.title}>
|
||||
<TabContentTitle ref={tabRefs[child.props.title]}>
|
||||
{child.props.title}
|
||||
</TabContentTitle>
|
||||
{child}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</TabContent>
|
||||
</TabViewStyled>
|
||||
);
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
import { BreakpointsProvider } from '@roleypoly/design-system/atoms/breakpoints';
|
||||
import { guildEnum } from '@roleypoly/design-system/fixtures/storyData';
|
||||
import * as React from 'react';
|
||||
import { guildEnum } from '../../fixtures/storyData';
|
||||
import { EditorShell } from './EditorShell';
|
||||
|
||||
export default {
|
||||
title: 'Organisms/Editor',
|
||||
component: EditorShell,
|
||||
decorators: [(story) => <BreakpointsProvider>{story()}</BreakpointsProvider>],
|
||||
};
|
||||
|
||||
export const Shell = () => <EditorShell guild={guildEnum.guildsList[0]} />;
|
||||
export const Shell = () => <EditorShell guild={guildEnum.guilds[0]} />;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import { Tab, TabView } from '@roleypoly/design-system/atoms/tab-view';
|
||||
import { EditorCategory } from '@roleypoly/design-system/molecules/editor-category';
|
||||
import { PresentableGuild } from '@roleypoly/types';
|
||||
import * as React from 'react';
|
||||
import { EditorCategory } from '../../molecules/editor-category';
|
||||
import { CategoryContainer } from './EditorShell.styled';
|
||||
|
||||
type Props = {
|
||||
|
|
Loading…
Add table
Reference in a new issue