-
Notifications
You must be signed in to change notification settings - Fork 29
Persist sidebar width and other improvements #1987
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| import '@h5web/lib'; // eslint-disable-line import/no-duplicates -- make sure lib styles come first in CSS bundle | ||
|
|
||
| import { KeepZoomProvider } from '@h5web/lib'; // eslint-disable-line import/no-duplicates | ||
| import { useToggle } from '@react-hookz/web'; | ||
| import { Suspense, useState } from 'react'; | ||
| import { ErrorBoundary } from 'react-error-boundary'; | ||
| import { ReflexContainer, ReflexElement, ReflexSplitter } from 'react-reflex'; | ||
| import { | ||
| Group, | ||
| Panel, | ||
| Separator, | ||
| useDefaultLayout, | ||
| usePanelRef, | ||
| } from 'react-resizable-panels'; | ||
|
|
||
| import styles from './App.module.css'; | ||
| import BreadcrumbsBar from './breadcrumbs/BreadcrumbsBar'; | ||
|
|
@@ -18,6 +23,10 @@ import Sidebar from './Sidebar'; | |
| import VisConfigProvider from './VisConfigProvider'; | ||
| import Visualizer from './visualizer/Visualizer'; | ||
|
|
||
| const SIDEBAR_ID = 'h5w-sidebar'; | ||
| const MAIN_AREA_ID = 'h5w-main-area'; | ||
| const RESIZE_TARGET_MIN_SIZE = { coarse: 6, fine: 6 }; // match CSS width (.splitter::before) | ||
|
|
||
| interface Props { | ||
| sidebarOpen?: boolean; | ||
| initialPath?: string; | ||
|
|
@@ -36,7 +45,6 @@ function App(props: Props) { | |
| } = props; | ||
|
|
||
| const [selectedPath, setSelectedPath] = useState<string>(initialPath); | ||
| const [isSidebarOpen, toggleSidebarOpen] = useToggle(initialSidebarOpen); | ||
| const [isInspecting, setInspecting] = useState(false); | ||
|
|
||
| const { valuesStore } = useDataContext(); | ||
|
|
@@ -45,6 +53,17 @@ function App(props: Props) { | |
| valuesStore.abortAll('entity changed', true); | ||
| } | ||
|
|
||
| const { defaultLayout, onLayoutChanged } = useDefaultLayout({ | ||
| id: 'h5web:layout', | ||
| }); | ||
|
|
||
| const sidebarPanelRef = usePanelRef(); | ||
| const [isSidebarOpen, setSidebarOpen] = useState( | ||
| initialSidebarOpen | ||
| ? !defaultLayout || defaultLayout[SIDEBAR_ID] > 0 | ||
| : false, | ||
| ); | ||
|
|
||
| return ( | ||
| <ErrorBoundary | ||
| FallbackComponent={ErrorFallback} | ||
|
|
@@ -54,32 +73,49 @@ function App(props: Props) { | |
| } | ||
| }} | ||
| > | ||
| <ReflexContainer | ||
| <Group | ||
| className={styles.root} | ||
| resizeTargetMinimumSize={RESIZE_TARGET_MIN_SIZE} | ||
| defaultLayout={initialSidebarOpen ? defaultLayout : undefined} | ||
| onLayoutChanged={onLayoutChanged} | ||
| data-fullscreen-root | ||
| data-allow-dark-mode={disableDarkMode ? undefined : ''} | ||
| orientation="vertical" | ||
| data-sidebar-collapsed={!isSidebarOpen || undefined} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to actually hide the content of the sidebar with CSS when the sidebar is collapsed. |
||
| > | ||
| <ReflexElement | ||
| <Panel | ||
| id={SIDEBAR_ID} | ||
| className={styles.sidebarArea} | ||
| style={{ display: isSidebarOpen ? undefined : 'none' }} | ||
| flex={25} | ||
| panelRef={sidebarPanelRef} | ||
| defaultSize={initialSidebarOpen ? '25%' : '0%'} | ||
| minSize={150} | ||
| collapsible | ||
| onResize={({ inPixels: size }) => { | ||
| const isNowOpen = size > 0; | ||
| if ( | ||
| (isNowOpen && !isSidebarOpen) || | ||
| (!isNowOpen && isSidebarOpen) | ||
| ) { | ||
| setSidebarOpen(isNowOpen); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition is to avoid unnecessary re-renders while resizing. |
||
| } | ||
| }} | ||
| > | ||
| <Sidebar selectedPath={selectedPath} onSelect={onSelectPath} /> | ||
| </ReflexElement> | ||
| </Panel> | ||
|
|
||
| <ReflexSplitter | ||
| className={styles.splitter} | ||
| style={{ display: isSidebarOpen ? undefined : 'none' }} | ||
| /> | ||
| <Separator className={styles.splitter} /> | ||
|
|
||
| <ReflexElement className={styles.mainArea} flex={75} minSize={500}> | ||
| <Panel id={MAIN_AREA_ID} className={styles.mainArea} minSize={500}> | ||
| <BreadcrumbsBar | ||
| path={selectedPath} | ||
| isSidebarOpen={isSidebarOpen} | ||
| isInspecting={isInspecting} | ||
| onToggleSidebar={toggleSidebarOpen} | ||
| onToggleSidebar={() => { | ||
| if (isSidebarOpen) { | ||
| sidebarPanelRef.current?.collapse(); | ||
| } else { | ||
| sidebarPanelRef.current?.expand(); | ||
| } | ||
| }} | ||
| onChangeInspecting={setInspecting} | ||
| onSelectPath={onSelectPath} | ||
| getFeedbackURL={getFeedbackURL} | ||
|
|
@@ -107,8 +143,8 @@ function App(props: Props) { | |
| </KeepZoomProvider> | ||
| </DimMappingProvider> | ||
| </VisConfigProvider> | ||
| </ReflexElement> | ||
| </ReflexContainer> | ||
| </Panel> | ||
| </Group> | ||
| </ErrorBoundary> | ||
| ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| .tabBar { | ||
| flex: none; | ||
| display: flex; | ||
| height: var(--toolbar-height); | ||
| font-size: 1.25rem; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,15 +13,15 @@ test('toggle sidebar', async () => { | |
|
|
||
| // Hide | ||
| await toggleBtn.click(); | ||
| await expect | ||
| .element(page.getByRole('treeitem', { name: 'source.h5' })) | ||
|
Comment on lines
+16
to
+17
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has to be async because of the local |
||
| .not.toBeInTheDocument(); | ||
| expect(toggleBtn).toHaveAttribute('aria-pressed', 'false'); | ||
| expect( | ||
| page.getByRole('treeitem', { name: 'source.h5' }), | ||
| ).not.toBeInTheDocument(); | ||
|
|
||
| // Show | ||
| await toggleBtn.click(); | ||
| await expect.element(getExplorerItem('source.h5')).toBeVisible(); | ||
| expect(toggleBtn).toHaveAttribute('aria-pressed', 'true'); | ||
| expect(getExplorerItem('source.h5')).toBeVisible(); | ||
| }); | ||
|
|
||
| test('switch between "display" and "inspect" modes', async () => { | ||
|
|
@@ -53,11 +53,13 @@ test('navigate with breadcrumbs', async () => { | |
| // Hide sidebar to show root crumb | ||
| const toggleBtn = page.getByRole('button', { name: 'Toggle sidebar' }); | ||
| await toggleBtn.click(); | ||
| expect( | ||
| page.getByRole('heading', { | ||
| name: 'source.h5 / entities / empty_dataset', | ||
| }), | ||
| ).toBeVisible(); | ||
| await expect | ||
| .element( | ||
| page.getByRole('heading', { | ||
| name: 'source.h5 / entities / empty_dataset', | ||
| }), | ||
| ) | ||
| .toBeVisible(); | ||
|
|
||
| // Select parent crumb | ||
| await page.getByRole('button', { name: 'entities' }).click(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| /* Global app styles for demo */ | ||
| @import 'react-reflex/styles.css'; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No more global styles 🎉 |
||
| @import '@h5web/lib/global-styles.css'; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ export default mergeConfig( | |
| setupFiles: 'src/setupTests.ts', | ||
| restoreMocks: true, | ||
| pool: 'threads', | ||
| testTimeout: 10_000, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's 15s by default, which I find to be quite long when debugging failing tests locally. Don't want to set it too low, though, even if our longest test lasts only about 4s. |
||
|
|
||
| browser: { | ||
| provider: playwright(), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hook provides local storage persistence.