From 73c46ec51652fe82d42616db68baf99c9bf8f785 Mon Sep 17 00:00:00 2001 From: Dennis Torres Date: Mon, 22 Apr 2024 10:30:15 -0400 Subject: [PATCH] Create Switch component --- .../src/components/switch/switch.stories.tsx | 43 +++++++------ packages/ui/src/components/switch/switch.tsx | 63 +++++++++++++++++++ 2 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 packages/ui/src/components/switch/switch.tsx diff --git a/packages/ui/src/components/switch/switch.stories.tsx b/packages/ui/src/components/switch/switch.stories.tsx index 3aa0e53e..28c05f4e 100644 --- a/packages/ui/src/components/switch/switch.stories.tsx +++ b/packages/ui/src/components/switch/switch.stories.tsx @@ -1,6 +1,7 @@ import type { Meta, StoryObj } from "@storybook/react"; -import { Form, Switch } from "react-aria-components"; +import { Form } from "react-aria-components"; import { useIntl } from "react-intl"; +import { Switch, SwitchButton, SwitchLabel, SwitchThumb } from "./switch"; const PreviewSwitch = (properties: { hasLabel: boolean; @@ -15,28 +16,26 @@ const PreviewSwitch = (properties: { return (
-
- + - - - - {hasLabel ? ( - - {intl.formatMessage({ - defaultMessage: "Receive notifications", - id: "D6jkwD", - })} - - ) : null} - -
+ + + {hasLabel ? ( + + {intl.formatMessage({ + defaultMessage: "Receive notifications", + id: "D6jkwD", + })} + + ) : null} +
); diff --git a/packages/ui/src/components/switch/switch.tsx b/packages/ui/src/components/switch/switch.tsx new file mode 100644 index 00000000..8f0f890b --- /dev/null +++ b/packages/ui/src/components/switch/switch.tsx @@ -0,0 +1,63 @@ +/* eslint-disable react/jsx-props-no-spreading */ +import React, { ComponentPropsWithoutRef, ElementRef, forwardRef, HTMLAttributes } from "react"; +import { Switch as SwitchPrimitive } from "react-aria-components"; +import { cx } from "@/helpers/cx"; + +export const Switch = forwardRef< + ElementRef, + ComponentPropsWithoutRef & { + className?: string | undefined; + isPrimary?: boolean | undefined; + } +>(({ className, isPrimary = false, ...properties }, reference) => { + const mergedClassName = cx("group inline-flex", className); + return ( + + ); +}); + +Switch.displayName = "Switch"; + +export const SwitchLabel = forwardRef< + HTMLSpanElement, + HTMLAttributes & { className?: string | undefined } +>(({ className, ...properties }, reference) => { + const mergedClassName = cx( + "ms-3 flex-1 text-sm font-medium leading-6 text-neutral-12 group-disabled:text-neutral-11", + className, + ); + return ; +}); + +SwitchLabel.displayName = "SwitchLabel"; + +export const SwitchButton = forwardRef< + HTMLSpanElement, + HTMLAttributes & { className?: string | undefined } +>(({ className, ...properties }, reference) => { + const mergedClassName = cx( + "group relative isolate inline-flex h-6 w-11 rounded-full border-2 border-transparent bg-neutral-a-3 transition duration-200 ease-in-out group-data-[is-primary=true]:bg-primary-9 group-focus-visible:outline group-focus-visible:outline-2 group-focus-visible:outline-primary-7 group-selected:bg-neutral-9 group-disabled:bg-neutral-a-3", + className, + ); + return ; +}); + +SwitchButton.displayName = "SwitchButton"; + +export const SwitchThumb = forwardRef< + HTMLSpanElement, + HTMLAttributes & { className?: string | undefined } +>(({ className, ...properties }, reference) => { + const mergedClassName = cx( + "pointer-events-none relative inline-block size-5 rounded-full bg-white shadow transition-all duration-200 ease-in-out will-change-transform translate-x-0 group-selected:translate-x-5 group-disabled:bg-neutral-2 group-disabled:shadow-none", + className, + ); + return ; +}); + +SwitchThumb.displayName = "SwitchThumb";