Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Add the new structure for the mobile layout #24227

Merged
merged 17 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions code/ui/manager/src/components/hooks/useMedia.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { useState, useEffect } from 'react';
import { useEffect, useState } from 'react';

const useMediaQuery = (query: string) => {
const [matches, setMatches] = useState(false);
export function useMediaQuery(query: string): boolean {
JReinhold marked this conversation as resolved.
Show resolved Hide resolved
const getMatches = (queryMatch: string): boolean => {
// Prevents SSR issues
if (typeof window !== 'undefined') {
return window.matchMedia(queryMatch).matches;
}
return false;
};

const [matches, setMatches] = useState<boolean>(getMatches(query));

function handleChange() {
setMatches(getMatches(query));
}

useEffect(() => {
const media = window.matchMedia(query);
if (media.matches !== matches) {
setMatches(media.matches);
}
const listener = () => setMatches(media.matches);
window.addEventListener('resize', listener);
return () => window.removeEventListener('resize', listener);
}, [matches, query]);
const matchMedia = window.matchMedia(query);

return matches;
};
// Triggered at the first client-side load and if query changes
handleChange();

// Listen matchMedia
matchMedia.addEventListener('change', handleChange);

export default useMediaQuery;
return () => {
matchMedia.removeEventListener('change', handleChange);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [query]);

return matches;
}
8 changes: 1 addition & 7 deletions code/ui/manager/src/components/layout/Layout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ const meta = {
theme: 'light',
layout: 'fullscreen',
},
decorators: [
(Story) => (
<div style={{ height: '100vh', width: '100vw' }}>
<Story />
</div>
),
],
decorators: [(Story) => <Story />],
cdedreuille marked this conversation as resolved.
Show resolved Hide resolved
render: (args) => {
const [managerLayoutState, setManagerLayoutState] = useState(args.managerLayoutState);

Expand Down
100 changes: 65 additions & 35 deletions code/ui/manager/src/components/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import React, { useEffect, useLayoutEffect, useState } from 'react';
import { styled } from '@storybook/theming';
import type { API_Layout, API_ViewMode } from '@storybook/types';
import { useDragging } from './useDragging';
import { useMediaQuery } from '../hooks/useMedia';
import { MobileNavigation } from '../mobile-navigation/MobileNavigation';
import { breakpointMin600 } from '../../constants';

interface InternalLayoutState {
isDragging: boolean;
Expand Down Expand Up @@ -110,6 +113,9 @@ const useLayoutSyncingState = (
};

export const Layout = ({ managerLayoutState, setManagerLayoutState, ...slots }: Props) => {
const isDesktop = useMediaQuery(breakpointMin600);
const isMobile = !isDesktop;

const {
navSize,
rightPanelWidth,
Expand All @@ -130,51 +136,70 @@ export const Layout = ({ managerLayoutState, setManagerLayoutState, ...slots }:
panelPosition={managerLayoutState.panelPosition}
isDragging={isDragging}
viewMode={managerLayoutState.viewMode}
breakpoint={breakpointMin600}
>
{showPages && <PagesContainer>{slots.slotPages}</PagesContainer>}
<ContentContainer>{slots.slotMain}</ContentContainer>
<SidebarContainer>
<Drag ref={sidebarResizerRef} />
{slots.slotSidebar}
</SidebarContainer>
{showPanel && (
<PanelContainer position={panelPosition}>
<Drag
orientation={panelPosition === 'bottom' ? 'horizontal' : 'vertical'}
position={panelPosition === 'bottom' ? 'left' : 'right'}
ref={panelResizerRef}
/>
{slots.slotPanel}
</PanelContainer>
<ContentContainer breakpoint={breakpointMin600}>{slots.slotMain}</ContentContainer>
{isDesktop && (
<>
<SidebarContainer>
<Drag ref={sidebarResizerRef} />
{slots.slotSidebar}
</SidebarContainer>
{showPanel && (
<PanelContainer position={panelPosition}>
<Drag
orientation={panelPosition === 'bottom' ? 'horizontal' : 'vertical'}
position={panelPosition === 'bottom' ? 'left' : 'right'}
ref={panelResizerRef}
/>
{slots.slotPanel}
</PanelContainer>
)}
</>
)}
{isMobile && <MobileNavigation />}
</LayoutContainer>
);
};

const LayoutContainer = styled.div<LayoutState>(
({ navSize, rightPanelWidth, bottomPanelHeight, viewMode, panelPosition, isDragging }) => {
const LayoutContainer = styled.div<LayoutState & { breakpoint: string }>(
({
navSize,
rightPanelWidth,
bottomPanelHeight,
viewMode,
panelPosition,
isDragging,
breakpoint,
JReinhold marked this conversation as resolved.
Show resolved Hide resolved
}) => {
return {
width: '100%',
height: '100vh',
display: 'grid',
height: '100svh',
JReinhold marked this conversation as resolved.
Show resolved Hide resolved
overflow: 'hidden',
gap: 0,
transition: isDragging ? null : 'all 0.2s ease-in-out', // transition when toggling panels, but not when dragging
gridTemplateColumns: `minmax(0, ${navSize}px) minmax(${MINIMUM_CONTENT_WIDTH_PX}px, 1fr) minmax(0, ${rightPanelWidth}px)`,
gridTemplateRows: `1fr ${bottomPanelHeight}px`,
gridTemplateAreas: (() => {
if (viewMode === 'docs') {
// remove panel in docs viewMode
return `"sidebar content content"
display: 'flex',
flexDirection: 'column',

[`@media ${breakpoint}`]: {
display: 'grid',
gap: 0,
transition: isDragging ? null : 'all 0.2s ease-in-out', // transition when toggling panels, but not when dragging
gridTemplateColumns: `minmax(0, ${navSize}px) minmax(${MINIMUM_CONTENT_WIDTH_PX}px, 1fr) minmax(0, ${rightPanelWidth}px)`,
gridTemplateRows: `1fr ${bottomPanelHeight}px`,
gridTemplateAreas: (() => {
if (viewMode === 'docs') {
// remove panel in docs viewMode
return `"sidebar content content"
"sidebar content content"`;
}
if (panelPosition === 'right') {
return `"sidebar content panel"
}
if (panelPosition === 'right') {
return `"sidebar content panel"
"sidebar content panel"`;
}
return `"sidebar content content"
}
return `"sidebar content content"
"sidebar panel panel"`;
})(),
})(),
},
};
}
);
Expand All @@ -186,11 +211,16 @@ const SidebarContainer = styled.div(({ theme }) => ({
borderRight: `1px solid ${theme.color.border}`,
}));

const ContentContainer = styled.div(({ theme }) => ({
display: 'grid',
const ContentContainer = styled.div<{ breakpoint: string }>(({ theme, breakpoint }) => ({
JReinhold marked this conversation as resolved.
Show resolved Hide resolved
flex: 1,
position: 'relative',
backgroundColor: theme.background.content,
gridArea: 'content',
display: 'grid', // This is needed to make the content container fill the available space

[`@media ${breakpoint}`]: {
flex: 'auto',
gridArea: 'content',
},
}));

const PagesContainer = styled.div(({ theme }) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { FC } from 'react';
import React from 'react';
import { styled } from '@storybook/theming';

const Container = styled.div(({ theme }) => ({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
bottom: 0,
left: 0,
width: '100%',
height: 40,
zIndex: 10,
background: theme.background.content,
padding: '0 6px',
borderTop: `1px solid ${theme.appBorderColor}`,
}));

export const MobileNavigation: FC = () => {
return <Container />;
};
2 changes: 1 addition & 1 deletion code/ui/manager/src/components/panel/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Tabs, Icons, IconButton } from '@storybook/components';
import type { State } from '@storybook/manager-api';
import { shortcutToHumanString } from '@storybook/manager-api';
import type { Addon_BaseType } from '@storybook/types';
import useMediaQuery from '../hooks/useMedia';
import { useMediaQuery } from '../hooks/useMedia';

export interface SafeTabProps {
title: Addon_BaseType['title'];
Expand Down
1 change: 1 addition & 0 deletions code/ui/manager/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const breakpointMin600 = '(min-width: 600px)';
JReinhold marked this conversation as resolved.
Show resolved Hide resolved