Skip to content

Commit 0be4926

Browse files
authored
fix: window reference on server (#234)
1 parent 8f2e64b commit 0be4926

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

.changeset/lazy-crabs-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@alauda/doom": patch
3+
---
4+
5+
fix: window reference on server

packages/doom/src/runtime/components/Masonry.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import type MasonryLayout from 'masonry-layout'
1+
import type { Options } from 'masonry-layout'
22
import { useLayoutEffect, useRef, type HTMLAttributes } from 'react'
33

44
export interface MasonryProps extends HTMLAttributes<HTMLDivElement> {
5-
options?: MasonryLayout.Options
5+
options?: Options
6+
}
7+
8+
let MasonryLayout: typeof import('masonry-layout')
9+
10+
if (typeof window !== 'undefined') {
11+
MasonryLayout = (await import('masonry-layout')).default
612
}
713

814
export const Masonry = ({ options, ...props }: MasonryProps) => {
@@ -13,11 +19,7 @@ export const Masonry = ({ options, ...props }: MasonryProps) => {
1319
return
1420
}
1521

16-
// eslint-disable-next-line @typescript-eslint/no-require-imports
17-
const masonry = new (require('masonry-layout') as typeof MasonryLayout)(
18-
ref.current,
19-
options,
20-
)
22+
const masonry = new MasonryLayout!(ref.current, options)
2123

2224
return () => {
2325
masonry.destroy?.()

packages/doom/src/runtime/components/Overview.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const getChildLink = (
5151
}
5252

5353
const masonryOptions = {
54-
itemSelector: `.${classes.overviewGroup}`,
54+
itemSelector: `.masonry-item`,
5555
gutter: 16,
5656
}
5757

@@ -232,7 +232,10 @@ export function Overview(props: {
232232

233233
<Masonry className={classes.overviewGroups} options={masonryOptions}>
234234
{group.items.map((item) => (
235-
<div className={classes.overviewGroup} key={item.link}>
235+
<div
236+
className={`${classes.overviewGroup} masonry-item`}
237+
key={item.link}
238+
>
236239
<h3>
237240
<Link href={item.link} {...renderInlineMarkdown(item.text)} />
238241
</h3>

0 commit comments

Comments
 (0)