Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add language control selector for static build #33

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/components/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {ReactElement, useEffect} from 'react';
import React, {ReactElement, useCallback, useEffect} from 'react';

import {
NavigationData,
Expand All @@ -17,6 +17,7 @@ import {
Lang,
Router,
Theme,
getLangPath,
getPageByType,
getPageType,
} from '@diplodoc/components';
Expand All @@ -39,6 +40,7 @@ import {ConstructorPage} from '../ConstructorPage';

export interface AppProps {
lang: Lang;
langs: Lang[];
router: Router;
type: DocumentType;
}
Expand Down Expand Up @@ -112,12 +114,19 @@ type TocData = DocPageData['toc'] & {
};

export function App(props: DocInnerProps): ReactElement {
const {data, router, lang} = props;
const {data, router, lang, langs} = props;
const {navigation} = data.toc as TocData;

const settings = useSettings();
const mobileView = useMobile();

const onChangeLang = useCallback(
(lang: Lang) => {
window.location.replace(getLangPath(router, lang));
},
[router],
);

const {theme, textSize, wideFormat, fullScreen, showMiniToc, onChangeFullScreen} = settings;
const fullHeader = !fullScreen && Boolean(navigation);
const headerHeight = fullHeader ? 64 : 0;
Expand All @@ -128,6 +137,7 @@ export function App(props: DocInnerProps): ReactElement {
data,
router,
lang,
langs,
wideFormat,
showMiniToc,
theme,
Expand All @@ -152,11 +162,16 @@ export function App(props: DocInnerProps): ReactElement {
});
}, [theme, mobileView, wideFormat, fullHeader]);

const pageContext = {
...settings,
onChangeLang,
};

if (!navigation) {
return (
<div className="App">
<ThemeProvider theme={theme} direction={direction}>
<Page {...pageProps} {...settings}>
<Page {...pageProps} {...pageContext}>
{type === DocumentType.PageConstructor && (
<PageConstructorProvider theme={theme}>
<PageConstructor
Expand Down Expand Up @@ -203,12 +218,20 @@ export function App(props: DocInnerProps): ReactElement {
custom={{
navigation: {
controls: () => (
<HeaderControls {...settings} mobileView={mobileView} />
<HeaderControls
{...pageContext}
{...pageProps}
onChangeLang={onChangeLang}
mobileView={mobileView}
/>
),
},
blocks: {
page: () => (
<Page {...pageProps} {...(headerWithControls ? {} : settings)}>
<Page
{...pageProps}
{...(headerWithControls ? {} : pageContext)}
>
{type === DocumentType.PageConstructor &&
'data' in data && (
<ConstructorPage
Expand Down
12 changes: 11 additions & 1 deletion src/components/HeaderControls/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {memo} from 'react';

import {ControlSizes, Controls, ControlsLayout, TextSizes, Theme} from '@diplodoc/components';
import {ControlSizes, Controls, ControlsLayout, Lang, TextSizes, Theme} from '@diplodoc/components';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type OnChangeCallback = (value: any) => void;
Expand All @@ -16,6 +16,9 @@ type Props = {
onChangeWideFormat: OnChangeCallback;
showMiniToc: boolean;
onChangeShowMiniToc: OnChangeCallback;
lang: Lang;
langs: Lang[];
onChangeLang?: (lang: Lang) => void;
};

export const HeaderControls = memo<Props>(
Expand All @@ -33,6 +36,10 @@ export const HeaderControls = memo<Props>(

showMiniToc,
onChangeShowMiniToc,

lang,
langs,
onChangeLang,
}) => {
return (
<ControlsLayout
Expand All @@ -51,6 +58,9 @@ export const HeaderControls = memo<Props>(
onChangeShowMiniToc={onChangeShowMiniToc}
textSize={textSize}
onChangeTextSize={onChangeTextSize}
lang={lang}
langs={langs}
onChangeLang={onChangeLang}
/>
</ControlsLayout>
);
Expand Down
Loading