mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
70c6c9df42
* Start * Try webframe * Lint and format * Move to package * Continue * Implement card and cleanup contexts * Start code blocks * Start rendering image * Continue with card * Start stack * Start text * Start modal * Format * Continue * Fix TS errors * Cleanup * Make markdown work with server components * Format * Add visual test * Lint and format * Fix lifecycle * Implement textinput * Divider * Remove console logs * Remove listSpaceIntegrationsBlocks
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { ContentKitAction, RequestRenderIntegrationUI } from '@gitbook/api';
|
|
import * as React from 'react';
|
|
|
|
/**
|
|
* Security restrictions for elements.
|
|
*/
|
|
export interface ContentKitSecurity {
|
|
/**
|
|
* Domains allowed for webframe communication.
|
|
* These domains will be allowed to send messages to ContentKit,
|
|
* and receive messages from ContentKit.
|
|
*/
|
|
firstPartyDomains: string[];
|
|
}
|
|
|
|
export type ContentKitRenderUpdate = Partial<
|
|
Pick<RequestRenderIntegrationUI, 'action' | 'props' | 'state'>
|
|
>;
|
|
|
|
export interface ContentKitClientContextType {
|
|
security: ContentKitSecurity;
|
|
|
|
/**
|
|
* Current value of the state.
|
|
*/
|
|
state: object;
|
|
|
|
/**
|
|
* Update the state.
|
|
*/
|
|
setState: React.Dispatch<React.SetStateAction<object>>;
|
|
|
|
/**
|
|
* Re-render the ContentKit component with a new set of props/state or an action.
|
|
*/
|
|
update: (newState: ContentKitRenderUpdate) => Promise<void>;
|
|
|
|
/**
|
|
* Dispatch an action.
|
|
*/
|
|
dispatchAction: (action: ContentKitAction, bubble?: boolean) => Promise<void>;
|
|
}
|
|
|
|
export const ContentKitClientContext = React.createContext<ContentKitClientContextType | null>(
|
|
null,
|
|
);
|
|
|
|
/**
|
|
* Get the current contentkit context.
|
|
*/
|
|
export function useContentKitClientContext(): ContentKitClientContextType {
|
|
const context = React.useContext(ContentKitClientContext);
|
|
if (!context) {
|
|
throw new Error('ContentKit component should be wrapped in <ContentKit>');
|
|
}
|
|
return context;
|
|
}
|