@automattic/vip-design-system 0.17.1 → 0.18.0

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.
@@ -6,5 +6,6 @@ module.exports = {
6
6
  '@storybook/addon-essentials',
7
7
  '@storybook/addon-links',
8
8
  '@storybook/addon-controls',
9
+ '@storybook/addon-storysource',
9
10
  ],
10
11
  };
@@ -7,6 +7,8 @@ exports.NewDialog = void 0;
7
7
 
8
8
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
9
 
10
+ var _react = _interopRequireDefault(require("react"));
11
+
10
12
  var _propTypes = _interopRequireDefault(require("prop-types"));
11
13
 
12
14
  var DialogPrimitive = _interopRequireWildcard(require("@radix-ui/react-dialog"));
@@ -59,14 +61,33 @@ var NewDialog = function NewDialog(_ref) {
59
61
  _ref$id = _ref.id,
60
62
  id = _ref$id === void 0 ? undefined : _ref$id;
61
63
 
64
+ var _React$useState = _react["default"].useState(open || defaultOpen),
65
+ isOpen = _React$useState[0],
66
+ setIsOpen = _React$useState[1];
67
+
62
68
  if (disabled) {
63
69
  return;
64
- }
70
+ } // if content is a function, pass in onClose
71
+
72
+
73
+ var isContentFunction = typeof content === 'function';
74
+
75
+ var handleOnOpenChange = function handleOnOpenChange(status) {
76
+ setIsOpen(status);
77
+
78
+ if (onOpenChange) {
79
+ onOpenChange(status);
80
+ }
81
+ };
82
+
83
+ _react["default"].useEffect(function () {
84
+ handleOnOpenChange(open);
85
+ }, [open]);
65
86
 
66
87
  return (0, _jsxRuntime.jsxs)(DialogPrimitive.Root, {
67
88
  id: id,
68
- open: open,
69
- onOpenChange: onOpenChange,
89
+ open: isOpen,
90
+ onOpenChange: handleOnOpenChange,
70
91
  defaultOpen: defaultOpen,
71
92
  allowPinchZoom: allowPinchZoom,
72
93
  children: [trigger && (0, _jsxRuntime.jsx)(DialogPrimitive.Trigger, {
@@ -84,7 +105,11 @@ var NewDialog = function NewDialog(_ref) {
84
105
  hidden: !showHeading
85
106
  }), (0, _jsxRuntime.jsx)("div", {
86
107
  role: "document",
87
- children: content
108
+ children: isContentFunction ? content({
109
+ onClose: function onClose() {
110
+ return setIsOpen(false);
111
+ }
112
+ }) : content
88
113
  })]
89
114
  })]
90
115
  })]
@@ -96,7 +121,7 @@ NewDialog.propTypes = {
96
121
  trigger: _propTypes["default"].node.isRequired,
97
122
  title: _propTypes["default"].node.isRequired,
98
123
  description: _propTypes["default"].node.isRequired,
99
- content: _propTypes["default"].node,
124
+ content: _propTypes["default"].oneOfType([_propTypes["default"].node, _propTypes["default"].func]),
100
125
  showHeading: _propTypes["default"].bool,
101
126
  disabled: _propTypes["default"].bool,
102
127
  style: _propTypes["default"].oneOfType([_propTypes["default"].object, _propTypes["default"].func]),
@@ -3,7 +3,7 @@
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
4
 
5
5
  exports.__esModule = true;
6
- exports["default"] = exports.HiddenHeadings = exports.Default = exports.CustomStyling = exports.CustomStateManagement = exports.CustomClose = exports.AutoOpen = void 0;
6
+ exports["default"] = exports.HiddenHeadings = exports.Default = exports.CustomStyling = exports.CustomStateManagement = exports.CustomOnClose = exports.CustomClose = exports.AutoOpen = void 0;
7
7
 
8
8
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
9
 
@@ -222,20 +222,56 @@ var CustomStateManagement = function CustomStateManagement() {
222
222
  }), " attribute."]
223
223
  }), (0, _jsxRuntime.jsx)(NewDialog.Root, (0, _extends2["default"])({}, defaultProps, {
224
224
  open: open,
225
- onOpenChange: setOpen,
225
+ onOpenChange: function onOpenChange(status) {
226
+ // eslint-disable-next-line no-console
227
+ console.log('New status changed', status);
228
+ setOpen(!!open);
229
+ },
226
230
  trigger: (0, _jsxRuntime.jsx)(_system.Button, {
227
231
  children: "Trigger Dialog"
228
232
  }),
229
233
  content: (0, _jsxRuntime.jsx)("div", {
230
- children: (0, _jsxRuntime.jsx)(_system.Button, {
231
- onClick: function onClick() {
232
- return setOpen(false);
233
- },
234
- children: "Close here instead"
234
+ sx: {
235
+ mt: 2
236
+ },
237
+ children: (0, _jsxRuntime.jsx)(NewDialog.Close, {
238
+ children: (0, _jsxRuntime.jsx)(_system.Button, {
239
+ children: "Close here instead"
240
+ })
235
241
  })
236
242
  })
237
243
  }))]
238
244
  });
239
245
  };
240
246
 
241
- exports.CustomStateManagement = CustomStateManagement;
247
+ exports.CustomStateManagement = CustomStateManagement;
248
+
249
+ var CustomOnClose = function CustomOnClose() {
250
+ return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
251
+ children: [(0, _jsxRuntime.jsx)(_system.Text, {
252
+ sx: {
253
+ fontSize: 3,
254
+ mb: 3
255
+ },
256
+ children: "This example shows how you can use the content as a function to use the onClose method (same behavior as the original Dialog component)."
257
+ }), (0, _jsxRuntime.jsx)(NewDialog.Root, (0, _extends2["default"])({}, defaultProps, {
258
+ trigger: (0, _jsxRuntime.jsx)(_system.Button, {
259
+ children: "Trigger Dialog"
260
+ }),
261
+ content: function content(_ref) {
262
+ var onClose = _ref.onClose;
263
+ return (0, _jsxRuntime.jsx)("div", {
264
+ sx: {
265
+ mt: 2
266
+ },
267
+ children: (0, _jsxRuntime.jsx)(_system.Button, {
268
+ onClick: onClose,
269
+ children: "Close here instead"
270
+ })
271
+ });
272
+ }
273
+ }))]
274
+ });
275
+ };
276
+
277
+ exports.CustomOnClose = CustomOnClose;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/vip-design-system",
3
- "version": "0.17.1",
3
+ "version": "0.18.0",
4
4
  "main": "build/system/index.js",
5
5
  "scripts": {
6
6
  "build-storybook": "build-storybook",
@@ -25,6 +25,7 @@
25
25
  "@radix-ui/react-switch": "^0.1.5",
26
26
  "@radix-ui/react-tooltip": "^0.1.7",
27
27
  "@radix-ui/react-visually-hidden": "^0.1.4",
28
+ "@storybook/addon-storysource": "^6.5.10",
28
29
  "babel-loader": "^8.2.2",
29
30
  "classnames": "^2.3.1",
30
31
  "framer-motion": "^3.9.1",
@@ -3,6 +3,7 @@
3
3
  /**
4
4
  * External dependencies
5
5
  */
6
+ import React from 'react';
6
7
  import PropTypes from 'prop-types';
7
8
  import * as DialogPrimitive from '@radix-ui/react-dialog';
8
9
  /**
@@ -30,15 +31,32 @@ export const NewDialog = ( {
30
31
  open = undefined,
31
32
  id = undefined,
32
33
  } ) => {
34
+ const [ isOpen, setIsOpen ] = React.useState( open || defaultOpen );
35
+
33
36
  if ( disabled ) {
34
37
  return;
35
38
  }
36
39
 
40
+ // if content is a function, pass in onClose
41
+ const isContentFunction = typeof content === 'function';
42
+
43
+ const handleOnOpenChange = status => {
44
+ setIsOpen( status );
45
+
46
+ if ( onOpenChange ) {
47
+ onOpenChange( status );
48
+ }
49
+ };
50
+
51
+ React.useEffect( () => {
52
+ handleOnOpenChange( open );
53
+ }, [ open ] );
54
+
37
55
  return (
38
56
  <DialogPrimitive.Root
39
57
  id={ id }
40
- open={ open }
41
- onOpenChange={ onOpenChange }
58
+ open={ isOpen }
59
+ onOpenChange={ handleOnOpenChange }
42
60
  defaultOpen={ defaultOpen }
43
61
  allowPinchZoom={ allowPinchZoom }
44
62
  >
@@ -55,7 +73,9 @@ export const NewDialog = ( {
55
73
  <DialogTitle title={ title } hidden={ ! showHeading } />
56
74
  <DialogDescription description={ description } hidden={ ! showHeading } />
57
75
 
58
- <div role="document">{ content }</div>
76
+ <div role="document">
77
+ { isContentFunction ? content( { onClose: () => setIsOpen( false ) } ) : content }
78
+ </div>
59
79
  </DialogPrimitive.Content>
60
80
  </DialogPrimitive.Portal>
61
81
  </DialogPrimitive.Root>
@@ -66,7 +86,7 @@ NewDialog.propTypes = {
66
86
  trigger: PropTypes.node.isRequired,
67
87
  title: PropTypes.node.isRequired,
68
88
  description: PropTypes.node.isRequired,
69
- content: PropTypes.node,
89
+ content: PropTypes.oneOfType( [ PropTypes.node, PropTypes.func ] ),
70
90
  showHeading: PropTypes.bool,
71
91
  disabled: PropTypes.bool,
72
92
  style: PropTypes.oneOfType( [ PropTypes.object, PropTypes.func ] ),
@@ -89,6 +89,7 @@ export const HiddenHeadings = () => (
89
89
  />
90
90
  </>
91
91
  );
92
+
92
93
  export const CustomStyling = () => (
93
94
  <>
94
95
  <Text sx={ { fontSize: 3, mb: 3 } }>Custom Styling on Dialog Content</Text>
@@ -136,6 +137,7 @@ export const CustomClose = () => (
136
137
  />
137
138
  </>
138
139
  );
140
+
139
141
  export const CustomStateManagement = () => {
140
142
  const [ open, setOpen ] = useState( false );
141
143
  return (
@@ -149,14 +151,42 @@ export const CustomStateManagement = () => {
149
151
  <NewDialog.Root
150
152
  { ...defaultProps }
151
153
  open={ open }
152
- onOpenChange={ setOpen }
154
+ onOpenChange={ status => {
155
+ // eslint-disable-next-line no-console
156
+ console.log( 'New status changed', status );
157
+
158
+ setOpen( !! open );
159
+ } }
153
160
  trigger={ <Button>Trigger Dialog</Button> }
154
161
  content={
155
- <div>
156
- <Button onClick={ () => setOpen( false ) }>Close here instead</Button>
162
+ <div sx={ { mt: 2 } }>
163
+ <NewDialog.Close>
164
+ <Button>Close here instead</Button>
165
+ </NewDialog.Close>
157
166
  </div>
158
167
  }
159
168
  />
160
169
  </>
161
170
  );
162
171
  };
172
+
173
+ export const CustomOnClose = () => {
174
+ return (
175
+ <>
176
+ <Text sx={ { fontSize: 3, mb: 3 } }>
177
+ This example shows how you can use the content as a function to use the onClose method (same
178
+ behavior as the original Dialog component).
179
+ </Text>
180
+
181
+ <NewDialog.Root
182
+ { ...defaultProps }
183
+ trigger={ <Button>Trigger Dialog</Button> }
184
+ content={ ( { onClose } ) => (
185
+ <div sx={ { mt: 2 } }>
186
+ <Button onClick={ onClose }>Close here instead</Button>
187
+ </div>
188
+ ) }
189
+ />
190
+ </>
191
+ );
192
+ };