-
-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: Add support for borderRadii on RectButton #2691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7ae5c20
Draft working solution on Android
kacperkapusciak abd333a
Adjust postion
kacperkapusciak 2cbef84
It kinda works
kacperkapusciak aa19f0d
Incorporate borderWidth to height and width
kacperkapusciak c6b6bd3
Refactor splitStyleProp
kacperkapusciak cbe8ffc
Add comment
kacperkapusciak c2afdc7
I lost a battle with Typescript
kacperkapusciak 4c30ea4
Rename example
kacperkapusciak 453c160
Ignore ts error, remove console log
kacperkapusciak f11887d
Remove dead code
kacperkapusciak 5ef7138
murder TS
tjzel 40d0e93
fix CI
tjzel 337bb90
Apply suggestions from code review
kacperkapusciak f3ec1cf
Merge branch 'main' into @kacperkapusciak/rectbutton-border-radius
j-piasecki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,138 @@ | ||
| import React from 'react'; | ||
| import { View, StyleSheet, Text, SafeAreaView } from 'react-native'; | ||
| import { | ||
| GestureHandlerRootView, | ||
| ScrollView, | ||
| RectButton, | ||
| } from 'react-native-gesture-handler'; | ||
|
|
||
| const MyButton = RectButton; | ||
|
|
||
| export default function ComplexUI() { | ||
| return ( | ||
| <GestureHandlerRootView style={styles.container}> | ||
| <SafeAreaView style={styles.container}> | ||
| <ScrollView> | ||
| <Avatars /> | ||
| <View style={styles.paddedContainer}> | ||
| <Gallery /> | ||
| <Gallery /> | ||
| <Gallery /> | ||
| <Gallery /> | ||
| <Gallery /> | ||
| </View> | ||
| </ScrollView> | ||
| </SafeAreaView> | ||
| </GestureHandlerRootView> | ||
| ); | ||
| } | ||
| const colors = ['#782AEB', '#38ACDD', '#57B495', '#FF6259', '#FFD61E']; | ||
|
|
||
| function Avatars() { | ||
| return ( | ||
| <ScrollView horizontal showsHorizontalScrollIndicator={false}> | ||
| {colors.map((color) => ( | ||
| <MyButton | ||
| key={color} | ||
| style={[styles.avatars, { backgroundColor: color }]}> | ||
| <Text style={styles.avatarLabel}>{color.slice(1, 3)}</Text> | ||
| </MyButton> | ||
| ))} | ||
| </ScrollView> | ||
| ); | ||
| } | ||
|
|
||
| function Gallery() { | ||
| return ( | ||
| <View style={[styles.container, styles.gap, styles.marginBottom]}> | ||
| <MyButton style={styles.fullWidthButton} /> | ||
| <View style={[styles.row, styles.gap]}> | ||
| <MyButton style={styles.leftButton} /> | ||
| <MyButton style={styles.rightButton} /> | ||
| </View> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
| }, | ||
| marginBottom: { | ||
| marginBottom: 20, | ||
| }, | ||
| paddedContainer: { | ||
| padding: 16, | ||
| }, | ||
| heading: { | ||
| fontSize: 40, | ||
| fontWeight: 'bold', | ||
| marginBottom: 24, | ||
| color: 'black', | ||
| }, | ||
| gap: { | ||
| gap: 10, | ||
| }, | ||
| listItem: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| justifyContent: 'space-between', | ||
| padding: 20, | ||
| backgroundColor: '#232736', | ||
| marginVertical: 4, | ||
| borderRadius: 20, | ||
| marginBottom: 8, | ||
| }, | ||
| listItemLabel: { | ||
| fontSize: 20, | ||
| flex: 1, | ||
| color: 'white', | ||
| marginLeft: 20, | ||
| }, | ||
| listItemIcon: { | ||
| fontSize: 32, | ||
| }, | ||
| row: { | ||
| flexDirection: 'row', | ||
| }, | ||
| avatars: { | ||
| width: 90, | ||
| height: 90, | ||
| borderWidth: 2, | ||
| borderColor: '#001A72', | ||
| borderTopLeftRadius: 30, | ||
| borderTopRightRadius: 5, | ||
| borderBottomLeftRadius: 5, | ||
| borderBottomRightRadius: 30, | ||
| marginHorizontal: 4, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| avatarLabel: { | ||
| color: '#F8F9FF', | ||
| fontSize: 24, | ||
| fontWeight: 'bold', | ||
| }, | ||
| fullWidthButton: { | ||
| width: '100%', | ||
| height: 160, | ||
| backgroundColor: '#FF6259', | ||
| borderTopRightRadius: 30, | ||
| borderTopLeftRadius: 30, | ||
| borderWidth: 1, | ||
| }, | ||
| leftButton: { | ||
| flex: 1, | ||
| height: 160, | ||
| backgroundColor: '#FFD61E', | ||
| borderBottomLeftRadius: 30, | ||
| borderWidth: 5, | ||
| }, | ||
| rightButton: { | ||
| flex: 1, | ||
| backgroundColor: '#782AEB', | ||
| height: 160, | ||
| borderBottomRightRadius: 30, | ||
| borderWidth: 8, | ||
| }, | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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,169 @@ | ||
| import { StyleProp, StyleSheet, ViewStyle } from 'react-native'; | ||
|
|
||
| const STYLE_GROUPS = { | ||
| borderRadiiStyles: { | ||
| borderRadius: true, | ||
| borderTopLeftRadius: true, | ||
| borderTopRightRadius: true, | ||
| borderBottomLeftRadius: true, | ||
| borderBottomRightRadius: true, | ||
| } as const, | ||
| outerStyles: { | ||
| borderColor: true, | ||
| borderWidth: true, | ||
| margin: true, | ||
| marginBottom: true, | ||
| marginEnd: true, | ||
| marginHorizontal: true, | ||
| marginLeft: true, | ||
| marginRight: true, | ||
| marginStart: true, | ||
| marginTop: true, | ||
| marginVertical: true, | ||
| width: true, | ||
| height: true, | ||
| } as const, | ||
| innerStyles: { | ||
| alignSelf: true, | ||
| display: true, | ||
| flexBasis: true, | ||
| flexGrow: true, | ||
| flexShrink: true, | ||
| maxHeight: true, | ||
| maxWidth: true, | ||
| minHeight: true, | ||
| minWidth: true, | ||
| zIndex: true, | ||
| } as const, | ||
| applyToAllStyles: { | ||
| flex: true, | ||
| position: true, | ||
| left: true, | ||
| right: true, | ||
| top: true, | ||
| bottom: true, | ||
| start: true, | ||
| end: true, | ||
| } as const, | ||
| } as const; | ||
|
|
||
| type BorderRadiiKey = keyof typeof STYLE_GROUPS.borderRadiiStyles; | ||
| type OuterKey = keyof typeof STYLE_GROUPS.outerStyles; | ||
| type InnerKey = keyof typeof STYLE_GROUPS.innerStyles; | ||
| type ApplyToAllKey = keyof typeof STYLE_GROUPS.applyToAllStyles; | ||
|
|
||
| type BorderRadiiStyles = Pick<ViewStyle, BorderRadiiKey>; | ||
| type OuterStyles = Pick<ViewStyle, OuterKey>; | ||
| type InnerStyles = Pick<ViewStyle, InnerKey>; | ||
| type ApplyToAllStyles = Pick<ViewStyle, ApplyToAllKey>; | ||
| type RestStyles = Omit< | ||
| ViewStyle, | ||
| BorderRadiiKey | OuterKey | InnerKey | ApplyToAllKey | ||
| >; | ||
|
|
||
| type GroupedStyles = { | ||
| borderRadiiStyles: BorderRadiiStyles; | ||
| outerStyles: OuterStyles; | ||
| innerStyles: InnerStyles; | ||
| applyToAllStyles: ApplyToAllStyles; | ||
| restStyles: RestStyles; | ||
| }; | ||
|
|
||
| const groupByStyle = (styles: ViewStyle): GroupedStyles => { | ||
| const borderRadiiStyles = {} as Record<string, unknown>; | ||
| const outerStyles = {} as Record<string, unknown>; | ||
| const innerStyles = {} as Record<string, unknown>; | ||
| const applyToAllStyles = {} as Record<string, unknown>; | ||
| const restStyles = {} as Record<string, unknown>; | ||
|
|
||
| let key: keyof ViewStyle; | ||
|
|
||
| for (key in styles) { | ||
| if (key in STYLE_GROUPS.borderRadiiStyles) { | ||
| borderRadiiStyles[key] = styles[key]; | ||
| } else if (key in STYLE_GROUPS.outerStyles) { | ||
| outerStyles[key] = styles[key]; | ||
| } else if (key in STYLE_GROUPS.innerStyles) { | ||
| innerStyles[key] = styles[key]; | ||
| } else if (key in STYLE_GROUPS.applyToAllStyles) { | ||
| applyToAllStyles[key] = styles[key]; | ||
| } else { | ||
| restStyles[key] = styles[key]; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| borderRadiiStyles, | ||
| outerStyles, | ||
| innerStyles, | ||
| applyToAllStyles, | ||
| restStyles, | ||
| }; | ||
| }; | ||
|
|
||
| // if borderWidth was specified it will adjust the border radii | ||
| // to remain the same curvature for both inner and outer views | ||
| // https://twitter.com/lilykonings/status/1567317037126680576 | ||
| const shrinkBorderRadiiByBorderWidth = ( | ||
| borderRadiiStyles: BorderRadiiStyles, | ||
| borderWidth: number | ||
| ) => { | ||
| const newBorderRadiiStyles = { ...borderRadiiStyles }; | ||
|
|
||
| let borderRadiusType: BorderRadiiKey; | ||
|
|
||
| for (borderRadiusType in newBorderRadiiStyles) { | ||
| newBorderRadiiStyles[borderRadiusType] = | ||
| (newBorderRadiiStyles[borderRadiusType] as number) - borderWidth; | ||
| } | ||
|
|
||
| return newBorderRadiiStyles; | ||
| }; | ||
|
|
||
| export function splitStyleProp<T extends ViewStyle>( | ||
| style?: StyleProp<T> | ||
| ): { | ||
| outerStyles: T; | ||
| innerStyles: T; | ||
| restStyles: T; | ||
| } { | ||
| const resolvedStyle = StyleSheet.flatten((style ?? {}) as ViewStyle); | ||
|
|
||
| let outerStyles = {} as T; | ||
| let innerStyles = { overflow: 'hidden', flexGrow: 1 } as T; | ||
| let restStyles = { flexGrow: 1 } as T; | ||
|
|
||
| const styleGroups = groupByStyle(resolvedStyle); | ||
|
|
||
| outerStyles = { | ||
| ...outerStyles, | ||
| ...styleGroups.borderRadiiStyles, | ||
| ...styleGroups.applyToAllStyles, | ||
| ...styleGroups.outerStyles, | ||
| }; | ||
| innerStyles = { | ||
| ...innerStyles, | ||
| ...styleGroups.applyToAllStyles, | ||
| ...styleGroups.innerStyles, | ||
| }; | ||
| restStyles = { | ||
| ...restStyles, | ||
| ...styleGroups.restStyles, | ||
| ...styleGroups.applyToAllStyles, | ||
| }; | ||
|
|
||
| // if borderWidth was specified it adjusts border radii | ||
| // to remain the same curvature for both inner and outer views | ||
| if (styleGroups.outerStyles.borderWidth != null) { | ||
| const { borderWidth } = styleGroups.outerStyles; | ||
|
|
||
| const innerBorderRadiiStyles = shrinkBorderRadiiByBorderWidth( | ||
| { ...styleGroups.borderRadiiStyles }, | ||
| borderWidth | ||
| ); | ||
|
|
||
| innerStyles = { ...innerStyles, ...innerBorderRadiiStyles }; | ||
| } | ||
|
|
||
| return { outerStyles, innerStyles, restStyles }; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.