From 6e08a9188fd53d245c6f1c117099622e3ce755a8 Mon Sep 17 00:00:00 2001 From: ldrv565 <40745264+ldrv565@users.noreply.github.com> Date: Mon, 27 Nov 2023 12:24:19 +0300 Subject: [PATCH] feat: add AsideFallback (#161) * feat: add AsideFallback * chore: share HEADER_DIVIDER_HEIGHT --- README.md | 27 +++++++++---- rollup.config.js | 1 + src/components/AsideHeader/AsideHeader.scss | 4 -- src/components/AsideHeader/AsideHeader.tsx | 15 -------- .../__stories__/AsideHeader.stories.tsx | 37 +++++++++++++++++- .../AsideHeader/components/Header.tsx | 4 +- .../components/PageLayout/AsideFallback.tsx | 38 +++++++++++++++++++ .../components/PageLayout/PageLayout.tsx | 21 ++++++---- src/components/AsideHeader/types.tsx | 1 - src/components/CompositeBar/constants.ts | 1 - src/components/CompositeBar/utils.ts | 3 +- src/components/constants.ts | 2 + src/components/index.ts | 1 + 13 files changed, 114 insertions(+), 41 deletions(-) create mode 100644 src/components/AsideHeader/components/PageLayout/AsideFallback.tsx diff --git a/README.md b/README.md index 3e6f8ac..3d7bf6e 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ npm install --dev @gravity-ui/uikit@^3.0.2 @bem-react/classname@1.6.0 react@^16. - DrawerItem - PageLayout - PageLayoutAside +- AsideFallback ## Optimization @@ -33,23 +34,35 @@ If your app content needs to be rendered faster than by passing it throw `AsideH you may need to switch usage of `AsideHeader` to advanced style with `PageLayout` like this: ```diff --import {AsideHeader} from '@gravity-ui/navigation'; -+import {PageLayout} from '@gravity-ui/navigation'; -+ -+const PageLayoutAside = React.lazy(() => -+ import('@gravity-ui/navigation').then((module) => ({default: module.PageLayoutAside})), +--- Main.tsx ++++ Main.tsx +-import {AsideHeader} from './AsideHeader' ++import {PageLayout, AsideFallback} from '@gravity-ui/navigation'; ++const Aside = React.lazy(() => ++ import('./Aside').then(({Aside}) => ({ default: Aside })) +); - + -+ -+ ++ }> ++ + + + + + + +--- Aside.tsx ++++ Aside.tsx +-import {AsideHeader} from '@gravity-ui/navigation'; ++import {PageLayoutAside} from '@gravity-ui/navigation'; + +export const Aside: FC = () => { + return ( +- ++ + ); +}; ``` ## Imports diff --git a/rollup.config.js b/rollup.config.js index 05d1c97..ce3ef55 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -21,6 +21,7 @@ const input = [ 'src/components/FooterItem/FooterItem.tsx', 'src/components/AsideHeader/components/PageLayout/PageLayout.tsx', 'src/components/AsideHeader/components/PageLayout/PageLayoutAside.tsx', + 'src/components/AsideHeader/components/PageLayout/AsideFallback.tsx', ]; const getPlugins = (outDir) => { diff --git a/src/components/AsideHeader/AsideHeader.scss b/src/components/AsideHeader/AsideHeader.scss index 21577ec..fc932b5 100644 --- a/src/components/AsideHeader/AsideHeader.scss +++ b/src/components/AsideHeader/AsideHeader.scss @@ -223,10 +223,6 @@ $block: '.#{variables.$ns}aside-header'; } } - &_reverse #{$block}__pane-container { - flex-direction: row-reverse; - } - &__content { width: calc(100% - var(--gn-aside-header-size)); z-index: 95; diff --git a/src/components/AsideHeader/AsideHeader.tsx b/src/components/AsideHeader/AsideHeader.tsx index 9f45eef..0e284dc 100644 --- a/src/components/AsideHeader/AsideHeader.tsx +++ b/src/components/AsideHeader/AsideHeader.tsx @@ -4,21 +4,6 @@ import {AsideHeaderProps} from './types'; import {PageLayout} from './components/PageLayout/PageLayout'; import {PageLayoutAside} from './components/PageLayout/PageLayoutAside'; -/** - * Simply usage of AsideHeader: - * @example - * - * - * Advanced usage of AsideHeader: - * @example - * - * - * - * - * - * - * - */ export const AsideHeader = React.forwardRef( ({compact, className, topAlert, ...props}, ref) => { return ( diff --git a/src/components/AsideHeader/__stories__/AsideHeader.stories.tsx b/src/components/AsideHeader/__stories__/AsideHeader.stories.tsx index 2ccd1a2..e3f61c3 100644 --- a/src/components/AsideHeader/__stories__/AsideHeader.stories.tsx +++ b/src/components/AsideHeader/__stories__/AsideHeader.stories.tsx @@ -8,6 +8,8 @@ import {PageLayout} from '../components/PageLayout/PageLayout'; import {PageLayoutAside} from '../components/PageLayout/PageLayoutAside'; import logoIcon from '../../../../.storybook/assets/logo.svg'; import {menuItemsShowcase} from './moc'; +import {AsideFallback} from '../components/PageLayout/AsideFallback'; +import {Button} from '@gravity-ui/uikit'; export default { title: 'components/AsideHeader', @@ -34,8 +36,7 @@ const AdvancedUsageTemplate: StoryFn = (args) => { const [compact, setCompact] = React.useState(args.initialCompact); return ( - - PageContent + { onChangeCompact={setCompact} {...args} /> + + PageContent ); }; @@ -80,3 +83,33 @@ HeaderAlertCentered.args = { dense: true, }, }; + +const fallbackArgs = { + headerDecoration: true, + subheaderItemsCount: 2, +}; + +const FallbackTemplate: StoryFn = ({ + headerDecoration, + subheaderItemsCount, +}) => { + const [compact, setCompact] = React.useState(false); + + return ( + + + + + + setCompact((prev) => !prev)}>Toggle compact + + + + ); +}; + +export const Fallback = FallbackTemplate.bind({}); +Fallback.args = fallbackArgs; diff --git a/src/components/AsideHeader/components/Header.tsx b/src/components/AsideHeader/components/Header.tsx index 7e9b8a1..31efa37 100644 --- a/src/components/AsideHeader/components/Header.tsx +++ b/src/components/AsideHeader/components/Header.tsx @@ -3,7 +3,7 @@ import React, {useCallback} from 'react'; import {Icon} from '@gravity-ui/uikit'; import {SubheaderMenuItem} from '../../types'; -import {ASIDE_HEADER_COMPACT_WIDTH} from '../../constants'; +import {ASIDE_HEADER_COMPACT_WIDTH, HEADER_DIVIDER_HEIGHT} from '../../constants'; import {Logo} from '../../Logo/Logo'; import {CompositeBar} from '../../CompositeBar/CompositeBar'; @@ -40,7 +40,7 @@ export const Header = () => { data={headerDividerCollapsedIcon} className={b('header-divider')} width={ASIDE_HEADER_COMPACT_WIDTH} - height="29" + height={HEADER_DIVIDER_HEIGHT} /> ); diff --git a/src/components/AsideHeader/components/PageLayout/AsideFallback.tsx b/src/components/AsideHeader/components/PageLayout/AsideFallback.tsx new file mode 100644 index 0000000..bd35e19 --- /dev/null +++ b/src/components/AsideHeader/components/PageLayout/AsideFallback.tsx @@ -0,0 +1,38 @@ +import React from 'react'; +import {useAsideHeaderContext} from '../../AsideHeaderContext'; +import headerDividerCollapsedIcon from '../../../../../assets/icons/divider-collapsed.svg'; +import {b} from '../../utils'; +import {Icon} from '@gravity-ui/uikit'; +import {ASIDE_HEADER_COMPACT_WIDTH, HEADER_DIVIDER_HEIGHT, ITEM_HEIGHT} from '../../../constants'; + +export interface Props { + headerDecoration?: boolean; + subheaderItemsCount?: number; +} + +export const AsideFallback: React.FC = ({headerDecoration, subheaderItemsCount = 0}) => { + const {compact} = useAsideHeaderContext(); + + const widthVar = compact ? '--gn-aside-header-min-width' : '--gn-aside-header-size'; + + const subheaderHeight = (1 + subheaderItemsCount) * ITEM_HEIGHT; + + return ( + + + + + {compact ? ( + + ) : null} + + + + + ); +}; diff --git a/src/components/AsideHeader/components/PageLayout/PageLayout.tsx b/src/components/AsideHeader/components/PageLayout/PageLayout.tsx index b0acd78..691a55c 100644 --- a/src/components/AsideHeader/components/PageLayout/PageLayout.tsx +++ b/src/components/AsideHeader/components/PageLayout/PageLayout.tsx @@ -1,30 +1,35 @@ -import React, {PropsWithChildren, useMemo} from 'react'; +import React, {PropsWithChildren, Suspense, useMemo} from 'react'; import {AsideHeaderContextProvider, useAsideHeaderContext} from '../../AsideHeaderContext'; import {Content, ContentProps} from '../../../Content'; -import {TopPanel} from '..'; import {ASIDE_HEADER_COMPACT_WIDTH, ASIDE_HEADER_EXPANDED_WIDTH} from '../../../constants'; import {LayoutProps} from '../../types'; import {b} from '../../utils'; import '../../AsideHeader.scss'; -export interface PageLayoutProps extends PropsWithChildren { - reverse?: boolean; -} +const TopPanel = React.lazy(() => + import('../TopPanel').then((module) => ({default: module.TopPanel})), +); -const Layout = ({compact, reverse, className, children, topAlert}: PageLayoutProps) => { +export interface PageLayoutProps extends PropsWithChildren {} + +const Layout = ({compact, className, children, topAlert}: PageLayoutProps) => { const size = compact ? ASIDE_HEADER_COMPACT_WIDTH : ASIDE_HEADER_EXPANDED_WIDTH; const asideHeaderContextValue = useMemo(() => ({size, compact}), [compact, size]); return ( - {topAlert && } + {topAlert && ( + + + + )} {children} diff --git a/src/components/AsideHeader/types.tsx b/src/components/AsideHeader/types.tsx index c96dff5..0457386 100644 --- a/src/components/AsideHeader/types.tsx +++ b/src/components/AsideHeader/types.tsx @@ -18,7 +18,6 @@ export interface LayoutProps { export interface AsideHeaderGeneralProps { logo: LogoProps; multipleTooltip?: boolean; - reverse?: boolean; className?: string; collapseTitle?: string; expandTitle?: string; diff --git a/src/components/CompositeBar/constants.ts b/src/components/CompositeBar/constants.ts index d16a43d..9c0beda 100644 --- a/src/components/CompositeBar/constants.ts +++ b/src/components/CompositeBar/constants.ts @@ -1,6 +1,5 @@ import {PopupPlacement} from '@gravity-ui/uikit'; -export const ITEM_HEIGHT = 40; export const ITEM_TYPE_REGULAR = 'regular'; export const COLLAPSE_ITEM_ID = 'collapse-item-id'; diff --git a/src/components/CompositeBar/utils.ts b/src/components/CompositeBar/utils.ts index c16c884..09a5eb2 100644 --- a/src/components/CompositeBar/utils.ts +++ b/src/components/CompositeBar/utils.ts @@ -1,7 +1,8 @@ import {Ellipsis} from '@gravity-ui/icons'; import {MenuItem} from './../types'; -import {COLLAPSE_ITEM_ID, ITEM_HEIGHT} from './constants'; +import {COLLAPSE_ITEM_ID} from './constants'; import {CompositeBarItem} from '../CompositeBar/CompositeBar'; +import {ITEM_HEIGHT} from '../constants'; export function getItemHeight(item: CompositeBarItem) { if (!isMenuItem(item)) { diff --git a/src/components/constants.ts b/src/components/constants.ts index fb21b55..05b827a 100644 --- a/src/components/constants.ts +++ b/src/components/constants.ts @@ -1,3 +1,5 @@ export const ASIDE_HEADER_ICON_SIZE = 18; export const ASIDE_HEADER_COMPACT_WIDTH = 56; export const ASIDE_HEADER_EXPANDED_WIDTH = 236; +export const ITEM_HEIGHT = 40; +export const HEADER_DIVIDER_HEIGHT = 29; diff --git a/src/components/index.ts b/src/components/index.ts index 7774b35..9da241b 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -5,6 +5,7 @@ export {Drawer, DrawerProps, DrawerItemProps, DrawerItem} from './Drawer/Drawer' export {FooterItem, FooterItemProps} from './FooterItem/FooterItem'; export {PageLayout, type PageLayoutProps} from './AsideHeader/components/PageLayout/PageLayout'; export {PageLayoutAside} from './AsideHeader/components/PageLayout/PageLayoutAside'; +export {AsideFallback} from './AsideHeader/components/PageLayout/AsideFallback'; export * from './ActionBar'; export * from './Title'; export * from './HotkeysPanel';