React Desktop is a popular library to bring the native desktop experience to the web. This library provides macOS and Windows OS components. A SegmentedControl component is a linear set of two or more segments where each of which functions as a mutually exclusive button. We can use the following approach in ReactJS to use the React Desktop macOS SegmentedControl Component.
SegmentedControl Props:
- box: It is used to indicate whether the item is in a box or not.
- height: It is used to set component height.
- hidden: It is used to set the component visibility.
- margin: It is used to set the outer margin of the component.
- marginBottom: It is used to set the outer bottom margin of the component.
- marginLeft: It is used to set the outer left margin of the component.
- marginRight: It is used to set the outer right margin of the component.
- marginTop: It is used to set the outer top margin of the component.
- width: It is used to set components width.
SegmentedControlItem Props:
- title: It is used to set the title for the item.
- onSelect: It is a callback function that is triggered on select of an item.
- selected: It is used to indicate whether the item is selected or not.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app foldername
-
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
-
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install react-desktop
Project Structure: It will look like the following.
Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React, { useState } from 'react' import { SegmentedControl, SegmentedControlItem } from 'react-desktop/macOs' ; export default function App() { // Our State object const [currentSelection, setCurrentSelection] = useState(1) return ( <div style={{ display: 'block' , width: 700, paddingLeft: 30 }}> <h4>React-Desktop macOS SegmentedControl Component</h4> <SegmentedControl box> <SegmentedControlItem key={1} title={ "TAB 1" } selected={currentSelection === 1} onSelect={() => setCurrentSelection(1)} > Sample Text 1 </SegmentedControlItem> <SegmentedControlItem key={2} title={ "TAB 2" } selected={currentSelection === 2} onSelect={() => setCurrentSelection(2)} > Sample Text 2 </SegmentedControlItem> </SegmentedControl> </div> ); } |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://reactdesktop.js.org/docs/mac-os/segmented-control