Skip to content

Commit

Permalink
feat: access sharedProps from custom renderers
Browse files Browse the repository at this point in the history
You don't need to call `useSharedProps` in custom renderers anymore.
  • Loading branch information
jsamr committed Jun 4, 2021
1 parent 63caa4d commit 0d2199e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 21 deletions.
3 changes: 2 additions & 1 deletion packages/render-html/src/RenderHTMLFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const renderHtmlFragmentPropTypes: RenderHTMLFragmentPropTypes = {
onTTreeChange: PropTypes.func,
onHTMLLoaded: PropTypes.func,
WebView: PropTypes.any,
defaultWebViewProps: PropTypes.object
defaultWebViewProps: PropTypes.object,
setMarkersForTNode: PropTypes.func
};

export const renderHTMLFragmentDefaultProps: {
Expand Down
4 changes: 2 additions & 2 deletions packages/render-html/src/TBlockRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { View } from 'react-native';
import { TBlock } from '@native-html/transient-render-engine';
import { useTNodeChildrenRenderer } from './context/TChildrenRendererContext';
import { TDefaultRenderer, TNodeRendererProps } from './shared-types';
import { TDefaultRenderer, TNodeSubRendererProps } from './shared-types';
import GenericPressable from './GenericPressable';
import useAssembledCommonProps from './hooks/useAssembledCommonProps';

Expand Down Expand Up @@ -40,7 +40,7 @@ export const TDefaultBlockRenderer: TDefaultRenderer<TBlock> = ({
return React.createElement(View, commonProps, children);
};

const TBlockRenderer = (props: TNodeRendererProps<TBlock>) => {
const TBlockRenderer = (props: TNodeSubRendererProps<TBlock>) => {
const { assembledProps, Renderer } = useAssembledCommonProps(
props,
TDefaultBlockRenderer
Expand Down
8 changes: 6 additions & 2 deletions packages/render-html/src/TNodeRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ const TNodeRenderer = function TNodeRenderer(
props: Omit<TNodeRendererProps<any>, 'markers'> & { parentMarkers: Markers }
) {
const { tnode } = props;
const { setMarkersForTNode } = useSharedProps();
const sharedProps = useSharedProps();
const markers = getMarkersFromTNode(tnode, props.parentMarkers);
const customMarkers = setMarkersForTNode(tnode, props.parentMarkers);
const customMarkers = sharedProps.setMarkersForTNode(
tnode,
props.parentMarkers
);
const resolvedMarkers =
markers && !customMarkers
? markers
Expand All @@ -31,6 +34,7 @@ const TNodeRenderer = function TNodeRenderer(
: null;
const tnodeProps = {
...props,
sharedProps,
markers: resolvedMarkers || props.parentMarkers
};
if (tnode instanceof TBlock) {
Expand Down
4 changes: 2 additions & 2 deletions packages/render-html/src/TPhrasingRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Text } from 'react-native';
import { TPhrasing } from '@native-html/transient-render-engine';
import { useTNodeChildrenRenderer } from './context/TChildrenRendererContext';
import { TDefaultRenderer, TNodeRendererProps } from './shared-types';
import { TDefaultRenderer, TNodeSubRendererProps } from './shared-types';
import useAssembledCommonProps from './hooks/useAssembledCommonProps';

export const TDefaultPhrasingRenderer: TDefaultRenderer<TPhrasing> = ({
Expand Down Expand Up @@ -37,7 +37,7 @@ export const TDefaultPhrasingRenderer: TDefaultRenderer<TPhrasing> = ({
);
};

const TPhrasingRenderer = (props: TNodeRendererProps<TPhrasing>) => {
const TPhrasingRenderer = (props: TNodeSubRendererProps<TPhrasing>) => {
const { assembledProps, Renderer } = useAssembledCommonProps(
props,
TDefaultPhrasingRenderer
Expand Down
6 changes: 3 additions & 3 deletions packages/render-html/src/TTextRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TText } from '@native-html/transient-render-engine';
import {
TDefaultRenderer,
TDefaultRendererProps,
TNodeRendererProps
TNodeSubRendererProps
} from './shared-types';
import { useInternalTextRenderer } from './context/RenderRegistryProvider';
import useAssembledCommonProps from './hooks/useAssembledCommonProps';
Expand All @@ -28,15 +28,15 @@ export const TDefaultTextRenderer: TDefaultRenderer<TText> = ({
);
};

function TStandardTextRenderer(props: TNodeRendererProps<TText>) {
function TStandardTextRenderer(props: TNodeSubRendererProps<TText>) {
const { assembledProps, Renderer } = useAssembledCommonProps(
props,
TDefaultTextRenderer
);
return React.createElement(Renderer, assembledProps);
}

export default function TTextRenderer(props: TNodeRendererProps<TText>) {
export default function TTextRenderer(props: TNodeSubRendererProps<TText>) {
const InternalTextRenderer = useInternalTextRenderer(props.tnode);
if (InternalTextRenderer) {
return React.createElement(InternalTextRenderer, { key: props.key });
Expand Down
11 changes: 9 additions & 2 deletions packages/render-html/src/hooks/useAssembledCommonProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
NativeStyleProp,
TDefaultRenderer,
TDefaultRendererProps,
TNodeRendererProps
TNodeSubRendererProps
} from '../shared-types';
import mergeCollapsedMargins from '../helpers/mergeCollapsedMargins';
import { useRendererConfig } from '../context/RenderRegistryProvider';
Expand All @@ -35,7 +35,13 @@ function getStylesForTnode<T extends TNode>(tnode: T): NativeStyleProp<T> {
}

export default function useAssembledCommonProps<T extends TNode>(
{ tnode, key, propsFromParent, markers }: TNodeRendererProps<T>,
{
tnode,
key,
propsFromParent,
markers,
sharedProps
}: TNodeSubRendererProps<T>,
TDefault: TDefaultRenderer<T>
): {
assembledProps: CustomTagRendererProps<T> & TDefaultRendererProps<T>;
Expand All @@ -48,6 +54,7 @@ export default function useAssembledCommonProps<T extends TNode>(
tnode,
markers,
propsFromParent,
sharedProps,
TDefaultRenderer: TDefault,
style: mergeCollapsedMargins(
propsFromParent.collapsedMarginTop,
Expand Down
7 changes: 4 additions & 3 deletions packages/render-html/src/renderers/ARenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
defaultHTMLElementModels,
DocumentContext
} from '@native-html/transient-render-engine';
import { useSharedProps } from '../context/SharedPropsContext';
import {
DefaultTagRendererProps,
RenderHTMLSharedProps
Expand Down Expand Up @@ -44,8 +43,10 @@ function useAnchorOnLinkPress(
export function useAElementProps<T extends TNode>(
props: DefaultTagRendererProps<T>
): DefaultTagRendererProps<T> {
const { tnode } = props;
const { onLinkPress } = useSharedProps();
const {
tnode,
sharedProps: { onLinkPress }
} = props;
const syntheticAnchorOnLinkPress = useAnchorOnLinkPress(tnode, onLinkPress);
if (typeof syntheticAnchorOnLinkPress !== 'function') {
return props;
Expand Down
9 changes: 3 additions & 6 deletions packages/render-html/src/renderers/IMGRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import React, { ClassAttributes } from 'react';
import { TBlock } from '@native-html/transient-render-engine';
import IMGElement, { IMGElementProps } from '../elements/IMGElement';
import { DefaultBlockRenderer } from '../render/render-types';
import {
useComputeMaxWidthForTag,
useSharedProps
} from '../context/SharedPropsContext';
import { useComputeMaxWidthForTag } from '../context/SharedPropsContext';
import { ImageStyle } from 'react-native';
import { defaultHTMLElementModels } from '@native-html/transient-render-engine';
import { DefaultTagRendererProps } from '../shared-types';
Expand All @@ -14,12 +11,12 @@ import useNormalizedUrl from '../hooks/useNormalizedUrl';
export function useIMGElementProps(
props: DefaultTagRendererProps<TBlock>
): IMGElementProps & ClassAttributes<any> {
const { style, tnode, onPress, key } = props;
const { style, tnode, onPress, key, sharedProps } = props;
const {
contentWidth,
enableExperimentalPercentWidth,
imagesInitialDimensions
} = useSharedProps();
} = sharedProps;
const computeImagesMaxWidth = useComputeMaxWidthForTag('img');
const src = tnode.attributes.src || '';
return {
Expand Down
12 changes: 12 additions & 0 deletions packages/render-html/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,14 @@ export interface TNodeRendererProps<T extends TNode> {
propsFromParent: PropsFromParent;
}

export interface TNodeSubRendererProps<T extends TNode>
extends TNodeRendererProps<T> {
/**
* Props shared across the whole render tree.
*/
sharedProps: Required<RenderHTMLSharedProps<any>>;
}

export interface TRendererBaseProps<T extends TNode>
extends TNodeRendererProps<T> {
/**
Expand Down Expand Up @@ -544,6 +552,10 @@ export interface DefaultTagRendererProps<T extends TNode>
* Styles extracted from tnode.style
*/
style: NativeStyleProp<T>;
/**
* Props shared across the whole render tree.
*/
sharedProps: Required<RenderHTMLSharedProps<any>>;
/**
* Default renderer for this tnode.
*/
Expand Down

0 comments on commit 0d2199e

Please sign in to comment.