Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions web/src/components/home-edit-board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ import type { LinkStyle, SiteCategory, SiteLink, TileSize } from "@/lib/types";
* `rectSortingStrategy` 只是按实测矩形做位移预览,砖全部等尺寸反而更准;
* `closestCorners` 也不依赖网格。
* 空分类的落点就是那块固定尺寸的「+」砖,命中面积随砖确定。
*
* 抖动(iOS 主屏式)分两层实现,互不打架:dnd-kit 的拖拽位移 `transform` 留在
* 外层 `<div>`(`setNodeRef` 那层),抖动的 `rotate` 挂在内层砖壳上(`TILE_JIGGLE_CLASS`
* + `tileJiggleStyle` 给的相位/周期)。正在拖的那块去掉动画类停抖,其余照抖;
* `DragOverlay` 的预览砖不抖。旋转不改布局盒,所以外层 div 的实测矩形不变,
* `rectSortingStrategy` / `closestCorners` 的输入稳定。
*/

/** 分类标题行投放目标(落到该分类末尾)的 id 前缀 */
Expand Down Expand Up @@ -98,6 +104,41 @@ const PLACEHOLDER_CLASS: Partial<Record<LinkStyle, string>> = {
wide: "min-h-[82px]",
};

/**
* 砖块抖动的工具类(Tailwind 从 `index.css` 的 `--animate-nav-tile-jiggle` 生成)。
* 必须是完整字面量:Tailwind 只扫源码正文,拼出来的类名不会被生成。
*/
export const TILE_JIGGLE_CLASS = "animate-nav-tile-jiggle";

/**
* 单程摆动的三档时长(ms):配 `alternate` 就是完整往返 320 / 360 / 400ms
* (规格 0.3–0.4s)。逐砖取其中一档,避免同周期同相位的砖在视觉上锁成一条线。
*/
const JIGGLE_SWING_MS = [160, 180, 200] as const;

/** 相位打散的窗口(ms):负延迟取 0–399,足够覆盖任何一个抖动周期 */
const JIGGLE_PHASE_MS = 400;

/**
* 一块砖的抖动相位与周期(内联 CSS,不用 JS 逐帧驱动)。
*
* 用 key(链接 id / `add:<分类 id>`)的 FNV-1a 32 位散列取延迟与时长:同一块砖
* 每次渲染算出来都一样(重渲染不会把动画拽回起点,拖拽结束后也回到自己的相位),
* 相邻砖的延迟又天然错开。负延迟 = 动画一开始就已经跑在周期中途。
*/
export function tileJiggleStyle(key: string): React.CSSProperties {
let hash = 2166136261;
for (let i = 0; i < key.length; i += 1) {
hash ^= key.charCodeAt(i);
hash = Math.imul(hash, 16777619);
}
const h = hash >>> 0;
return {
animationDelay: `-${h % JIGGLE_PHASE_MS}ms`,
animationDuration: `${JIGGLE_SWING_MS[h % JIGGLE_SWING_MS.length]}ms`,
};
}

type Order = Record<string, string[]>;

function containerOf(order: Order, id: string): string | null {
Expand Down Expand Up @@ -224,13 +265,17 @@ function EditableTile({
{...listeners}
aria-label={link.title}
title={link.title}
// 抖动挂在内层砖壳:拖拽位移的 transform 在外层 div(分层见文件头注释),
// 拖起中这块停抖,松手后按自己的相位继续
style={isDragging ? undefined : tileJiggleStyle(link.id)}
className={cn(
linkTileClassName(linkStyle, tileSize),
// compact / wide 仍在网格里,砖壳要撑满格子(<button> 的 width:auto 会按
// 内容收缩,不补 w-full 的话长名称的砖会比旁边的窄);classic 砖宽固定,
// 不能补 w-full,否则宽度被容器抢走
linkStyle !== "classic" && "w-full min-w-0",
"cursor-grab select-none active:cursor-grabbing",
!isDragging && TILE_JIGGLE_CLASS,
)}
onClick={() => onEdit(link.id)}
>
Expand Down Expand Up @@ -310,12 +355,15 @@ function AddLinkTile({
aria-label={`向「${category.name}」新增链接`}
title={`向「${category.name}」新增链接`}
onClick={() => onAdd(category.id)}
// 这块就是「+」砖,空分类时兼任落点:它自己就是抖动的那个元素,不另包一层
style={tileJiggleStyle(`add:${category.id}`)}
className={cn(
linkTileClassName(linkStyle, tileSize),
PLACEHOLDER_CLASS[linkStyle],
// 同上一块砖:compact / wide 撑满格子,classic 保持砖宽固定
linkStyle !== "classic" && "w-full min-w-0",
"items-center justify-center border border-dashed border-white/40 bg-transparent text-white/80 hover:bg-white/5 hover:text-white",
TILE_JIGGLE_CLASS,
isOver && "bg-white/10",
)}
>
Expand Down Expand Up @@ -517,6 +565,7 @@ export function HomeEditBoard({
</div>
</FadeIn>
<DragOverlay>
{/* 拖拽预览:刻意不加抖动类(被拖起的那块整体停抖) */}
{activeLink ? (
<div
className={cn(
Expand Down
30 changes: 30 additions & 0 deletions web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@
--radius-4xl: calc(var(--radius) * 2.6);
}

/*
* 编辑模式砖块的 iOS 式抖动(home-edit-board.tsx 用 `animate-nav-tile-jiggle`):
* 绕砖自身中心摆动 ±1.2°,单程 0.18s —— 配 `alternate` 就是完整往返 0.36s,
* `ease-in-out` 让两端自然减速,得到钟摆式的手感。每块砖的相位与周期由内联
* `animation-delay` / `animation-duration` 打散(见 `tileJiggleStyle`),所以不会
* 整齐划一地同相位摆。
*
* 关键帧只动单一变换属性 `rotate`,不写 `transform: rotate(...)`:砖壳的
* `.glass-hover:hover` 用 `transform: translateY(-1px)` 做上浮、dnd-kit 的拖拽
* 位移又挂在它的父层,三者互不覆盖。rotate 同样走合成层,不引起任何布局位移。
*/
@theme {
--animate-nav-tile-jiggle: nav-tile-jiggle 0.18s ease-in-out infinite
alternate;

@keyframes nav-tile-jiggle {
from {
rotate: -1.2deg;
}
to {
rotate: 1.2deg;
}
}
}

:root {
--background: oklch(1 0 0);
--foreground: oklch(0.145 0 0);
Expand Down Expand Up @@ -215,4 +240,9 @@
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}

/* 系统要求减少动态效果:编辑模式的砖块不抖,其余行为不变 */
.animate-nav-tile-jiggle {
animation: none;
}
}
32 changes: 32 additions & 0 deletions web/test/public-edit-mode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,38 @@ describe("进入 / 退出编辑模式", () => {
expect(screen.queryByRole("button", { name: "删除 GitHub" })).toBeNull();
});

it("编辑模式下链接砖与「+」砖都抖、相位各自错开;退出后一个都不留", async () => {
const user = userEvent.setup();
const { container } = renderPage();
await enterEdit(user);

const tiles = [
screen.getByRole("button", { name: "GitHub" }),
screen.getByRole("button", { name: "MDN" }),
screen.getByRole("button", { name: "向「开发」新增链接" }),
// 「空」分类只有这块「+」砖:它空/非空都照抖
screen.getByRole("button", { name: "向「空」新增链接" }),
];
const delays = new Set<string>();
for (const tile of tiles) {
expect(tile).toHaveClass("animate-nav-tile-jiggle");
expect(tile.style.animationDelay).toMatch(/^-\d+ms$/);
expect(["160ms", "180ms", "200ms"]).toContain(
tile.style.animationDuration,
);
delays.add(tile.style.animationDelay);
}
// 不得整齐划一地同相位摆动
expect(delays.size).toBeGreaterThan(1);

await user.click(screen.getByRole("button", { name: "完成编辑" }));

// 普通模式(外跳链接)一个抖动类都不带
expect(container.querySelectorAll(".animate-nav-tile-jiggle")).toHaveLength(
0,
);
});

it("Esc 退出编辑模式", async () => {
const user = userEvent.setup();
renderPage();
Expand Down
52 changes: 52 additions & 0 deletions web/test/tile-jiggle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, it } from "vitest";
import {
TILE_JIGGLE_CLASS,
tileJiggleStyle,
} from "@/components/home-edit-board";

/**
* 编辑模式砖块抖动的相位/周期(home-edit-board.tsx 的 `tileJiggleStyle`)。
*
* 规格:每块砖的相位/周期要错开(不得整齐划一同相位摆动),但同一块砖必须稳定
* —— 用 id 的散列而不是 Math.random,重渲染才不会把动画拽回起点。
*/

/** 与实现同口径的六个 key:三个链接 id + 三块「+」砖(含空分类那块) */
const KEYS = ["l1", "l2", "l3", "add:c-dev", "add:c-empty", "add:c-work"];

/** `-180ms` → 180(毫秒数,延迟本身取负) */
function delayMs(style: { animationDelay?: string | number }): number {
const raw = String(style.animationDelay);
expect(raw.startsWith("-")).toBe(true);
expect(raw.endsWith("ms")).toBe(true);
return Number(raw.slice(1, -2));
}

describe("tileJiggleStyle:每块砖的抖动相位与周期", () => {
it("同一个 key 每次算出来都一样(重渲染不重启动画)", () => {
for (const key of KEYS) {
expect(tileJiggleStyle(key)).toEqual(tileJiggleStyle(key));
}
});

it("延迟是负值、落在 0–399ms;时长只取 160 / 180 / 200 三档", () => {
for (const key of KEYS) {
const style = tileJiggleStyle(key);
const delay = delayMs(style);
expect(delay).toBeGreaterThanOrEqual(0);
expect(delay).toBeLessThan(400);
expect(["160ms", "180ms", "200ms"]).toContain(
String(style.animationDuration),
);
}
});

it("六块砖的相位错开:延迟去重后至少三个值", () => {
const delays = new Set(KEYS.map((key) => delayMs(tileJiggleStyle(key))));
expect(delays.size).toBeGreaterThanOrEqual(3);
});

it("工具类是完整字面量(Tailwind 只扫字面量)", () => {
expect(TILE_JIGGLE_CLASS).toBe("animate-nav-tile-jiggle");
});
});
Loading