The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
[Dashboards] Auto focus effect on standalone widget (#17623)
nitinb643fbe425b8
diff --git a/packages/twenty-front/src/modules/page-layout/hooks/useEditPageLayoutWidget.ts b/packages/twenty-front/src/modules/page-layout/hooks/useEditPageLayoutWidget.tsindex 1ac39506dc3..2656da741cb 100644--- a/packages/twenty-front/src/modules/page-layout/hooks/useEditPageLayoutWidget.ts+++ b/packages/twenty-front/src/modules/page-layout/hooks/useEditPageLayoutWidget.ts@@ -1,6 +1,8 @@import { useCallback } from 'react';+import { useSetRecoilState } from 'recoil';import { useCommandMenu } from '@/command-menu/hooks/useCommandMenu';+import { commandMenuPageState } from '@/command-menu/states/commandMenuPageState';import { CommandMenuPages } from '@/command-menu/types/CommandMenuPages';import { useSetRecoilComponentState } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentState';@@ -24,6 +26,7 @@ export const useEditPageLayoutWidget = (pageLayoutIdFromProps?: string) => {const { navigatePageLayoutCommandMenu } = useNavigatePageLayoutCommandMenu();const { closeCommandMenu } = useCommandMenu();+ const setCommandMenuPage = useSetRecoilState(commandMenuPageState);const handleEditWidget = useCallback(({@@ -53,12 +56,14 @@ export const useEditPageLayoutWidget = (pageLayoutIdFromProps?: string) => {return;}+ setCommandMenuPage(CommandMenuPages.Root);closeCommandMenu();},[setPageLayoutEditingWidgetId,navigatePageLayoutCommandMenu,closeCommandMenu,+ setCommandMenuPage,],);diff --git a/packages/twenty-front/src/modules/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidget.tsx b/packages/twenty-front/src/modules/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidget.tsxindex 308b4b2d04e..215163a4baa 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidget.tsx+++ b/packages/twenty-front/src/modules/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidget.tsx@@ -11,6 +11,7 @@ import { isPageLayoutInEditModeComponentState } from '@/page-layout/states/isPagimport { pageLayoutEditingWidgetIdComponentState } from '@/page-layout/states/pageLayoutEditingWidgetIdComponentState';import { type PageLayoutWidget } from '@/page-layout/types/PageLayoutWidget';import { DashboardsBlockEditor } from '@/page-layout/widgets/standalone-rich-text/components/DashboardsBlockEditor';+import { StandaloneRichTextWidgetAutoFocusEffect } from '@/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidgetAutoFocusEffect';import { BLOCK_EDITOR_GLOBAL_HOTKEYS_CONFIG } from '@/ui/input/editor/constants/BlockEditorGlobalHotkeysConfig';import { useAttachmentSync } from '@/ui/input/editor/hooks/useAttachmentSync';import { parseInitialBlocknote } from '@/ui/input/editor/utils/parseInitialBlocknote';@@ -182,6 +183,11 @@ export const StandaloneRichTextWidget = ({ref={containerElementRef}isPageLayoutInEditMode={isPageLayoutInEditMode}>+ <StandaloneRichTextWidgetAutoFocusEffect+ shouldFocus={isEditable}+ editor={editor}+ containerElement={containerElementRef.current}+ /><ScrollWrappercomponentInstanceId={`scroll-wrapper-rich-text-widget-${widget.id}`}>diff --git a/packages/twenty-front/src/modules/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidgetAutoFocusEffect.tsx b/packages/twenty-front/src/modules/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidgetAutoFocusEffect.tsxnew file mode 100644index 00000000000..748f870b02b--- /dev/null+++ b/packages/twenty-front/src/modules/page-layout/widgets/standalone-rich-text/components/StandaloneRichTextWidgetAutoFocusEffect.tsx@@ -0,0 +1,34 @@+import { useEffect } from 'react';++import { type BLOCK_SCHEMA } from '@/activities/blocks/constants/Schema';++type StandaloneRichTextWidgetAutoFocusEffectProps = {+ shouldFocus: boolean;+ editor: typeof BLOCK_SCHEMA.BlockNoteEditor;+ containerElement?: HTMLElement | null;+};++export const StandaloneRichTextWidgetAutoFocusEffect = ({+ shouldFocus,+ editor,+ containerElement,+}: StandaloneRichTextWidgetAutoFocusEffectProps) => {+ useEffect(() => {+ if (shouldFocus) {+ const alreadyFocused =+ containerElement?.contains(document.activeElement) ?? false;++ if (!alreadyFocused) {+ const rafId = requestAnimationFrame(() => {+ editor.focus();+ });++ return () => {+ cancelAnimationFrame(rafId);+ };+ }+ }+ }, [shouldFocus, editor, containerElement]);++ return null;+};