Skip to content

Commit bd71d97

Browse files
authored
<Notification/>- migrating from sass to stylable (#5484)
1 parent 23b1df5 commit bd71d97

11 files changed

+338
-301
lines changed

src/Notification/ActionButton/ActionButton.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import Button from '../../Button';
44
import TextButton from '../../TextButton';
5+
import { dataHooks } from '../constants';
56

67
const ActionButton = ({ children, onClick, type, link, target }) => {
78
const commonProps = {
8-
dataHook: 'notification-cta-button',
9+
dataHook: dataHooks.notificationCtaButton,
910
onClick,
1011
};
1112

src/Notification/Notification.driver.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ export interface NotificationDriver extends BaseDriver {
99
isSuccessNotification: () => boolean;
1010
isWarningNotification: () => boolean;
1111
isPremiumNotification: () => boolean;
12-
isSmallSize: () => boolean;
13-
isBigSize: () => boolean;
1412
getLabelText: () => string;
1513
hasActionButton: () => boolean;
1614
getActionButtonText: () => string;

src/Notification/Notification.driver.js

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
11
import ReactTestUtils from 'react-dom/test-utils';
2+
import { dataHooks, THEMES, TYPE_POSITIONS_MAP } from './constants';
23

34
const notificationDriverFactory = ({ element }) => {
4-
const notificationWrapperSelector = '[data-hook="notification-wrapper"]';
5-
const labelTextSelector = '[data-hook="notification-label"]';
6-
const actionButtonSelector = '[data-hook="notification-cta-button"]';
7-
const closeButtonSelector = '[data-hook="notification-close-button"]';
5+
const getElementByDataHook = dataHook =>
6+
element.querySelector(`[data-hook="${dataHook}"]`);
87

9-
const classExists = className =>
10-
element
11-
.querySelector(notificationWrapperSelector)
12-
.classList.contains(className);
8+
const notificationWrapper = getElementByDataHook(
9+
dataHooks.notificationWrapper,
10+
);
11+
const labelText = getElementByDataHook(dataHooks.notificationLabel);
12+
const actionButton = getElementByDataHook(dataHooks.notificationCtaButton);
13+
const closeButton = getElementByDataHook(dataHooks.notificationCloseButton);
14+
15+
const getTheme = () => element.getAttribute('data-theme');
16+
const getType = () => element.getAttribute('data-type');
1317

1418
return {
1519
exists: () => !!element,
16-
visible: () => !!element.querySelector(notificationWrapperSelector),
17-
hasTheme: theme => classExists(`${theme}Theme`),
18-
isStandardNotification: () => classExists('standardTheme'),
19-
isErrorNotification: () => classExists('errorTheme'),
20-
isSuccessNotification: () => classExists('successTheme'),
21-
isWarningNotification: () => classExists('warningTheme'),
22-
isPremiumNotification: () => classExists('premiumTheme'),
23-
isSmallSize: () => classExists('smallSize'),
24-
isBigSize: () => classExists('bigSize'),
25-
getLabelText: () => element.querySelector(labelTextSelector).textContent,
26-
hasActionButton: () => !!element.querySelector(actionButtonSelector),
27-
getActionButtonText: () =>
28-
element.querySelector(actionButtonSelector).textContent,
29-
hasCloseButton: () =>
30-
!!element.querySelector('[data-hook="notification-close-button"]'),
31-
isRelativelyPositioned: () => classExists('relativePosition'),
32-
isFixedPositioned: () => classExists('fixedPosition'),
33-
isAbsolutePositioned: () => classExists('absolutePosition'),
34-
clickOnCloseButton: () =>
35-
ReactTestUtils.Simulate.click(element.querySelector(closeButtonSelector)),
36-
clickOnActionButton: () =>
37-
ReactTestUtils.Simulate.click(
38-
element.querySelector(actionButtonSelector),
39-
),
40-
getZIndex: () =>
41-
Number(
42-
element.querySelector(notificationWrapperSelector).style['z-index'],
43-
),
20+
visible: () => !!getElementByDataHook(dataHooks.notificationWrapper),
21+
hasTheme: () => !!getTheme(),
22+
isStandardNotification: () => getTheme() === THEMES.standard,
23+
isErrorNotification: () => getTheme() === THEMES.error,
24+
isSuccessNotification: () => getTheme() === THEMES.success,
25+
isWarningNotification: () => getTheme() === THEMES.warning,
26+
isPremiumNotification: () => getTheme() === THEMES.premium,
27+
getLabelText: () => labelText.textContent,
28+
hasActionButton: () => !!actionButton,
29+
getActionButtonText: () => actionButton.textContent,
30+
hasCloseButton: () => !!closeButton,
31+
isRelativelyPositioned: () => getType() === TYPE_POSITIONS_MAP.relative,
32+
isFixedPositioned: () => getType() === TYPE_POSITIONS_MAP.fixed,
33+
isAbsolutePositioned: () => getType() === TYPE_POSITIONS_MAP.absolute,
34+
clickOnCloseButton: () => ReactTestUtils.Simulate.click(closeButton),
35+
clickOnActionButton: () => ReactTestUtils.Simulate.click(actionButton),
36+
getZIndex: () => Number(notificationWrapper.style['z-index']),
4437
};
4538
};
4639

src/Notification/Notification.js

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
import React, { Children } from 'react';
22
import PropTypes from 'prop-types';
33
import { TransitionGroup, CSSTransition } from 'react-transition-group';
4-
import classNames from 'classnames';
54
import * as Composite from '../Composite';
65
import CloseButton from '../CloseButton';
76
import TextLabel from './TextLabel';
87
import ActionButton from './ActionButton';
9-
import css from './Notification.scss';
8+
import styles from './Notification.st.css';
109
import StatusComplete from 'wix-ui-icons-common/StatusComplete';
1110
import StatusWarning from 'wix-ui-icons-common/StatusWarning';
1211
import StatusAlert from 'wix-ui-icons-common/StatusAlert';
12+
import { dataHooks } from './constants';
1313

1414
export const LOCAL_NOTIFICATION = 'local';
1515
export const GLOBAL_NOTIFICATION = 'global';
1616
export const STICKY_NOTIFICATION = 'sticky';
1717
export const DEFAULT_AUTO_HIDE_TIMEOUT = 6000;
1818
export const DEFAULT_TIMEOUT = DEFAULT_AUTO_HIDE_TIMEOUT;
1919

20-
export const notificationTypeToPosition = {
21-
[LOCAL_NOTIFICATION]: 'absolute',
22-
[GLOBAL_NOTIFICATION]: 'relative',
23-
[STICKY_NOTIFICATION]: 'fixed',
24-
};
25-
2620
const animationsTimeouts = {
2721
enter: 500,
2822
exit: 350,
2923
};
3024

3125
const themeIcon = {
32-
error: <StatusAlert className={css.iconStyling} />,
33-
success: <StatusComplete className={css.iconStyling} />,
34-
warning: <StatusWarning className={css.iconStyling} />,
26+
error: <StatusAlert className={styles.iconStyling} />,
27+
success: <StatusComplete className={styles.iconStyling} />,
28+
warning: <StatusWarning className={styles.iconStyling} />,
3529
};
3630

3731
function FirstChild(props) {
@@ -69,26 +63,26 @@ class Notification extends React.PureComponent {
6963
hideByTimer: false,
7064
};
7165

72-
this.startCloseTimer(props);
66+
this._startCloseTimer(props);
7367
}
7468

75-
startCloseTimer({ autoHideTimeout }) {
69+
_startCloseTimer({ autoHideTimeout }) {
7670
if (autoHideTimeout) {
7771
this.closeTimeout = setTimeout(
78-
() => this.hideNotificationOnTimeout(),
72+
() => this._hideNotificationOnTimeout(),
7973
autoHideTimeout || DEFAULT_AUTO_HIDE_TIMEOUT,
8074
);
8175
}
8276
}
8377

84-
clearCloseTimeout() {
78+
_clearCloseTimeout() {
8579
if (this.closeTimeout) {
8680
clearTimeout(this.closeTimeout);
8781
this.closeTimeout = null;
8882
}
8983
}
9084

91-
hideNotificationOnCloseClick = () => {
85+
_hideNotificationOnCloseClick = () => {
9286
this.setState({ hideByCloseClick: true });
9387

9488
setTimeout(
@@ -97,7 +91,7 @@ class Notification extends React.PureComponent {
9791
);
9892
};
9993

100-
hideNotificationOnTimeout = () => {
94+
_hideNotificationOnTimeout = () => {
10195
this.setState({ hideByTimer: true });
10296

10397
setTimeout(
@@ -106,7 +100,7 @@ class Notification extends React.PureComponent {
106100
);
107101
};
108102

109-
bypassCloseFlags() {
103+
_bypassCloseFlags() {
110104
this.setState({
111105
hideByCloseClick: false,
112106
hideByTimer: false,
@@ -115,67 +109,63 @@ class Notification extends React.PureComponent {
115109

116110
UNSAFE_componentWillReceiveProps(nextProps) {
117111
if (nextProps.show) {
118-
this.bypassCloseFlags();
119-
this.clearCloseTimeout();
120-
this.startCloseTimer(nextProps);
112+
this._bypassCloseFlags();
113+
this._clearCloseTimeout();
114+
this._startCloseTimer(nextProps);
121115
}
122116
}
123117

124118
componentWillUnmount() {
125-
this.clearCloseTimeout();
119+
this._clearCloseTimeout();
126120
}
127121

128-
shouldShowNotification() {
122+
_shouldShowNotification() {
129123
return (
130124
this.props.show && !this.state.hideByCloseClick && !this.state.hideByTimer
131125
);
132126
}
133127

134-
renderNotification() {
135-
const { zIndex, children, type, theme } = this.props;
128+
_renderNotification() {
129+
const { zIndex, children, theme } = this.props;
136130
const childrenComponents = mapChildren(children);
137131

138132
return (
139133
<CSSTransition
140134
classNames={{
141-
enter: css.notificationAnimationEnter,
142-
enterActive: css.notificationAnimationEnterActive,
143-
exit: css.notificationAnimationExit,
144-
exitActive: css.notificationAnimationExitActive,
135+
enter: styles.notificationAnimationEnter,
136+
enterActive: styles.notificationAnimationEnterActive,
137+
exit: styles.notificationAnimationExit,
138+
exitActive: styles.notificationAnimationExitActive,
145139
}}
146140
timeout={animationsTimeouts}
147141
>
148142
<div
149-
data-hook="notification-wrapper"
143+
data-hook={dataHooks.notificationWrapper}
150144
style={{ zIndex }}
151-
className={classNames(
152-
css.notification,
153-
css[`${theme}Theme`],
154-
css[`${notificationTypeToPosition[type]}Position`],
155-
)}
145+
className={styles.notification}
156146
role="alert"
157147
aria-labelledby="notification-label"
158148
aria-live="polite"
159149
>
160150
{themeIcon[theme]}
161151
<div
162152
id="notification-label"
163-
className={css.label}
153+
className={styles.label}
164154
children={childrenComponents.label}
165155
/>
166156

167157
{childrenComponents.ctaButton && (
168158
<div
169-
className={css.button}
159+
className={styles.button}
170160
children={childrenComponents.ctaButton}
171161
/>
172162
)}
173163

174164
{childrenComponents.closeButton && (
175165
<div
176-
data-hook="notification-close-button"
177-
className={css.closeButton}
178-
onClick={this.hideNotificationOnCloseClick}
166+
data-hook={dataHooks.notificationCloseButton}
167+
className={styles.closeButton}
168+
onClick={this._hideNotificationOnCloseClick}
179169
children={childrenComponents.closeButton}
180170
/>
181171
)}
@@ -185,11 +175,16 @@ class Notification extends React.PureComponent {
185175
}
186176

187177
render() {
188-
const { dataHook } = this.props;
178+
const { dataHook, theme, type } = this.props;
189179
return (
190-
<div data-hook={dataHook} className={css.root}>
180+
<div
181+
{...styles('root', { theme, type }, this.props)}
182+
data-hook={dataHook}
183+
data-theme={theme}
184+
data-type={type}
185+
>
191186
<TransitionGroup component={FirstChild}>
192-
{this.shouldShowNotification() ? this.renderNotification() : null}
187+
{this._shouldShowNotification() ? this._renderNotification() : null}
193188
</TransitionGroup>
194189
</div>
195190
);

0 commit comments

Comments
 (0)