Files
twenty/packages/twenty-ui/src/input/button/components/LightIconButton.tsx
T
Charles Bochet 647c32ff3e Deprecate runtime theme objects in favor of CSS variables (#18402)
## Summary

- **Eliminate `ICON_SIZES` / `ICON_STROKES` constants**: all icon
dimensions are now resolved at runtime via
`resolveThemeVariableAsNumber(themeCssVariables.icon.size.X)`, ensuring
values always come from computed CSS variables
- **No more consumer imports from `twenty-ui/theme`**: moved
`ColorSchemeContext`, `ColorSchemeProvider`, `ThemeColor`,
`MAIN_COLOR_NAMES`, `getNextThemeColor`, `AnimationDuration` to
`twenty-ui/theme-constants`
- **Remove `ThemeContext` / `ThemeContextProvider` / `ThemeProvider` /
`ThemeType`**: replaced across ~300 files with `themeCssVariables` (for
CSS contexts) or `resolveThemeVariable` / `resolveThemeVariableAsNumber`
(for JS runtime values)
- **Simplify provider chain**: only `ColorSchemeProvider` remains — it
toggles `light`/`dark` class on `document.documentElement` and provides
`colorScheme` via React context
- **Fix pre-existing test failures**: `useIcons.test.ts`
(non-configurable ES module spy) and
`turnRecordFilterGroupIntoGqlOperationFilter.test.ts`
(`Omit<RecordFilter, 'id'>` type mismatch)

### Theme access pattern (before → after)

| Context | Before | After |
|---------|--------|-------|
| CSS (Linaria) | `${({ theme }) => theme.font.color.primary}` |
`${themeCssVariables.font.color.primary}` |
| JS runtime (icon size, animation) | `theme.icon.size.md` /
`ICON_SIZES.md` |
`resolveThemeVariableAsNumber(themeCssVariables.icon.size.md)` |
| Color scheme check | `theme.name === 'dark'` |
`useContext(ColorSchemeContext).colorScheme === 'dark'` |
2026-03-05 14:39:01 +01:00

131 lines
3.5 KiB
TypeScript

import { styled } from '@linaria/react';
import { type IconComponent } from '@ui/display';
import { ThemeContext, themeCssVariables } from '@ui/theme-constants';
import { type ComponentProps, type MouseEvent, useContext } from 'react';
export type LightIconButtonAccent = 'secondary' | 'tertiary';
export type LightIconButtonSize = 'small' | 'medium';
export type LightIconButtonProps = {
className?: string;
testId?: string;
Icon?: IconComponent;
title?: string;
size?: LightIconButtonSize;
accent?: LightIconButtonAccent;
active?: boolean;
disabled?: boolean;
focus?: boolean;
onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
} & Pick<ComponentProps<'button'>, 'aria-label' | 'title'>;
const StyledButton = styled.button<
Pick<LightIconButtonProps, 'accent' | 'active' | 'size' | 'focus'>
>`
align-items: center;
background: transparent;
border: none;
border: ${({ disabled, focus }) =>
!disabled && focus ? `1px solid ${themeCssVariables.color.blue}` : 'none'};
border-radius: ${themeCssVariables.border.radius.sm};
box-shadow: ${({ disabled, focus }) =>
!disabled && focus ? `0 0 0 3px ${themeCssVariables.color.blue3}` : 'none'};
color: ${({ accent, active, disabled, focus }) => {
switch (accent) {
case 'secondary':
return active || focus
? themeCssVariables.color.blue
: !disabled
? themeCssVariables.font.color.secondary
: themeCssVariables.font.color.extraLight;
case 'tertiary':
return active || focus
? themeCssVariables.color.blue
: !disabled
? themeCssVariables.font.color.tertiary
: themeCssVariables.font.color.extraLight;
}
return '';
}};
cursor: ${({ disabled }) => (disabled ? 'not-allowed' : 'pointer')};
display: flex;
flex-direction: row;
font-family: ${themeCssVariables.font.family};
font-weight: ${themeCssVariables.font.weight.regular};
gap: ${themeCssVariables.spacing[1]};
height: ${({ size }) =>
size === 'small'
? themeCssVariables.spacing[6]
: themeCssVariables.spacing[8]};
justify-content: center;
padding: ${themeCssVariables.spacing[1]};
transition: background 0.1s ease;
white-space: nowrap;
width: ${({ size }) =>
size === 'small'
? themeCssVariables.spacing[6]
: themeCssVariables.spacing[8]};
min-width: ${({ size }) =>
size === 'small'
? themeCssVariables.spacing[6]
: themeCssVariables.spacing[8]};
&:hover {
background: ${({ disabled }) =>
!disabled
? themeCssVariables.background.transparent.light
: 'transparent'};
}
&:focus {
outline: none;
}
&:active {
background: ${({ disabled }) =>
!disabled
? themeCssVariables.background.transparent.medium
: 'transparent'};
}
`;
export const LightIconButton = ({
'aria-label': ariaLabel,
className,
testId,
Icon,
active = false,
size = 'small',
accent = 'secondary',
disabled = false,
focus = false,
onClick,
title,
}: LightIconButtonProps) => {
const { theme } = useContext(ThemeContext);
return (
<StyledButton
data-testid={testId}
aria-label={ariaLabel}
onClick={onClick}
disabled={disabled}
focus={focus && !disabled}
accent={accent}
className={className}
size={size}
active={active}
title={title}
>
{Icon && (
<Icon
size={size === 'medium' ? theme.icon.size.md : theme.icon.size.sm}
/>
)}
</StyledButton>
);
};