Skip to content

Commit

Permalink
refactoring: move components to ui and utils to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
dgaponov committed Jun 16, 2024
1 parent 3860843 commit 2f5b3d9
Show file tree
Hide file tree
Showing 41 changed files with 466 additions and 355 deletions.
10 changes: 5 additions & 5 deletions src/components/Theme/Theme.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {Col, Grid, Row} from '@gravity-ui/page-constructor';
import React from 'react';

import {ColorsTab} from './ColorsTab';
import {ThemeCreator} from './ThemeCreator';
import {DEFAULT_THEME} from './constants';
import {DEFAULT_THEME} from './lib/constants';
import {ColorsTab} from './ui/ColorsTab';
import {ThemeCreatorContextProvider} from './ui/ThemeCreatorContextProvider';

export const Theme = () => {
return (
<ThemeCreator theme={DEFAULT_THEME}>
<ThemeCreatorContextProvider initialTheme={DEFAULT_THEME}>
<Grid>
<Row>
<Col sizes={12}>
<ColorsTab />
</Col>
</Row>
</Grid>
</ThemeCreator>
</ThemeCreatorContextProvider>
);
};
33 changes: 0 additions & 33 deletions src/components/Theme/ThemeCreator/ThemeCreator.tsx

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/Theme/ThemeCreator/ThemeCreatorContext.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/Theme/ThemeCreator/index.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/components/Theme/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {useThemeCreator, useThemeCreatorMethods} from './useThemeCreator';
export {useThemePalette, useThemePaletteColor} from './useThemePalette';
export {useThemeUtilityColor} from './useThemeUtilityColor';
export {useThemePrivateColorOptions} from './useThemePrivateColorOptions';
31 changes: 0 additions & 31 deletions src/components/Theme/hooks/useThemeColor.ts

This file was deleted.

65 changes: 3 additions & 62 deletions src/components/Theme/hooks/useThemeCreator.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,6 @@
import React from 'react';

import {ThemeCreatorContext} from '../ThemeCreator';
import {ThemeVariant} from '../types';
import {
addColorToTheme,
getThemeColorOptions,
getThemePalette,
removeColorFromTheme,
renameColorInTheme,
updateColorInTheme,
} from '../utils';
import type {AddColorToThemeParams, UpdateColorInThemeParams} from '../utils';
import {ThemeCreatorContext, ThemeCreatorMethodsContext} from '../lib/themeCreatorContext';

export const useThemeCreator = () => {
const {state, updateState} = React.useContext(ThemeCreatorContext);

const addColor = React.useCallback(
(params?: AddColorToThemeParams) => {
const newState = addColorToTheme(state, params);
updateState(newState);
},
[state],
);

const updateColor = React.useCallback(
(params: UpdateColorInThemeParams) => {
const newState = updateColorInTheme(state, params);
updateState(newState);
},
[state],
);

const removeColor = React.useCallback(
(colorTitle: string) => {
const newState = removeColorFromTheme(state, colorTitle);
updateState(newState);
},
[state],
);

const renameColor = React.useCallback(
(oldTitle: string, newTitle: string) => {
const newState = renameColorInTheme(state, oldTitle, newTitle);
updateState(newState);
},
[state],
);

const palette = React.useMemo(() => getThemePalette(state), [state]);

const getThemePrivateColorOptions = React.useCallback(
(themeVariant: ThemeVariant) => getThemeColorOptions({themeState: state, themeVariant}),
[state],
);

return {
addColor,
updateColor,
removeColor,
renameColor,
palette,
getThemePrivateColorOptions,
};
};
export const useThemeCreator = () => React.useContext(ThemeCreatorContext);
export const useThemeCreatorMethods = () => React.useContext(ThemeCreatorMethodsContext);
36 changes: 36 additions & 0 deletions src/components/Theme/hooks/useThemePalette.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';

import {getThemePalette} from '../lib/themeCreatorUtils';
import type {ThemeVariant} from '../lib/types';

import {useThemeCreator, useThemeCreatorMethods} from './useThemeCreator';

export const useThemePalette = () => {
const themeState = useThemeCreator();
return React.useMemo(() => getThemePalette(themeState), [themeState]);
};

type UseThemePaletteColorParams = {
token: string;
theme: ThemeVariant;
};

export const useThemePaletteColor = ({token, theme}: UseThemePaletteColorParams) => {
const themeState = useThemeCreator();
const {updateColor} = useThemeCreatorMethods();

const value = React.useMemo(() => themeState.palette[theme][token], [themeState, token, theme]);

const updateValue = React.useCallback(
(newValue: string) => {
updateColor({
theme,
title: token,
value: newValue,
});
},
[token, theme, updateColor],
);

return [value, updateValue] as const;
};
30 changes: 0 additions & 30 deletions src/components/Theme/hooks/useThemePaletteColor.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/components/Theme/hooks/useThemePrivateColorOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

import {getThemeColorOptions} from '../lib/themeCreatorUtils';
import {ThemeVariant} from '../lib/types';

import {useThemeCreator} from './useThemeCreator';

export const useThemePrivateColorOptions = (themeVariant: ThemeVariant) => {
const themeState = useThemeCreator();

return React.useMemo(
() => getThemeColorOptions({themeState, themeVariant}),
[themeState, themeVariant],
);
};
30 changes: 30 additions & 0 deletions src/components/Theme/hooks/useThemeUtilityColor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';

import type {ColorsOptions, ThemeVariant} from '../lib/types';

import {useThemeCreator, useThemeCreatorMethods} from './useThemeCreator';

type UseThemeColorParams = {
name: keyof ColorsOptions;
theme: ThemeVariant;
};

export const useThemeUtilityColor = ({name, theme}: UseThemeColorParams) => {
const themeState = useThemeCreator();
const {changeUtilityColor} = useThemeCreatorMethods();

const value = React.useMemo(() => themeState.colors[theme][name], [themeState, name, theme]);

const updateValue = React.useCallback(
(newValue: string) => {
changeUtilityColor({
themeVariant: theme,
name,
value: newValue,
});
},
[name, theme, changeUtilityColor],
);

return [value, updateValue] as const;
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions src/components/Theme/lib/themeCreatorContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import noop from 'lodash/noop';
import {createContext} from 'react';

import {DEFAULT_THEME} from './constants';
import {initThemeCreator} from './themeCreatorUtils';
import type {
AddColorToThemeParams,
ChangeUtilityColorInThemeParams,
RenameColorInThemeParams,
UpdateColorInThemeParams,
} from './themeCreatorUtils';
import type {ThemeCreatorState} from './types';

export const ThemeCreatorContext = createContext<ThemeCreatorState>(
initThemeCreator(DEFAULT_THEME),
);

export interface ThemeCreatorMethodsContextType {
addColor: (params?: AddColorToThemeParams) => void;
updateColor: (params: UpdateColorInThemeParams) => void;
removeColor: (title: string) => void;
renameColor: (params: RenameColorInThemeParams) => void;
changeUtilityColor: (params: ChangeUtilityColorInThemeParams) => void;
}

export const ThemeCreatorMethodsContext = createContext<ThemeCreatorMethodsContextType>({
addColor: noop,
updateColor: noop,
removeColor: noop,
renameColor: noop,
changeUtilityColor: noop,
});
Loading

0 comments on commit 2f5b3d9

Please sign in to comment.