-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27e01a1
commit 3706c8c
Showing
8 changed files
with
1,164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
react-native/components/createProportionalColumnComponent/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import * as React from 'react' | ||
import { StyleSheet, View, type ViewStyle } from 'react-native' | ||
import type { ProportionalColumnProps } from '../../types/ProportionalColumnProps' | ||
|
||
const globalStyles = StyleSheet.create({ | ||
fillsContainerLeft: { | ||
width: '100%', | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'flex-start' | ||
}, | ||
fillsContainerCentered: { | ||
width: '100%', | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'center' | ||
}, | ||
fillsContainerRight: { | ||
width: '100%', | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'flex-end' | ||
}, | ||
fillsContainerStretched: { | ||
width: '100%', | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'stretch' | ||
}, | ||
fitsContentLeft: { | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'flex-start' | ||
}, | ||
fitsContentCentered: { | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'center' | ||
}, | ||
fitsContentRight: { | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'flex-end' | ||
}, | ||
fitsContentStretched: { | ||
height: '100%', | ||
flexDirection: 'column', | ||
alignItems: 'stretch' | ||
} | ||
}) | ||
|
||
/** | ||
* Creates a new React component which fills its container verticall and | ||
* presents a fixed set of rows with proportional height and optional spacing. | ||
* @template T Keys representing the rows within. | ||
* @param spacing The spacing between the rows. | ||
* @param widths The proportional heights of the rows. | ||
* @returns The created React component. | ||
*/ | ||
export function createProportionalColumnComponent<T extends readonly never[]> ( | ||
spacing: number, | ||
widths: { [TKey in keyof T]: number } | ||
): React.FunctionComponent<ProportionalColumnProps<T>> { | ||
const viewStyles: Array<{ readonly view: ViewStyle }> = [] | ||
|
||
for (const width of widths) { | ||
viewStyles.push( | ||
StyleSheet.create({ | ||
view: { | ||
flexBasis: 0, | ||
flexGrow: width | ||
} | ||
}) | ||
) | ||
} | ||
|
||
const localStyles = StyleSheet.create({ | ||
spacer: { | ||
flexBasis: spacing | ||
} | ||
}) | ||
|
||
const ProportionalColumn: React.FunctionComponent<ProportionalColumnProps<T>> = ({ width, horizontalAlignment, children }) => { | ||
const intercalatedChildren: Array<null | JSX.Element> = [] | ||
|
||
let index = 0 | ||
|
||
for (const view of children) { | ||
if (index > 0 && spacing !== 0) { | ||
intercalatedChildren.push( | ||
<View | ||
key={`separator${index - 1}`} | ||
style={localStyles.spacer} | ||
pointerEvents="none" | ||
/> | ||
) | ||
} | ||
|
||
intercalatedChildren.push( | ||
<View | ||
key={String(index)} | ||
pointerEvents="box-none" | ||
style={viewStyles[index]?.view} | ||
> | ||
{view} | ||
</View> | ||
) | ||
|
||
index++ | ||
} | ||
|
||
return ( | ||
<View | ||
style={ | ||
width === 'fitsContent' | ||
? horizontalAlignment === 'left' | ||
? globalStyles.fitsContentLeft | ||
: horizontalAlignment === 'centered' | ||
? globalStyles.fitsContentCentered | ||
: horizontalAlignment === 'right' | ||
? globalStyles.fitsContentRight | ||
: globalStyles.fitsContentStretched | ||
: horizontalAlignment === 'left' | ||
? globalStyles.fillsContainerLeft | ||
: horizontalAlignment === 'centered' | ||
? globalStyles.fillsContainerCentered | ||
: horizontalAlignment === 'right' | ||
? globalStyles.fillsContainerRight | ||
: globalStyles.fillsContainerStretched | ||
} | ||
pointerEvents="box-none" | ||
> | ||
{intercalatedChildren} | ||
</View> | ||
) | ||
} | ||
|
||
return ProportionalColumn | ||
} |
98 changes: 98 additions & 0 deletions
98
react-native/components/createProportionalColumnComponent/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# `react-native-app-helpers/createProportionalColumnComponent` | ||
|
||
Creates a new React component which fills its container vertically and presents | ||
a fixed set of rows with proportional width and optional spacing. | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { createProportionalColumnComponent } from "react-native-app-helpers"; | ||
|
||
const ExampleComponent = createProportionalColumnComponent(23, [27, 18, 33, 44]); | ||
|
||
const ExampleScreen = () => ( | ||
<ExampleComponent width="fitsContent" horizontalAlignment="top"> | ||
<Text> | ||
This is 27/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 18/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 33/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 44/122 of the height. | ||
</Text> | ||
</ExampleComponent> | ||
); | ||
``` | ||
|
||
```tsx | ||
import { createProportionalColumnComponent } from "react-native-app-helpers"; | ||
|
||
const ExampleComponent = createProportionalColumnComponent(23, [27, 18, 33, 44]); | ||
|
||
const ExampleScreen = () => ( | ||
<ExampleComponent width="fillsContainer" horizontalAlignment="centered"> | ||
<Text> | ||
This is 27/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 18/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 33/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 44/122 of the height. | ||
</Text> | ||
</ExampleComponent> | ||
); | ||
``` | ||
|
||
```tsx | ||
import { createProportionalColumnComponent } from "react-native-app-helpers"; | ||
|
||
const ExampleComponent = createProportionalColumnComponent(23, [27, 18, 33, 44]); | ||
|
||
const ExampleScreen = () => ( | ||
<ExampleComponent width="fillsContainer" horizontalAlignment="bottom"> | ||
<Text> | ||
This is 27/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 18/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 33/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 44/122 of the height. | ||
</Text> | ||
</ExampleComponent> | ||
); | ||
``` | ||
|
||
```tsx | ||
import { createProportionalColumnComponent } from "react-native-app-helpers"; | ||
|
||
const ExampleComponent = createProportionalColumnComponent(23, [27, 18, 33, 44]); | ||
|
||
const ExampleScreen = () => ( | ||
<ExampleComponent width="fillsContainer" horizontalAlignment="stretched"> | ||
<Text> | ||
This is 27/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 18/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 33/122 of the height. | ||
</Text> | ||
<Text> | ||
This is 44/122 of the height. | ||
</Text> | ||
</ExampleComponent> | ||
); | ||
``` |
Oops, something went wrong.