Skip to content

Commit 5551140

Browse files
Allow touchable to be stylable (#299)
* feat: allow touchable to be stylable * Review: remove tabstyles from ios * Review: rename touchableStyle to tabStyle * Include Tips and Tricks section and add the implemented style documentation * Remove superflous array notation from style prop
1 parent d93cfc4 commit 5551140

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,41 @@ An object container
225225
- `fontFamily`: overrides font-family of selected segment text
226226
- `fontWeight`: overrides font-weight of selected segment text
227227

228+
### `tabStyle`
229+
230+
(Android and Web only) Styles the clickable surface which is responsible to change tabs
231+
| Type | Required | Platform |
232+
| ------ | -------- | -------- |
233+
| object | No | Android, Web |
234+
235+
Extends [ViewStyles](https://reactnative.dev/docs/view-style-props)
236+
237+
## Tips and Tricks
238+
239+
### How can I increase the height of the tab ?
240+
241+
For android and IOS, simply pass `prop.style`:
242+
243+
```json
244+
{
245+
"height": number
246+
}
247+
```
248+
249+
For react-native-web, additionally pass :
250+
251+
```json
252+
{
253+
"paddingVertical": number,
254+
or
255+
"height": number
256+
}
257+
```
258+
259+
### Adding padding makes text disappear on Android
260+
261+
If padding amount exceeds the fixed height of the container, it will shrink the text. So either increase the height or reduce your padding.
262+
228263
## Maintainers
229264

230265
- [M.Haris Baig](https://github.com/harisbaig100)

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
NativeMethods,
99
Constructor,
1010
TargetedEvent,
11+
ViewStyle,
1112
} from 'react-native';
1213

1314
export interface NativeSegmentedControlIOSChangeEvent extends TargetedEvent {
@@ -104,6 +105,11 @@ export interface SegmentedControlProps extends ViewProps {
104105
* Active Font style properties of the Segmented Control
105106
*/
106107
activeFontStyle?: FontStyle;
108+
109+
/**
110+
* Touchable style properties for Segmented Control Tab
111+
*/
112+
tabStyle?: ViewStyle;
107113
}
108114

109115
/**

js/SegmentedControl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const SegmentedControl = ({
3232
values,
3333
tintColor,
3434
backgroundColor,
35+
tabStyle,
3536
fontStyle,
3637
activeFontStyle,
3738
appearance,
@@ -101,6 +102,7 @@ const SegmentedControl = ({
101102
key={index}
102103
value={value}
103104
tintColor={tintColor}
105+
tabStyle={tabStyle}
104106
fontStyle={fontStyle}
105107
activeFontStyle={activeFontStyle}
106108
appearance={colorScheme}

js/SegmentedControlTab.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
useColorScheme,
1616
} from 'react-native';
1717

18-
import type {FontStyle} from './types';
18+
import type {FontStyle, ViewStyle} from './types';
1919

2020
type Props = $ReadOnly<{|
2121
value: string | number | Object,
@@ -25,6 +25,7 @@ type Props = $ReadOnly<{|
2525
enabled: boolean,
2626
fontStyle?: FontStyle,
2727
activeFontStyle?: FontStyle,
28+
tabStyle?: ViewStyle,
2829
appearance?: 'dark' | 'light' | null,
2930
|}>;
3031

@@ -42,6 +43,7 @@ export const SegmentedControlTab = ({
4243
fontStyle = {},
4344
activeFontStyle = {},
4445
appearance,
46+
tabStyle,
4547
}: Props): React.Node => {
4648
const colorSchemeHook = useColorScheme();
4749
const colorScheme = appearance || colorSchemeHook;
@@ -82,11 +84,11 @@ export const SegmentedControlTab = ({
8284

8385
return (
8486
<TouchableOpacity
85-
style={styles.container}
87+
style={[styles.container, tabStyle]}
8688
disabled={!enabled}
8789
onPress={onSelect}
8890
accessibilityState={{selected: selected, disabled: !enabled}}>
89-
<View style={[styles.default]}>
91+
<View style={styles.default}>
9092
{typeof value === 'number' || typeof value === 'object' ? (
9193
<Image source={value} style={styles.segmentImage} />
9294
) : isBase64(value) ? (

js/types.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import type {ColorValue} from 'react-native/Libraries/StyleSheet/StyleSheetTypes';
66
import type {SyntheticEvent} from 'react-native/Libraries/Types/CoreEventTypes';
77
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
8+
import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
89

910
export type Event = SyntheticEvent<
1011
$ReadOnly<{|
@@ -13,6 +14,8 @@ export type Event = SyntheticEvent<
1314
|}>,
1415
>;
1516

17+
export type ViewStyle = ViewStyleProp;
18+
1619
export type FontStyle = $ReadOnly<{|
1720
/**
1821
* Font Color of Segmented Control
@@ -99,4 +102,9 @@ export type SegmentedControlProps = $ReadOnly<{|
99102
* it will override fontStyle for the selected segment
100103
*/
101104
activeFontStyle?: FontStyle,
105+
106+
/**
107+
* Touchable style properties for Segmented Control Tab
108+
*/
109+
tabStyle?: ViewStyle,
102110
|}>;

0 commit comments

Comments
 (0)