@automattic/vip-design-system 0.27.0 → 0.27.2

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.
@@ -21,7 +21,7 @@ export const backgrounds = {
21
21
  };
22
22
 
23
23
  function ThemeChanger( { background } ) {
24
- const [ colorMode, setColorMode ] = useColorMode();
24
+ const [ _, setColorMode ] = useColorMode();
25
25
  const newColorMode = darkBackground === background ? 'dark' : 'default';
26
26
 
27
27
  useEffect( () => {
@@ -9,7 +9,7 @@ var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runt
9
9
 
10
10
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
11
 
12
- var _react = _interopRequireDefault(require("react"));
12
+ var _react = _interopRequireWildcard(require("react"));
13
13
 
14
14
  var _propTypes = _interopRequireDefault(require("prop-types"));
15
15
 
@@ -21,7 +21,22 @@ var _ScreenReaderText = require("../ScreenReaderText/ScreenReaderText");
21
21
 
22
22
  var _jsxRuntime = require("theme-ui/jsx-runtime");
23
23
 
24
- var _excluded = ["disabled", "defaultValue", "onChange", "name", "options", "className"];
24
+ var _excluded = ["children"],
25
+ _excluded2 = ["id", "value", "className", "label", "labelProps"],
26
+ _excluded3 = ["disabled", "defaultValue", "onChange", "name", "options", "className"];
27
+
28
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
29
+
30
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
31
+
32
+ var prefix = 'vip-radio-component-';
33
+ var itemStyle = {
34
+ display: 'flex',
35
+ alignItems: 'center',
36
+ minHeight: function minHeight(theme) {
37
+ return theme.space[4] - theme.space[2] + "px";
38
+ }
39
+ };
25
40
  var inputStyle = (0, _extends2["default"])({}, _ScreenReaderText.screenReaderTextClass, {
26
41
  width: '16px',
27
42
  height: '16px',
@@ -82,65 +97,89 @@ var labelStyle = {
82
97
  };
83
98
 
84
99
  var CustomLabel = function CustomLabel(_ref) {
85
- var children = _ref.children;
100
+ var children = _ref.children,
101
+ restLabel = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
86
102
  return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
87
103
  children: /*#__PURE__*/_react["default"].cloneElement(_react["default"].Children.only(children), (0, _extends2["default"])({}, children.props, {
88
104
  sx: (0, _extends2["default"])({}, labelStyle, children.props.sx),
89
- className: children.props.className + " vip-radio-component-item-label"
90
- }))
105
+ className: children.props.className + " " + prefix + "item-label"
106
+ }, restLabel))
91
107
  });
92
108
  };
93
109
 
94
110
  CustomLabel.propTypes = {
95
- children: _propTypes["default"].shape({
96
- props: {
97
- className: _propTypes["default"].any,
98
- sx: _propTypes["default"].object
99
- }
100
- }).isRequired
111
+ children: _propTypes["default"].any
101
112
  };
102
113
 
103
- var Radio = /*#__PURE__*/_react["default"].forwardRef(function (_ref2, forwardRef) {
104
- var disabled = _ref2.disabled,
105
- defaultValue = _ref2.defaultValue,
106
- onChange = _ref2.onChange,
107
- _ref2$name = _ref2.name,
108
- name = _ref2$name === void 0 ? '' : _ref2$name,
109
- _ref2$options = _ref2.options,
110
- options = _ref2$options === void 0 ? [] : _ref2$options,
111
- className = _ref2.className,
112
- props = (0, _objectWithoutPropertiesLoose2["default"])(_ref2, _excluded);
114
+ var RadioOption = function RadioOption(_ref2) {
115
+ var _ref2$option = _ref2.option,
116
+ id = _ref2$option.id,
117
+ value = _ref2$option.value,
118
+ className = _ref2$option.className,
119
+ label = _ref2$option.label,
120
+ _ref2$option$labelPro = _ref2$option.labelProps,
121
+ labelProps = _ref2$option$labelPro === void 0 ? {} : _ref2$option$labelPro,
122
+ restOption = (0, _objectWithoutPropertiesLoose2["default"])(_ref2$option, _excluded2),
123
+ name = _ref2.name,
124
+ onChangeHandler = _ref2.onChangeHandler,
125
+ checked = _ref2.checked;
126
+ return (0, _jsxRuntime.jsxs)("div", {
127
+ sx: itemStyle,
128
+ className: (0, _classnames["default"])(prefix + "item", prefix + "item-" + id, checked ? prefix + "item-checked" : '', className),
129
+ children: [(0, _jsxRuntime.jsx)("input", (0, _extends2["default"])({
130
+ type: "radio",
131
+ id: id,
132
+ name: name,
133
+ value: "" + value,
134
+ sx: inputStyle,
135
+ onChange: onChangeHandler,
136
+ className: prefix + "item-input",
137
+ checked: checked
138
+ }, restOption)), typeof label === 'string' ? (0, _jsxRuntime.jsx)(_Label.Label, (0, _extends2["default"])({
139
+ className: prefix + "item-label",
140
+ htmlFor: id,
141
+ sx: labelStyle
142
+ }, labelProps, {
143
+ children: label
144
+ })) : (0, _jsxRuntime.jsx)(CustomLabel, (0, _extends2["default"])({}, labelProps, {
145
+ children: label
146
+ }))]
147
+ });
148
+ };
149
+
150
+ RadioOption.propTypes = {
151
+ option: _propTypes["default"].object,
152
+ name: _propTypes["default"].string,
153
+ onChangeHandler: _propTypes["default"].func,
154
+ checked: _propTypes["default"].bool
155
+ };
156
+
157
+ var Radio = /*#__PURE__*/_react["default"].forwardRef(function (_ref3, forwardRef) {
158
+ var disabled = _ref3.disabled,
159
+ defaultValue = _ref3.defaultValue,
160
+ onChange = _ref3.onChange,
161
+ _ref3$name = _ref3.name,
162
+ name = _ref3$name === void 0 ? '' : _ref3$name,
163
+ _ref3$options = _ref3.options,
164
+ options = _ref3$options === void 0 ? [] : _ref3$options,
165
+ className = _ref3.className,
166
+ props = (0, _objectWithoutPropertiesLoose2["default"])(_ref3, _excluded3);
167
+ var onChangeHandler = (0, _react.useCallback)(function (e) {
168
+ var optionTriggered = options.find(function (option) {
169
+ return "" + option.value === "" + e.target.value;
170
+ });
171
+ onChange(e, optionTriggered);
172
+ }, []);
113
173
  var renderedOptions = options.map(function (option) {
114
- return (0, _jsxRuntime.jsxs)("div", {
115
- sx: {
116
- display: 'flex',
117
- alignItems: 'center',
118
- minHeight: function minHeight(theme) {
119
- return theme.space[4] - theme.space[2] + "px";
120
- }
121
- },
122
- className: (0, _classnames["default"])('vip-radio-component-item', "vip-radio-component-item-" + option.id),
123
- children: [(0, _jsxRuntime.jsx)("input", {
124
- type: "radio",
125
- id: option.id,
126
- name: name,
127
- value: "" + option.value,
128
- sx: inputStyle,
129
- onChange: onChange,
130
- className: (0, _classnames["default"])('vip-radio-component-item-input', option == null ? void 0 : option.className),
131
- checked: "" + option.value === "" + defaultValue
132
- }), typeof option.label === 'string' ? (0, _jsxRuntime.jsx)(_Label.Label, {
133
- className: (0, _classnames["default"])('vip-radio-component-item-label', option == null ? void 0 : option.className),
134
- htmlFor: option.id,
135
- sx: labelStyle,
136
- children: option.label
137
- }) : (0, _jsxRuntime.jsx)(CustomLabel, {
138
- children: option.label
139
- })]
140
- }, option.id);
174
+ return (0, _jsxRuntime.jsx)(RadioOption, {
175
+ name: name,
176
+ option: option,
177
+ onChangeHandler: onChangeHandler,
178
+ checked: "" + defaultValue === "" + (option == null ? void 0 : option.value)
179
+ }, option == null ? void 0 : option.id);
141
180
  });
142
181
  return (0, _jsxRuntime.jsx)("div", (0, _extends2["default"])({
143
- className: (0, _classnames["default"])('vip-radio-component', "vip-radio-component-" + name, className),
182
+ className: (0, _classnames["default"])(prefix, "" + prefix + name, className),
144
183
  ref: forwardRef
145
184
  }, props, {
146
185
  children: renderedOptions
@@ -153,7 +192,7 @@ Radio.propTypes = {
153
192
  disabled: _propTypes["default"].bool,
154
193
  defaultValue: _propTypes["default"].any,
155
194
  onChange: _propTypes["default"].func,
156
- options: _propTypes["default"].array,
195
+ options: _propTypes["default"].any,
157
196
  name: _propTypes["default"].string,
158
197
  className: _propTypes["default"].any
159
198
  };
@@ -5,7 +5,7 @@ exports["default"] = exports.Default = void 0;
5
5
 
6
6
  var _react = require("react");
7
7
 
8
- var _ = require("..");
8
+ var _2 = require("..");
9
9
 
10
10
  var _Radio = require("./Radio");
11
11
 
@@ -35,11 +35,11 @@ var Default = function Default() {
35
35
  checked = _useState[0],
36
36
  setChecked = _useState[1];
37
37
 
38
- var _useState2 = (0, _react.useState)('a'),
38
+ var _useState2 = (0, _react.useState)('b'),
39
39
  checked2 = _useState2[0],
40
40
  setChecked2 = _useState2[1];
41
41
 
42
- return (0, _jsxRuntime.jsxs)(_.Form.Root, {
42
+ return (0, _jsxRuntime.jsxs)(_2.Form.Root, {
43
43
  children: [(0, _jsxRuntime.jsxs)("p", {
44
44
  children: ["Per recommendation, if you have a Radio button, use a Fieldset with a legend as wrapper to your options.", ' ', (0, _jsxRuntime.jsx)("a", {
45
45
  href: "https://a11y-collective.github.io/demos/en/accessible-code/form-fieldsets.html",
@@ -95,7 +95,7 @@ var Default = function Default() {
95
95
  htmlFor: "option-custom-a",
96
96
  className: "custom-class",
97
97
  sx: {
98
- color: 'primary'
98
+ color: 'error'
99
99
  },
100
100
  children: "(Custom) All domains listed on this environment, and all subdomains"
101
101
  }),
@@ -103,14 +103,24 @@ var Default = function Default() {
103
103
  }, {
104
104
  value: 'b',
105
105
  label: 'All domains listed on this environment',
106
+ labelProps: {
107
+ id: 'label-option-custom-b-custom-props'
108
+ },
106
109
  className: 'custom-class-for-this',
110
+ 'aria-describedby': 'describe-radio-all-domains-subdomains',
107
111
  id: 'option-custom-b'
108
112
  }],
109
- onChange: function onChange(e) {
110
- return setChecked2(e.target.value);
113
+ onChange: function onChange(_, option) {
114
+ return setChecked2(option.value);
111
115
  }
112
116
  })
113
117
  })]
118
+ }), (0, _jsxRuntime.jsx)("p", {
119
+ id: "describe-radio-all-domains-subdomains",
120
+ sx: {
121
+ mt: 2
122
+ },
123
+ children: "This is a explanation for custom option b"
114
124
  })]
115
125
  });
116
126
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/vip-design-system",
3
- "version": "0.27.0",
3
+ "version": "0.27.2",
4
4
  "main": "build/system/index.js",
5
5
  "scripts": {
6
6
  "build-storybook": "build-storybook",
@@ -4,12 +4,20 @@
4
4
  /**
5
5
  * External dependencies
6
6
  */
7
- import React from 'react';
7
+ import React, { useCallback } from 'react';
8
8
  import PropTypes from 'prop-types';
9
9
  import classNames from 'classnames';
10
10
  import { Label } from './Label';
11
11
  import { screenReaderTextClass } from '../ScreenReaderText/ScreenReaderText';
12
12
 
13
+ const prefix = 'vip-radio-component-';
14
+
15
+ const itemStyle = {
16
+ display: 'flex',
17
+ alignItems: 'center',
18
+ minHeight: theme => `${ theme.space[ 4 ] - theme.space[ 2 ] }px`,
19
+ };
20
+
13
21
  const inputStyle = {
14
22
  ...screenReaderTextClass,
15
23
  width: '16px',
@@ -68,19 +76,70 @@ const labelStyle = {
68
76
  },
69
77
  };
70
78
 
71
- const CustomLabel = ( { children } ) => (
72
- <>
73
- { React.cloneElement( React.Children.only( children ), {
74
- ...children.props,
75
- sx: { ...labelStyle, ...children.props.sx },
76
- className: `${ children.props.className } vip-radio-component-item-label`,
77
- } ) }
78
- </>
79
- );
79
+ const CustomLabel = ( { children, ...restLabel } ) => {
80
+ return (
81
+ <>
82
+ { React.cloneElement( React.Children.only( children ), {
83
+ ...children.props,
84
+ sx: { ...labelStyle, ...children.props.sx },
85
+ className: `${ children.props.className } ${ prefix }item-label`,
86
+ ...restLabel,
87
+ } ) }
88
+ </>
89
+ );
90
+ };
80
91
 
81
92
  CustomLabel.propTypes = {
82
- children: PropTypes.shape( { props: { className: PropTypes.any, sx: PropTypes.object } } )
83
- .isRequired,
93
+ children: PropTypes.any,
94
+ };
95
+
96
+ const RadioOption = ( {
97
+ option: { id, value, className, label, labelProps = {}, ...restOption },
98
+ name,
99
+ onChangeHandler,
100
+ checked,
101
+ } ) => (
102
+ <div
103
+ sx={ itemStyle }
104
+ className={ classNames(
105
+ `${ prefix }item`,
106
+ `${ prefix }item-${ id }`,
107
+ checked ? `${ prefix }item-checked` : '',
108
+ className
109
+ ) }
110
+ >
111
+ <input
112
+ type="radio"
113
+ id={ id }
114
+ name={ name }
115
+ value={ `${ value }` }
116
+ sx={ inputStyle }
117
+ onChange={ onChangeHandler }
118
+ className={ `${ prefix }item-input` }
119
+ checked={ checked }
120
+ { ...restOption }
121
+ />
122
+
123
+ { typeof label === 'string' ? (
124
+ <Label
125
+ className={ `${ prefix }item-label` }
126
+ htmlFor={ id }
127
+ sx={ labelStyle }
128
+ { ...labelProps }
129
+ >
130
+ { label }
131
+ </Label>
132
+ ) : (
133
+ <CustomLabel { ...labelProps }>{ label }</CustomLabel>
134
+ ) }
135
+ </div>
136
+ );
137
+
138
+ RadioOption.propTypes = {
139
+ option: PropTypes.object,
140
+ name: PropTypes.string,
141
+ onChangeHandler: PropTypes.func,
142
+ checked: PropTypes.bool,
84
143
  };
85
144
 
86
145
  const Radio = React.forwardRef(
@@ -88,51 +147,26 @@ const Radio = React.forwardRef(
88
147
  { disabled, defaultValue, onChange, name = '', options = [], className, ...props },
89
148
  forwardRef
90
149
  ) => {
150
+ const onChangeHandler = useCallback( e => {
151
+ const optionTriggered = options.find(
152
+ option => `${ option.value }` === `${ e.target.value }`
153
+ );
154
+ onChange( e, optionTriggered );
155
+ }, [] );
156
+
91
157
  const renderedOptions = options.map( option => (
92
- <div
93
- sx={ {
94
- display: 'flex',
95
- alignItems: 'center',
96
- minHeight: theme => `${ theme.space[ 4 ] - theme.space[ 2 ] }px`,
97
- } }
98
- key={ option.id }
99
- className={ classNames(
100
- 'vip-radio-component-item',
101
- `vip-radio-component-item-${ option.id }`
102
- ) }
103
- >
104
- <input
105
- type="radio"
106
- id={ option.id }
107
- name={ name }
108
- value={ `${ option.value }` }
109
- sx={ inputStyle }
110
- onChange={ onChange }
111
- className={ classNames( 'vip-radio-component-item-input', option?.className ) }
112
- checked={ `${ option.value }` === `${ defaultValue }` }
113
- />
114
-
115
- { typeof option.label === 'string' ? (
116
- <Label
117
- className={ classNames( 'vip-radio-component-item-label', option?.className ) }
118
- htmlFor={ option.id }
119
- sx={ labelStyle }
120
- >
121
- { option.label }
122
- </Label>
123
- ) : (
124
- <CustomLabel>{ option.label }</CustomLabel>
125
- ) }
126
- </div>
158
+ <RadioOption
159
+ key={ option?.id }
160
+ name={ name }
161
+ option={ option }
162
+ onChangeHandler={ onChangeHandler }
163
+ checked={ `${ defaultValue }` === `${ option?.value }` }
164
+ />
127
165
  ) );
128
166
 
129
167
  return (
130
168
  <div
131
- className={ classNames(
132
- 'vip-radio-component',
133
- `vip-radio-component-${ name }`,
134
- className
135
- ) }
169
+ className={ classNames( prefix, `${ prefix }${ name }`, className ) }
136
170
  ref={ forwardRef }
137
171
  { ...props }
138
172
  >
@@ -148,7 +182,7 @@ Radio.propTypes = {
148
182
  disabled: PropTypes.bool,
149
183
  defaultValue: PropTypes.any,
150
184
  onChange: PropTypes.func,
151
- options: PropTypes.array,
185
+ options: PropTypes.any,
152
186
  name: PropTypes.string,
153
187
  className: PropTypes.any,
154
188
  };
@@ -20,7 +20,7 @@ export default {
20
20
 
21
21
  export const Default = () => {
22
22
  const [ checked, setChecked ] = useState( 'a' );
23
- const [ checked2, setChecked2 ] = useState( 'a' );
23
+ const [ checked2, setChecked2 ] = useState( 'b' );
24
24
 
25
25
  return (
26
26
  <Form.Root>
@@ -67,7 +67,7 @@ export const Default = () => {
67
67
  <Label
68
68
  htmlFor="option-custom-a"
69
69
  className="custom-class"
70
- sx={ { color: 'primary' } }
70
+ sx={ { color: 'error' } }
71
71
  >
72
72
  (Custom) All domains listed on this environment, and all subdomains
73
73
  </Label>
@@ -77,14 +77,22 @@ export const Default = () => {
77
77
  {
78
78
  value: 'b',
79
79
  label: 'All domains listed on this environment',
80
+ labelProps: {
81
+ id: 'label-option-custom-b-custom-props',
82
+ },
80
83
  className: 'custom-class-for-this',
84
+ 'aria-describedby': 'describe-radio-all-domains-subdomains',
81
85
  id: 'option-custom-b',
82
86
  },
83
87
  ] }
84
- onChange={ e => setChecked2( e.target.value ) }
88
+ onChange={ ( _, option ) => setChecked2( option.value ) }
85
89
  />
86
90
  </Flex>
87
91
  </fieldset>
92
+
93
+ <p id="describe-radio-all-domains-subdomains" sx={ { mt: 2 } }>
94
+ This is a explanation for custom option b
95
+ </p>
88
96
  </Form.Root>
89
97
  );
90
98
  };