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

#1093: Компонент Snackbar не отображается поверх модального окона #1217

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 25 additions & 3 deletions src/components/Snackbar/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class SnackBarExample extends React.Component {

this.state = {
text: '',
snackbar: null
snackbar: null,
hasModal: false,
};

this.openBaseWithAction = this.openBaseWithAction.bind(this);
Expand All @@ -29,6 +30,7 @@ class SnackBarExample extends React.Component {
if (this.state.snackbar) return;
this.setState({ snackbar:
<Snackbar
duration={4000}
onClose={() => this.setState({ snackbar: null })}
action="Поделиться"
onActionClick={() => this.setState({ text: 'Добавляем метку.' })}
Expand Down Expand Up @@ -68,8 +70,25 @@ class SnackBarExample extends React.Component {
}

render() {
const closeModal = () => this.setState({ hasModal: false });
const modal = (
<ModalRoot activeModal={this.state.hasModal ? 'modal' : null} onClose={closeModal}>
<ModalPage
id="modal"
onClose={closeModal}
header={
<ModalPageHeader
left={IS_PLATFORM_ANDROID && <PanelHeaderButton onClick={closeModal}><Icon24Cancel /></PanelHeaderButton>}
right={IS_PLATFORM_IOS && <PanelHeaderButton onClick={closeModal}><Icon24Done /></PanelHeaderButton>}
>Модалка</ModalPageHeader>
}
>
<div style={{ height: '600px' }} />
</ModalPage>
</ModalRoot>
);
return (
<View activePanel="example">
<View activePanel="example" modal={modal}>
<Panel id="example">
<PanelHeader>Snackbar</PanelHeader>
<Group>
Expand All @@ -78,8 +97,11 @@ class SnackBarExample extends React.Component {
<CellButton onClick={this.openWithAvatar}>Уведомление с аватаркой</CellButton>
</Group>

{this.state.text && <Group><Div>{this.state.text}</Div></Group>}
<Group>
<CellButton onClick={() => this.setState({ hasModal: true })}>Открыть модалку</CellButton>
</Group>

{this.state.text && <Group><Div>{this.state.text}</Div></Group>}
{this.state.snackbar}
</Panel>
</View>
Expand Down
1 change: 1 addition & 0 deletions src/components/Snackbar/Snackbar.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.Snackbar {
user-select: none;
z-index: 101;
}

.Snackbar__in {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Snackbar/Snackbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { rubber } from '../../lib/touch';
import withAdaptivity, { AdaptivityProps, ViewWidth } from '../../hoc/withAdaptivity';
import Text from '../Typography/Text/Text';
import Button from '../Button/Button';
import { withSnackbarPortal } from './hoc';

export interface SnackbarProps extends HTMLAttributes<HTMLElement>, HasPlatform, AdaptivityProps {
/**
Expand Down Expand Up @@ -262,6 +263,6 @@ class Snackbar extends PureComponent<SnackbarProps, SnackbarState> {
}
}

export default withPlatform(withAdaptivity(Snackbar, {
export default withPlatform(withAdaptivity(withSnackbarPortal(Snackbar), {
viewWidth: true,
}));
31 changes: 31 additions & 0 deletions src/components/Snackbar/hoc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { createContext, ComponentType, useMemo, useState, useContext } from 'react';
import { createPortal } from 'react-dom';

export const SnackbarContext = createContext<{
isolated?: boolean;
node?: HTMLElement;
}>({ isolated: true });

export const withSnackbarContainer = <T extends ComponentType<any>>(Cmp: T) => ((props: any) => {
const [snackbarNode, setSnackbarNode] = useState<HTMLElement>(null);
const contextValue = useMemo(() => ({ node: snackbarNode }), [snackbarNode]);
const snackbarContainer = <div ref={setSnackbarNode} />;
return (
<SnackbarContext.Provider value={contextValue}>
<Cmp {...props} snackbar={snackbarContainer} />
</SnackbarContext.Provider>
);
}) as T;

export const withSnackbarPortal = <T extends ComponentType<any>>(Cmp: T) => ((props: any) => {
const { isolated, node } = useContext(SnackbarContext);

const jsx = <Cmp {...props} />;
if (isolated) {
return jsx;
}
if (!node) {
return null;
}
return createPortal(jsx, node);
}) as T;
7 changes: 5 additions & 2 deletions src/components/View/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import withContext from '../../hoc/withContext';
import { ConfigProviderContext, ConfigProviderContextInterface } from '../ConfigProvider/ConfigProviderContext';
import { createCustomEvent } from '../../lib/utils';
import { SplitColContext, SplitColContextProps } from '../SplitCol/SplitCol';
import { withSnackbarContainer } from '../Snackbar/hoc';

export const transitionStartEventName = 'VKUI:View:transition-start';
export const transitionEndEventName = 'VKUI:View:transition-end';
Expand Down Expand Up @@ -42,6 +43,7 @@ export interface ViewProps extends HTMLAttributes<HTMLElement>, HasChildren, Has
activePanel: string;
popout?: ReactNode;
modal?: ReactNode;
snackbar?: ReactNode;
onTransition?(params: { isBack: boolean; from: string; to: string }): void;
/**
* callback свайпа назад
Expand Down Expand Up @@ -442,7 +444,7 @@ class View extends Component<ViewProps, ViewState> {
}

render() {
const { style, popout, modal, platform } = this.props;
const { style, popout, modal, snackbar, platform } = this.props;
const { prevPanel, nextPanel, activePanel, swipeBackPrevPanel, swipeBackNextPanel, swipeBackResult } = this.state;

const hasPopout = !!popout;
Expand Down Expand Up @@ -497,13 +499,14 @@ class View extends Component<ViewProps, ViewState> {
</div>
{hasPopout && <div className="View__popout">{popout}</div>}
{hasModal && <div className="View__modal">{modal}</div>}
{snackbar}
</Touch>
);
}
}

export default withContext(withContext(
withPlatform(View),
withPlatform(withSnackbarContainer(View)),
SplitColContext,
'splitCol',
), ConfigProviderContext, 'configProvider');