carbon-react 102.16.0 → 102.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,21 +55,8 @@ const Drawer = ({
55
55
  const isControlled = (0, _react.useRef)(expanded !== undefined);
56
56
  const [isOpening, setIsOpening] = (0, _react.useState)(false);
57
57
  const [isClosing, setIsClosing] = (0, _react.useState)(false);
58
- const [isExpanded, setIsExpanded] = (0, _react.useState)(defaultExpanded);
58
+ const [isExpanded, setIsExpanded] = (0, _react.useState)(isControlled.current ? expanded : defaultExpanded);
59
59
  const timer = (0, _react.useRef)();
60
- (0, _react.useEffect)(() => {
61
- const message = "Drawer should not switch from uncontrolled to controlled" + " (or vice versa). Decide between using a controlled or uncontrolled Drawer element" + " for the lifetime of the component";
62
- (0, _invariant.default)(isControlled.current === (expanded !== undefined), message);
63
-
64
- if (expanded !== undefined) {
65
- setIsExpanded(expanded);
66
- }
67
- }, [expanded]);
68
- (0, _react.useEffect)(() => {
69
- return function cleanup() {
70
- clearTimeout(timer.current);
71
- };
72
- }, []);
73
60
  const getAnimationDuration = (0, _react.useCallback)(() => {
74
61
  if (animationDuration.indexOf("ms") !== -1) {
75
62
  const animationTime = animationDuration.substring(0, animationDuration.length - 2);
@@ -101,6 +88,33 @@ const Drawer = ({
101
88
  }, timeout);
102
89
  }
103
90
  }, [getAnimationDuration, isExpanded]);
91
+
92
+ function usePrevious(arg) {
93
+ const ref = (0, _react.useRef)();
94
+ (0, _react.useEffect)(() => {
95
+ ref.current = arg;
96
+ });
97
+ return ref.current;
98
+ }
99
+
100
+ const previousValue = usePrevious(expanded);
101
+ (0, _react.useEffect)(() => {
102
+ const message = "Drawer should not switch from uncontrolled to controlled" + " (or vice versa). Decide between using a controlled or uncontrolled Drawer element" + " for the lifetime of the component";
103
+ (0, _invariant.default)(isControlled.current === (expanded !== undefined), message);
104
+
105
+ if (isControlled.current && previousValue !== expanded) {
106
+ setIsExpanded(expanded);
107
+
108
+ if (!showControls && ![expanded, previousValue].includes(undefined)) {
109
+ toggleAnimation();
110
+ }
111
+ }
112
+ }, [expanded, toggleAnimation, previousValue, showControls]);
113
+ (0, _react.useEffect)(() => {
114
+ return function cleanup() {
115
+ clearTimeout(timer.current);
116
+ };
117
+ }, []);
104
118
  const toggleDrawer = (0, _react.useCallback)(ev => {
105
119
  setIsExpanded(!isExpanded);
106
120
  if (onChange) onChange(ev, !isExpanded);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-react",
3
- "version": "102.16.0",
3
+ "version": "102.16.1",
4
4
  "description": "A library of reusable React components for easily building user interfaces.",
5
5
  "engineStrict": true,
6
6
  "engines": {