UNPKG

3.88 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _mouseUpListener = require('./mouse-up-listener');
8
9var _mouseUpListener2 = _interopRequireDefault(_mouseUpListener);
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13var _isInteractiveStyleField = function _isInteractiveStyleField(styleFieldName) {
14 return styleFieldName === ':hover' || styleFieldName === ':active' || styleFieldName === ':focus';
15};
16
17var resolveInteractionStyles = function resolveInteractionStyles(config) {
18 var ExecutionEnvironment = config.ExecutionEnvironment,
19 getComponentField = config.getComponentField,
20 getState = config.getState,
21 mergeStyles = config.mergeStyles,
22 props = config.props,
23 setState = config.setState,
24 style = config.style;
25
26
27 var newComponentFields = {};
28 var newProps = {};
29
30 // Only add handlers if necessary
31 if (style[':hover']) {
32 // Always call the existing handler if one is already defined.
33 // This code, and the very similar ones below, could be abstracted a bit
34 // more, but it hurts readability IMO.
35 var existingOnMouseEnter = props.onMouseEnter;
36 newProps.onMouseEnter = function (e) {
37 existingOnMouseEnter && existingOnMouseEnter(e);
38 setState(':hover', true);
39 };
40
41 var existingOnMouseLeave = props.onMouseLeave;
42 newProps.onMouseLeave = function (e) {
43 existingOnMouseLeave && existingOnMouseLeave(e);
44 setState(':hover', false);
45 };
46 }
47
48 if (style[':active']) {
49 var existingOnMouseDown = props.onMouseDown;
50 newProps.onMouseDown = function (e) {
51 existingOnMouseDown && existingOnMouseDown(e);
52 newComponentFields._lastMouseDown = Date.now();
53 setState(':active', 'viamousedown');
54 };
55
56 var existingOnKeyDown = props.onKeyDown;
57 newProps.onKeyDown = function (e) {
58 existingOnKeyDown && existingOnKeyDown(e);
59 if (e.key === ' ' || e.key === 'Enter') {
60 setState(':active', 'viakeydown');
61 }
62 };
63
64 var existingOnKeyUp = props.onKeyUp;
65 newProps.onKeyUp = function (e) {
66 existingOnKeyUp && existingOnKeyUp(e);
67 if (e.key === ' ' || e.key === 'Enter') {
68 setState(':active', false);
69 }
70 };
71 }
72
73 if (style[':focus']) {
74 var existingOnFocus = props.onFocus;
75 newProps.onFocus = function (e) {
76 existingOnFocus && existingOnFocus(e);
77 setState(':focus', true);
78 };
79
80 var existingOnBlur = props.onBlur;
81 newProps.onBlur = function (e) {
82 existingOnBlur && existingOnBlur(e);
83 setState(':focus', false);
84 };
85 }
86
87 if (style[':active'] && !getComponentField('_radiumMouseUpListener') && ExecutionEnvironment.canUseEventListeners) {
88 newComponentFields._radiumMouseUpListener = _mouseUpListener2.default.subscribe(function () {
89 Object.keys(getComponentField('state')._radiumStyleState).forEach(function (key) {
90 if (getState(':active', key) === 'viamousedown') {
91 setState(':active', false, key);
92 }
93 });
94 });
95 }
96
97 // Merge the styles in the order they were defined
98 var interactionStyles = props.disabled ? [style[':disabled']] : Object.keys(style).filter(function (name) {
99 return _isInteractiveStyleField(name) && getState(name);
100 }).map(function (name) {
101 return style[name];
102 });
103
104 var newStyle = mergeStyles([style].concat(interactionStyles));
105
106 // Remove interactive styles
107 newStyle = Object.keys(newStyle).reduce(function (styleWithoutInteractions, name) {
108 if (!_isInteractiveStyleField(name) && name !== ':disabled') {
109 styleWithoutInteractions[name] = newStyle[name];
110 }
111 return styleWithoutInteractions;
112 }, {});
113
114 return {
115 componentFields: newComponentFields,
116 props: newProps,
117 style: newStyle
118 };
119};
120
121exports.default = resolveInteractionStyles;
122module.exports = exports['default'];
\No newline at end of file