Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(ui): forward v3 Toggle refs to the native switch (#8432)

Andrew Huang6c34b685be6f
diff --git a/frontend/src/components/v3/generic/Toggle/Toggle.stories.tsx b/frontend/src/components/v3/generic/Toggle/Toggle.stories.tsx
index 263e7e3808..d0cb35ab2a 100644
--- a/frontend/src/components/v3/generic/Toggle/Toggle.stories.tsx
+++ b/frontend/src/components/v3/generic/Toggle/Toggle.stories.tsx
@@ -1,5 +1,7 @@
+import { useRef } from "react";
import type { Meta, StoryObj } from "@storybook/react-vite";
+import { Button } from "../Button";
import { Field, FieldContent, FieldDescription, FieldGroup, FieldTitle } from "../Field";
import { Label } from "../Label";
import { Toggle } from "./Toggle";
@@ -267,6 +269,31 @@ export const WithLabel: Story = {
)
};
+export const NativeFocus: Story = {
+ name: "Example: Native Focus",
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "The ref points to the native switch button and supports focus(). With React Hook Form's Controller, pass field.ref to ref, field.value to checked, field.onChange to onCheckedChange, and field.onBlur to onBlur so setFocus targets the toggle."
+ }
+ }
+ },
+ render: function Render() {
+ const ref = useRef<HTMLButtonElement>(null);
+
+ return (
+ <div className="flex items-center gap-3">
+ <Toggle ref={ref} id="toggle-focus" />
+ <Label htmlFor="toggle-focus">Enable notifications</Label>
+ <Button variant="outline" onClick={() => ref.current?.focus()}>
+ Focus Toggle
+ </Button>
+ </div>
+ );
+ }
+};
+
export const WithDescription: Story = {
name: "Example: With Description",
parameters: {
diff --git a/frontend/src/components/v3/generic/Toggle/Toggle.tsx b/frontend/src/components/v3/generic/Toggle/Toggle.tsx
index 0526551381..02036968af 100644
--- a/frontend/src/components/v3/generic/Toggle/Toggle.tsx
+++ b/frontend/src/components/v3/generic/Toggle/Toggle.tsx
@@ -60,25 +60,30 @@ const toggleThumbVariants = cva(
)
);
-type ToggleProps = React.ComponentProps<typeof SwitchPrimitive.Root> &
+type ToggleProps = React.ComponentPropsWithoutRef<typeof SwitchPrimitive.Root> &
VariantProps<typeof toggleVariants> & {
size?: "sm" | "default";
};
-function Toggle({ className, variant, size = "default", ...props }: ToggleProps) {
- return (
- <SwitchPrimitive.Root
- data-slot="toggle"
- data-size={size}
- className={cn(toggleVariants({ variant }), className)}
- {...props}
- >
- <SwitchPrimitive.Thumb
- data-slot="toggle-thumb"
- className={cn(toggleThumbVariants(), "border")}
- />
- </SwitchPrimitive.Root>
- );
-}
+const Toggle = React.forwardRef<React.ElementRef<typeof SwitchPrimitive.Root>, ToggleProps>(
+ ({ className, variant, size = "default", ...props }, ref) => {
+ return (
+ <SwitchPrimitive.Root
+ ref={ref}
+ data-slot="toggle"
+ data-size={size}
+ className={cn(toggleVariants({ variant }), className)}
+ {...props}
+ >
+ <SwitchPrimitive.Thumb
+ data-slot="toggle-thumb"
+ className={cn(toggleThumbVariants(), "border")}
+ />
+ </SwitchPrimitive.Root>
+ );
+ }
+);
+
+Toggle.displayName = "Toggle";
export { Toggle };