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

fix: sharebutton visible on desktop #377

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions demo/src/Components/DocPage/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import {configure as configureUikit} from '@gravity-ui/uikit';
import cn from 'bem-cn-lite';

import {updateBodyClassName} from '../utils';
import {updateBodyClassName, useMobile} from '../utils';

import {getContent} from './data';
import './index.scss';
Expand Down Expand Up @@ -224,6 +224,7 @@ const DocPageDemo = (
const subscribe = useSubscribe();
const bookmarks = useBookmarks();
const notification = useNotification();
const mobileView = useMobile();
const search = useSearchResults(args['Search'] || '');
const pdf = usePdf(args['Pdf'] || '');

Expand All @@ -246,8 +247,8 @@ const DocPageDemo = (
theme,
textSize,
singlePage,
isMobile: mobileView,
};

Object.assign(
props,
...[
Expand Down
7 changes: 4 additions & 3 deletions demo/src/Components/LeadingPage/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useEffect} from 'react';
import {DocLeadingPage, DocLeadingPageData, Theme} from '@diplodoc/components';
import cn from 'bem-cn-lite';

import {updateBodyClassName} from '../utils';
import {updateBodyClassName, useMobile} from '../utils';

import pageContent from './page.json';

Expand All @@ -14,19 +14,20 @@ type Args = {
};

const DocLeadingPageDemo = (args: Args) => {
const isMobile = args['Mobile'];
const theme = args['Theme'];
const router = {pathname: '/docs/compute'};
const mobileView = useMobile();

useEffect(() => {
updateBodyClassName(theme);
}, [theme]);

return (
<div className={isMobile === 'true' ? 'mobile' : 'desktop'}>
<div className={mobileView ? 'mobile' : 'desktop'}>
<div className={layoutBlock('content')}>
<DocLeadingPage
{...(pageContent as DocLeadingPageData)}
isMobile={mobileView}
wideFormat={true}
router={router}
/>
Expand Down
29 changes: 29 additions & 0 deletions demo/src/Components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Theme} from '@diplodoc/components';
import {useCallback, useEffect, useState} from 'react';

export function updateBodyClassName(theme: Theme) {
const bodyEl = document.body;
Expand All @@ -10,3 +11,31 @@ export function updateBodyClassName(theme: Theme) {
bodyEl.classList.toggle('g-root_theme_light', theme === 'light');
bodyEl.classList.toggle('g-root_theme_dark', theme === 'dark');
}

const isBrowser = () => typeof document !== 'undefined';

function getMobileView() {
if (!isBrowser()) {
return false;
}

return document.body.clientWidth < 768;
}

export function useMobile() {
const [mobileView, setMobileView] = useState<boolean>(getMobileView());

const onResizeHandler = useCallback(() => {
setMobileView(getMobileView());
}, []);

useEffect(onResizeHandler, [onResizeHandler]);

useEffect(() => {
window.addEventListener('resize', onResizeHandler);

return () => window.removeEventListener('resize', onResizeHandler);
}, [onResizeHandler]);

return mobileView;
}
4 changes: 4 additions & 0 deletions src/components/DocLeadingPage/DocLeadingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {DocPageTitle} from '../DocPageTitle';
import {HTML} from '../HTML';
import {Text} from '../Text';
import {Notification, NotificationProps} from '../Notification';
import {ShareButton} from '../ShareButton';

import './DocLeadingPage.scss';

Expand All @@ -25,6 +26,7 @@ export interface DocLeadingPageProps extends DocLeadingPageData, NotificationPro
tocTitleIcon?: React.ReactNode;
useMainTag?: boolean;
legacyToc?: boolean;
isMobile?: boolean;
}

export interface DocLinkProps {
Expand Down Expand Up @@ -118,6 +120,7 @@ export const DocLeadingPage: React.FC<DocLeadingPageProps> = ({
legacyToc,
notification,
notificationCb,
isMobile,
}) => {
const modes = {
'regular-page-width': !wideFormat,
Expand All @@ -138,6 +141,7 @@ export const DocLeadingPage: React.FC<DocLeadingPageProps> = ({
<DocLayout.Center>
<Notification notification={notification} notificationCb={notificationCb} />
<ContentWrapper className={b('main')} useMainTag={useMainTag}>
{isMobile && <ShareButton className={b('share-button')} title={title} />}
<DocPageTitle stage={toc.stage} className={b('title')}>
<HTML>{title}</HTML>
</DocPageTitle>
Expand Down
5 changes: 0 additions & 5 deletions src/components/DocPage/DocPage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@
&:hover::before {
background-color: transparent;
}

// ShareButton in title is currently only available on mobile devices.
@media (min-width: map.get(variables.$screenBreakpoints, 'md')) {
display: none;
}
}

&__content {
Expand Down
5 changes: 3 additions & 2 deletions src/components/DocPage/DocPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface DocPageProps extends DocPageData, DocSettings, NotificationProp
pdfLink?: string;
onMiniTocItemClick?: (event: MouseEvent) => void;
useMainTag?: boolean;
isMobile?: boolean;
}

type DocPageInnerProps = InnerProps<DocPageProps, DocSettings>;
Expand Down Expand Up @@ -403,9 +404,9 @@ class DocPage extends React.Component<DocPageInnerProps, DocPageState> {
}

private renderTitle() {
const {title, headerHeight, meta, bookmarkedPage, onChangeBookmarkPage} = this.props;
const {title, meta, bookmarkedPage, onChangeBookmarkPage, isMobile} = this.props;
const withBookmarks = onChangeBookmarkPage;
const withShare = Number(headerHeight) > 0 && !this.showMiniToc;
const withShare = isMobile;

if (!title) {
return null;
Expand Down