refactor: migrate spring constants to centralized module

- Removed the spring.ts file and replaced direct imports with centralized Spring presets in various components.
- Updated transition properties in multiple files to utilize the new Spring module for consistency and maintainability.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-04-23 15:45:18 +08:00
parent a41b695e75
commit ec6bc963a7
16 changed files with 153 additions and 135 deletions
@@ -1,101 +0,0 @@
import type { Spring } from "motion/react"
export const reboundPreset: Spring = {
type: "spring",
bounce: 10,
stiffness: 140,
damping: 8,
}
export const microDampingPreset: Spring = {
type: "spring",
damping: 24,
}
export const microReboundPreset: Spring = {
type: "spring",
stiffness: 300,
damping: 20,
}
export const softSpringPreset: Spring = {
duration: 0.35,
type: "spring",
stiffness: 120,
damping: 20,
}
export const softBouncePreset: Spring = {
type: "spring",
damping: 10,
stiffness: 100,
}
/**
* A smooth spring with a predefined duration and no bounce.
*/
export const smoothPreset: Spring = {
type: "spring",
duration: 0.5,
bounce: 0,
}
/**
* A smooth spring with a predefined duration and no bounce that can be tuned.
*
* @param duration The perceptual duration, which defines the pace of the spring.
* @param extraBounce How much additional bounce should be added to the base bounce of 0.
*/
export function smooth(duration = 0.5, extraBounce = 0): Spring {
return {
type: "spring",
duration,
bounce: extraBounce,
}
}
/**
* A spring with a predefined duration and small amount of bounce that feels more snappy.
*/
export const snappyPreset: Spring = {
type: "spring",
duration: 0.5,
bounce: 0.15,
}
/**
* A spring with a predefined duration and small amount of bounce that feels more snappy and can be tuned.
*
* @param duration The perceptual duration, which defines the pace of the spring.
* @param extraBounce How much additional bounciness should be added to the base bounce of 0.15.
*/
export function snappy(duration = 0.5, extraBounce = 0): Spring {
return {
type: "spring",
duration,
bounce: 0.15 + extraBounce,
}
}
/**
* A spring with a predefined duration and higher amount of bounce.
*/
export const bouncyPreset: Spring = {
type: "spring",
duration: 0.5,
bounce: 0.3,
}
/**
* A spring with a predefined duration and higher amount of bounce that can be tuned.
*
* @param duration The perceptual duration, which defines the pace of the spring.
* @param extraBounce How much additional bounce should be added to the base bounce of 0.3.
*/
export function bouncy(duration = 0.5, extraBounce = 0): Spring {
return {
type: "spring",
duration,
bounce: 0.3 + extraBounce,
}
}
@@ -1,4 +1,5 @@
import { Focusable } from "@follow/components/common/Focusable.js"
import { Spring } from "@follow/components/constants/spring.js"
import { ActionButton, MotionButtonBase } from "@follow/components/ui/button/index.js"
import type { HTMLMediaState } from "@follow/hooks"
import { useRefValue, useVideo } from "@follow/hooks"
@@ -25,7 +26,6 @@ import { useEventCallback } from "usehooks-ts"
import { AudioPlayer } from "~/atoms/player"
import { IconScaleTransition } from "~/components/ux/transition/icon"
import { softSpringPreset } from "../constants/spring"
import { VolumeSlider } from "./VolumeSlider"
type VideoPlayerProps = {
@@ -63,8 +63,8 @@ export const VideoPlayer = forwardRef<VideoPlayerRef, VideoPlayerProps>(
const isPlayer = variant === "player"
const [clickToStatus, setClickToStatus] = useState(null as "play" | "pause" | null)
const scaleValue = useSpring(1, softSpringPreset)
const opacityValue = useSpring(0, softSpringPreset)
const scaleValue = useSpring(1, Spring.presets.softSpring)
const opacityValue = useSpring(0, Spring.presets.softSpring)
const handleClick = useEventCallback((e?: any) => {
if (!isPlayer) return
e?.stopPropagation()
@@ -1,3 +1,4 @@
import { Spring } from "@follow/components/constants/spring.js"
import { ActionButton, MotionButtonBase } from "@follow/components/ui/button/index.js"
import { RootPortal } from "@follow/components/ui/portal/index.js"
import { useMeasure } from "@follow/hooks"
@@ -19,7 +20,6 @@ import { COPY_MAP } from "~/constants"
import { tipcClient } from "~/lib/client"
import { replaceImgUrlIfNeed } from "~/lib/img-proxy"
import { microReboundPreset } from "../constants/spring"
import { FixedModalCloseButton } from "../modal/components/close"
import { useCurrentModal } from "../modal/stacked/hooks"
import { VideoPlayer } from "./VideoPlayer"
@@ -81,7 +81,7 @@ const Wrapper: Component<{
initial={{ scale: 0.94, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.94, opacity: 0 }}
transition={microReboundPreset}
transition={Spring.presets.microRebound}
>
<div
className={cn(
@@ -1,7 +1,6 @@
import { Spring } from "@follow/components/constants/spring.js"
import type { MotionProps, Target } from "motion/react"
import { microReboundPreset } from "../../constants/spring"
const enterStyle: Target = {
scale: 1,
opacity: 1,
@@ -21,7 +20,7 @@ export const modalMontionConfig = {
type: "tween",
},
},
transition: microReboundPreset,
transition: Spring.presets.microRebound,
} satisfies MotionProps
// Radix context menu z-index 999
@@ -1,11 +1,10 @@
import { Spring } from "@follow/components/constants/spring.js"
import { cn } from "@follow/utils/utils"
import type { Spring, Target } from "motion/react"
import type { Target, Transition } from "motion/react"
import { AnimatePresence, m } from "motion/react"
import * as React from "react"
import { cloneElement, useEffect, useState } from "react"
import { smoothPreset } from "~/components/ui/constants/spring"
type TransitionType = {
initial: Target | boolean
animate: Target
@@ -88,7 +87,7 @@ const Presets = {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: smoothPreset,
transition: Spring.presets.smooth,
},
}
export const IconTransition = (
@@ -97,7 +96,7 @@ export const IconTransition = (
initial?: Target
animate?: Target
exit?: Target
transition?: Spring
transition?: Transition
preset?: "fade"
}>,
@@ -1,3 +1,4 @@
import { Spring } from "@follow/components/constants/spring.js"
import { useMobile } from "@follow/components/hooks/useMobile.js"
import { Folo } from "@follow/components/icons/folo.js"
import { Logo } from "@follow/components/icons/logo.js"
@@ -10,6 +11,7 @@ import {
TooltipTrigger,
} from "@follow/components/ui/tooltip/index.js"
import type { LoginRuntime } from "@follow/shared/auth"
import { stopPropagation } from "@follow/utils/dom"
import { clsx } from "@follow/utils/utils"
import { m } from "motion/react"
import { useTranslation } from "react-i18next"
@@ -104,10 +106,14 @@ export const LoginModalContent = (props: LoginModalContentProps) => {
<m.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 10, transition: { type: "tween" } }}
transition={{ type: "spring" }}
exit={{ opacity: 0, y: 10 }}
transition={Spring.presets.snappy}
>
<div className="bg-background w-[25rem] rounded-xl border p-3 px-8 shadow-2xl shadow-stone-300 dark:border-neutral-700 dark:shadow-stone-800">
<div
onClick={stopPropagation}
tabIndex={-1}
className="bg-background w-[25rem] rounded-xl border p-3 px-8 shadow-2xl shadow-stone-300 dark:border-neutral-700 dark:shadow-stone-800"
>
{Inner}
</div>
</m.div>
@@ -1,10 +1,10 @@
import { Spring } from "@follow/components/constants/spring.js"
import { Button } from "@follow/components/ui/button/index.js"
import { LoadingWithIcon } from "@follow/components/ui/loading/index.jsx"
import { from } from "dnum"
import { AnimatePresence, m } from "motion/react"
import { useCallback, useState } from "react"
import { softSpringPreset } from "~/components/ui/constants/spring"
import { useCurrentModal } from "~/components/ui/modal/stacked/hooks"
import { useI18n } from "~/hooks/common"
import { useBoostFeedMutation, useBoostStatusQuery } from "~/modules/boost/query"
@@ -84,7 +84,11 @@ export const BoostModalContent = ({ feedId }: { feedId: string }) => {
{boostFeedMutation.isSuccess ? (
<>
<m.p initial={{ opacity: 0 }} animate={{ opacity: 1 }} transition={softSpringPreset}>
<m.p
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={Spring.presets.softSpring}
>
{t("boost.boost_success_thanks")}
</m.p>
<Button variant="primary" onClick={() => dismiss()}>
@@ -1,10 +1,9 @@
import { Spring } from "@follow/components/constants/spring.js"
import { RadioCard } from "@follow/components/ui/radio-group/RadioCard.js"
import { RadioGroup } from "@follow/components/ui/radio-group/RadioGroup.js"
import dayjs from "dayjs"
import { m } from "motion/react"
import { softSpringPreset } from "~/components/ui/constants/spring"
const radios = [
{
value: 1,
@@ -36,7 +35,7 @@ export const RadioCards = ({
initial={{ height: "auto", opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={softSpringPreset}
transition={Spring.presets.softSpring}
>
{radios.map((item) => (
<RadioCard
@@ -1,3 +1,4 @@
import { Spring } from "@follow/components/constants/spring.js"
import { useMobile } from "@follow/components/hooks/useMobile.js"
import { Button } from "@follow/components/ui/button/index.js"
import { Card, CardContent, CardFooter, CardHeader } from "@follow/components/ui/card/index.jsx"
@@ -25,7 +26,6 @@ import { useForm } from "react-hook-form"
import { useTranslation } from "react-i18next"
import { z } from "zod"
import { smoothPreset } from "~/components/ui/constants/spring"
import { Media } from "~/components/ui/media"
import { useModalStack } from "~/components/ui/modal/stacked/hooks"
import { useFollow } from "~/hooks/biz/useFollow"
@@ -274,7 +274,7 @@ export function DiscoverForm({ type = "search" }: { type?: string }) {
x: field.value === "lists" ? "calc(100% + 2px)" : 0,
width: "calc(50% - 4px)",
}}
transition={smoothPreset}
transition={Spring.presets.smooth}
/>
<button
type="button"
@@ -1,10 +1,10 @@
import { Spring } from "@follow/components/constants/spring.js"
import { IN_ELECTRON } from "@follow/shared/constants"
import { AnimatePresence } from "motion/react"
import { useEffect, useRef, useState } from "react"
import { useShowSourceContent } from "~/atoms/source-content"
import { m } from "~/components/common/Motion"
import { softSpringPreset } from "~/components/ui/constants/spring"
import { EntryContentLoading } from "../loading"
@@ -47,7 +47,7 @@ export const SourceContentView = ({ src }: { src: string }) => {
className="size-full"
initial={{ opacity: 0 }}
animate={{ opacity: loading ? 0 : 1 }}
transition={softSpringPreset}
transition={Spring.presets.softSpring}
>
<ViewTag
ref={webviewRef}
@@ -74,7 +74,7 @@ export const SourceContentPanel = ({ src }: { src: string | null }) => {
animate="visible"
exit="exit"
variants={variants}
transition={softSpringPreset}
transition={Spring.presets.softSpring}
>
<SourceContentView src={src} />
</m.div>
@@ -2,6 +2,7 @@
* @see https://github.com/toeverything/AFFiNE/blob/98e35384a6f71bf64c668b8f13afcaf28c9b8e97/packages/frontend/core/src/modules/find-in-page/view/find-in-page-modal.tsx
* @copyright AFFiNE, Folo
*/
import { Spring } from "@follow/components/constants/spring.js"
import { RootPortal } from "@follow/components/ui/portal/index.jsx"
import { useInputComposition, useRefValue } from "@follow/hooks"
import { useSubscribeElectronEvent } from "@follow/shared/event"
@@ -11,7 +12,6 @@ import type { FC } from "react"
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react"
import { useDebounceCallback, useEventCallback } from "usehooks-ts"
import { softSpringPreset } from "~/components/ui/constants/spring"
import { tipcClient } from "~/lib/client"
import { observeResize } from "~/lib/observe-resize"
@@ -258,7 +258,7 @@ export const CmdF = () => {
initial={{ opacity: 0.8, y: -150 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -150 }}
transition={softSpringPreset}
transition={Spring.presets.softSpring}
>
<CmdFImpl
onClose={() => {
@@ -1,4 +1,5 @@
import { useFocusable } from "@follow/components/common/Focusable.js"
import { Spring } from "@follow/components/constants/spring.js"
import { useMobile } from "@follow/components/hooks/useMobile.js"
import { Tooltip, TooltipContent, TooltipTrigger } from "@follow/components/ui/tooltip/index.jsx"
import { FeedViewType } from "@follow/constants"
@@ -18,7 +19,6 @@ import {
useAudioPlayerAtomSelector,
useAudioPlayerAtomValue,
} from "~/atoms/player"
import { microReboundPreset } from "~/components/ui/constants/spring"
import { VolumeSlider } from "~/components/ui/media/VolumeSlider"
import { HotKeyScopeMap } from "~/constants"
import type { NavigateEntryOptions } from "~/hooks/biz/useNavigateEntry"
@@ -59,7 +59,7 @@ export const CornerPlayer = ({ className, ...rest }: ControlButtonProps) => {
initial={{ y: 50, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
exit={{ y: 50, opacity: 0 }}
transition={{ ...microReboundPreset, duration: 0.2 }}
transition={{ ...Spring.presets.microRebound, duration: 0.2 }}
onClick={(e) => e.stopPropagation()}
>
<CornerPlayerImpl {...rest} />
@@ -1,9 +1,9 @@
import { Spring } from "@follow/components/constants/spring.js"
import { m } from "motion/react"
import { useCallback } from "react"
import { useTranslation } from "react-i18next"
import { getUpdaterStatus, setUpdaterStatus, useUpdaterStatus } from "~/atoms/updater"
import { softBouncePreset } from "~/components/ui/constants/spring"
export const UpdateNotice = () => {
const updaterStatus = useUpdaterStatus()
@@ -32,7 +32,7 @@ export const UpdateNotice = () => {
onClick={handleClick}
initial={{ y: 50, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={softBouncePreset}
transition={Spring.presets.softBounce}
>
<div className="font-medium">{t("notify.update_info", { app_name: APP_NAME })}</div>
<div className="text-xs text-zinc-500">{t("notify.update_info_3")}</div>
@@ -1,3 +1,4 @@
import { Spring } from "@follow/components/constants/spring.js"
import { tracker } from "@follow/tracker"
import { cn } from "@follow/utils/utils"
import { m, useMotionTemplate, useMotionValue } from "motion/react"
@@ -6,7 +7,6 @@ import { useTranslation } from "react-i18next"
import { useAudioPlayerAtomSelector } from "~/atoms/player"
import { getUpdaterStatus, setUpdaterStatus, useUpdaterStatus } from "~/atoms/updater"
import { softBouncePreset } from "~/components/ui/constants/spring"
import { tipcClient } from "~/lib/client"
import { handlers } from "~/tipc"
@@ -76,7 +76,7 @@ export const UpdateNotice = () => {
onClick={handleClick}
initial={{ y: 50, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={softBouncePreset}
transition={Spring.presets.softBounce}
>
<m.div
layout
+1
View File
@@ -6,6 +6,7 @@
"./assets/*": "./assets/*",
"./atoms/*": "./src/atoms/*",
"./common/*": "./src/common/*",
"./constants/*": "./src/constants/*",
"./dayjs": "./src/utils/dayjs.ts",
"./hooks/*": "./src/hooks/*",
"./icons/*": "./src/icons/*",
+111
View File
@@ -0,0 +1,111 @@
import type { Spring } from "motion/react"
const reboundPreset: Spring = {
type: "spring",
bounce: 10,
stiffness: 140,
damping: 8,
}
const microDampingPreset: Spring = {
type: "spring",
damping: 24,
}
const microReboundPreset: Spring = {
type: "spring",
stiffness: 300,
damping: 20,
}
const softSpringPreset: Spring = {
duration: 0.35,
type: "spring",
stiffness: 120,
damping: 20,
}
const softBouncePreset: Spring = {
type: "spring",
damping: 10,
stiffness: 100,
}
/**
* A smooth spring with a predefined duration and no bounce.
*/
const smoothPreset: Spring = {
type: "spring",
duration: 0.5,
bounce: 0,
}
/**
* A spring with a predefined duration and small amount of bounce that feels more snappy.
*/
const snappyPreset: Spring = {
type: "spring",
duration: 0.5,
bounce: 0.15,
}
/**
* A spring with a predefined duration and higher amount of bounce.
*/
const bouncyPreset: Spring = {
type: "spring",
duration: 0.5,
bounce: 0.3,
}
class SpringPresets {
rebound = reboundPreset
microDamping = microDampingPreset
microRebound = microReboundPreset
softSpring = softSpringPreset
softBounce = softBouncePreset
smooth = smoothPreset
snappy = snappyPreset
bouncy = bouncyPreset
}
class SpringStatic {
presets = new SpringPresets()
/**
* A smooth spring with a predefined duration and no bounce that can be tuned.
*
* @param duration The perceptual duration, which defines the pace of the spring.
* @param extraBounce How much additional bounce should be added to the base bounce of 0.
*/
smooth(duration = 0.5, extraBounce = 0): Spring {
return {
type: "spring",
duration,
bounce: extraBounce,
}
}
/**
* A spring with a predefined duration and small amount of bounce that feels more snappy.
*/
snappy(duration = 0.5, extraBounce = 0): Spring {
return {
type: "spring",
duration,
bounce: 0.15 + extraBounce,
}
}
/**
* A spring with a predefined duration and higher amount of bounce that can be tuned.
*/
bouncy(duration = 0.5, extraBounce = 0): Spring {
return {
type: "spring",
duration,
bounce: 0.3 + extraBounce,
}
}
}
const SpringClass = new SpringStatic()
export { SpringClass as Spring }