-
Notifications
You must be signed in to change notification settings - Fork 22
/
SurfaceRootContext.tsx
44 lines (33 loc) · 1.24 KB
/
SurfaceRootContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// Copyright 2023 DXOS.org
//
import { createContext, useContext, type Context, type Provider, type ForwardedRef } from 'react';
import { type SurfaceProps } from './Surface';
// TODO(wittjosiah): Factor out.
type WithRequiredProperty<Type, Key extends keyof Type> = Type & {
[Property in Key]-?: Type[Property];
};
type SurfaceComponentProps = WithRequiredProperty<SurfaceProps, 'data'>;
/**
* Determines the priority of the surface when multiple components are resolved.
*/
export type SurfaceDisposition = 'hoist' | 'fallback';
export type SurfaceResult = {
node: JSX.Element;
disposition?: SurfaceDisposition;
};
/**
* Function which resolves a Surface.
*
* If a null value is returned, the rendering is deferred to other plugins.
*/
export type SurfaceComponent = (
props: SurfaceComponentProps,
forwardedRef: ForwardedRef<HTMLElement>,
) => JSX.Element | SurfaceResult | null;
export type SurfaceRootContext = {
components: Record<string, SurfaceComponent>;
};
const SurfaceRootContext: Context<SurfaceRootContext> = createContext<SurfaceRootContext>({ components: {} });
export const useSurfaceRoot = () => useContext(SurfaceRootContext);
export const SurfaceProvider: Provider<SurfaceRootContext> = SurfaceRootContext.Provider;