Skip to content

Commit

Permalink
refactor(mobile): impl all-docs masonry with css grid, close AF-1598
Browse files Browse the repository at this point in the history
  • Loading branch information
CatsJuice committed Nov 7, 2024
1 parent 0147bbb commit bff5508
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
36 changes: 12 additions & 24 deletions packages/frontend/core/src/mobile/views/all-docs/doc/masonry.css.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import { style } from '@vanilla-extract/css';

export const invisibleWrapper = style({
position: 'absolute',
padding: 'inherit',
width: '100%',
height: 0,
overflow: 'hidden',
visibility: 'hidden',
pointerEvents: 'none',
});
export const invisibleList = style({
width: `calc(50% - 17px / 2)`,
});
export const stacks = style({
position: 'relative',
width: '100%',
display: 'flex',
gap: 17,
padding: 16,
export const paddingX = 16;
export const columnGap = 17;

export const masonry = style({
padding: `16px ${paddingX}px`,
columnGap: columnGap,
});
export const stack = style({
width: 0,
flex: 1,
display: 'flex',
flexDirection: 'column',
gap: 10,
export const masonryItem = style({
display: 'grid',
gridTemplateRows: '1fr auto',
breakInside: 'avoid',
marginBottom: 10,
});
export const space = style({ width: '100%', height: 16 });
42 changes: 24 additions & 18 deletions packages/frontend/core/src/mobile/views/all-docs/doc/masonry.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import { useGlobalEvent } from '@affine/core/mobile/hooks/use-global-events';
import type { DocMeta } from '@blocksuite/affine/store';
import { useMemo } from 'react';
import { useCallback, useEffect, useState } from 'react';

import { DocCard } from '../../../components';
import * as styles from './masonry.css';

// TODO(@CatsJuice): Large amount docs performance
export const MasonryDocs = ({
items,
showTags,
}: {
items: DocMeta[];
showTags?: boolean;
}) => {
// card preview is loaded lazily, it's meaningless to calculate height
const stacks = useMemo(() => {
return items.reduce(
(acc, item, i) => {
acc[i % 2].push(item);
return acc;
},
[[], []] as [DocMeta[], DocMeta[]]
const [columnCount, setColumnCount] = useState(2);

const updateColumnCount = useCallback(() => {
const maxCardWidth = 220;
const windowWidth = window.innerWidth;
const newColumnCount = Math.floor(
(windowWidth - styles.paddingX * 2 - styles.columnGap) / maxCardWidth
);
}, [items]);
setColumnCount(Math.max(newColumnCount, 2));
}, []);

useGlobalEvent('resize', updateColumnCount);
useEffect(() => {
updateColumnCount();
}, [updateColumnCount]);

return (
<div className={styles.stacks}>
{stacks.map((stack, i) => (
<ul key={i} className={styles.stack}>
{stack.map(item => (
<DocCard showTags={showTags} key={item.id} meta={item} />
))}
</ul>
<div className={styles.masonry} style={{ columnCount }}>
{items.map(item => (
<DocCard
key={item.id}
showTags={showTags}
meta={item}
className={styles.masonryItem}
/>
))}
</div>
);
Expand Down

0 comments on commit bff5508

Please sign in to comment.