@ai-matrx/canvas 0.2.1 → 0.3.0

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/react.cjs CHANGED
@@ -57,6 +57,7 @@ __export(react_exports, {
57
57
  useCanvasHostPorts: () => useCanvasHostPorts,
58
58
  useCanvasIsPresented: () => useCanvasIsPresented,
59
59
  useCanvasKind: () => useCanvasKind,
60
+ useCanvasKindToggle: () => useCanvasKindToggle,
60
61
  useCanvasKinds: () => useCanvasKinds,
61
62
  useCanvasNativeBodies: () => useCanvasNativeBodies,
62
63
  useCanvasState: () => useCanvasState,
@@ -1278,6 +1279,44 @@ function selectCanvasWindowItemCount(s, windowId) {
1278
1279
  }
1279
1280
  var NO_WINDOWS = Object.freeze({});
1280
1281
 
1282
+ // src/core/toggleKind.ts
1283
+ var CANVAS_DEFAULT_KEY = "default";
1284
+ function selectCanvasKindVisibility(state, kind, key = CANVAS_DEFAULT_KEY) {
1285
+ const itemId = canvasItemId(kind, key);
1286
+ if (!state.items[itemId]) return "absent";
1287
+ const windowId = canvasWindowOfItem(state, itemId);
1288
+ if (!windowId) return "background";
1289
+ const isOpen = canvasWindowFrame(state, windowId)?.isOpen ?? false;
1290
+ const pane = Object.values(state.panes).find((p) => p.itemIds.includes(itemId));
1291
+ return isOpen && pane?.activeItemId === itemId ? "visible" : "background";
1292
+ }
1293
+ function toggleKind(controller, input) {
1294
+ const key = input.key ?? CANVAS_DEFAULT_KEY;
1295
+ const itemId = canvasItemId(input.kind, key);
1296
+ const before = selectCanvasKindVisibility(controller.getState(), input.kind, key);
1297
+ if (before === "visible") {
1298
+ if ((input.whenVisible ?? "close") === "close") {
1299
+ controller.close(itemId);
1300
+ } else {
1301
+ controller.hide(canvasWindowOfItem(controller.getState(), itemId) ?? void 0);
1302
+ }
1303
+ return selectCanvasKindVisibility(controller.getState(), input.kind, key);
1304
+ }
1305
+ if (before === "background") {
1306
+ controller.open({ kind: input.kind, key });
1307
+ const windowId = canvasWindowOfItem(controller.getState(), itemId);
1308
+ if (windowId) controller.show(windowId);
1309
+ return selectCanvasKindVisibility(controller.getState(), input.kind, key);
1310
+ }
1311
+ const opened = controller.open({
1312
+ kind: input.kind,
1313
+ key,
1314
+ ...input.data !== void 0 ? { data: input.data } : {},
1315
+ ...input.title !== void 0 ? { title: input.title } : {}
1316
+ });
1317
+ return opened ? selectCanvasKindVisibility(controller.getState(), input.kind, key) : "absent";
1318
+ }
1319
+
1281
1320
  // src/react/layer.tsx
1282
1321
  var import_react = require("react");
1283
1322
  var import_react_dom = require("react-dom");
@@ -1577,6 +1616,17 @@ function useOptionalCanvasState(selector, fallback) {
1577
1616
  const read = () => store ? selector(store.getState()) : fallback;
1578
1617
  return (0, import_react2.useSyncExternalStore)(store ? store.subscribe : NO_SUBSCRIPTION, read, read);
1579
1618
  }
1619
+ function useCanvasKindToggle(input) {
1620
+ const controller = useCanvasContext().controller;
1621
+ const key = input.key ?? CANVAS_DEFAULT_KEY;
1622
+ const visibility = useCanvasState((s) => selectCanvasKindVisibility(s, input.kind, key));
1623
+ return {
1624
+ visibility,
1625
+ isOpen: visibility !== "absent",
1626
+ isVisible: visibility === "visible",
1627
+ toggle: () => toggleKind(controller, input)
1628
+ };
1629
+ }
1580
1630
  function useCanvasKinds() {
1581
1631
  (0, import_react2.useSyncExternalStore)(subscribeCanvasKinds, getCanvasKindsVersion, getCanvasKindsVersion);
1582
1632
  return listCanvasKinds();
@@ -2517,14 +2567,18 @@ function SplitChild({ size, handle, children }) {
2517
2567
  ] });
2518
2568
  }
2519
2569
  var isMac2 = () => typeof navigator !== "undefined" && /mac|iphone|ipad/i.test(navigator.platform ?? "");
2520
- function CanvasToggle({ className }) {
2570
+ function CanvasToggle({
2571
+ className,
2572
+ variant = "transparent"
2573
+ }) {
2521
2574
  const { controller, windowId } = useCanvasInternals();
2522
2575
  const isOpen = useCanvasState((s) => selectCanvasIsOpen(s, windowId));
2523
2576
  const count = useCanvasState((s) => selectCanvasWindowItemCount(s, windowId));
2524
2577
  const hint = canvasShortcutHint("canvas.toggle", isMac2());
2578
+ const Button = variant === "glass" ? import_tap_target.TapTargetButton : import_tap_target.TapTargetButtonTransparent;
2525
2579
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("span", { className: ["mxc-toggle", className].filter(Boolean).join(" "), "data-open": isOpen ? "" : void 0, children: [
2526
2580
  /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2527
- import_tap_target.TapTargetButtonTransparent,
2581
+ Button,
2528
2582
  {
2529
2583
  ariaLabel: isOpen ? "Hide canvas" : "Show canvas",
2530
2584
  tooltip: `${isOpen ? "Hide canvas" : "Show canvas"}${hint ? ` (${hint})` : ""}`,