Skip to content

Commit b81799e

Browse files
committed
Change context to an object
1 parent d9db2e4 commit b81799e

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

dotcom-rendering/src/components/Island.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useContext } from 'react';
22
import type { ScheduleOptions, SchedulePriority } from '../lib/scheduler';
3-
import { IslandDepthContext } from './IslandDepthContext';
3+
import { IslandContext, IslandProvider } from './IslandContext';
44

55
type DeferredProps = {
66
visible: {
@@ -58,16 +58,17 @@ export const Island = ({ priority, defer, children, role }: IslandProps) => {
5858
const name = String(children.type.name);
5959

6060
/**
61-
* Current depth of island if nested within another island. This is used so
62-
* that we only hydrate top-level islands which are then responsible for
63-
* hydrating the entire subtree and any child islands.
61+
* Context tracks whether current island is a child of another
6462
*/
65-
const islandDepth = useContext(IslandDepthContext);
63+
const island = useContext(IslandContext);
6664

6765
return (
68-
<IslandDepthContext.Provider value={islandDepth + 1}>
66+
<IslandProvider value={{ child: true }}>
67+
<code style={{ fontFamily: 'monospace', color: 'hotpink' }}>
68+
{name} = {island.child ? 'child' : 'parent'}
69+
</code>
6970
{/* Child islands defer to nearest parent island for hydration */}
70-
{islandDepth > 0 ? (
71+
{island.child ? (
7172
children
7273
) : (
7374
<gu-island
@@ -81,7 +82,7 @@ export const Island = ({ priority, defer, children, role }: IslandProps) => {
8182
{children}
8283
</gu-island>
8384
)}
84-
</IslandDepthContext.Provider>
85+
</IslandProvider>
8586
);
8687
};
8788

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createContext } from 'react';
2+
3+
type Config = {
4+
child: boolean;
5+
};
6+
7+
/**
8+
* Context to track whether the current island is a child of another island.
9+
* Child islands defer to the nearest parent island for hydration.
10+
*/
11+
export const IslandContext = createContext<Config>({ child: false });
12+
13+
export const IslandProvider = ({
14+
value,
15+
children,
16+
}: {
17+
value: Config;
18+
children: React.ReactNode;
19+
}) => <IslandContext.Provider value={value}>{children}</IslandContext.Provider>;

dotcom-rendering/src/components/IslandDepthContext.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)