@devries-cameron20 / composio Mirror · read-only / commits / d8ec506ec
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 19:45 UTC

fix docs Decimal widget loading (#3575)

## Summary - stop loading the Decimal widget globally from the docs root layout - load the Decimal script only when the user clicks Ask AI or uses the shortcut - preserve Decimal theme syncing after the widget has loaded ## Why Slack/GSC investigation pointed at mobile CLS regressions on docs pages. Lighthouse trace comparison isolated the layout shift to the Decimal iframe/sidebar; blocking Decimal took /docs CLS from ~0.302 to 0, while blocking PostHog did not change CLS. ## Validation - bun run types:check - bunx eslint app/layout.tsx components/ask-ai-button.tsx components/custom-search-dialog.tsx components/decimal-widget.ts - bun run test - local Playwright check on http://localhost:3000/docs: no getdecimal scripts or requests before interaction; after clicking Ask AI the Decimal script loads and the sidebar opens ## Notes - full `bun run lint` is still red on existing unrelated docs lint issues on next, including @next/next/no-html-link-for-pages and react-hooks compiler rules in other files

Malay Vasa committed Jun 15, 2026 at 18:10 UTC d8ec506ec80280d1074cc532791e298af4c8ffe0
5 files changed +229 -100
docs/app/layout.tsx
-2
@@ -5,7 +5,6 @@ import './global.css';
5 import { JetBrains_Mono } from 'next/font/google';
6 import localFont from 'next/font/local';
7 import { PostHogProvider } from '@/components/posthog-provider';
8 -import { DecimalWidget } from '@/components/decimal-widget';
8 import CustomSearchDialog from '@/components/custom-search-dialog';
9 import { ScrollReset } from '@/components/scroll-reset';
10 import { source } from '@/lib/source';
@@ -129,7 +128,6 @@ export default function Layout({ children }: LayoutProps<'/'>) {
128 {children}
129 </RootProvider>
130 </PostHogProvider>
132 - <DecimalWidget />
131 </body>
132 </html>
133 );
docs/components/ask-ai-button.tsx
+24 -27
@@ -1,30 +1,23 @@
1 'use client';
2
3 -import { useEffect, useState } from 'react';
3 +import { useEffect, useSyncExternalStore } from 'react';
4 import { Search, MessageSquare } from 'lucide-react';
5 import { useSearchContext } from '@fumadocs/ui/contexts/search';
6 import { useI18n } from '@fumadocs/ui/contexts/i18n';
7
8 -import type { DecimalAPI } from './decimal-widget';
8 +import {
9 + closeDecimalWidget,
10 + getDecimal,
11 + isDecimalWidgetVisible,
12 + loadDecimalWidget,
13 + openDecimalWidget,
14 +} from './decimal-widget';
15
10 -function getDecimal() {
11 - return (window as typeof window & { Decimal?: DecimalAPI }).Decimal;
12 -}
13 -
14 -function isWidgetVisible(): boolean {
15 - const sidebar = document.querySelector('.decimal-widget-sidebar');
16 - return sidebar?.classList.contains('open') ?? false;
17 -}
16 +export async function toggleDecimalWidget() {
17 + const decimal = getDecimal() ?? (await loadDecimalWidget());
18 + if (!decimal) return;
19
19 -export function toggleDecimalWidget() {
20 - const decimal = getDecimal();
21 - if (!decimal) {
22 - setTimeout(() => {
23 - getDecimal()?.show();
24 - }, 500);
25 - return;
26 - }
27 - isWidgetVisible() ? decimal.hide() : decimal.show();
20 + isDecimalWidgetVisible() ? closeDecimalWidget() : openDecimalWidget(decimal);
21 }
22
23 export function detectMac(): boolean {
@@ -42,17 +35,17 @@ export function detectMac(): boolean {
35 }
36
37 function useIsMac() {
45 - const [isMac, setIsMac] = useState(true);
46 - useEffect(() => {
47 - setIsMac(detectMac());
48 - }, []);
49 - return isMac;
38 + return useSyncExternalStore(
39 + () => () => {},
40 + detectMac,
41 + () => true,
42 + );
43 }
44
45 const handleKeyDown = (e: KeyboardEvent) => {
46 if ((e.metaKey || e.ctrlKey) && e.key === 'i') {
47 e.preventDefault();
55 - toggleDecimalWidget();
48 + void toggleDecimalWidget();
49 }
50 };
51
@@ -92,7 +85,9 @@ export function SearchAndAskAI() {
85 )}
86 <button
87 type="button"
95 - onClick={toggleDecimalWidget}
88 + onClick={() => {
89 + void toggleDecimalWidget();
90 + }}
91 className="inline-flex items-center gap-2 rounded-lg border border-[var(--composio-orange)]/20 bg-[var(--composio-orange)]/5 p-1.5 ps-2.5 text-sm text-[var(--composio-orange)] transition-colors hover:bg-[var(--composio-orange)]/10 shrink-0"
92 >
93 Ask AI
@@ -125,7 +120,9 @@ export function SearchAndAskAIMobile() {
120 <button
121 type="button"
122 aria-label="Ask AI"
128 - onClick={toggleDecimalWidget}
123 + onClick={() => {
124 + void toggleDecimalWidget();
125 + }}
126 className="inline-flex items-center justify-center rounded-md p-2 text-sm font-medium transition-colors duration-100 hover:bg-fd-accent hover:text-fd-accent-foreground"
127 >
128 <MessageSquare className="size-4.5" />
docs/components/custom-search-dialog.tsx
+1 -1
@@ -275,7 +275,7 @@ export default function CustomSearchDialog({
275 className="inline-flex items-center gap-1.5 text-xs text-[var(--composio-orange)] hover:text-[var(--composio-orange)]/80 transition-colors"
276 onClick={() => {
277 props.onOpenChange(false);
278 - toggleDecimalWidget();
278 + void toggleDecimalWidget();
279 }}
280 >
281 <BotMessageSquare className="size-3.5" />
docs/components/decimal-widget.ts new
+204
@@ -0,0 +1,204 @@
1 +'use client';
2 +
3 +export type DecimalAPI = {
4 + show: () => void;
5 + hide: () => void;
6 + theme: (config: Record<string, string>) => void;
7 +};
8 +
9 +const DECIMAL_SCRIPT_ID = 'decimal-widget-script';
10 +const DECIMAL_SRC = 'https://app.getdecimal.ai/widget/v1/widget.js';
11 +const DECIMAL_WIDGET_ID = 'wgt_Ze0kCx97w7YXIydXpEAbAVWfu7FO6HG1';
12 +const DECIMAL_MOBILE_CLOSE_ID = 'decimal-mobile-close-button';
13 +const MOBILE_VIEWPORT_QUERY = '(max-width: 767px)';
14 +const DECIMAL_PUBLIC_CONFIG =
15 + 'eyJhbGciOiJIUzI1NiJ9.eyJ3aWQiOiJ3Z3RfWmUwa0N4OTd3N1lYSXlkWHBFQWJBVldmdTdGTzZIRzEiLCJkb21haW5zIjpbImNvbXBvc2lvLmRldiIsImNvbXBvc2lvLWRlY2ltYWwudmVyY2VsLmFwcCIsImxvY2FsaG9zdDozMDAwIiwiZG9jcy5jb21wb3Npby5kZXYiLCJmdW1hZG9jcy1wc2kudmVyY2VsLmFwcCJdLCJpYXQiOjE3Njk1MDE3NTZ9.j7odPAOmoKSkdkFHQCs7FDpAxHfJuzUOEMb_OuHi81I';
16 +
17 +const DARK_THEME = {
18 + colorScheme: 'dark',
19 + primaryColor: '#1e1d1c',
20 + backgroundColor: '#131211',
21 + textColor: '#FAFAFA',
22 + textColorSecondary: '#FFFFFF',
23 + textColorMuted: '#A1A1AA',
24 + borderColor: '#27272A',
25 +};
26 +
27 +const LIGHT_THEME = {
28 + colorScheme: 'light',
29 + primaryColor: '#171414',
30 + backgroundColor: '#ffffff',
31 + textColor: '#171414',
32 + textColorSecondary: '#ffffff',
33 + textColorMuted: '#5c5858',
34 + borderColor: '#e5e0df',
35 +};
36 +
37 +let loadPromise: Promise<DecimalAPI | undefined> | undefined;
38 +let themeObserver: MutationObserver | undefined;
39 +let closeButtonViewportQuery: MediaQueryList | undefined;
40 +let decimalWidgetRequestedOpen = false;
41 +
42 +export function getDecimal() {
43 + return (window as typeof window & { Decimal?: DecimalAPI }).Decimal;
44 +}
45 +
46 +export function isDecimalWidgetVisible(): boolean {
47 + const sidebar = document.querySelector('.decimal-widget-sidebar');
48 + return sidebar?.classList.contains('open') ?? false;
49 +}
50 +
51 +export function applyDecimalTheme() {
52 + const decimal = getDecimal();
53 + if (!decimal) return;
54 +
55 + const isDark = document.documentElement.classList.contains('dark');
56 + decimal.theme(isDark ? DARK_THEME : LIGHT_THEME);
57 +}
58 +
59 +function watchThemeChanges() {
60 + if (themeObserver) return;
61 +
62 + applyDecimalTheme();
63 + themeObserver = new MutationObserver((mutations) => {
64 + for (const mutation of mutations) {
65 + if (mutation.attributeName === 'class') {
66 + applyDecimalTheme();
67 + }
68 + }
69 + });
70 + themeObserver.observe(document.documentElement, { attributes: true });
71 +}
72 +
73 +function waitForDecimal(timeoutMs = 2000): Promise<DecimalAPI | undefined> {
74 + const startedAt = Date.now();
75 +
76 + return new Promise((resolve) => {
77 + const check = () => {
78 + const decimal = getDecimal();
79 + if (decimal) {
80 + watchThemeChanges();
81 + resolve(decimal);
82 + return;
83 + }
84 +
85 + if (Date.now() - startedAt >= timeoutMs) {
86 + resolve(undefined);
87 + return;
88 + }
89 +
90 + window.setTimeout(check, 50);
91 + };
92 +
93 + check();
94 + });
95 +}
96 +
97 +function finishLoading(resolve: (decimal: DecimalAPI | undefined) => void) {
98 + waitForDecimal().then((decimal) => {
99 + if (!decimal) {
100 + loadPromise = undefined;
101 + }
102 + resolve(decimal);
103 + });
104 +}
105 +
106 +function isMobileViewport() {
107 + return window.matchMedia(MOBILE_VIEWPORT_QUERY).matches;
108 +}
109 +
110 +function syncMobileCloseButtonVisibility() {
111 + const button = document.getElementById(DECIMAL_MOBILE_CLOSE_ID);
112 + if (!button) return;
113 +
114 + button.style.display = isMobileViewport() && decimalWidgetRequestedOpen ? 'inline-flex' : 'none';
115 +}
116 +
117 +function ensureMobileCloseButton() {
118 + let button = document.getElementById(DECIMAL_MOBILE_CLOSE_ID) as HTMLButtonElement | null;
119 + if (!button) {
120 + button = document.createElement('button');
121 + button.id = DECIMAL_MOBILE_CLOSE_ID;
122 + button.type = 'button';
123 + button.setAttribute('aria-label', 'Close Ask AI');
124 + button.innerHTML = '&times;';
125 + button.style.cssText = [
126 + 'position: fixed',
127 + 'top: calc(env(safe-area-inset-top, 0px) + 12px)',
128 + 'right: 12px',
129 + 'z-index: 2147483647',
130 + 'width: 44px',
131 + 'height: 44px',
132 + 'align-items: center',
133 + 'justify-content: center',
134 + 'border-radius: 9999px',
135 + 'border: 1px solid rgba(23, 20, 20, 0.12)',
136 + 'background: rgba(255, 255, 255, 0.96)',
137 + 'color: #171414',
138 + 'box-shadow: 0 12px 32px rgba(23, 20, 20, 0.18)',
139 + 'font-size: 28px',
140 + 'font-weight: 400',
141 + 'line-height: 1',
142 + 'display: none',
143 + ].join(';');
144 + button.addEventListener('click', closeDecimalWidget);
145 + document.body.appendChild(button);
146 + }
147 +
148 + if (!closeButtonViewportQuery) {
149 + closeButtonViewportQuery = window.matchMedia(MOBILE_VIEWPORT_QUERY);
150 + closeButtonViewportQuery.addEventListener('change', syncMobileCloseButtonVisibility);
151 + }
152 +
153 + syncMobileCloseButtonVisibility();
154 +}
155 +
156 +export function closeDecimalWidget() {
157 + decimalWidgetRequestedOpen = false;
158 + getDecimal()?.hide();
159 + syncMobileCloseButtonVisibility();
160 +}
161 +
162 +export function openDecimalWidget(decimal: DecimalAPI) {
163 + decimalWidgetRequestedOpen = true;
164 + decimal.show();
165 + ensureMobileCloseButton();
166 +}
167 +
168 +export function loadDecimalWidget(): Promise<DecimalAPI | undefined> {
169 + const existingDecimal = getDecimal();
170 + if (existingDecimal) {
171 + watchThemeChanges();
172 + return Promise.resolve(existingDecimal);
173 + }
174 +
175 + if (loadPromise) return loadPromise;
176 +
177 + loadPromise = new Promise((resolve) => {
178 + const existingScript = document.getElementById(DECIMAL_SCRIPT_ID);
179 + if (existingScript) {
180 + finishLoading(resolve);
181 + return;
182 + }
183 +
184 + const script = document.createElement('script');
185 + script.id = DECIMAL_SCRIPT_ID;
186 + script.src = DECIMAL_SRC;
187 + script.async = true;
188 + script.dataset.widgetId = DECIMAL_WIDGET_ID;
189 + script.dataset.publicConfig = DECIMAL_PUBLIC_CONFIG;
190 + script.dataset.displayMode = 'push-sidebar';
191 +
192 + script.onload = () => {
193 + finishLoading(resolve);
194 + };
195 + script.onerror = () => {
196 + loadPromise = undefined;
197 + resolve(undefined);
198 + };
199 +
200 + document.head.appendChild(script);
201 + });
202 +
203 + return loadPromise;
204 +}
docs/components/decimal-widget.tsx deleted
-70
@@ -1,70 +0,0 @@
1 -'use client';
2 -
3 -import Script from 'next/script';
4 -import { useEffect, useState } from 'react';
5 -
6 -export type DecimalAPI = {
7 - show: () => void;
8 - hide: () => void;
9 - theme: (config: Record<string, string>) => void;
10 -};
11 -
12 -const DARK_THEME = {
13 - colorScheme: 'dark',
14 - primaryColor: '#1e1d1c',
15 - backgroundColor: '#131211',
16 - textColor: '#FAFAFA',
17 - textColorSecondary: '#FFFFFF',
18 - textColorMuted: '#A1A1AA',
19 - borderColor: '#27272A',
20 -};
21 -
22 -const LIGHT_THEME = {
23 - colorScheme: 'light',
24 - primaryColor: '#171414',
25 - backgroundColor: '#ffffff',
26 - textColor: '#171414',
27 - textColorSecondary: '#ffffff',
28 - textColorMuted: '#5c5858',
29 - borderColor: '#e5e0df',
30 -};
31 -
32 -export function DecimalWidget() {
33 - const [scriptLoaded, setScriptLoaded] = useState(false);
34 -
35 - useEffect(() => {
36 - if (!scriptLoaded) return;
37 -
38 - const Decimal = (window as typeof window & { Decimal?: DecimalAPI }).Decimal;
39 - if (!Decimal) return;
40 -
41 - const applyTheme = () => {
42 - const isDark = document.documentElement.classList.contains('dark');
43 - Decimal.theme(isDark ? DARK_THEME : LIGHT_THEME);
44 - };
45 -
46 - applyTheme();
47 -
48 - const observer = new MutationObserver((mutations) => {
49 - for (const mutation of mutations) {
50 - if (mutation.attributeName === 'class') {
51 - applyTheme();
52 - }
53 - }
54 - });
55 - observer.observe(document.documentElement, { attributes: true });
56 -
57 - return () => observer.disconnect();
58 - }, [scriptLoaded]);
59 -
60 - return (
61 - <Script
62 - src="https://app.getdecimal.ai/widget/v1/widget.js"
63 - data-widget-id="wgt_Ze0kCx97w7YXIydXpEAbAVWfu7FO6HG1"
64 - data-public-config="eyJhbGciOiJIUzI1NiJ9.eyJ3aWQiOiJ3Z3RfWmUwa0N4OTd3N1lYSXlkWHBFQWJBVldmdTdGTzZIRzEiLCJkb21haW5zIjpbImNvbXBvc2lvLmRldiIsImNvbXBvc2lvLWRlY2ltYWwudmVyY2VsLmFwcCIsImxvY2FsaG9zdDozMDAwIiwiZG9jcy5jb21wb3Npby5kZXYiLCJmdW1hZG9jcy1wc2kudmVyY2VsLmFwcCJdLCJpYXQiOjE3Njk1MDE3NTZ9.j7odPAOmoKSkdkFHQCs7FDpAxHfJuzUOEMb_OuHi81I"
65 - data-display-mode="push-sidebar"
66 - strategy="afterInteractive"
67 - onLoad={() => setScriptLoaded(true)}
68 - />
69 - );
70 -}