Skip to content

Commit

Permalink
пренесла на другую ветку
Browse files Browse the repository at this point in the history
  • Loading branch information
Daria Dmitrienko committed Oct 10, 2024
1 parent 11deff0 commit d34adac
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 19 deletions.
8 changes: 6 additions & 2 deletions src/components/arrow-button/ArrowButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import styles from './ArrowButton.module.scss';
/** Функция для обработки открытия/закрытия формы */
export type OnClick = () => void;

export const ArrowButton = () => {
export interface ArrowButtonProps {
onClick?: OnClick
}

export const ArrowButton = ({ onClick }: ArrowButtonProps) => {
return (
/* Не забываем указаывать role и aria-label атрибуты для интерактивных элементов */
<div
<div onClick={onClick}
role='button'
aria-label='Открыть/Закрыть форму параметров статьи'
tabIndex={0}
Expand Down
127 changes: 121 additions & 6 deletions src/components/article-params-form/ArticleParamsForm.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,135 @@
import { ArrowButton } from 'components/arrow-button';
import { Button } from 'components/button';
import { useRef, useState } from 'react';

import styles from './ArticleParamsForm.module.scss';
import { Select } from '../select';
import { RadioGroup } from '../radio-group'
import { fontFamilyOptions, fontColors, backgroundColors, contentWidthArr, fontSizeOptions, defaultArticleState, ArticleStateType } from 'src/constants/articleProps';
import { useOutsideClickClose } from '../select/hooks/useOutsideClickClose';
import { Separator } from '../separator';
import { Spacer } from '../spacer';


interface propsArticleParamsForm {
onChange?: (data: ArticleStateType) => void;
}

export const ArticleParamsForm = ({ onChange }: propsArticleParamsForm) => {
const [sidebarIsOpen, setIsSidebarOpen] = useState(false);
const [selectedFont, setSelectedFont] = useState(defaultArticleState.fontFamilyOption);
const [selectedFontSize, setSelectedFontSize] = useState(defaultArticleState.fontSizeOption);
const [selectedFontColor, setSelectedFontColor] = useState(defaultArticleState.fontColor);
const [selectedBackgroundColor, setSelectedBackgroundColor] = useState(defaultArticleState.backgroundColor);
const [selectedWidthContent, setSelectedWidthContent] = useState(defaultArticleState.contentWidth);

const rootRef = useRef<HTMLDivElement>(null);
let lastStyles = useRef(defaultArticleState);

useOutsideClickClose({
isOpen: sidebarIsOpen,
rootRef: rootRef,
onClose: () => {
setSelectedFont(lastStyles.current.fontFamilyOption)
setSelectedFontSize(lastStyles.current.fontSizeOption)
setSelectedFontColor(lastStyles.current.fontColor)
setSelectedBackgroundColor(lastStyles.current.backgroundColor)
setSelectedWidthContent(lastStyles.current.contentWidth)
},
onChange: setIsSidebarOpen,
});

const toggleSidebar = () => {
// Вызов функции для изменения состояния
setIsSidebarOpen(!sidebarIsOpen);
}

const submitHandler = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

lastStyles.current = {
fontFamilyOption: selectedFont,
fontColor: selectedFontColor,
backgroundColor: selectedBackgroundColor,
contentWidth: selectedWidthContent,
fontSizeOption: selectedFontSize,
};

if (onChange) {
onChange(lastStyles.current)
}
}

const resetHandler = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setSelectedFontColor(defaultArticleState.fontColor)
setSelectedFont(defaultArticleState.fontFamilyOption)
setSelectedFontSize(defaultArticleState.fontSizeOption)
setSelectedBackgroundColor(defaultArticleState.backgroundColor)
setSelectedWidthContent(defaultArticleState.contentWidth)
if (onChange) {
onChange(defaultArticleState)
}
}

export const ArticleParamsForm = () => {
return (
<>
<ArrowButton />
<aside className={styles.container}>
<form className={styles.form}>
<div ref={rootRef}>
<ArrowButton onClick={toggleSidebar} />
<aside className={`${styles.container} ${sidebarIsOpen ? styles.container_open : ''}`}>

<form className={styles.form} onSubmit={submitHandler} onReset={resetHandler} >

<Select
selected={selectedFont}
onChange={setSelectedFont}
options={fontFamilyOptions}
title='шрифт'
/>

<Spacer />

<RadioGroup
selected={selectedFontSize}
name='radio'
onChange={setSelectedFontSize}
options={fontSizeOptions}
title='размер шрифта'
/>

<Spacer />

<Select
selected={selectedFontColor}
onChange={setSelectedFontColor}
options={fontColors}
title='цвет шрифта'
/>
<Spacer />

<Separator />
<Spacer />

<Select
selected={selectedBackgroundColor}
onChange={setSelectedBackgroundColor}
options={backgroundColors}
title='цвет фона'
/>
<Spacer />

<Select
selected={selectedWidthContent}
onChange={setSelectedWidthContent}
options={contentWidthArr}
title='ширина контента'
/>

<div className={styles.bottomContainer}>
<Button title='Сбросить' type='reset' />
<Button title='Применить' type='submit' />
</div>
</form>
</aside>
</>
</div>
);
};
4 changes: 2 additions & 2 deletions src/components/select/hooks/useOutsideClickClose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export const useOutsideClickClose = ({
}
};

window.addEventListener('click', handleClick);
window.addEventListener('mousedown', handleClick);

return () => {
window.removeEventListener('click', handleClick);
window.removeEventListener('mousedown', handleClick);
};
}, [onClose, onChange, isOpen]);
};
2 changes: 1 addition & 1 deletion src/components/separator/index.module.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.separator {
width: 100%;
height: 1px;
background: #000000;
background: #D7D7D7;
}
16 changes: 16 additions & 0 deletions src/components/spacer/Spacer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Meta, StoryObj } from '@storybook/react';

import { Spacer } from './Spacer';

const meta: Meta<typeof Spacer> = {
component: Spacer,
};

export default meta;
type Story = StoryObj<typeof Spacer>;

export const SelectStory: Story = {
render: () => {
return <Spacer />;
},
};
5 changes: 5 additions & 0 deletions src/components/spacer/Spacer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import styles from './index.module.scss';

export const Spacer = () => {
return <div className={styles.spacer}></div>;
};
4 changes: 4 additions & 0 deletions src/components/spacer/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.spacer {
width: 100%;
height: 50px;
}
1 change: 1 addition & 0 deletions src/components/spacer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Spacer } from './Spacer';
23 changes: 15 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createRoot } from 'react-dom/client';
import { StrictMode, CSSProperties } from 'react';
import { StrictMode, CSSProperties, useState } from 'react';
import clsx from 'clsx';

import { Article } from './components/article/Article';
import { ArticleParamsForm } from './components/article-params-form/ArticleParamsForm';
import { defaultArticleState } from './constants/articleProps';
import { ArticleStateType, defaultArticleState, fontFamilyOptions } from './constants/articleProps';

import './styles/index.scss';
import styles from './styles/index.module.scss';
Expand All @@ -13,19 +13,26 @@ const domNode = document.getElementById('root') as HTMLDivElement;
const root = createRoot(domNode);

const App = () => {
const [appState, setAppState] = useState(defaultArticleState);

const changeHandler = (newAppState: ArticleStateType) => {
console.log('changeee')
setAppState(newAppState)
}

return (
<div
className={clsx(styles.main)}
style={
{
'--font-family': defaultArticleState.fontFamilyOption.value,
'--font-size': defaultArticleState.fontSizeOption.value,
'--font-color': defaultArticleState.fontColor.value,
'--container-width': defaultArticleState.contentWidth.value,
'--bg-color': defaultArticleState.backgroundColor.value,
'--font-family': appState.fontFamilyOption.value,
'--font-size': appState.fontSizeOption.value,
'--font-color': appState.fontColor.value,
'--container-width': appState.contentWidth.value,
'--bg-color': appState.backgroundColor.value,
} as CSSProperties
}>
<ArticleParamsForm />
<ArticleParamsForm onChange={changeHandler} />
<Article />
</div>
);
Expand Down

0 comments on commit d34adac

Please sign in to comment.