Sider.js
7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import { useContext, useRef, useState, useEffect } from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import BarsOutlined from '@ant-design/icons/BarsOutlined';
import RightOutlined from '@ant-design/icons/RightOutlined';
import LeftOutlined from '@ant-design/icons/LeftOutlined';
import { LayoutContext } from './layout';
import { ConfigContext } from '../config-provider';
import isNumeric from '../_util/isNumeric';
var dimensionMaxMap = {
xs: '479.98px',
sm: '575.98px',
md: '767.98px',
lg: '991.98px',
xl: '1199.98px',
xxl: '1599.98px'
};
export var SiderContext = /*#__PURE__*/React.createContext({});
var generateId = function () {
var i = 0;
return function () {
var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
i += 1;
return "".concat(prefix).concat(i);
};
}();
var Sider = function Sider(_a) {
var customizePrefixCls = _a.prefixCls,
className = _a.className,
trigger = _a.trigger,
children = _a.children,
_a$defaultCollapsed = _a.defaultCollapsed,
defaultCollapsed = _a$defaultCollapsed === void 0 ? false : _a$defaultCollapsed,
_a$theme = _a.theme,
theme = _a$theme === void 0 ? 'dark' : _a$theme,
_a$style = _a.style,
style = _a$style === void 0 ? {} : _a$style,
_a$collapsible = _a.collapsible,
collapsible = _a$collapsible === void 0 ? false : _a$collapsible,
_a$reverseArrow = _a.reverseArrow,
reverseArrow = _a$reverseArrow === void 0 ? false : _a$reverseArrow,
_a$width = _a.width,
width = _a$width === void 0 ? 200 : _a$width,
_a$collapsedWidth = _a.collapsedWidth,
collapsedWidth = _a$collapsedWidth === void 0 ? 80 : _a$collapsedWidth,
zeroWidthTriggerStyle = _a.zeroWidthTriggerStyle,
breakpoint = _a.breakpoint,
onCollapse = _a.onCollapse,
onBreakpoint = _a.onBreakpoint,
props = __rest(_a, ["prefixCls", "className", "trigger", "children", "defaultCollapsed", "theme", "style", "collapsible", "reverseArrow", "width", "collapsedWidth", "zeroWidthTriggerStyle", "breakpoint", "onCollapse", "onBreakpoint"]);
var _useContext = useContext(LayoutContext),
siderHook = _useContext.siderHook;
var _useState = useState('collapsed' in props ? props.collapsed : defaultCollapsed),
_useState2 = _slicedToArray(_useState, 2),
collapsed = _useState2[0],
setCollapsed = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
below = _useState4[0],
setBelow = _useState4[1];
useEffect(function () {
if ('collapsed' in props) {
setCollapsed(props.collapsed);
}
}, [props.collapsed]);
var handleSetCollapsed = function handleSetCollapsed(value, type) {
if (!('collapsed' in props)) {
setCollapsed(value);
}
if (onCollapse) {
onCollapse(value, type);
}
}; // ========================= Responsive =========================
var responsiveHandlerRef = useRef();
responsiveHandlerRef.current = function (mql) {
setBelow(mql.matches);
if (onBreakpoint) {
onBreakpoint(mql.matches);
}
if (collapsed !== mql.matches) {
handleSetCollapsed(mql.matches, 'responsive');
}
};
useEffect(function () {
function responsiveHandler(mql) {
return responsiveHandlerRef.current(mql);
}
var mql;
if (typeof window !== 'undefined') {
var _window = window,
matchMedia = _window.matchMedia;
if (matchMedia && breakpoint && breakpoint in dimensionMaxMap) {
mql = matchMedia("(max-width: ".concat(dimensionMaxMap[breakpoint], ")"));
try {
mql.addEventListener('change', responsiveHandler);
} catch (error) {
mql.addListener(responsiveHandler);
}
responsiveHandler(mql);
}
}
return function () {
try {
mql === null || mql === void 0 ? void 0 : mql.removeEventListener('change', responsiveHandler);
} catch (error) {
mql === null || mql === void 0 ? void 0 : mql.removeListener(responsiveHandler);
}
};
}, []);
useEffect(function () {
var uniqueId = generateId('ant-sider-');
siderHook.addSider(uniqueId);
return function () {
return siderHook.removeSider(uniqueId);
};
}, []);
var toggle = function toggle() {
handleSetCollapsed(!collapsed, 'clickTrigger');
};
var _useContext2 = useContext(ConfigContext),
getPrefixCls = _useContext2.getPrefixCls;
var renderSider = function renderSider() {
var _classNames;
var prefixCls = getPrefixCls('layout-sider', customizePrefixCls);
var divProps = omit(props, ['collapsed']);
var rawWidth = collapsed ? collapsedWidth : width; // use "px" as fallback unit for width
var siderWidth = isNumeric(rawWidth) ? "".concat(rawWidth, "px") : String(rawWidth); // special trigger when collapsedWidth == 0
var zeroWidthTrigger = parseFloat(String(collapsedWidth || 0)) === 0 ? /*#__PURE__*/React.createElement("span", {
onClick: toggle,
className: classNames("".concat(prefixCls, "-zero-width-trigger"), "".concat(prefixCls, "-zero-width-trigger-").concat(reverseArrow ? 'right' : 'left')),
style: zeroWidthTriggerStyle
}, trigger || /*#__PURE__*/React.createElement(BarsOutlined, null)) : null;
var iconObj = {
expanded: reverseArrow ? /*#__PURE__*/React.createElement(RightOutlined, null) : /*#__PURE__*/React.createElement(LeftOutlined, null),
collapsed: reverseArrow ? /*#__PURE__*/React.createElement(LeftOutlined, null) : /*#__PURE__*/React.createElement(RightOutlined, null)
};
var status = collapsed ? 'collapsed' : 'expanded';
var defaultTrigger = iconObj[status];
var triggerDom = trigger !== null ? zeroWidthTrigger || /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-trigger"),
onClick: toggle,
style: {
width: siderWidth
}
}, trigger || defaultTrigger) : null;
var divStyle = _extends(_extends({}, style), {
flex: "0 0 ".concat(siderWidth),
maxWidth: siderWidth,
minWidth: siderWidth,
width: siderWidth
});
var siderCls = classNames(prefixCls, "".concat(prefixCls, "-").concat(theme), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-collapsed"), !!collapsed), _defineProperty(_classNames, "".concat(prefixCls, "-has-trigger"), collapsible && trigger !== null && !zeroWidthTrigger), _defineProperty(_classNames, "".concat(prefixCls, "-below"), !!below), _defineProperty(_classNames, "".concat(prefixCls, "-zero-width"), parseFloat(siderWidth) === 0), _classNames), className);
return /*#__PURE__*/React.createElement("aside", _extends({
className: siderCls
}, divProps, {
style: divStyle
}), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-children")
}, children), collapsible || below && zeroWidthTrigger ? triggerDom : null);
};
return /*#__PURE__*/React.createElement(SiderContext.Provider, {
value: {
siderCollapsed: collapsed,
collapsedWidth: collapsedWidth
}
}, renderSider());
};
export default Sider;