Skip to content

Instantly share code, notes, and snippets.

@eric-horodyski
Last active September 1, 2023 10:54
Show Gist options
  • Save eric-horodyski/4b940e889fef1bb169b067c9e078a5e4 to your computer and use it in GitHub Desktop.
Save eric-horodyski/4b940e889fef1bb169b067c9e078a5e4 to your computer and use it in GitHub Desktop.
Ionic Framework React: Sliding Segments
import {
IonSegment,
IonSegmentButton,
IonLabel,
IonSlide,
IonSlides,
} from "@ionic/react";
import React, { useRef, useState } from "react";
const SlidingSegments: React.FC<ContainerProps> = () => {
const segments = ["friends", "enemies"];
const slider = useRef<HTMLIonSlidesElement>(null);
const [value, setValue] = useState("friends");
const handleSegmentChange = (e: any) => {
const selectedSegment = segments.findIndex((el) => el === e.detail.value!);
setValue(e.detail.value!);
slider.current!.slideTo(selectedSegment);
};
const handleSlideChange = async (e: any) => {
const activeSlide = await slider.current?.getActiveIndex();
setValue(segments[activeSlide!]);
};
return (
<>
<IonSegment onIonChange={(e) => handleSegmentChange(e)} value={value}>
{segments.map((segment, idx) => (
<IonSegmentButton key={idx} value={segment}>
<IonLabel>{segment}</IonLabel>
</IonSegmentButton>
))}
</IonSegment>
<IonSlides
pager={true}
ref={slider}
onIonSlideDidChange={(e) => handleSlideChange(e)}
>
<IonSlide>
<h1>Slide 1</h1>
</IonSlide>
<IonSlide>
<h1>Slide 2</h1>
</IonSlide>
</IonSlides>
</>
);
};
export default SlidingSegments;
@Rohancu
Copy link

Rohancu commented Sep 1, 2023

rohan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment