Skip to content

Commit

Permalink
feat!: support configure (#95)
Browse files Browse the repository at this point in the history
* feat!: support configure, add props for overriding default texts instead of dict

* fix: tune rollup config

* fix(AsideHeader): fix default texts

* chore: remove useless option
  • Loading branch information
Lunory authored Aug 23, 2023
1 parent d84fd93 commit c0140a7
Show file tree
Hide file tree
Showing 30 changed files with 163 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .storybook/decorators/withLang.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import type {Decorator} from '@storybook/react';
import {Lang, configure} from '../../src/components/utils/configure';
import {Lang, configure} from '../../src';

export const withLang: Decorator = (Story, context) => {
const lang = context.globals.lang;
Expand Down
2 changes: 1 addition & 1 deletion .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react';
import type {Decorator, Preview} from '@storybook/react';
import {ThemeProvider, MobileProvider, Lang, configure as uiKitConfigure} from '@gravity-ui/uikit';
import {configure as componentsConfigure} from '@gravity-ui/components';
import {configure} from '../src/components/utils/configure';
import {configure} from '../src';
import {withMobile} from './decorators/withMobile';
import {withLang} from './decorators/withLang';

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@gravity-ui/i18n": "^1.1.0",
"react-transition-group": "^4.4.1",
"tslib": "^2.4.0"
},
Expand Down
22 changes: 14 additions & 8 deletions src/components/AsideHeader/AsideHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import {block} from '../utils/cn';

import {MenuItem, AsideHeaderDict, Dict, LogoProps, SubheaderMenuItem} from '../types';
import {MenuItem, LogoProps, SubheaderMenuItem} from '../types';

import {ASIDE_HEADER_COMPACT_WIDTH, ASIDE_HEADER_EXPANDED_WIDTH, defaultDict} from '../constants';
import {ASIDE_HEADER_COMPACT_WIDTH, ASIDE_HEADER_EXPANDED_WIDTH} from '../constants';

import {Button, Icon} from '@gravity-ui/uikit';

Expand All @@ -12,6 +12,7 @@ import {Logo} from '../Logo/Logo';
import {CompositeBar} from '../CompositeBar/CompositeBar';
import {Content, RenderContentType} from '../Content';
import {fakeDisplayName} from '../helpers';
import i18n from './i18n';

import controlMenuButtonIcon from '../../../assets/icons/control-menu-button.svg';
import headerDividerCollapsedIcon from '../../../assets/icons/divider-collapsed.svg';
Expand All @@ -29,8 +30,10 @@ interface AsideHeaderGeneralProps {
logo: LogoProps;
compact: boolean;
multipleTooltip?: boolean;
dict?: AsideHeaderDict;
className?: string;
collapseTitle?: string;
expandTitle?: string;
menuMoreTitle?: string;
renderContent?: RenderContentType;
renderFooter?: (data: {
size: number;
Expand Down Expand Up @@ -82,7 +85,8 @@ export class AsideHeader extends React.Component<AsideHeaderInnerProps> {
}

private renderFirstPane = (size: number) => {
const {dict, menuItems, panelItems, headerDecoration, multipleTooltip} = this.props;
const {menuItems, panelItems, headerDecoration, multipleTooltip, menuMoreTitle} =
this.props;

return (
<React.Fragment>
Expand All @@ -94,7 +98,7 @@ export class AsideHeader extends React.Component<AsideHeaderInnerProps> {
<CompositeBar
type="menu"
items={menuItems}
dict={dict}
menuMoreTitle={menuMoreTitle ?? i18n('label_more')}
onItemClick={this.onItemClick}
multipleTooltip={multipleTooltip}
/>
Expand Down Expand Up @@ -174,15 +178,17 @@ export class AsideHeader extends React.Component<AsideHeaderInnerProps> {
};

private renderCollapseButton = () => {
const {dict, compact} = this.props;
const typeButton = compact ? Dict.ExpandButton : Dict.CollapseButton;
const {expandTitle, collapseTitle, compact} = this.props;
const buttonTitle = compact
? expandTitle || i18n('button_expand')
: collapseTitle || i18n('button_collapse');

return (
<Button
className={b('collapse-button', {compact})}
view="flat"
onClick={this.onCollapseButtonClick}
title={dict?.[typeButton] ?? defaultDict[typeButton]}
title={buttonTitle}
>
<NotIcon
data={controlMenuButtonIcon}
Expand Down
5 changes: 5 additions & 0 deletions src/components/AsideHeader/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"button_collapse": "Collapse",
"button_expand": "Expand",
"label_more": "More"
}
7 changes: 7 additions & 0 deletions src/components/AsideHeader/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {registerKeyset} from '../../utils/registerKeyset';

import en from './en.json';
import ru from './ru.json';

const COMPONENT = 'AsideHeader';
export default registerKeyset({en, ru}, COMPONENT);
5 changes: 5 additions & 0 deletions src/components/AsideHeader/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"button_collapse": "Свернуть",
"button_expand": "Развернуть",
"label_more": "Ещё"
}
8 changes: 4 additions & 4 deletions src/components/CompositeBar/CompositeBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AutoSizer, {Size} from 'react-virtualized-auto-sizer';
import {List} from '@gravity-ui/uikit';

import {block} from '../utils/cn';
import {AsideHeaderDict, MenuItem, SubheaderMenuItem} from '../types';
import {MenuItem, SubheaderMenuItem} from '../types';
import {
getItemsHeight,
getItemHeight,
Expand Down Expand Up @@ -37,7 +37,7 @@ export type CompositeBarProps = CompositeBarItems & {
event: React.MouseEvent<HTMLDivElement, MouseEvent>,
) => void;
multipleTooltip?: boolean;
dict?: AsideHeaderDict;
menuMoreTitle?: string;
};

type CompositeBarViewProps = CompositeBarProps & {
Expand Down Expand Up @@ -192,7 +192,7 @@ const CompositeBarView: FC<CompositeBarViewProps> = ({
export const CompositeBar: FC<CompositeBarProps> = ({
type,
items,
dict,
menuMoreTitle,
onItemClick,
multipleTooltip = false,
}) => {
Expand All @@ -203,7 +203,7 @@ export const CompositeBar: FC<CompositeBarProps> = ({

if (type === 'menu') {
const minHeight = getItemsMinHeight(items);
const collapseItem = getMoreButtonItem(dict);
const collapseItem = getMoreButtonItem(menuMoreTitle);
node = (
<div className={b({autosizer: true})} style={{minHeight}}>
{items.length !== 0 && (
Expand Down
9 changes: 3 additions & 6 deletions src/components/CompositeBar/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Ellipsis} from '@gravity-ui/icons';
import {AsideHeaderDict, Dict, MenuItem} from './../types';
import {MenuItem} from './../types';
import {COLLAPSE_ITEM_ID, ITEM_HEIGHT} from './constants';
import {defaultDict} from '../constants';
import {CompositeBarItem} from '../CompositeBar/CompositeBar';

export function getItemHeight(item: CompositeBarItem) {
Expand Down Expand Up @@ -54,12 +53,10 @@ export function getItemsMinHeight(items: MenuItem[]) {
);
}

export function getMoreButtonItem(dict?: AsideHeaderDict): MenuItem {
const title = dict?.[Dict.MoreButton] ?? defaultDict[Dict.MoreButton];

export function getMoreButtonItem(menuMoreTitle?: string): MenuItem {
return {
id: COLLAPSE_ITEM_ID,
title,
title: menuMoreTitle,
icon: Ellipsis,
iconSize: 18,
};
Expand Down
30 changes: 16 additions & 14 deletions src/components/MobileHeader/Burger/Burger.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import React from 'react';
import {block} from '../../utils/cn';

import {defaultDict} from '../../constants';

import './Burger.scss';

const b = block('mobile-header-burger');

export interface BurgerProps {
interface BurgerProps {
closeTitle: string;
openTitle: string;
opened?: boolean;
className?: string;
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
}

export const Burger = React.memo(({opened, className, onClick}: BurgerProps) => (
<button
className={b({opened}, className)}
onClick={onClick}
aria-label={opened ? defaultDict['button_close-burger'] : defaultDict['button_open-burger']}
>
<span className={b('icon')}>
<span className={b('icon-dash')}></span>
</span>
</button>
));
export const Burger = React.memo(
({closeTitle, openTitle, opened, className, onClick}: BurgerProps) => (
<button
className={b({opened}, className)}
onClick={onClick}
aria-label={opened ? closeTitle : openTitle}
>
<span className={b('icon')}>
<span className={b('icon-dash')}></span>
</span>
</button>
),
);

Burger.displayName = 'Burger';
7 changes: 7 additions & 0 deletions src/components/MobileHeader/MobileHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
EVENT_NAMES,
BURGER_PANEL_ITEM_ID,
} from './constants';
import i18n from './i18n';

import './MobileHeader.scss';

Expand All @@ -31,6 +32,8 @@ interface PanelItem extends Omit<DrawerItemProps, 'visible'> {}
export interface MobileHeaderProps {
logo: LogoProps;
burgerMenu: BurgerMenuProps;
burgerCloseTitle?: string;
burgerOpenTitle?: string;
panelItems?: PanelItem[];
renderContent?: RenderContentType;
sideItemRenderContent?: RenderContentType;
Expand All @@ -44,6 +47,8 @@ export const MobileHeader = React.forwardRef<HTMLDivElement, MobileHeaderProps>(
{
logo,
burgerMenu,
burgerCloseTitle = i18n('burger_button_close'),
burgerOpenTitle = i18n('burger_button_open'),
panelItems = [],
renderContent,
sideItemRenderContent,
Expand Down Expand Up @@ -196,6 +201,8 @@ export const MobileHeader = React.forwardRef<HTMLDivElement, MobileHeaderProps>(
opened={visiblePanel === burgerPanelItem.id}
onClick={() => onPanelToggle(BURGER_PANEL_ITEM_ID)}
className={b('burger')}
closeTitle={burgerCloseTitle}
openTitle={burgerOpenTitle}
/>
<Logo {...logo} compact={compact} onClick={onLogoClick} />

Expand Down
4 changes: 4 additions & 0 deletions src/components/MobileHeader/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"burger_button_close": "Close menu",
"burger_button_open": "Open menu"
}
7 changes: 7 additions & 0 deletions src/components/MobileHeader/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {registerKeyset} from '../../utils/registerKeyset';

import en from './en.json';
import ru from './ru.json';

const COMPONENT = 'MobileHeader';
export default registerKeyset({en, ru}, COMPONENT);
4 changes: 4 additions & 0 deletions src/components/MobileHeader/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"burger_button_close": "Закрыть меню",
"burger_button_open": "Открыть меню"
}
21 changes: 12 additions & 9 deletions src/components/Settings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ The components provides layouting functionality of settings panel with the follo

#### Settings

| Property | Type | Required | Default | Description |
| :------------- | :----------------- | :------: | :------- | :----------------------------------------- |
| loading | boolean | | | Flag of loading status |
| renderLoading | Function | | | content for loading status |
| renderNotFound | Function | | | Empty panel content |
| initialPage | string | | | Inititial page in `/groupId/pageId` format |
| view | 'normal', 'mobile' | | 'normal' | Change view for Mobile |
| onPageChange | Function | | | Page change handler |
| onClose | Function | | | Settings close handler |
| Property | Type | Required | Default | Description |
| :---------------- | :----------------- | :------: | :----------------- | :----------------------------------------- |
| loading | boolean | | | Flag of loading status |
| renderLoading | Function | | | content for loading status |
| renderNotFound | Function | | | Empty panel content |
| initialPage | string | | | Inititial page in `/groupId/pageId` format |
| view | 'normal', 'mobile' | | 'normal' | Change view for Mobile |
| onPageChange | Function | | | Page change handler |
| onClose | Function | | | Settings close handler |
| title | string | | 'Settings' | Page title |
| filterPlaceholder | string | | 'Search settings' | Filter placeholder text |
| emptyPlaceholder | string | | 'No results found' | Filter empty text |

#### Settings.Group

Expand Down
Loading

0 comments on commit c0140a7

Please sign in to comment.