@qq1948037137/adminsmart-ui 0.1.26 → 0.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -161,6 +161,7 @@ var Button = Object.assign(wrapAntComponent(import_antd.Button, "Button"), {
161
161
  // src/components/ThemeModeButton.tsx
162
162
  var import_icons = require("@ant-design/icons");
163
163
  var import_react2 = require("react");
164
+ var import_react_dom = require("react-dom");
164
165
 
165
166
  // src/components/overlay.ts
166
167
  var import_antd2 = require("antd");
@@ -171,14 +172,136 @@ var Tooltip = Object.assign(wrapAntComponent(import_antd2.Tooltip, "Tooltip"), {
171
172
 
172
173
  // src/components/ThemeModeButton.tsx
173
174
  var import_jsx_runtime = require("react/jsx-runtime");
175
+ var documentTransitions = /* @__PURE__ */ new WeakMap();
176
+ var transitionAttribute = "data-adminsmart-theme-transition";
177
+ var transitionStyles = `
178
+ /* \u53EA\u6539\u53D8\u672C\u7EC4\u4EF6\u6301\u6709\u7684\u539F\u751F\u9875\u9762\u5FEB\u7167\uFF0C\u4E0D\u7ED9\u4E1A\u52A1\u63A7\u4EF6\u65BD\u52A0\u5168\u5C40\u52A8\u753B\u3002 */
179
+ html[${transitionAttribute}]::view-transition-old(root),
180
+ html[${transitionAttribute}]::view-transition-new(root) { animation: none; mix-blend-mode: normal; }
181
+ html[${transitionAttribute}='dark']::view-transition-old(root),
182
+ html[${transitionAttribute}='light']::view-transition-new(root) { z-index: 1; }
183
+ html[${transitionAttribute}='dark']::view-transition-new(root),
184
+ html[${transitionAttribute}='light']::view-transition-old(root) { z-index: 2; }
185
+ `;
186
+ function reportThemeError(view, error) {
187
+ if (typeof view.reportError === "function") view.reportError(error);
188
+ else view.setTimeout(() => {
189
+ throw error;
190
+ }, 0);
191
+ }
192
+ function waitForThemeCommit(view) {
193
+ return new Promise((resolve) => {
194
+ let frame;
195
+ let timer;
196
+ const finish = () => {
197
+ if (frame !== void 0) view.cancelAnimationFrame?.(frame);
198
+ if (timer !== void 0) view.clearTimeout(timer);
199
+ resolve();
200
+ };
201
+ timer = view.setTimeout(finish, 100);
202
+ frame = view.requestAnimationFrame(finish);
203
+ });
204
+ }
174
205
  var ThemeModeButton = (0, import_react2.forwardRef)(
175
- function ThemeModeButton2({ mode, onModeChange, tooltipProps, onClick, ...props }, ref) {
206
+ function ThemeModeButton2({ mode, onModeChange, transition = "reveal", transitionDuration = 500, tooltipProps, onClick, ...props }, ref) {
176
207
  const nextMode = mode === "dark" ? "light" : "dark";
177
208
  const actionLabel = nextMode === "dark" ? "\u5207\u6362\u5230\u591C\u95F4\u4E3B\u9898" : "\u5207\u6362\u5230\u65E5\u95F4\u4E3B\u9898";
178
209
  const nextModeIcon = nextMode === "dark" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.MoonOutlined, { "aria-hidden": true }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.SunOutlined, { "aria-hidden": true });
210
+ const activeCleanup = (0, import_react2.useRef)(null);
211
+ const [transitioning, setTransitioning] = (0, import_react2.useState)(false);
212
+ (0, import_react2.useEffect)(() => () => activeCleanup.current?.(), []);
179
213
  const handleClick = (event) => {
180
214
  onClick?.(event);
181
- if (!event.defaultPrevented) onModeChange?.(nextMode);
215
+ if (event.defaultPrevented || !onModeChange || activeCleanup.current) return;
216
+ const ownerDocument = event.currentTarget.ownerDocument;
217
+ const view = ownerDocument.defaultView;
218
+ const root = ownerDocument.documentElement;
219
+ const duration = Number.isFinite(transitionDuration) ? transitionDuration : 500;
220
+ if (documentTransitions.has(ownerDocument)) return;
221
+ if (!view || transition === "none" || duration <= 0 || view.matchMedia?.("(prefers-reduced-motion: reduce)").matches || typeof ownerDocument.startViewTransition !== "function" || typeof root.animate !== "function") {
222
+ const result = onModeChange(nextMode);
223
+ if (view) void Promise.resolve(result).catch((error) => reportThemeError(view, error));
224
+ return;
225
+ }
226
+ const bounds = event.currentTarget.getBoundingClientRect();
227
+ const x = event.detail > 0 ? event.clientX : bounds.left + bounds.width / 2;
228
+ const y = event.detail > 0 ? event.clientY : bounds.top + bounds.height / 2;
229
+ const radius = Math.hypot(Math.max(x, view.innerWidth - x), Math.max(y, view.innerHeight - y));
230
+ const previousAttribute = root.getAttribute(transitionAttribute);
231
+ const style = ownerDocument.createElement("style");
232
+ let nativeTransition;
233
+ let animation;
234
+ let cancelled = false;
235
+ let applied = false;
236
+ const cleanup = () => {
237
+ if (cancelled) return;
238
+ cancelled = true;
239
+ animation?.cancel();
240
+ nativeTransition?.skipTransition();
241
+ style.remove();
242
+ if (documentTransitions.get(ownerDocument) === cleanup) {
243
+ documentTransitions.delete(ownerDocument);
244
+ if (previousAttribute === null) root.removeAttribute(transitionAttribute);
245
+ else root.setAttribute(transitionAttribute, previousAttribute);
246
+ }
247
+ if (activeCleanup.current === cleanup) {
248
+ activeCleanup.current = null;
249
+ setTransitioning(false);
250
+ }
251
+ };
252
+ activeCleanup.current = cleanup;
253
+ documentTransitions.set(ownerDocument, cleanup);
254
+ setTransitioning(true);
255
+ style.textContent = transitionStyles;
256
+ style.dataset.adminsmartThemeTransition = "";
257
+ ownerDocument.head.appendChild(style);
258
+ root.setAttribute(transitionAttribute, nextMode);
259
+ try {
260
+ nativeTransition = ownerDocument.startViewTransition(async () => {
261
+ if (cancelled) return;
262
+ applied = true;
263
+ let result = void 0;
264
+ try {
265
+ (0, import_react_dom.flushSync)(() => {
266
+ result = onModeChange(nextMode);
267
+ });
268
+ await result;
269
+ if (!cancelled && typeof view.requestAnimationFrame === "function") {
270
+ await waitForThemeCommit(view);
271
+ }
272
+ } catch (error) {
273
+ reportThemeError(view, error);
274
+ throw error;
275
+ }
276
+ });
277
+ void nativeTransition.updateCallbackDone.catch(() => void 0);
278
+ void nativeTransition.ready.then(() => {
279
+ if (cancelled) return;
280
+ const clipPath = [`circle(0px at ${x}px ${y}px)`, `circle(${radius}px at ${x}px ${y}px)`];
281
+ animation = root.animate({ clipPath: nextMode === "dark" ? clipPath : clipPath.reverse() }, {
282
+ duration,
283
+ // 宿主可调整节奏,不替换主题算法。
284
+ easing: "ease-in",
285
+ // 与 Ant 官网圆形过渡保持同类节奏。
286
+ pseudoElement: nextMode === "dark" ? "::view-transition-new(root)" : "::view-transition-old(root)"
287
+ // 只处理深色快照。
288
+ });
289
+ return animation.finished;
290
+ }).catch(() => {
291
+ if (!cancelled && !applied) {
292
+ applied = true;
293
+ const result = onModeChange(nextMode);
294
+ void Promise.resolve(result).catch((error) => reportThemeError(view, error));
295
+ }
296
+ }).finally(cleanup).catch((error) => reportThemeError(view, error));
297
+ void nativeTransition.finished.catch(() => void 0).finally(cleanup);
298
+ } catch {
299
+ cleanup();
300
+ if (!applied) {
301
+ const result = onModeChange(nextMode);
302
+ void Promise.resolve(result).catch((error) => reportThemeError(view, error));
303
+ }
304
+ }
182
305
  };
183
306
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Tooltip, { title: actionLabel, ...tooltipProps, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
184
307
  Button,
@@ -188,8 +311,11 @@ var ThemeModeButton = (0, import_react2.forwardRef)(
188
311
  icon: nextModeIcon,
189
312
  "aria-label": actionLabel,
190
313
  "aria-pressed": mode === "dark",
314
+ "aria-busy": transitioning || void 0,
191
315
  ...props,
316
+ style: { minWidth: 44, minHeight: 44, ...props.style },
192
317
  ref,
318
+ disabled: transitioning ? true : props.disabled,
193
319
  onClick: handleClick
194
320
  }
195
321
  ) });