-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experimental SSR component
- Loading branch information
1 parent
0ea7a26
commit fbd8819
Showing
11 changed files
with
160 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type React from "react"; | ||
import { cloneElement, isValidElement, useId, useState } from "react"; | ||
|
||
import { useContainerWidth } from "../hooks"; | ||
import { cssClass } from "../../core/utils"; | ||
import { CommonPhotoAlbumProps } from "../../types"; | ||
|
||
export type SSRProps = { | ||
/** Photo album layout breakpoints. */ | ||
breakpoints: number[]; | ||
/** Photo album instance, which must be the only child. */ | ||
children: React.ReactElement<Pick<CommonPhotoAlbumProps, "breakpoints" | "defaultContainerWidth">>; | ||
}; | ||
|
||
export default function SSR({ breakpoints, children }: SSRProps) { | ||
const uid = `ssr-${useId().replace(/:/g, "")}`; | ||
const { containerRef, containerWidth } = useContainerWidth(breakpoints); | ||
const [hydratedBreakpoint, setHydratedBreakpoint] = useState<number>(); | ||
|
||
if (!Array.isArray(breakpoints) || breakpoints.length === 0 || !isValidElement(children)) return null; | ||
|
||
if (containerWidth !== undefined && hydratedBreakpoint === undefined) { | ||
setHydratedBreakpoint(containerWidth); | ||
} | ||
|
||
const containerClass = cssClass(uid); | ||
const breakpointClass = (breakpoint: number) => cssClass(`${uid}-${breakpoint}`); | ||
|
||
const allBreakpoints = [Math.min(...breakpoints) / 2, ...breakpoints]; | ||
allBreakpoints.sort((a, b) => a - b); | ||
|
||
return ( | ||
<> | ||
{hydratedBreakpoint === undefined && ( | ||
<style> | ||
{[ | ||
`.${containerClass}{container-type:inline-size}`, | ||
`${allBreakpoints.map((breakpoint) => `.${breakpointClass(breakpoint)}`).join()}{display:none}`, | ||
...allBreakpoints.map( | ||
(breakpoint, index, array) => | ||
`@container(min-width:${index > 0 ? breakpoint : 0}px)${index < array.length - 1 ? ` and (max-width:${array[index + 1] - 1}px)` : ""}{.${breakpointClass(breakpoint)}{display:block}}`, | ||
), | ||
].join("\n")} | ||
</style> | ||
)} | ||
|
||
<div ref={containerRef} className={containerClass}> | ||
{allBreakpoints.map( | ||
(breakpoint) => | ||
(hydratedBreakpoint === undefined || hydratedBreakpoint === breakpoint) && ( | ||
<div key={breakpoint} className={breakpointClass(breakpoint)}> | ||
{cloneElement(children, { breakpoints, defaultContainerWidth: breakpoint })} | ||
</div> | ||
), | ||
)} | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./SSR"; | ||
export { default } from "./SSR"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./StaticPhotoAlbum"; | ||
export { default } from "./StaticPhotoAlbum"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { RowsPhotoAlbum, UnstableSSR as SSR } from "../src"; | ||
import { render } from "./test-utils"; | ||
import photos from "./photos"; | ||
|
||
describe("SSR", () => { | ||
it("works as expected", () => { | ||
const { getTracks, rerender } = render( | ||
<SSR breakpoints={[]}> | ||
<RowsPhotoAlbum photos={photos} /> | ||
</SSR>, | ||
); | ||
expect(getTracks().length).toBe(0); | ||
|
||
rerender( | ||
<SSR breakpoints={[300, 600, 900]}> | ||
<RowsPhotoAlbum photos={photos.slice(0, 1)} /> | ||
</SSR>, | ||
); | ||
expect(getTracks().length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters