The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix: preserve theme tokens when CSS variables are unresolved (#24250)
anamikasingh200187166db8d56
diff --git a/packages/twenty-front-component-renderer/.storybook/preview.tsx b/packages/twenty-front-component-renderer/.storybook/preview.tsxindex 251bd8a638a..888fe1723ee 100644--- a/packages/twenty-front-component-renderer/.storybook/preview.tsx+++ b/packages/twenty-front-component-renderer/.storybook/preview.tsx@@ -1,6 +1,9 @@import { type Preview } from '@storybook/react-vite';import { ThemeProvider } from 'twenty-ui/theme-constants';+import 'twenty-ui/theme-light.css';+import 'twenty-ui/theme-dark.css';+const preview: Preview = {tags: ['autodocs'],decorators: [diff --git a/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsx b/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsxindex 824b1178857..bbe23d0cd69 100644--- a/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsx+++ b/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsx@@ -234,3 +234,31 @@ export const TypographyPreact: Story = createGalleryStory('twenty-ui-typography-gallery','preact',);++const themeTokenTest: Story['play'] = async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ const iconWrapper = await canvas.findByTestId(+ 'theme-token-icon-wrapper',+ {},+ { timeout: 30000 },+ );++ await waitFor(() => {+ const iconBox = iconWrapper.getBoundingClientRect();++ expect(Math.round(iconBox.width)).toBe(16);+ expect(Math.round(iconBox.height)).toBe(16);+ });++ expect(errorHandler).not.toHaveBeenCalled();+};++export const ThemeTokensReact: Story = {+ ...createGalleryStory('twenty-ui-theme-tokens'),+ play: themeTokenTest,+};+export const ThemeTokensPreact: Story = {+ ...createGalleryStory('twenty-ui-theme-tokens', 'preact'),+ play: themeTokenTest,+};diff --git a/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/twenty-ui-theme-tokens.front-component.tsx b/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/twenty-ui-theme-tokens.front-component.tsxnew file mode 100644index 00000000000..e39cb0cca6b--- /dev/null+++ b/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/twenty-ui-theme-tokens.front-component.tsx@@ -0,0 +1,30 @@+import { defineFrontComponent } from 'twenty-sdk/define';+import { IconInfoCircle } from 'twenty-ui/icon';+import { ThemeProvider, useTheme } from 'twenty-ui/theme-constants';++const TokenSizedIcon = () => {+ const theme = useTheme();++ return (+ <div+ data-testid="theme-token-icon-wrapper"+ style={{ display: 'inline-flex' }}+ >+ <IconInfoCircle size={theme.icon.size.md} />+ </div>+ );+};++const ThemeTokensComponent = () => (+ <ThemeProvider colorScheme="light">+ <TokenSizedIcon />+ </ThemeProvider>+);++export default defineFrontComponent({+ universalIdentifier: 'test-20ui0-0000-0000-0000-000000000113',+ name: 'twenty-ui-theme-tokens',+ description:+ 'Renders an icon sized from a useTheme() design token under ThemeProvider in the sandbox',+ component: ThemeTokensComponent,+});diff --git a/packages/twenty-ui/src/theme-constants/ThemeProvider.tsx b/packages/twenty-ui/src/theme-constants/ThemeProvider.tsxindex 540743d11ff..f5782afcb5a 100644--- a/packages/twenty-ui/src/theme-constants/ThemeProvider.tsx+++ b/packages/twenty-ui/src/theme-constants/ThemeProvider.tsx@@ -1,3 +1,4 @@+import { isNonEmptyString } from '@sniptt/guards';import { clsx } from 'clsx';import { createContext, useLayoutEffect, useRef, useState } from 'react';@@ -53,6 +54,19 @@ export type ThemeContextType = {export type ThemeOverrides = Record<string, string | number>;+const resolveTokenValue = (+ cssVariableReference: string,+ computedValue: string,+): string | number => {+ if (!isNonEmptyString(computedValue)) {+ return cssVariableReference;+ }++ const numericValue = Number(computedValue);++ return Number.isNaN(numericValue) ? computedValue : numericValue;+};+const computeThemeFromCss = (sourceElement?: HTMLElement): ThemeType => {if (typeof document === 'undefined' ||@@ -73,9 +87,11 @@ const computeThemeFromCss = (sourceElement?: HTMLElement): ThemeType => {if (typeof value === 'string' && value.startsWith('var(')) {const varName = value.slice(4, -1);- const raw = computedStyle.getPropertyValue(varName).trim();- const num = Number(raw);- result[key] = raw !== '' && !isNaN(num) ? num : raw;++ result[key] = resolveTokenValue(+ value,+ computedStyle.getPropertyValue(varName).trim(),+ );} else if (typeof value === 'object' && value !== null) {result[key] = resolve(value as Record<string, unknown>);} else {diff --git a/packages/twenty-ui/src/theme-constants/__tests__/ThemeProvider.test.tsx b/packages/twenty-ui/src/theme-constants/__tests__/ThemeProvider.test.tsxindex 651483cf71a..2c0407d0003 100644--- a/packages/twenty-ui/src/theme-constants/__tests__/ThemeProvider.test.tsx+++ b/packages/twenty-ui/src/theme-constants/__tests__/ThemeProvider.test.tsx@@ -1,6 +1,8 @@import { render } from '@testing-library/react';-import { ThemeProvider } from '../ThemeProvider';+import { ThemeProvider, type ThemeType } from '../ThemeProvider';+import { themeCssVariables } from '../themeCssVariables';+import { useTheme } from '../useTheme';const readInlineScale = () =>document.documentElement.style.getPropertyValue('--t-scale-user');@@ -79,3 +81,65 @@ describe('ThemeProvider scale', () => {expect(readInlineScale()).toBe('1.25');});});++describe('ThemeProvider token resolution', () => {+ let latestTheme: ThemeType | undefined;++ const ThemeSpy = () => {+ latestTheme = useTheme();+ return null;+ };++ const mockResolvedTokens = (resolvedTokens: Record<string, string>) => {+ jest.spyOn(window, 'getComputedStyle').mockReturnValue({+ getPropertyValue: (property: string) => resolvedTokens[property] ?? '',+ } as CSSStyleDeclaration);+ };++ const renderThemeSpy = () =>+ render(+ <ThemeProvider colorScheme="light">+ <ThemeSpy />+ </ThemeProvider>,+ );++ beforeEach(() => {+ latestTheme = undefined;+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ it('should keep the CSS variable reference when no custom property resolves', () => {+ mockResolvedTokens({});++ renderThemeSpy();++ expect(latestTheme?.icon.size.md).toBe(themeCssVariables.icon.size.md);+ expect(latestTheme?.font.color.primary).toBe(+ themeCssVariables.font.color.primary,+ );+ });++ it('should resolve numeric custom properties and keep the reference for the rest', () => {+ mockResolvedTokens({ '--t-icon-size-md': '16' });++ renderThemeSpy();++ expect(latestTheme?.icon.size.md).toBe(16);+ expect(latestTheme?.icon.size.sm).toBe(themeCssVariables.icon.size.sm);+ });++ it('should resolve string custom properties to their computed value', () => {+ mockResolvedTokens({+ '--t-font-color-primary': '#181d27',+ '--t-border-radius-sm': '4px',+ });++ renderThemeSpy();++ expect(latestTheme?.font.color.primary).toBe('#181d27');+ expect(latestTheme?.border.radius.sm).toBe('4px');+ });+});