Skip to content

Commit

Permalink
feat: configure margins removal for nested lists in renderersProps.ol|ul
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr committed Jun 4, 2021
1 parent e0fe7c6 commit 316e706
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
4 changes: 3 additions & 1 deletion packages/render-html/src/context/defaultRendererProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const defaultRendererProps: Required<RenderersPropsBase> = {
},
a: {
onPress: (_e, href) => Linking.canOpenURL(href) && Linking.openURL(href)
}
},
ol: {},
ul: {}
};

export default defaultRendererProps;
19 changes: 4 additions & 15 deletions packages/render-html/src/elements/ListElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import type {
DefaultSupportedListStyleType,
DefaultTagRendererProps,
ListElementConfig,
ListStyleSpec,
TChildProps,
UnitaryListStyleSpec
Expand All @@ -19,7 +20,8 @@ import { DEFAULT_TEXT_COLOR } from '../constants';
import pick from 'ramda/src/pick';

export interface ListElementProps<T extends 'ol' | 'ul'>
extends DefaultTagRendererProps<TBlock> {
extends DefaultTagRendererProps<TBlock>,
ListElementConfig {
listType: T;
/**
* Get default list-style-type given the number of nest level for this list.
Expand All @@ -29,20 +31,7 @@ export interface ListElementProps<T extends 'ol' | 'ul'>
getFallbackListStyleTypeFromNestLevel: (
nestLevel: number
) => DefaultSupportedListStyleType;
/**
* Remove top margin if this element parent is an `li` element and it
* is its first child.
*
* @defaultValue true
*/
enableRemoveTopMarginIfNested?: boolean;
/**
* Remove bottom margin if this element parent is an `li` element and it
* is its last child.
*
* @defaultValue true
*/
enableRemoveBottomMarginIfNested?: boolean;

/**
* If `true` and the direction is set to `'rtl'` (either via `dir` attribute
* or `direction` CSS property):
Expand Down
7 changes: 5 additions & 2 deletions packages/render-html/src/renderers/OLRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
DefaultSupportedListStyleType
} from '../shared-types';
import OLElement, { OLElementProps } from '../elements/OLElement';
import { useRendererProps } from '../context/RenderersPropsProvider';

function getListStyleTypeFromNestLevel(
function getFallbackListStyleTypeFromNestLevel(
nestLevel: number
): DefaultSupportedListStyleType {
switch (nestLevel % 3) {
Expand All @@ -27,10 +28,12 @@ export function useOLElementProps(
props: DefaultTagRendererProps<TBlock>
): OLElementProps {
const listStyleSpecs = props.sharedProps.customListStyleSpecs;
const config = useRendererProps('ol');
return {
...props,
listStyleSpecs,
getFallbackListStyleTypeFromNestLevel: getListStyleTypeFromNestLevel
getFallbackListStyleTypeFromNestLevel,
...config
};
}

Expand Down
7 changes: 5 additions & 2 deletions packages/render-html/src/renderers/ULRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
} from '../shared-types';
import { TBlock } from '@native-html/transient-render-engine';
import ULElement, { ULElementProps } from '../elements/ULElement';
import { useRendererProps } from '../context/RenderersPropsProvider';

function getListStyleTypeFromNestLevel(
function getFallbackListStyleTypeFromNestLevel(
nestLevel: number
): DefaultSupportedListStyleType {
switch (nestLevel % 3) {
Expand All @@ -25,10 +26,12 @@ export function useULElementProps(
props: DefaultTagRendererProps<TBlock>
): ULElementProps {
const listStyleSpecs = props.sharedProps.customListStyleSpecs;
const config = useRendererProps('ul');
return {
...props,
listStyleSpecs,
getFallbackListStyleTypeFromNestLevel: getListStyleTypeFromNestLevel
getFallbackListStyleTypeFromNestLevel,
...config
};
}

Expand Down
24 changes: 24 additions & 0 deletions packages/render-html/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ export interface GenericPressableProps extends AccessibilityProps {
onPress?: TouchableHighlightProps['onPress'];
}

/**
* Configuration for ol and ul.
*
* @public
*/
export interface ListElementConfig {
/**
* Remove top margin if this element parent is an `li` element and it
* is its first child.
*
* @defaultValue true
*/
enableRemoveTopMarginIfNested?: boolean;
/**
* Remove bottom margin if this element parent is an `li` element and it
* is its last child.
*
* @defaultValue true
*/
enableRemoveBottomMarginIfNested?: boolean;
}

/**
* Props for custom renderers. The convention is to declare a field per renderer.
* In doing so, you can benefit from `useRendererProps('tagname')` in custom renderers.
Expand Down Expand Up @@ -91,6 +113,8 @@ export interface RenderersPropsBase extends Record<string, any> {
*/
enableExperimentalPercentWidth?: boolean;
};
ul: ListElementConfig;
ol: ListElementConfig;
}

/**
Expand Down

0 comments on commit 316e706

Please sign in to comment.