Skip to content

Commit

Permalink
feat: add portals for modal
Browse files Browse the repository at this point in the history
  • Loading branch information
TomatoVan committed Nov 6, 2023
1 parent 18a141f commit 37bb475
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 26 deletions.
3 changes: 3 additions & 0 deletions extractedTranslations/en/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Войти": ""
}
14 changes: 1 addition & 13 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import React, { Suspense, useState } from 'react';
import React, { Suspense } from 'react';
import './styles/index.scss';
import { classNames } from 'shared/lib/classNames/classNames';
import { useTheme } from 'app/providers/ThemeProvider';
import { AppRouter } from 'app/providers/router';
import { Navbar } from 'widgets/Navbar';
import { Sidebar } from 'widgets/Sidebar';
import { Modal } from 'shared/ui/Modal/Modal';

function App() {
const { theme } = useTheme();

const [isOpen, setIsOpen] = useState(false);

return (
<div className={classNames('app', {}, [theme])}>
<Suspense fallback="">
<Navbar />
<button type="button" onClick={() => setIsOpen(true)}>
toggle
</button>
<Modal isOpen={isOpen} onClose={() => setIsOpen(false)}>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A ab
accusantium aliquid architecto autem consequuntur debitis et facere,
laudantium modi nostrum nulla obcaecati odit quas quod sapiente,
veritatis? Illo, veritatis.
</Modal>
<div className="content-page">
<Sidebar />
<AppRouter />
Expand Down
7 changes: 5 additions & 2 deletions src/app/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
@import "themes/normal";
@import "themes/dark";

.app {
body {
font: var(--font-m);
background: var(--bg-color);
color: var(--primary-color);
}

.app {
background: var(--bg-color);
min-height: 100vh;
}

Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { render } from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { ThemeProvider } from 'app/providers/ThemeProvider';
import App from './app/App';
import 'app/styles/index.scss';

import './shared/config/i18n/i18n';
import { ErrorBoundary } from './app/providers/ErrorBoundary';
Expand Down
7 changes: 6 additions & 1 deletion src/shared/ui/Button/Button.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
color: var(--primary-color);
}

.clear {
.clear,
.clearInverted {
padding: 0;
border: none;
background: none;
outline: none;
}

.clearInverted {
color: var(--inverted-primary-color);
}

.outline {
padding: 10px 15px;
border: 1px solid var(--primary-color);
Expand Down
6 changes: 6 additions & 0 deletions src/shared/ui/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Clear.args = {
theme: ButtonTheme.CLEAR,
};

export const ClearInverted = Template.bind({});
ClearInverted.args = {
children: 'Text',
theme: ButtonTheme.CLEAR_INVERTED,
};

export const Outline = Template.bind({});
Outline.args = {
children: 'Text',
Expand Down
1 change: 1 addition & 0 deletions src/shared/ui/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cls from './Button.module.scss';

export enum ButtonTheme {
CLEAR = 'clear',
CLEAR_INVERTED = 'clearInverted',
OUTLINE = 'outline',
BACKGROUND = 'background',
BACKGROUND_INVERTED = 'backgroundInverted',
Expand Down
13 changes: 8 additions & 5 deletions src/shared/ui/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useState,
} from 'react';
import { classNames } from 'shared/lib/classNames/classNames';
import { Portal } from 'shared/ui/Portal/Portal';
import cls from './Modal.module.scss';

interface ModalProps {
Expand Down Expand Up @@ -65,12 +66,14 @@ export const Modal = (props: ModalProps) => {
};

return (
<div className={classNames(cls.Modal, mods, [className])}>
<div className={cls.overlay} onClick={closeHandler}>
<div className={cls.content} onClick={onContentClick}>
{children}
<Portal>
<div className={classNames(cls.Modal, mods, [className])}>
<div className={cls.overlay} onClick={closeHandler}>
<div className={cls.content} onClick={onContentClick}>
{children}
</div>
</div>
</div>
</div>
</Portal>
);
};
16 changes: 16 additions & 0 deletions src/shared/ui/Portal/Portal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ReactNode } from 'react';
import { createPortal } from 'react-dom';

interface PortalProps {
children: ReactNode;
element?: HTMLElement;
}

export const Portal = (props: PortalProps) => {
const {
children,
element = document.body,
} = props;

return createPortal(children, element);
};
35 changes: 30 additions & 5 deletions src/widgets/Navbar/ui/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import { classNames } from 'shared/lib/classNames/classNames';
import { Modal } from 'shared/ui/Modal/Modal';
import React, { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, ButtonTheme } from 'shared/ui/Button/Button';
import cls from './Navbar.module.scss';

interface NavbarProps {
className?: string;
}

export const Navbar = ({ className }: NavbarProps) => (
<div className={classNames(cls.Navbar, {}, [className])}>
<div className={cls.links} />
</div>
);
export const Navbar = ({ className }: NavbarProps) => {
const { t } = useTranslation();
const [isAuthModal, setIsAuthModal] = useState(false);

const onToggleModal = useCallback(() => {
setIsAuthModal((prev) => !prev);
}, []);

return (
<div className={classNames(cls.Navbar, {}, [className])}>
<Button
theme={ButtonTheme.CLEAR_INVERTED}
className={cls.links}
onClick={onToggleModal}
>
{t('Войти')}
</Button>
<Modal isOpen={isAuthModal} onClose={onToggleModal}>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A ab
accusantium aliquid architecto autem consequuntur debitis et facere,
laudantium modi nostrum nulla obcaecati odit quas quod sapiente,
veritatis? Illo, veritatis.
</Modal>
</div>
);
};

0 comments on commit 37bb475

Please sign in to comment.