React Native Segmented Control

repository·master·Indexed 20 days ago

https://github.com/react-native-segmented-control/segmented-control

A React Native library for rendering a segmented control. It utilizes the native UISegmentedControl on iOS and provides a JavaScript implementation that mocks the iOS 13 style for Android and Web. This module serves as a replacement for the SegmentedControlIOS component previously found in the React Native core.

Tokens
1.3K
Snippets
6
Records
7
Agent score
21%

What's inside @react-native-segmented-control/segmented-control

  1. Customize SegmentedControl height and padding

    master

    Increasing Tab Height

    • Android and iOS: Pass height in the style prop.
    • Web: Pass height or paddingVertical in the style prop.

    Android Padding Warning

    On Android, if the padding amount exceeds the fixed height of the container, the text will shrink. To avoid this, either increase the height or reduce the padding.

    // Example style for height
    {
      "height": 40
    }
  2. Configure linking for React Native

    master

    React Native 0.60+

    Autolinking is supported. After installing, run:

    npx pod-install

    Android requires no additional linking as the module is implemented in JS.

    React Native <= 0.59

    Run the following command to link the package:

    react-native link @react-native-segmented-control/segmented-control

    Upgrading to 0.60+

    If you are upgrading a project that previously used manual linking, ensure you unlink the old version first:

    react-native unlink @react-native-segmented-control/segmented-control
  3. Install @react-native-segmented-control/segmented-control

    master

    Install the library using your preferred package manager:

    pnpm

    pnpm install --save @react-native-segmented-control/segmented-control

    yarn

    yarn add @react-native-segmented-control/segmented-control

    npm

    npm install --save @react-native-segmented-control/segmented-control

    Note for iOS/Expo users: This module requires custom native code and is NOT supported in the Expo Go app on iOS. You must use a custom development client or eject into the bare workflow.

  4. Use SegmentedControl in your application

    master

    To use the component, import SegmentedControl and provide a values array. To handle selection changes and programmatically control the index, manage the selectedIndex via component state and update it within the onChange callback.

    import SegmentedControl from '@react-native-segmented-control/segmented-control';
    
    // Inside your component
    return (
      <SegmentedControl
        values={['One', 'Two']}
        selectedIndex={this.state.selectedIndex}
        onChange={(event) => {
          this.setState({selectedIndex: event.nativeEvent.selectedSegmentIndex});
        }}
      />
    );
  5. Reference SegmentedControl Props

    master

    The SegmentedControl component accepts the following props:

    PropTypeRequiredDescription
    values(string | number | Image)[]NoThe labels for the control's segment buttons, in order.
    selectedIndexnumberNoThe index in props.values of the segment to be (pre)selected.
    onChangefunctionNoCallback called when the user taps a segment; passes the event as an argument.
    onValueChangefunctionNoCallback called when the user taps a segment; passes the segment's value as an argument.
    enabledboolNoIf false, the user cannot interact with the control. Default is true.
    momentaryboolNo (iOS only)If true, selecting a segment won't persist visually. onValueChange still works.
    tintColorstringNoAccent color of the control.
    backgroundColorstringNo (iOS 13+)Background color of the control.
    appearance'dark' | 'light'No (iOS 13+, Android, Web)Overrides the control's appearance irrespective of OS theme.
    fontStyleobjectNo (iOS 13+, Android, Web)Object containing color, fontSize, fontFamily, and fontWeight.
    activeFontStyleobjectNo (iOS 13+, Android, Web)Object containing color, fontSize, fontFamily, and fontWeight for the selected segment.
    tabStyleobjectNo (Android, Web)Styles the clickable surface. Extends ViewStyles.
    sliderStyleobjectNo (Android, Web)Styles the slider component (Animated.View). Extends ViewStyles.
  6. Use the SegmentedControl component

    master

    The SegmentedControl component is the primary entrypoint for this library. It is exported as the default export from the package. You can import it directly to render a segmented control UI in your React Native application.

    import SegmentedControl from '@react-native-segmented-control/segmented-control';
    
    // Usage within a component
    <SegmentedControl
      values={['Option 1', 'Option 2']}
      selectedIndex={0}
      onValueChange={(value) => console.log(value)}
    />