5 changed files with 97 additions and 9 deletions
@ -0,0 +1,17 @@ |
|||||||
|
import { describe, expect, it } from 'vitest' |
||||||
|
import { libraryPublicationGridColumnClass } from '@/hooks/usePanelMode' |
||||||
|
|
||||||
|
describe('libraryPublicationGridColumnClass', () => { |
||||||
|
it('uses 1 column on mobile', () => { |
||||||
|
expect(libraryPublicationGridColumnClass(true, 'single')).toBe('grid-cols-1') |
||||||
|
expect(libraryPublicationGridColumnClass(true, 'double')).toBe('grid-cols-1') |
||||||
|
}) |
||||||
|
|
||||||
|
it('uses 2 columns in double-pane desktop', () => { |
||||||
|
expect(libraryPublicationGridColumnClass(false, 'double')).toBe('grid-cols-2') |
||||||
|
}) |
||||||
|
|
||||||
|
it('uses 3 columns in single-pane desktop', () => { |
||||||
|
expect(libraryPublicationGridColumnClass(false, 'single')).toBe('grid-cols-3') |
||||||
|
}) |
||||||
|
}) |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
import storage from '@/services/local-storage.service' |
||||||
|
import { useEffect, useState } from 'react' |
||||||
|
|
||||||
|
export function usePanelMode(): 'single' | 'double' { |
||||||
|
const [panelMode, setPanelMode] = useState<'single' | 'double'>(() => storage.getPanelMode()) |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
const onPanelMode = (ev: Event) => { |
||||||
|
const mode = (ev as CustomEvent<{ mode: 'single' | 'double' }>).detail?.mode |
||||||
|
if (mode === 'single' || mode === 'double') setPanelMode(mode) |
||||||
|
} |
||||||
|
window.addEventListener('panelModeChanged', onPanelMode) |
||||||
|
return () => window.removeEventListener('panelModeChanged', onPanelMode) |
||||||
|
}, []) |
||||||
|
|
||||||
|
return panelMode |
||||||
|
} |
||||||
|
|
||||||
|
export function libraryPublicationGridColumnClass( |
||||||
|
isSmallScreen: boolean, |
||||||
|
panelMode: 'single' | 'double' |
||||||
|
): string { |
||||||
|
if (isSmallScreen) return 'grid-cols-1' |
||||||
|
if (panelMode === 'double') return 'grid-cols-2' |
||||||
|
return 'grid-cols-3' |
||||||
|
} |
||||||
Loading…
Reference in new issue