From 45436d28d2a103e078ae6eb08534b772b6840587 Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Thu, 14 Sep 2023 01:04:07 +0200 Subject: [PATCH] Add typings for React render callback children (#8109) --- modules/react/src/deckgl.ts | 4 +-- modules/react/src/utils/extract-jsx-layers.ts | 35 +++++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/modules/react/src/deckgl.ts b/modules/react/src/deckgl.ts index 1e03a06661e..4419c546856 100644 --- a/modules/react/src/deckgl.ts +++ b/modules/react/src/deckgl.ts @@ -30,7 +30,7 @@ import { import {Deck} from '@deck.gl/core'; import useIsomorphicLayoutEffect from './utils/use-isomorphic-layout-effect'; -import extractJSXLayers from './utils/extract-jsx-layers'; +import extractJSXLayers, {DeckGLRenderCallback} from './utils/extract-jsx-layers'; import positionChildrenUnderViews from './utils/position-children-under-views'; import extractStyles from './utils/extract-styles'; @@ -58,7 +58,7 @@ export type DeckGLProps = Omit< Deck?: typeof Deck; width?: string | number; height?: string | number; - children?: React.ReactNode; + children?: React.ReactNode | DeckGLRenderCallback; ContextProvider?: React.Context['Provider']; }; diff --git a/modules/react/src/utils/extract-jsx-layers.ts b/modules/react/src/utils/extract-jsx-layers.ts index d23ea271a12..0fe462ddd23 100644 --- a/modules/react/src/utils/extract-jsx-layers.ts +++ b/modules/react/src/utils/extract-jsx-layers.ts @@ -3,10 +3,39 @@ import {createElement} from 'react'; import {inheritsFrom} from './inherits-from'; import {Layer, View} from '@deck.gl/core'; import {isComponent} from './evaluate-children'; -import type {LayersList} from '@deck.gl/core'; +import type {LayersList, Viewport} from '@deck.gl/core'; + +export type DeckGLRenderCallbackArgs = { + /** + * the left offset of the current view, in pixels + */ + x: number; + /** + * the top offset of the current view, in pixels + */ + y: number; + /** + * the width of the current view, in pixels + */ + width: number; + /** + * the height of the current view, in pixels + */ + height: number; + /** + * the view state of the current view + */ + viewState: any; + /** + * the `Viewport` instance of the current view + */ + viewport: Viewport; +}; + +export type DeckGLRenderCallback = (args: DeckGLRenderCallbackArgs) => React.ReactNode; // recursively wrap render callbacks in `View` -function wrapInView(node: React.ReactNode): React.ReactNode { +function wrapInView(node: React.ReactNode | DeckGLRenderCallback): React.ReactNode { if (typeof node === 'function') { // React.Children does not traverse functions. // All render callbacks must be protected under a @@ -33,7 +62,7 @@ export default function extractJSXLayers({ layers = [], views = null }: { - children?: React.ReactNode; + children?: React.ReactNode | DeckGLRenderCallback; layers?: LayersList; views?: View | View[] | null; }): {