Below is an example of how you might implement a reusable TSX component using shadcn/ui’s ToggleGroup to handle multiple toggle pairs (play/pause, mute/unmute, halt/unhalt, connected/disconnected, sync/out‑of‑sync), with each state coming from above via props.
Note:
• Make sure you have the shadcn/ui toggle-group component installed and imported correctly.
• Adjust the import paths to match your project structure.
ToggleControls.tsx
import React from "react";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
interface ToggleControlsProps {
// Boolean flags and their setters passed from the parent.
play: boolean;
setPlay: (value: boolean) => void;
mute: boolean;
setMute: (value: boolean) => void;
halt: boolean;
setHalt: (value: boolean) => void;
connected: boolean;
setConnected: (value: boolean) => void;
sync: boolean;
setSync: (value: boolean) => void;
}
const ToggleControls: React.FC<ToggleControlsProps> = ({
play,
setPlay,
mute,
setMute,
halt,
setHalt,
connected,
setConnected,
sync,
setSync
}) => {
return (
<div style={{ display: "flex", alignItems: "center", gap: "12px" }}>
{/* Play / Pause Toggle */}
<ToggleGroup
type="single"
value={play ? "play" : "pause"}
onValueChange={(value) => setPlay(value === "play")}
>
<ToggleGroupItem value="play">Play</ToggleGroupItem>
<ToggleGroupItem value="pause">Pause</ToggleGroupItem>
</ToggleGroup>
{/* Mute / Unmute Toggle */}
<ToggleGroup
type="single"
value={mute ? "mute" : "unmute"}
onValueChange={(value) => setMute(value === "mute")}
>
<ToggleGroupItem value="mute">Mute</ToggleGroupItem>
<ToggleGroupItem value="unmute">Unmute</ToggleGroupItem>
</ToggleGroup>
{/* Halt / Unhalt Toggle */}
<ToggleGroup
type="single"
value={halt ? "halt" : "unhalt"}
onValueChange={(value) => setHalt(value === "halt")}
>
<ToggleGroupItem value="halt">Halt</ToggleGroupItem>
<ToggleGroupItem value="unhalt">Unhalt</ToggleGroupItem>
</ToggleGroup>
{/* Separator */}
<div
style={{
width: "1px",
height: "32px",
backgroundColor: "#ccc",
margin: "0 8px"
}}
/>
{/* Connected / Disconnected Toggle */}
<ToggleGroup
type="single"
value={connected ? "connected" : "disconnected"}
onValueChange={(value) => setConnected(value === "connected")}
>
<ToggleGroupItem value="connected">Connected</ToggleGroupItem>
<ToggleGroupItem value="disconnected">
Disconnected
</ToggleGroupItem>
</ToggleGroup>
{/* Sync / Out-of-sync Toggle */}
<ToggleGroup
type="single"
value={sync ? "sync" : "out-of-sync"}
onValueChange={(value) => setSync(value === "sync")}
>
<ToggleGroupItem value="sync">Sync</ToggleGroupItem>
<ToggleGroupItem value="out-of-sync">
Out of Sync
</ToggleGroupItem>
</ToggleGroup>
</div>
);
};
export default ToggleControls;
Example Usage in a Parent Component
import React, { useState } from "react";
import ToggleControls from "@/components/ToggleControls";
const ParentComponent: React.FC = () => {
// States maintained in the parent component
const [play, setPlay] = useState(true);
const [mute, setMute] = useState(false);
const [halt, setHalt] = useState(false);
const [connected, setConnected] = useState(true);
const [sync, setSync] = useState(true);
return (
<div style={{ padding: "16px" }}>
<h2>Media Controls</h2>
<ToggleControls
play={play}
setPlay={setPlay}
mute={mute}
setMute={setMute}
halt={halt}
setHalt={setHalt}
connected={connected}
setConnected={setConnected}
sync={sync}
setSync={setSync}
/>
<div style={{ marginTop: "16px" }}>
<p>Play: {play ? "Playing" : "Paused"}</p>
<p>Mute: {mute ? "Muted" : "Unmuted"}</p>
<p>Halt: {halt ? "Halted" : "Running"}</p>
<p>Connection: {connected ? "Connected" : "Disconnected"}</p>
<p>Sync Status: {sync ? "Synced" : "Out of Sync"}</p>
</div>
</div>
);
};
export default ParentComponent;
Explanation
-
Component Props:
The ToggleControls component accepts five boolean values and five corresponding setter functions as props. This allows the parent component to fully control the state.
-
ToggleGroup Implementation:
For each pair of states (e.g., play/pause), we render a ToggleGroup with two ToggleGroupItem children. The value prop is derived from the current boolean state (for example, if play is true, the value is "play"; otherwise, "pause"). The onValueChange callback sets the state appropriately.
-
Separator:
A simple styled <div> is used as a separator between the two groups of toggles.
-
Usage in Parent:
The parent component (ParentComponent) manages the state and passes both the current values and their corresponding setter functions to ToggleControls. You can also display the state or use it elsewhere.
This approach keeps the toggle logic encapsulated in a reusable component while enabling full control from the parent component. Adjust styling and imports based on your project’s conventions and requirements.