Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(menus): state and timeout based fix for menu sequences (#1635)
Seamus9fd543038755
diff --git a/js/app/packages/app/component/Launcher.tsx b/js/app/packages/app/component/Launcher.tsxindex 9b3e9718a4..e469853e0c 100644--- a/js/app/packages/app/component/Launcher.tsx+++ b/js/app/packages/app/component/Launcher.tsx@@ -308,7 +308,10 @@ export const CREATABLE_BLOCKS: CreatableBlock[] = [const USE_ENTITY_COLORS = true;-export const [createMenuOpen, setCreateMenuOpen] = createControlledOpenSignal();+export const [createMenuOpen, setCreateMenuOpen] = createControlledOpenSignal(+ false,+ { id: 'launcher' }+);type LauncherMenuItemProps = {creatableBlock: CreatableBlock;diff --git a/js/app/packages/app/component/bulk-edit-entity/BulkEditEntityModal.tsx b/js/app/packages/app/component/bulk-edit-entity/BulkEditEntityModal.tsxindex 2d39578d6d..ffefe38177 100644--- a/js/app/packages/app/component/bulk-edit-entity/BulkEditEntityModal.tsx+++ b/js/app/packages/app/component/bulk-edit-entity/BulkEditEntityModal.tsx@@ -137,7 +137,9 @@ const [globalModalProps, setGlobalModalProps] = createSignal<{onFinish?: () => void;onCancel?: () => void;} | null>(null);-const [modalOpen, setModalOpen] = createControlledOpenSignal();+const [modalOpen, setModalOpen] = createControlledOpenSignal(false, {+ id: 'entity-edit',+});export const openBulkEditModal = (props: {view: 'rename' | 'moveToProject' | 'delete';diff --git a/js/app/packages/app/component/command/state.ts b/js/app/packages/app/component/command/state.tsindex 6a418586ce..010a4d3d0e 100644--- a/js/app/packages/app/component/command/state.ts+++ b/js/app/packages/app/component/command/state.ts@@ -51,7 +51,9 @@ export interface ICommandState {}function createCommandState(): ICommandState {- const [isOpen, setIsOpen] = createControlledOpenSignal();+ const [isOpen, setIsOpen] = createControlledOpenSignal(false, {+ id: 'command',+ });const [query, setQuery] = createSignal('');const [selectedIndex, setSelectedIndex] = createSignal(0);const [categoryFilter, setCategoryFilter] =diff --git a/js/app/packages/app/component/global-share-modal/GlobalShareModal.tsx b/js/app/packages/app/component/global-share-modal/GlobalShareModal.tsxindex 4ea3303acd..9f77c506d2 100644--- a/js/app/packages/app/component/global-share-modal/GlobalShareModal.tsx+++ b/js/app/packages/app/component/global-share-modal/GlobalShareModal.tsx@@ -16,7 +16,9 @@ export type GlobalShareModalProps = {const [globalModalProps, setGlobalModalProps] =createSignal<GlobalShareModalProps | null>(null);-const [modalOpen, setModalOpen] = createControlledOpenSignal();+const [modalOpen, setModalOpen] = createControlledOpenSignal(false, {+ id: 'global-share',+});export const isShareableEntityType = (type: EntityData['type']diff --git a/js/app/packages/app/component/property-edit-modal/state/propertyEditor.ts b/js/app/packages/app/component/property-edit-modal/state/propertyEditor.tsindex b90a43c337..1f3e18f0a8 100644--- a/js/app/packages/app/component/property-edit-modal/state/propertyEditor.ts+++ b/js/app/packages/app/component/property-edit-modal/state/propertyEditor.ts@@ -9,7 +9,7 @@ import { createControlledOpenSignal } from '@core/util/createControlledOpenSignaexport type PropertyEditorMode = 'selector' | 'direct';export const [propertyEditorOpen, setPropertyEditorOpen] =- createControlledOpenSignal();+ createControlledOpenSignal(false, { id: 'property-edit' });interface PropertyEditorState {mode: PropertyEditorMode;diff --git a/js/app/packages/core/util/createControlledOpenSignal.ts b/js/app/packages/core/util/createControlledOpenSignal.tsindex 85dac3fdaa..0868093c88 100644--- a/js/app/packages/core/util/createControlledOpenSignal.ts+++ b/js/app/packages/core/util/createControlledOpenSignal.ts@@ -1,50 +1,122 @@-import { DEV_MODE_ENV } from '@core/constant/featureFlags';import { createSignal } from 'solid-js';import { attributesToSelector } from './attributeToSelector';-let previouslyFocusedElement = document.activeElement;-export const createControlledOpenSignal = (value?: boolean) => {+/** actively open menus that may try to return focus */+const activeFocusOwners = new Set<string>();++/** the return-focus target of the first-opened menu */+let rootFocusElement: Element | null = null;++let ownerIdCounter = 0;+let pendingFocusReturn: (() => void) | null = null;++/** simple id generator */+function generateOwnerId(prefix?: string): string {+ return `${prefix ?? 'menu'}-${++ownerIdCounter}`;+}++/**+ * Focus and update lock metadata+ */+function acquireFocusLock(id: string): void {+ if (pendingFocusReturn) {+ pendingFocusReturn = null;+ }++ const wasEmpty = activeFocusOwners.size === 0;++ if (wasEmpty && rootFocusElement === null) {+ rootFocusElement = document.activeElement;+ }+ activeFocusOwners.add(id);+}++/**+ * Release focus and handle return to last state.+ * @param id+ */+function releaseFocusLock(id: string, shouldReturnFocus: boolean): void {+ activeFocusOwners.delete(id);++ if (activeFocusOwners.size === 0 && rootFocusElement) {+ if (shouldReturnFocus) {+ // Defer focus return to allow another menu to acquire first. Sometimes+ // next menu opens first, but sometime we close before next menu opens.+ const elementToFocus = rootFocusElement;++ pendingFocusReturn = () => {+ // Only return focus if no one else has acquired+ if (activeFocusOwners.size === 0) {+ focusLast(elementToFocus);+ rootFocusElement = null;+ }+ pendingFocusReturn = null;+ };++ // queueMicrotask so it runs after synchronous code but before next setTimeout+ queueMicrotask(() => {+ if (pendingFocusReturn) {+ pendingFocusReturn();+ }+ });+ } else {+ rootFocusElement = null;+ }+ }+}++function focusLast(element: Element) {+ // has to fire two tasks after to prevent Kobalte menus stealing focus and opening menu on up/down keypress+ // i guess not - until it does once again+ setTimeout(() => {+ // Don't steal focus if another menu has acquired in the meantime+ if (activeFocusOwners.size > 0) {+ return;+ }++ if (element instanceof HTMLElement) {+ if (element.isConnected) {+ element.focus();+ } else {+ // This only works for restoring previously focused entity in UnifiedList, this a workaround previous focused Entity nodes being removed from the dom and focusing to body+ // attributeToSelector still doesn't guarentee node is unique for all cases+ // new rendered node might have different arribute value+ const selector = attributesToSelector(element);+ const fallbackElement = document.querySelector(selector) as HTMLElement;++ if (fallbackElement instanceof HTMLElement) {+ fallbackElement.focus();+ }+ }+ }+ });+}++export const createControlledOpenSignal = (+ value?: boolean,+ options?: {+ id?: string;+ }+) => {+ const ownerId = generateOwnerId(options?.id);const [createMenuOpen, setCreateMenuOpen] = createSignal(value ?? false);const customSetter = (next: boolean | ((prev: boolean) => boolean),shouldReturnFocusToPreviousElement = true) => {- const activeElement = document.activeElement;+ const prevOpen = createMenuOpen();const isOpenResult = setCreateMenuOpen(next);- if (isOpenResult) {- previouslyFocusedElement = activeElement;-- if (DEV_MODE_ENV)- console.info('Borrowing focus from', previouslyFocusedElement);+ if (!prevOpen && isOpenResult) {+ acquireFocusLock(ownerId);return;}- if (!shouldReturnFocusToPreviousElement) return;-- // has to fire two tasks after to prevent Kobalte menus stealing focus and opening menu on up/down keypress- // i guess not - until it does once again- setTimeout(() => {- if (previouslyFocusedElement instanceof HTMLElement) {- if (previouslyFocusedElement.isConnected) {- previouslyFocusedElement.focus();- } else {- // This only works for restoring previously focused entity in UnifiedList, this a workaround previous focused Entity nodes being removed from the dom and focusing to body-- // attributeToSelector still doesn't guarentee node is unique for all cases- // new rendered node might have different arribute value- previouslyFocusedElement = document.querySelector(- attributesToSelector(previouslyFocusedElement)- ) as HTMLElement;- if (previouslyFocusedElement instanceof HTMLElement) {- previouslyFocusedElement.focus();- if (DEV_MODE_ENV)- console.info('returning focus to', previouslyFocusedElement);- }- }- }- });+ if (prevOpen && !isOpenResult) {+ releaseFocusLock(ownerId, shouldReturnFocusToPreviousElement);+ }};+return [createMenuOpen, customSetter] as const;};