Skip to content

Commit

Permalink
feat: useRendererProps hook
Browse files Browse the repository at this point in the history
Use props for specific renderers, from the values of
`renderersProps` record.
For example, `useRendererProps('p')`.
  • Loading branch information
jsamr committed Jun 4, 2021
1 parent 512a7cc commit 87982c4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
11 changes: 10 additions & 1 deletion packages/render-html/src/context/SharedPropsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const defaultSharedPropsContext: Required<RenderHTMLPassedProps> = {
listsPrefixesRenderers: {},
onLinkPress: (_e, href) => Linking.canOpenURL(href) && Linking.openURL(href),
WebView: () => null,
defaultWebViewProps: {}
defaultWebViewProps: {},
renderersProps: {}
};

const SharedPropsContext = React.createContext<Required<RenderHTMLPassedProps>>(
Expand All @@ -30,6 +31,14 @@ export function useSharedProps() {
return React.useContext(SharedPropsContext);
}

export function useRendererProps<
R extends Record<string, any> = Record<string, any>
>(k: keyof R) {
return React.useContext(SharedPropsContext).renderersProps[
k as string
] as R[typeof k];
}

export function useDefaultTextProps(): TextProps {
return useSharedProps().defaultTextProps;
}
Expand Down
1 change: 1 addition & 0 deletions packages/render-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { default as extendDefaultRenderer } from './render/extendDefaultRenderer
export { default as splitBoxModelStyle } from './helpers/splitBoxModelStyle';
export {
useSharedProps,
useRendererProps,
useComputeMaxWidthForTag
} from './context/SharedPropsContext';

Expand Down
8 changes: 4 additions & 4 deletions packages/render-html/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export interface RenderHTMLPassedProps<P = any> {
href: string,
htmlAttribs: HtmlAttributesDictionary
) => void;
/**
* Props to use in custom renderers with `useRendererProps` or `useSharedProps`.
*/
renderersProps?: Record<string, any>;
/**
* Default props for Text elements in the render tree.
*
Expand Down Expand Up @@ -235,10 +239,6 @@ export interface RenderHTMLProps<P = any>
* Your custom renderers.
*/
renderers?: CustomTagRendererRecord;
/**
* Set of props accessible into your custom renderers in `passProps` (4th argument)
*/
renderersProps?: any;
/**
* Custom style for the default container of the rendered HTML.
*/
Expand Down

0 comments on commit 87982c4

Please sign in to comment.