Skip to content

Commit

Permalink
feat: moved overlay controls into separated context (#157)
Browse files Browse the repository at this point in the history
* feat: moved overlay controls into context

* feat: separated context for overlays

* feat: clean up code and added README

* feat: added hide left controls to example

* fix: add menu to custom overlay controls
  • Loading branch information
flops authored Jul 18, 2024
1 parent 16a3808 commit 29fca8c
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 211 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ interface DashKitProps {
itemsStateAndParams: ItemsStateAndParams;
settings: SettingsProps;
context: ContextProps;
overlayControls?: Record<string, OverlayControlItem[]>;
overlayControls?: Record<string, OverlayControlItem[]> | null;
overlayMenuItems?: MenuItems[] | null;
noOverlay?: boolean;
focusable?: boolean;
draggableHandleClassName?: string;
Expand All @@ -51,7 +52,8 @@ interface DashKitProps {
- **itemsStateAndParams**: [itemsStateAndParams](#temsStateAndParams).
- **settings**: DashKit settings.
- **context**: Object that will be propped up on all widgets.
- **overlayControls**: Object that overrides widget controls at the time of editing. If not transmitted, basic controls will be displayed.
- **overlayControls**: Object that overrides widget controls at the time of editing. If not transmitted, basic controls will be displayed. If `null` passed only close button or custom menu will be displayed.
- **overlayMenuItems**: Custom dropdown menu items
- **noOverlay**: If `true`, overlay and controls are not displayed while editing.
- **focusable**: If `true`, grid items will be focusable.
- **draggableHandleClassName** : СSS class name of the element that makes the widget draggable.
Expand Down Expand Up @@ -343,6 +345,10 @@ type MenuItem = {
};

// use array of menu items in settings
<Dashkit overlayMenuItems={[] as Array<MenuItem> | null} />

[deprecated]
// overlayMenuItems property has greater priority over setSettings menu
DashKit.setSettings({menu: [] as Array<MenuItem>});
```

Expand Down Expand Up @@ -386,7 +392,7 @@ type ItemDropProps = {
#### Example:

```jsx
const menuItems = [
const overlayMenuItems = [
{
id: 'chart',
icon: <Icon data={ChartColumn} />,
Expand All @@ -405,7 +411,7 @@ const onDrop = (dropProps: ItemDropProps) => {

<DashKitDnDWrapper>
<DashKit editMode={true} config={config} onChange={onChange} onDrop={onDrop} />
<ActionPanel items={menuItems} />
<ActionPanel items={overlayMenuItems} />
</DashKitDnDWrapper>
```

Expand Down
4 changes: 3 additions & 1 deletion src/components/DashKit/DashKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AddConfigItem,
ContextProps,
DashKitGroup,
MenuItem,
Plugin,
SetConfigItem,
SetNewItemOptions,
Expand All @@ -29,7 +30,8 @@ interface DashKitGeneralProps {
config: Config;
editMode: boolean;
draggableHandleClassName?: string;
overlayControls?: Record<string, OverlayControlItem[]>;
overlayControls?: Record<string, OverlayControlItem[]> | null;
overlayMenuItems?: MenuItem[] | null;
}

interface DashKitDefaultProps {
Expand Down
28 changes: 0 additions & 28 deletions src/components/DashKit/__stories__/CssApiShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,11 @@ import {
import {Icon} from '@gravity-ui/uikit';

import {ActionPanel, DashKit} from '../../..';
import {MenuItems} from '../../../helpers';
import {i18n} from '../../../i18n';
import {CogIcon} from '../../../icons/CogIcon';
import {CopyIcon} from '../../../icons/CopyIcon';
import {DeleteIcon} from '../../../icons/DeleteIcon';

import {Demo, DemoRow} from './Demo';
import {getConfig} from './utils';

export const CssApiShowcase: React.FC = () => {
React.useEffect(() => {
DashKit.setSettings({
menu: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
],
});
}, []);

const items = React.useMemo(
() => [
{
Expand Down
36 changes: 7 additions & 29 deletions src/components/DashKit/__stories__/DashKitDnDShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,11 @@ import {ChartColumn, Copy, Heading, Sliders, TextAlignLeft} from '@gravity-ui/ic
import {Icon} from '@gravity-ui/uikit';

import {ActionPanel, DashKit, DashKitDnDWrapper, DashKitProps} from '../../..';
import {MenuItems} from '../../../helpers';
import {i18n} from '../../../i18n';
import {CogIcon} from '../../../icons/CogIcon';
import {CopyIcon} from '../../../icons/CopyIcon';
import {DeleteIcon} from '../../../icons/DeleteIcon';

import {Demo, DemoRow} from './Demo';
import {getConfig} from './utils';

export const DashKitDnDShowcase: React.FC = () => {
React.useEffect(() => {
DashKit.setSettings({
menu: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
],
});
}, []);

const onClick = () => {
console.log('click');

Check warning on line 13 in src/components/DashKit/__stories__/DashKitDnDShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
};
Expand Down Expand Up @@ -156,7 +128,13 @@ export const DashKitDnDShowcase: React.FC = () => {
>
<Demo title="Drag'n'Drop example">
<DemoRow title="Component view">
<DashKit editMode={true} config={config} onChange={onChange} onDrop={onDrop} />
<DashKit
editMode={true}
config={config}
overlayControls={null}
onChange={onChange}
onDrop={onDrop}
/>
<ActionPanel items={items} />
</DemoRow>
</Demo>
Expand Down
41 changes: 20 additions & 21 deletions src/components/DashKit/__stories__/DashKitGroupsShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,25 @@ import {fixedGroup, getConfig} from './utils';
export const DashKitGroupsShowcase: React.FC = () => {
const [editMode, setEditMode] = React.useState(true);

React.useEffect(() => {
DashKit.setSettings({
menu: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
],
});
const overlayMenuItems = React.useMemo(() => {
return [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
];
}, []);

const onClick = () => {
Expand Down Expand Up @@ -220,6 +218,7 @@ export const DashKitGroupsShowcase: React.FC = () => {
config={config}
onChange={onChange}
onDrop={onDrop}
overlayMenuItems={overlayMenuItems}
/>
<ActionPanel items={items} />
</DemoRow>
Expand Down
70 changes: 45 additions & 25 deletions src/components/DashKit/__stories__/DashKitShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {CopyIcon} from '../../../icons/CopyIcon';
import {DeleteIcon} from '../../../icons/DeleteIcon';
import {TickIcon} from '../../../icons/TickIcon';
import {WarningIcon} from '../../../icons/WarningIcon';
import type {ConfigItem} from '../../../index';
import type {ConfigItem, OverlayControlItem} from '../../../index';
import {cn} from '../../../utils/cn';

import {Demo, DemoRow} from './Demo';
Expand All @@ -38,9 +38,10 @@ type DashKitDemoState = {

lastAction: string;
customControlsActionData: number;
showCustomMenu: boolean;
enableActionPanel: boolean;
enableAnimations: boolean;
enableOverlayControls: boolean;
overlayMenuItems: DashKitProps['overlayMenuItems'];
};

export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {
Expand All @@ -57,21 +58,20 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {

lastAction: 'Nothing',
customControlsActionData: 0,
showCustomMenu: true,
enableActionPanel: false,
enableAnimations: true,
enableOverlayControls: true,
overlayMenuItems: null,
};

private controls: Record<string, OverlayControlItem[]>;

private dashKitRef = React.createRef<DashKit>();

componentDidMount() {
this.toggleCustomMenu(true);
}
constructor(props: {}) {
super(props);

render() {
console.log('customControlsActionData', this.state.customControlsActionData);
const {editMode} = this.state;
const controls = {
this.controls = {
custom: [
{
title: 'Edit custom widget',
Expand All @@ -87,21 +87,29 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {
icon: TickIcon,
handler: () => console.log('overlayControls::custom click'),

Check warning on line 88 in src/components/DashKit/__stories__/DashKitShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
iconSize: 16,
visible: (item) => item.type !== 'text',
},
{
allWidgetsControls: true,
excludeWidgetsTypes: ['title'],
id: MenuItems.Settings,
title: 'Settings default',
icon: WarningIcon,
visible: (item) => item.type !== 'text',
},
{
allWidgetsControls: true,
title: 'Icon tooltip 2',
handler: () => console.log('overlayControls::custom click'),

Check warning on line 103 in src/components/DashKit/__stories__/DashKitShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
visible: (item) => item.type !== 'text',
},
],
};
}

render() {
console.log('customControlsActionData', this.state.customControlsActionData);
const {editMode} = this.state;

return (
<Demo title="DashKit">
Expand All @@ -128,11 +136,22 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {
<Button
view="normal"
size="m"
onClick={() => this.toggleCustomMenu(false)}
onClick={this.toggleOverlayControls}
className={b('btn-contol')}
disabled={!editMode}
>
{this.state.showCustomMenu
{this.state.enableOverlayControls
? 'Hide left controls'
: 'Show left controls'}
</Button>
<Button
view="normal"
size="m"
onClick={this.toggleCustomMenu}
className={b('btn-contol')}
disabled={!editMode}
>
{this.state.overlayMenuItems
? 'Disable custom menu'
: 'Enable custom menu'}
</Button>
Expand Down Expand Up @@ -207,7 +226,8 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {
onChange={this.onChange}
settings={this.state.settings}
ref={this.dashKitRef}
overlayControls={controls}
overlayControls={this.state.enableOverlayControls ? this.controls : null}
overlayMenuItems={this.state.overlayMenuItems}
focusable={!editMode}
/>
</DemoRow>
Expand Down Expand Up @@ -346,41 +366,41 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {
return Boolean(this.state.config.items.find((item) => item.id === titleId));
}

private toggleCustomMenu = (init = false) => {
const {showCustomMenu} = this.state;
if (showCustomMenu) {
DashKit.setSettings({menu: []});
private toggleOverlayControls = () => {
this.setState({enableOverlayControls: !this.state.enableOverlayControls});
};

private toggleCustomMenu = () => {
if (this.state.overlayMenuItems) {
this.setState({overlayMenuItems: null});
} else {
DashKit.setSettings({
menu: [
this.setState({
overlayMenuItems: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
handler: () => {
console.log('menu::settings::click');
},
visible: (item) => item.type !== 'custom',
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
visible: (item) => item.type !== 'custom',
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
visible: (item) => item.type !== 'custom',
},
],
});
}
this.setState({
showCustomMenu: !showCustomMenu,
lastAction: init
? this.state.lastAction
: `[DashKit.setSettings] toggle show custom widget menu: ${new Date().toISOString()}`,
});
};

private toggleActionPanel() {
Expand Down
Loading

0 comments on commit 29fca8c

Please sign in to comment.