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

feat(Notifications): static component implementation (DATAUI-1582) #57

Merged
merged 19 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
# Artifacts
dist
build
.DS_Store
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"@gravity-ui/i18n": "^1.0.0",
"@gravity-ui/icons": "^1.1.0",
"lodash": "^4.17.21",
"resize-observer-polyfill": "^1.5.1"
"resize-observer-polyfill": "^1.5.1",
"swipejs": "^2.3.1"
},
"devDependencies": {
"@commitlint/cli": "^17.0.0",
Expand Down
135 changes: 135 additions & 0 deletions src/components/Notification/Notification.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
@use '../variables';
@use '@gravity-ui/uikit/styles/mixins';

$block: '.#{variables.$ns}notification';

$notificationSourceIconSize: 36px;

#{$block} {
display: flex;
padding: 12px;
gap: 12px;
border-radius: 4px;
box-sizing: border-box;

&:hover {
background: var(--yc-color-base-simple-hover);
}

&__right {
display: flex;
flex-direction: column;
gap: 4px;
flex: 1;
overflow-x: hidden;
}

&__right-top-part {
display: flex;
align-items: center;
width: 100%;
overflow-x: hidden;
}

&__right-meta-and-title {
flex: 1;
min-width: 0;
overflow-x: hidden;
}

&__right-meta,
&__right-title {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

&__right-meta {
display: flex;
gap: 4px;
color: var(--yc-color-text-secondary);
}

&__right-title {
font-weight: 500;
font-size: 13px;
line-height: 18px;

color: var(--yc-color-text-primary);
}

&__right-content {
font-size: 13px;
line-height: 18px;

color: var(--yc-color-text-secondary);
}

&_unread {
background: var(--yc-color-base-selection);
&:hover {
background: var(--yc-color-base-selection-hover);
}
}

&__actions {
display: flex;
align-items: center;
flex-wrap: wrap;
}

&__actions_right-bottom-actions {
margin-top: 8px;
gap: 8px;
}

&__actions_right-side-actions {
opacity: 0;
}
&:hover &__actions_right-side-actions {
opacity: 1;
}
&_mobile &__actions_right-side-actions {
opacity: 1;
}

&__action_icon {
color: var(--yc-color-text-secondary);
}

&_theme_success {
border-left: 4px solid var(--yc-color-line-positive);
}
&_theme_info {
border-left: 4px solid var(--yc-color-line-info);
}
&_theme_warning {
border-left: 4px solid var(--yc-color-line-warning);
}
&_theme_danger {
border-left: 4px solid var(--yc-color-line-danger);
}

// swipe js styles (slightly modified)
&__swipe {
overflow: hidden;
visibility: hidden;
position: relative;
}
&__swipe-wrap {
overflow: hidden;
position: relative;
display: flex;
align-items: stretch;
}
&__swipe-wrap > div {
width: 100%;
position: relative;
}

&__swipe-action {
display: flex;
align-items: center;
justify-content: center;
}
}
89 changes: 89 additions & 0 deletions src/components/Notification/Notification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {Icon, Link, useMobile} from '@gravity-ui/uikit';
import React from 'react';
import {CnMods, block} from '../utils/cn';
import './Notification.scss';
import {NotificationProps, NotificationSourceProps} from './definitions';

const b = block('notification');

type Props = {notification: NotificationProps};

export const Notification = React.memo(function Notification(props: Props) {
const [mobile] = useMobile();
const {notification} = props;
const {title, content, formattedDate, source, unread, theme} = notification;

const modifiers: CnMods = {unread, theme, mobile};

return (
<div
className={b(modifiers, notification.className)}
onMouseEnter={notification.onMouseEnter}
onMouseLeave={notification.onMouseLeave}
onClick={notification.onClick}
>
{source ? <div className={b('left')}>{renderSourceIcon(source)}</div> : null}
<div className={b('right')}>
Ruminat marked this conversation as resolved.
Show resolved Hide resolved
<div className={b('right-top-part')}>
<div className={b('right-meta-and-title')}>
<div className={b('right-meta')}>
{source?.title ? renderSourceTitle(source.title, source.href) : null}
{source?.title && formattedDate ? <span>•</span> : null}
{formattedDate ? (
<div className={b('right-date')}>{formattedDate}</div>
) : null}
</div>
{title ? <div className={b('right-title')}>{title}</div> : null}
</div>
{props.notification.sideActions ? (
<div className={b('actions', {'right-side-actions': true})}>
{props.notification.sideActions}
</div>
) : null}
</div>
<div className={b('right-content')}>{content}</div>
{props.notification.bottomActions ? (
<div className={b('actions', {'right-bottom-actions': true})}>
{props.notification.bottomActions}
</div>
) : null}
</div>
</div>
);
});

function renderSourceTitle(title: string, href: string | undefined): JSX.Element {
return href ? (
<Link className={b('right-source-title')} href={href} target="_blank" title={title}>
{title}
</Link>
) : (
<div className={b('right-source-title')} title={title}>
{title}
</div>
);
}

function renderSourceIcon(source: NotificationSourceProps): JSX.Element | null {
const iconElement = getIconElement(source);

if (!iconElement) return null;

return source.href ? (
<Link href={source.href} target="_blank">
{iconElement}
</Link>
) : (
iconElement
);
}

function getIconElement(source: NotificationSourceProps): JSX.Element | null {
if ('icon' in source && source.icon) {
return <Icon className={b('source-icon')} size={36} data={source.icon} />;
korvin89 marked this conversation as resolved.
Show resolved Hide resolved
} else if ('imageSrc' in source && source.imageSrc) {
return <img className={b('source-icon')} src={source.imageSrc} />;
} else {
return null;
}
}
31 changes: 31 additions & 0 deletions src/components/Notification/NotificationAction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {Button, Icon, Tooltip} from '@gravity-ui/uikit';
import React from 'react';
import {block} from '../utils/cn';
import './Notification.scss';
import {NotificationActionProps} from './definitions';

const b = block('notification');

type Props = {action: NotificationActionProps};

export const NotificationAction = React.memo(function NotificationAction({action}: Props) {
const content = renderContent(action);

const button = (
<Button
className={b('action', {icon: Boolean(action.icon)})}
view={action.view ?? 'flat'}
korvin89 marked this conversation as resolved.
Show resolved Hide resolved
href={action.href}
target={action.target}
onClick={action.onClick}
>
{content}
</Button>
);

return action.icon ? <Tooltip content={action.text}>{button}</Tooltip> : button;
korvin89 marked this conversation as resolved.
Show resolved Hide resolved
});

function renderContent(action: NotificationActionProps): React.ReactNode {
return action.icon ? <Icon data={action.icon} /> : action.text;
}
76 changes: 76 additions & 0 deletions src/components/Notification/NotificationWithSwipe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';
import Swipe from 'swipejs';
import {block} from '../utils/cn';
import {Notification} from './Notification';
import './Notification.scss';
import {NotificationProps} from './definitions';

const b = block('notification');

type Props = {notification: NotificationProps};

export const NotificationWithSwipe = React.memo(function NotificationWithSwipe(props: Props) {
const ref = React.useRef<HTMLDivElement>(null);
const notification = props.notification;
const swipeActions = notification.swipeActions;

const startSlide = React.useMemo(
() => (swipeActions && 'left' in swipeActions ? 1 : 0),
[swipeActions],
);

const callback = React.useCallback(
(index: number) => {
if (!swipeActions) return;

if ('left' in swipeActions) {
if (index === 0) {
swipeActions.left.onActivate();
} else if ('right' in swipeActions && index === 2) {
swipeActions.right.onActivate();
}
} else if ('right' in swipeActions && index === 1) {
swipeActions.right.onActivate();
}
},
[swipeActions],
);

React.useEffect(() => {
if (!ref.current) return undefined;

const swipe = new Swipe(ref.current, {
startSlide,
callback,

speed: 100,
draggable: true,
continuous: false,
disableScroll: false,
stopPropagation: true,
autoRestart: false,
});

return () => {
swipe.kill();
};
}, [callback, startSlide]);

return (
<div className={b('swipe')} ref={ref}>
<div className={b('swipe-wrap')}>
{notification.swipeActions && 'left' in notification.swipeActions ? (
<div className={b('swipe-action')}>
{notification.swipeActions.left.content}
</div>
) : null}
<Notification {...props} />
{notification.swipeActions && 'right' in notification.swipeActions ? (
<div className={b('swipe-action')}>
{notification.swipeActions.right.content}
</div>
) : null}
</div>
</div>
);
});
Loading