Skip to content

Commit 101cef7

Browse files
committed
♻️(frontend) refacto useCunninghamTheme
Refacto useCunninghamTheme, we don't need a function to have access to the tokens anymore.
1 parent 419079a commit 101cef7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+159
-176
lines changed

src/frontend/apps/impress/src/components/Card.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const Card = ({
1818
$background="white"
1919
$radius="4px"
2020
$css={css`
21-
border: 1px solid ${colorsTokens()['greyscale-200']};
21+
border: 1px solid ${colorsTokens['greyscale-200']};
2222
${$css}
2323
`}
2424
{...props}

src/frontend/apps/impress/src/components/DropdownMenu.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export const DropdownMenu = ({
3535
label,
3636
topMessage,
3737
}: PropsWithChildren<DropdownMenuProps>) => {
38-
const theme = useCunninghamTheme();
39-
const spacings = theme.spacingsTokens();
40-
const colors = theme.colorsTokens();
38+
const { spacingsTokens, colorsTokens } = useCunninghamTheme();
4139
const [isOpen, setIsOpen] = useState(false);
4240
const blockButtonRef = useRef<HTMLDivElement>(null);
4341

@@ -120,11 +118,11 @@ export const DropdownMenu = ({
120118
key={option.label}
121119
$align="center"
122120
$justify="space-between"
123-
$background={colors['greyscale-000']}
124-
$color={colors['primary-600']}
121+
$background={colorsTokens['greyscale-000']}
122+
$color={colorsTokens['primary-600']}
125123
$padding={{ vertical: 'xs', horizontal: 'base' }}
126124
$width="100%"
127-
$gap={spacings['base']}
125+
$gap={spacingsTokens['base']}
128126
$css={css`
129127
border: none;
130128
${index === 0 &&
@@ -148,7 +146,11 @@ export const DropdownMenu = ({
148146
}
149147
`}
150148
>
151-
<Box $direction="row" $align="center" $gap={spacings['base']}>
149+
<Box
150+
$direction="row"
151+
$align="center"
152+
$gap={spacingsTokens['base']}
153+
>
152154
{option.icon && (
153155
<Icon
154156
$size="20px"

src/frontend/apps/impress/src/components/quick-search/QuickSearchInput.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const QuickSearchInput = ({
2727
}: Props) => {
2828
const { t } = useTranslation();
2929
const { spacingsTokens } = useCunninghamTheme();
30-
const spacing = spacingsTokens();
3130

3231
if (children) {
3332
return (
@@ -44,7 +43,7 @@ export const QuickSearchInput = ({
4443
$direction="row"
4544
$align="center"
4645
className="quick-search-input"
47-
$gap={spacing['2xs']}
46+
$gap={spacingsTokens['2xs']}
4847
$padding={{ all: 'base' }}
4948
>
5049
{!loading && <Icon iconName="search" $variation="600" />}

src/frontend/apps/impress/src/components/quick-search/QuickSearchItemContent.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const QuickSearchItemContent = ({
1717
right,
1818
}: QuickSearchItemContentProps) => {
1919
const { spacingsTokens } = useCunninghamTheme();
20-
const spacings = spacingsTokens();
2120

2221
const { isDesktop } = useResponsiveStore();
2322

@@ -32,7 +31,7 @@ export const QuickSearchItemContent = ({
3231
<Box
3332
$direction="row"
3433
$align="center"
35-
$gap={spacings['2xs']}
34+
$gap={spacingsTokens['2xs']}
3635
$width="100%"
3736
>
3837
{left}

src/frontend/apps/impress/src/components/separators/HorizontalSeparator.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const HorizontalSeparator = ({
2626
$background={
2727
variant === SeparatorVariant.DARK
2828
? '#e5e5e533'
29-
: colorsTokens()['greyscale-100']
29+
: colorsTokens['greyscale-100']
3030
}
3131
className="--docs--horizontal-separator"
3232
/>

src/frontend/apps/impress/src/components/separators/SeparatedSection.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ export const SeparatedSection = ({
1313
showSeparator = true,
1414
children,
1515
}: PropsWithChildren<Props>) => {
16-
const theme = useCunninghamTheme();
17-
const colors = theme.colorsTokens();
18-
const spacings = theme.spacingsTokens();
16+
const { colorsTokens, spacingsTokens } = useCunninghamTheme();
1917
return (
2018
<Box
2119
$css={css`
2220
width: 100%;
23-
padding: ${spacings['sm']} 0;
21+
padding: ${spacingsTokens['sm']} 0;
2422
${showSeparator &&
2523
css`
26-
border-bottom: 1px solid ${colors?.['greyscale-200']};
24+
border-bottom: 1px solid ${colorsTokens['greyscale-200']};
2725
`}
2826
`}
2927
>

src/frontend/apps/impress/src/cunningham/__tests__/useCunninghamTheme.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe('<useCunninghamTheme />', () => {
44
it('has the logo correctly set', () => {
55
const { themeTokens, setTheme } = useCunninghamTheme.getState();
66
setTheme('default');
7-
const logo = themeTokens().logo;
7+
const logo = themeTokens.logo;
88
expect(logo?.src).toBe('/assets/logo-gouv.svg');
99
expect(logo?.widthHeader).toBe('110px');
1010
expect(logo?.widthFooter).toBe('220px');

src/frontend/apps/impress/src/cunningham/useCunninghamTheme.tsx

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,53 @@ import { create } from 'zustand';
33

44
import { tokens } from './cunningham-tokens';
55

6-
type Tokens = typeof tokens.themes.default;
6+
type Tokens = typeof tokens.themes.default &
7+
Partial<(typeof tokens.themes)[keyof typeof tokens.themes]>;
78
type ColorsTokens = Tokens['theme']['colors'];
89
type FontSizesTokens = Tokens['theme']['font']['sizes'];
910
type SpacingsTokens = Tokens['theme']['spacings'];
1011
type ComponentTokens = Tokens['components'];
1112
export type Theme = keyof typeof tokens.themes;
1213

13-
interface AuthStore {
14-
theme: string;
14+
interface ThemeStore {
15+
theme: Theme;
1516
setTheme: (theme: Theme) => void;
16-
themeTokens: () => Partial<Tokens['theme']>;
17-
colorsTokens: () => Partial<ColorsTokens>;
18-
fontSizesTokens: () => Partial<FontSizesTokens>;
19-
spacingsTokens: () => Partial<SpacingsTokens>;
20-
componentTokens: () => ComponentTokens;
17+
themeTokens: Partial<Tokens['theme']>;
18+
colorsTokens: Partial<ColorsTokens>;
19+
fontSizesTokens: Partial<FontSizesTokens>;
20+
spacingsTokens: Partial<SpacingsTokens>;
21+
componentTokens: ComponentTokens;
2122
}
2223

23-
export const useCunninghamTheme = create<AuthStore>((set, get) => {
24-
const currentTheme = () =>
25-
merge(
26-
tokens.themes['default'],
27-
tokens.themes[get().theme as keyof typeof tokens.themes],
28-
) as Tokens;
29-
30-
return {
31-
theme: 'default',
32-
themeTokens: () => currentTheme().theme,
33-
colorsTokens: () => currentTheme().theme.colors,
34-
componentTokens: () => currentTheme().components,
35-
spacingsTokens: () => currentTheme().theme.spacings,
36-
fontSizesTokens: () => currentTheme().theme.font.sizes,
37-
setTheme: (theme: Theme) => {
38-
set({ theme });
39-
},
40-
};
41-
});
24+
const getMergedTokens = (theme: Theme) => {
25+
return merge({}, tokens.themes['default'], tokens.themes[theme]);
26+
};
27+
28+
const DEFAULT_THEME: Theme = 'default';
29+
const defaultTokens = getMergedTokens(DEFAULT_THEME);
30+
31+
const initialState: ThemeStore = {
32+
theme: DEFAULT_THEME,
33+
setTheme: () => {},
34+
themeTokens: defaultTokens.theme,
35+
colorsTokens: defaultTokens.theme.colors,
36+
componentTokens: defaultTokens.components,
37+
spacingsTokens: defaultTokens.theme.spacings,
38+
fontSizesTokens: defaultTokens.theme.font.sizes,
39+
};
40+
41+
export const useCunninghamTheme = create<ThemeStore>((set) => ({
42+
...initialState,
43+
setTheme: (theme: Theme) => {
44+
const newTokens = getMergedTokens(theme);
45+
46+
set({
47+
theme,
48+
themeTokens: newTokens.theme,
49+
colorsTokens: newTokens.theme.colors,
50+
componentTokens: newTokens.components,
51+
spacingsTokens: newTokens.theme.spacings,
52+
fontSizesTokens: newTokens.theme.font.sizes,
53+
});
54+
},
55+
}));

src/frontend/apps/impress/src/features/docs/doc-editor/components/DocEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const DocEditor = ({ doc, versionId }: DocEditorProps) => {
6767
</Box>
6868

6969
<Box
70-
$background={colorsTokens()['primary-bg']}
70+
$background={colorsTokens['primary-bg']}
7171
$direction="row"
7272
$width="100%"
7373
$css="overflow-x: clip; flex: 1;"

src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DividerBlock.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export const DividerBlock = createReactBlockSpec(
2222
<Box
2323
as="hr"
2424
$width="100%"
25-
$background={colorsTokens()['greyscale-300']}
25+
$background={colorsTokens['greyscale-300']}
2626
$margin="1rem 0"
27-
$css={`border: 1px solid ${colorsTokens()['greyscale-300']};`}
27+
$css={`border: 1px solid ${colorsTokens['greyscale-300']};`}
2828
/>
2929
);
3030
},

0 commit comments

Comments
 (0)