refactor(desktop): update radio and checkbox components for improved functionality

- Replaced Radio component with RadioGroupItem and RadioCard for better structure and styling.
- Introduced a new Checkbox component with enhanced state management and animations.
- Updated various files to utilize the new components, ensuring consistent behavior across the application.
- Removed deprecated Radio component and associated styles from the codebase.
This commit is contained in:
Innei
2025-04-30 15:29:48 +08:00
parent f48697c416
commit 2d67706046
14 changed files with 308 additions and 159 deletions
@@ -98,6 +98,7 @@ export const parseHtml = (
}),
input: ({ node, ...props }) => {
if (props.type === "checkbox") {
// @ts-expect-error
return createElement(Checkbox, {
...props,
disabled: false,
@@ -1,6 +1,6 @@
import { Button } from "@follow/components/ui/button/index.js"
import { Input } from "@follow/components/ui/input/index.js"
import { Radio, RadioGroup } from "@follow/components/ui/radio-group/index.js"
import { RadioGroup, RadioGroupItem } from "@follow/components/ui/radio-group/motion.js"
import {
Select,
SelectContent,
@@ -100,8 +100,8 @@ export const FeedFilter = ({ index }: { index: number }) => {
})
}}
>
<Radio disabled={disabled} label={t("actions.action_card.all")} value="all" />
<Radio
<RadioGroupItem disabled={disabled} label={t("actions.action_card.all")} value="all" />
<RadioGroupItem
disabled={disabled}
label={t("actions.action_card.custom_filters")}
value="filter"
@@ -333,7 +333,7 @@ function Step({ step, currentStep }: { step: number; currentStep: number }) {
inactive: {
backgroundColor: "var(--fo-background)",
borderColor: "hsl(var(--border) / 0.5)",
color: "hsl(var(--color-textTertiary))",
color: "#bbb",
},
active: {
backgroundColor: "var(--fo-background)",
@@ -1,4 +1,5 @@
import { Radio, RadioGroup } from "@follow/components/ui/radio-group/index.js"
import { RadioGroup } from "@follow/components/ui/radio-group/index.js"
import { RadioCard } from "@follow/components/ui/radio-group/RadioCard.js"
import { useState } from "react"
import { useTranslation } from "react-i18next"
@@ -43,26 +44,20 @@ export function BehaviorGuide() {
updateSettings(value as Behavior)
}}
>
<Radio
wrapperClassName="border rounded-lg p-3 has-[:checked]:bg-theme-accent has-[:checked]:text-white transition-colors"
<RadioCard
wrapperClassName="has-[:checked]:bg-theme-accent has-[:checked]:font-normal rounded-lg border p-3 transition-colors has-[:checked]:text-white"
label={t("new_user_guide.step.behavior.unread_question.option1")}
value="radical"
className="hidden"
labelClassName="pl-0"
/>
<Radio
wrapperClassName="border rounded-lg p-3 has-[:checked]:bg-theme-accent has-[:checked]:text-white transition-colors"
<RadioCard
wrapperClassName="has-[:checked]:bg-theme-accent has-[:checked]:font-normal rounded-lg border p-3 transition-colors has-[:checked]:text-white"
label={t("new_user_guide.step.behavior.unread_question.option2")}
value="balanced"
className="hidden"
labelClassName="pl-0"
/>
<Radio
wrapperClassName="border rounded-lg p-3 has-[:checked]:bg-theme-accent has-[:checked]:text-white transition-colors"
<RadioCard
wrapperClassName="has-[:checked]:bg-theme-accent has-[:checked]:font-normal rounded-lg border p-3 transition-colors has-[:checked]:text-white"
label={t("new_user_guide.step.behavior.unread_question.option3")}
value="conservative"
className="hidden"
labelClassName="pl-0"
/>
</RadioGroup>
</div>
@@ -11,7 +11,7 @@ import {
} from "@follow/components/ui/form/index.js"
import { Input } from "@follow/components/ui/input/Input.js"
import { Label } from "@follow/components/ui/label/index.jsx"
import { Radio, RadioGroup } from "@follow/components/ui/radio-group/index.js"
import { RadioGroup, RadioGroupItem } from "@follow/components/ui/radio-group/motion.js"
import { Slider } from "@follow/components/ui/slider/index.js"
import { ELECTRON_BUILD } from "@follow/shared/constants"
import { env } from "@follow/shared/env.desktop"
@@ -184,8 +184,11 @@ const ExportFeedsForm = () => {
}}
>
<div className="flex gap-4">
<Radio label={t("general.export.folder_mode.option.view")} value="view" />
<Radio
<RadioGroupItem
label={t("general.export.folder_mode.option.view")}
value="view"
/>
<RadioGroupItem
label={t("general.export.folder_mode.option.category")}
value="category"
/>
@@ -1,4 +1,5 @@
import { useDraggable } from "@dnd-kit/core"
import { Checkbox } from "@follow/components/ui/checkbox/index.js"
import { ScrollArea } from "@follow/components/ui/scroll-area/index.js"
import { cn, isKeyForMultiSelectPressed } from "@follow/utils/utils"
import { forwardRef, memo, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react"
@@ -229,6 +230,7 @@ const FeedListImpl = forwardRef<HTMLDivElement, { className?: string; view: numb
<SortByAlphabeticalInbox view={view} data={inboxesData} />
</>
)}
{(hasListData || hasInboxData) && (
<div
className={cn(
@@ -1,4 +1,5 @@
import { isMobile } from "@follow/components/hooks/useMobile.js"
import { Checkbox } from "@follow/components/ui/checkbox/index.js"
import { RootPortal } from "@follow/components/ui/portal/index.js"
import { views } from "@follow/constants"
import { stopPropagation } from "@follow/utils/dom"
@@ -60,60 +60,7 @@
--animation-input: 0;
}
}
/* Radio styling */
@layer components {
.radio {
flex-shrink: 0;
height: 1rem;
width: 1rem;
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border-radius: 9999px;
border-width: 1px;
border-color: var(--fallback-bc, theme(colors.theme.accent.DEFAULT));
--bc: theme(colors.background);
--fallback-bc: var(--bc);
--chkbg: var(--bc);
--fallback-b1: theme(colors.background);
}
.radio-current {
--chkbg: currentColor;
--fallback-bc: currentColor;
}
.radio-accent {
--chkbg: theme(colors.theme.accent.DEFAULT);
--fallback-bc: theme(colors.theme.accent.DEFAULT);
--fallback-b1: theme(colors.background);
}
.radio-sm {
@apply size-[0.9rem];
}
.radio-md {
@apply size-[1.2rem];
}
.radio:checked,
.radio[aria-checked="true"] {
--tw-bg-opacity: 1;
background-color: var(
--fallback-bc,
oklch(theme(colors.theme.accent.DEFAULT) / var(--tw-bg-opacity))
);
background-image: none;
animation: radiomark var(--animation-input, 0.2s) ease-out;
box-shadow:
0 0 0 4px var(--fallback-b1, oklch(var(--b1) / 1)) inset,
0 0 0 4px var(--fallback-b1, oklch(var(--b1) / 1)) inset;
}
@keyframes radiomark {
0% {
box-shadow:
+1 -1
View File
@@ -84,4 +84,4 @@
"@follow/configs": "workspace:*",
"@types/katex": "0.16.7"
}
}
}
@@ -1,22 +1,81 @@
"use client"
import { cn } from "@follow/utils/utils"
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
import type {HTMLMotionProps} from "motion/react";
import { m } from "motion/react"
import * as React from "react"
export const Checkbox = React.forwardRef<
HTMLInputElement,
React.ComponentPropsWithoutRef<"input"> & {
onCheckedChange?: (checked: boolean) => void
}
>(({ className, onCheckedChange, ...props }, ref) => (
<input
type="checkbox"
ref={ref}
className={cn("checkbox", className)}
{...props}
onChange={(event) => {
onCheckedChange?.(event.target.checked)
props.onChange?.(event)
}}
/>
))
type CheckboxProps = React.ComponentProps<typeof CheckboxPrimitive.Root> & HTMLMotionProps<"button">
function Checkbox({ className, onCheckedChange, ...props }: CheckboxProps) {
const [isChecked, setIsChecked] = React.useState(props?.checked ?? props?.defaultChecked ?? false)
React.useEffect(() => {
if (props?.checked !== undefined) setIsChecked(props.checked)
}, [props?.checked])
const handleCheckedChange = React.useCallback(
(checked: boolean) => {
setIsChecked(checked)
onCheckedChange?.(checked)
},
[onCheckedChange],
)
return (
<CheckboxPrimitive.Root {...props} onCheckedChange={handleCheckedChange} asChild>
<m.button
data-slot="checkbox"
className={cn(
"bg-material-opaque cursor-checkbox focus-visible:ring-border data-[state=checked]:bg-accent peer flex size-5 shrink-0 items-center justify-center rounded-sm transition-colors duration-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:text-white",
className,
)}
whileTap={{ scale: 0.95 }}
whileHover={{ scale: 1.05 }}
{...props}
>
<CheckboxPrimitive.Indicator forceMount asChild>
<m.svg
data-slot="checkbox-indicator"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth="3.5"
stroke="currentColor"
className="size-3.5"
initial="unchecked"
animate={isChecked ? "checked" : "unchecked"}
>
<m.path
strokeLinecap="round"
strokeLinejoin="round"
d="M4.5 12.75l6 6 9-13.5"
variants={{
checked: {
pathLength: 1,
opacity: 1,
transition: {
duration: 0.2,
delay: 0.2,
},
},
unchecked: {
pathLength: 0,
opacity: 0,
transition: {
duration: 0.2,
},
},
}}
/>
</m.svg>
</CheckboxPrimitive.Indicator>
</m.button>
</CheckboxPrimitive.Root>
)
}
Checkbox.displayName = CheckboxPrimitive.Root.displayName
export { Checkbox, type CheckboxProps }
@@ -1,45 +0,0 @@
import { cn } from "@follow/utils/utils"
import type { FC, ReactNode } from "react"
import { useId } from "react"
import { useEventCallback } from "usehooks-ts"
import { useRadioContext, useRadioGroupValue } from "./context"
export const Radio: FC<
React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> & {
label: ReactNode
wrapperClassName?: string
labelClassName?: string
}
> = (props) => {
const { id, label, className, wrapperClassName, labelClassName, value, onChange, ...rest } = props
const { onChange: ctxOnChange } = useRadioContext() || {}
const fallbackId = useId()
const ctxValue = useRadioGroupValue()
const handleChange: React.ChangeEventHandler<HTMLInputElement> = useEventCallback((e) => {
ctxOnChange?.(e.target.value)
onChange?.(e)
})
return (
<label className={cn("flex items-center", wrapperClassName)} htmlFor={id ?? fallbackId}>
<input
{...rest}
type="radio"
id={id ?? fallbackId}
className={cn(
"radio radio-accent radio-sm accent-accent disabled:radio-current disabled:text-theme-disabled disabled:cursor-not-allowed",
className,
)}
value={value}
checked={ctxValue === value}
onChange={handleChange}
/>
<span className={cn(rest.disabled ? "text-theme-disabled" : "", "pl-2", labelClassName)}>
{label}
</span>
</label>
)
}
@@ -1,3 +1,4 @@
export * from "./context"
export * from "./Radio"
export * as MotionRadio from "./motion"
export * from "./RadioCard"
export * from "./RadioGroup"
@@ -0,0 +1,99 @@
"use client"
import { Spring } from "@follow/components/constants/spring"
import { cn } from "@follow/utils/utils"
import * as RadioGroupPrimitive from "@radix-ui/react-radio-group"
import type { HTMLMotionProps, Transition } from "motion/react"
import { AnimatePresence, m } from "motion/react"
import * as React from "react"
type RadioGroupProps = React.ComponentProps<typeof RadioGroupPrimitive.Root> & {
transition?: Transition
}
function RadioGroup({ className, ...props }: RadioGroupProps) {
return (
<RadioGroupPrimitive.Root
data-slot="radio-group"
className={cn("grid gap-2.5", className)}
{...props}
/>
)
}
type RadioGroupIndicatorProps = React.ComponentProps<typeof RadioGroupPrimitive.Indicator> & {
transition: Transition
}
function RadioGroupIndicator({ className, transition, ...props }: RadioGroupIndicatorProps) {
return (
<RadioGroupPrimitive.Indicator
data-slot="radio-group-indicator"
asChild
className={cn("absolute inset-0 flex items-center justify-center", className)}
{...props}
>
<AnimatePresence>
<m.div
key="radio-group-indicator-circle"
data-slot="radio-group-indicator-circle"
initial={{ opacity: 0, scale: 0 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0 }}
transition={transition}
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
className="size-3 fill-current text-current"
>
<circle cx="12" cy="12" r="10" />
</svg>
</m.div>
</AnimatePresence>
</RadioGroupPrimitive.Indicator>
)
}
type RadioGroupItemProps = React.ComponentProps<typeof RadioGroupPrimitive.Item> &
HTMLMotionProps<"button"> & {
transition?: Transition
label?: string
}
function RadioGroupItem({
className,
transition = Spring.presets.smooth,
label,
...props
}: RadioGroupItemProps) {
const id = React.useId()
return (
<RadioGroupPrimitive.Item asChild {...props}>
<div className="flex items-center gap-2">
<m.button
type="button"
data-slot="radio-group-item"
className={cn(
"border-border text-accent ring-material-opaque focus-visible:ring-border flex aspect-square size-5 items-center justify-center rounded-full border focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className,
)}
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
{...props}
>
<RadioGroupIndicator data-slot="radio-group-item-indicator" transition={transition} />
</m.button>
{label && <label htmlFor={id}>{label}</label>}
</div>
</RadioGroupPrimitive.Item>
)
}
export { RadioGroup, RadioGroupItem, type RadioGroupItemProps, type RadioGroupProps }
@@ -1,24 +1,110 @@
"use client"
import { cn } from "@follow/utils/utils"
import * as SwitchPrimitives from "@radix-ui/react-switch"
import type { SwitchProps as SwitchPrimitiveProps } from "@headlessui/react"
import { Switch as SwitchPrimitive } from "@headlessui/react"
import type { HTMLMotionProps } from "motion/react"
import { m as motion } from "motion/react"
import * as React from "react"
export const Switch = React.forwardRef<
React.ElementRef<typeof SwitchPrimitives.Root>,
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>
>(({ className, ...props }, ref) => (
<SwitchPrimitives.Root
className={cn(
"cursor-switch focus-visible:ring-accent focus-visible:ring-offset-background peer inline-flex h-5 w-9 shrink-0 items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
"data-[state=checked]:bg-accent data-[state=unchecked]:bg-theme-inactive dark:data-[state=unchecked]:bg-theme-accent-100/10 duration-200",
"hover:data-[state=unchecked]:bg-theme-inactive/80 dark:hover:data-[state=unchecked]:bg-theme-accent-100/15",
"hover:data-[state=checked]:bg-accent/80 dark:hover:data-[state=checked]:bg-accent",
"group",
className,
)}
{...props}
ref={ref}
>
<SwitchPrimitives.Thumb className="bg-background pointer-events-none block size-4 rounded-full ring-0 transition-transform duration-200 data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0 group-hover:dark:data-[state=checked]:!bg-black/80" />
</SwitchPrimitives.Root>
))
Switch.displayName = SwitchPrimitives.Root.displayName
type SwitchProps<TTag extends React.ElementType = typeof motion.button> =
SwitchPrimitiveProps<TTag> &
Omit<HTMLMotionProps<"button">, "children"> & {
leftIcon?: React.ReactNode
rightIcon?: React.ReactNode
thumbIcon?: React.ReactNode
onCheckedChange?: (checked: boolean) => void
as?: TTag
}
function Switch({
className,
leftIcon,
rightIcon,
thumbIcon,
onChange,
onCheckedChange,
as = motion.button,
...props
}: SwitchProps) {
const [isChecked, setIsChecked] = React.useState(props.checked ?? props.defaultChecked ?? false)
const [isTapped, setIsTapped] = React.useState(false)
React.useEffect(() => {
setIsChecked(props.checked ?? props.defaultChecked ?? false)
}, [props.checked, props.defaultChecked])
const handleChange = React.useCallback(
(checked: boolean) => {
setIsChecked(checked)
onCheckedChange?.(checked)
onChange?.(checked)
},
[onCheckedChange, onChange],
)
return (
// @ts-expect-error
<SwitchPrimitive
data-slot="switch"
checked={isChecked}
onChange={handleChange}
className={cn(
"focus-visible:ring-border cursor-switch data-[checked]:bg-accent bg-material-opaque relative flex h-6 w-10 shrink-0 items-center justify-start rounded-full p-[3px] transition-colors duration-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[checked]:justify-end",
className,
)}
as={as}
whileTap="tap"
initial={false}
onTapStart={() => setIsTapped(true)}
onTapCancel={() => setIsTapped(false)}
onTap={() => setIsTapped(false)}
{...props}
>
{leftIcon && (
<motion.div
data-slot="switch-left-icon"
animate={isChecked ? { scale: 1, opacity: 1 } : { scale: 0, opacity: 0 }}
transition={{ type: "spring", bounce: 0 }}
className="absolute left-1 top-1/2 -translate-y-1/2 text-neutral-400 dark:text-neutral-500 [&_svg]:size-3"
>
{typeof leftIcon !== "string" ? leftIcon : null}
</motion.div>
)}
{rightIcon && (
<motion.div
data-slot="switch-right-icon"
animate={isChecked ? { scale: 0, opacity: 0 } : { scale: 1, opacity: 1 }}
transition={{ type: "spring", bounce: 0 }}
className="absolute right-1 top-1/2 -translate-y-1/2 text-neutral-500 dark:text-neutral-400 [&_svg]:size-3"
>
{typeof rightIcon !== "string" ? rightIcon : null}
</motion.div>
)}
<motion.span
data-slot="switch-thumb"
whileTap="tab"
className={cn(
"bg-background relative z-[1] flex items-center justify-center rounded-full text-neutral-500 shadow-lg ring-0 dark:text-neutral-400 [&_svg]:size-3",
)}
layout
transition={{ type: "spring", stiffness: 300, damping: 25 }}
style={{
width: 18,
height: 18,
}}
animate={
isTapped
? { width: 21, transition: { duration: 0.1 } }
: { width: 18, transition: { duration: 0.1 } }
}
>
{thumbIcon && typeof thumbIcon !== "string" ? thumbIcon : null}
</motion.span>
</SwitchPrimitive>
)
}
export { Switch, type SwitchProps }